//**************************************************************************
// Build the filter list
//**************************************************************************
function popFilter(track,requiredId)
{ 
	//var oViewBy=document.getElementById('viewBy');
	for (var i=0; i < document.filterForm.viewBy.length; i++)
 {
  if (document.filterForm.viewBy[i].checked)
  {
   var rad_val = document.filterForm.viewBy[i].value;
  }
 }
	var serverPage="includes/ajax/buildFilter.php?requiredId="+requiredId;
	serverPage = serverPage + "&track=" + escape(track) + "&selected=" + rad_val;
	var xmlhttp = false;
	// Check if we are using IE
	try
	{
		// If the javascript version is greater than 5
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (error)
	{
		// If not, use the older activeX object
		try
		{
			// If we are using IE
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			// Else we must be using another browser
			xmlhttp = false;
		}
	}

	// If we are using a non-ie browser, create a javascript instance object
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		xmlhttp = new XMLHttpRequest();
	}
	
	xmlhttp.open("GET", serverPage); 
	xmlhttp.onreadystatechange = function()
	{ 
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
			var oFilterDiv= document.getElementById('filterDiv');
			oFilterDiv.innerHTML = xmlhttp.responseText; 
		} 
	} 
	xmlhttp.send(null); 
}

//**************************************************************************
// Login to system for quotations
//**************************************************************************
function logoutQuote()
{ 
	var serverPage="includes/ajax/logoutQuote.php";
	
	var xmlhttp = false;
	// Check if we are using IE
	try
	{
		// If the javascript version is greater than 5
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (error)
	{
		// If not, use the older activeX object
		try
		{
			// If we are using IE
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			// Else we must be using another browser
			xmlhttp = false;
		}
	}

	// If we are using a non-ie browser, create a javascript instance object
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		xmlhttp = new XMLHttpRequest();
	}
	
	xmlhttp.open("GET", serverPage); 
	xmlhttp.onreadystatechange = function()
	{ 
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
			alertError(xmlhttp.responseText);
		} 
	} 
	xmlhttp.send(null); 
}

//**************************************************************************
// Login to system for quotations
//**************************************************************************
function loginQuote()
{ 
	var oUser=document.getElementById('username');
	var oPass=document.getElementById('password');
	if(oUser.value=="")
	{
		alert('No user name was entered.');
		return false;
	}
	if(oPass.value=="")
	{
		alert('No password was entered.');
		return false;
	}
	var serverPage="includes/ajax/loginQuote.php?";
	serverPage = serverPage + "username=" + oUser.value + "&password=" + oPass.value;
	var xmlhttp = false;
	// Check if we are using IE
	try
	{
		// If the javascript version is greater than 5
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (error)
	{
		// If not, use the older activeX object
		try
		{
			// If we are using IE
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			// Else we must be using another browser
			xmlhttp = false;
		}
	}

	// If we are using a non-ie browser, create a javascript instance object
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		xmlhttp = new XMLHttpRequest();
	}
	
	xmlhttp.open("GET", serverPage); 
	xmlhttp.onreadystatechange = function()
	{ 
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
			var responseArray= xmlhttp.responseText.split(',');
			var keyArray = [];
			var valueArray= [];
			for(x=0;x<responseArray.length;x++)
			{
				keyArray.push(responseArray[x]);
				x++;
				valueArray.push(responseArray[x]);
			}
			for(x=0;x<keyArray.length;x++)
			{
				switch(keyArray[x])
				{
					case 'loginId':
						Set_Cookie('wciLoginId',valueArray[x],0,'/','','');
						break;
				}
			}
			var oLoginBlock= document.getElementById('loginBlock');
			//oLoginBlock.style.display='none';
			alert('You are now logged in to the quotations system. You must refresh the page in order to view any quotation related functions.');
		} 
	} 
	xmlhttp.send(null); 
}

//**************************************************************************
// Set Active Quotation
//**************************************************************************
function setActiveQuotation(quotationId)
{ 
var serverPage='';
serverPage="includes/ajax/setActiveQuotation.php?quotationId=" + quotationId;
var xmlhttp = false;
	// Check if we are using IE
	try
	{
		// If the javascript version is greater than 5
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (error)
	{
		// If not, use the older activeX object
		try
		{
			// If we are using IE
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			// Else we must be using another browser
			xmlhttp = false;
		}
	}

	// If we are using a non-ie browser, create a javascript instance object
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		xmlhttp = new XMLHttpRequest();
	}
	xmlhttp.open("GET", serverPage); 
	xmlhttp.onreadystatechange = function()
	{ 
		
		//if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		if (xmlhttp.readyState == 4)
		{
			//alert(xmlhttp.status); 
			
			/*if(eventType==0)
			{
				setFields(xmlhttp.responseText); 
			}
			else
			{
				setBasket(xmlhttp.responseText); 
			}
			*/
		} 
	} 
	xmlhttp.send(null); 
	
}

//**************************************************************************
// Update Quotation Items Price
//**************************************************************************
function updateItemPrice(quotationItemId,price)
{ 
var serverPage='';
serverPage="includes/ajax/updateItemPrice.php?quotationItemId=" + quotationItemId + "&price=" + price;
var xmlhttp = false;
	// Check if we are using IE
	try
	{
		// If the javascript version is greater than 5
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (error)
	{
		// If not, use the older activeX object
		try
		{
			// If we are using IE
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			// Else we must be using another browser
			xmlhttp = false;
		}
	}

	// If we are using a non-ie browser, create a javascript instance object
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		xmlhttp = new XMLHttpRequest();
	}
	xmlhttp.open("GET", serverPage); 
	xmlhttp.onreadystatechange = function()
	{ 
		
		//if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		if (xmlhttp.readyState == 4)
		{
			//alert(xmlhttp.status); 
			
			/*if(eventType==0)
			{
				setFields(xmlhttp.responseText); 
			}
			else
			{
				setBasket(xmlhttp.responseText); 
			}
			*/
		} 
	} 
	xmlhttp.send(null); 
}

//**************************************************************************
// Quantity change
//**************************************************************************
function productQuantityChange(productId,eventType,upgradeArray,addonArray)
{ 
	var oProductQuantity=document.getElementById('productQuantity');
	
	var sStub="";
	if(eventType==0)
	{
		sStub="includes/ajax/productCalculator.php?quantity=";
	}
	if(eventType==1)
	{
		sStub="includes/ajax/basketAdd.php?quantity=";
	}
	if(eventType==2)
	{
		sStub="includes/ajax/quotationAdd.php?quantity=";
	}
	var serverPage='';
	if(oProductQuantity==null)
	{
		serverPage = sStub + "1&productId=" + productId;
	}
	else
	{
		if (parseInt(oProductQuantity.value)==oProductQuantity.value)
		{
			serverPage = sStub + oProductQuantity.value + "&productId=" + productId;
		}
		else
		{
			return false;
		}
	}
	
	if(upgradeArray!=null)
	{
		for(x=0;x<upgradeArray.length;x++)
		{
				var obj=document.getElementById(upgradeArray[x]);
				serverPage= serverPage +"&"+upgradeArray[x]+'='+obj.checked;
		}
	}
	
	if(addonArray!=null)
	{
		for(x=0;x<addonArray.length;x++)
		{
			var obj=document.getElementById(addonArray[x]);
			if(parseInt(obj.value)==obj.value)
			{
				serverPage= serverPage +"&"+addonArray[x]+'='+obj.value;
			}
			else
			{
				serverPage= serverPage +"&"+addonArray[x]+'=0';
			}
		}
	}

	// If this is a basket add call then tack on the basketId if it exists
	if(eventType==1)
	{
		basketId=Get_Cookie('wciBasketId');
		if(basketId!=null)
		{
			serverPage= serverPage +"&basketId="+basketId;
		}
	}
	
	serverPage=serverPage+"&bustCache="+Math.random();

	var oXmlhttp = false;
	// Check if we are using IE
	try
	{
		// If the javascript version is greater than 5
		oXmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (error)
	{
		// If not, use the older activeX object
		try
		{
			// If we are using IE
			oXmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			// Else we must be using another browser
			oXmlhttp = false;
		}
	}

	// If we are using a non-ie browser, create a javascript instance object
	if (!oXmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		oXmlhttp = new XMLHttpRequest();
	}
	oXmlhttp.open("GET", serverPage);
	oXmlhttp.onreadystatechange = function()
	{ 
		if (oXmlhttp.readyState == 4)
		{	
			if(eventType==0)
			{
				setFields(oXmlhttp.responseText); 
			}
			else if(eventType==1)
			{
				setBasket(oXmlhttp.responseText); 
			}
			else if(eventType==2)
			{
				alertError(oXmlhttp.responseText);
				window.location = "quoteItems.php";
			}
		} 
	} 
	oXmlhttp.send(null); 
}
//**************************************************************************
// Set fields
//**************************************************************************
function setFields(responseText)
{ 
	var responseArray= responseText.split(',');
	var keyArray = [];
	var valueArray= [];
	for(x=0;x<responseArray.length;x++)
	{
		keyArray.push(responseArray[x]);
		x++;
		valueArray.push(responseArray[x]);
	}
	for(x=0;x<keyArray.length;x++)
	{
		var obj=document.getElementById(keyArray[x]);
		obj.innerHTML=parseFloat(valueArray[x]).toFixed(2);
	}
}

function alertError(responseText)
{
	var responseArray= responseText.split(',');
	var keyArray = [];
	var valueArray= [];
	for(x=0;x<responseArray.length;x++)
	{
		keyArray.push(responseArray[x]);
		x++;
		valueArray.push(responseArray[x]);
	}
	for(x=0;x<keyArray.length;x++)
	{
		if (keyArray[x] == 'error')
		{
			alert(valueArray[x]);
		}
	}
}

//**************************************************************************
// Set basket
//**************************************************************************
function setBasket(responseText)
{ 
	var responseArray= responseText.split(',');
	var keyArray = [];
	var valueArray= [];
	for(x=0;x<responseArray.length;x++)
	{
		keyArray.push(responseArray[x]);
		x++;
		valueArray.push(responseArray[x]);
	}
	for(x=0;x<keyArray.length;x++)
	{
		switch(keyArray[x])
		{
			case 'basketId':
				Set_Cookie('wciBasketId',valueArray[x],30,'/','','');
				break;
			case 'items':
				var obj=document.getElementById('basketButton');
				obj.innerText='Basket('+valueArray[x]+')';
				break;
		}
		alert('Item added to basket.');
	}
}

//**************************************************************************
// Set cookie value
//**************************************************************************
function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


//**************************************************************************
// Get cookie value
//**************************************************************************
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if (!b_cookie_found )
	{
		return null;
	}
}
				
