
// declare a global  XMLHTTP Request object
var XmlHttpObj;

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}

// called from onChange or onClick event of the dropdown list
function startListOnChange(startList, endfield) 
{
    
    // get selected continent from dropdown list
    var selectedObj = startList.options[startList.selectedIndex].value;
    
    // url of page that will send xml data back to client browser
    var requestUrl;
    requestUrl = "xml_data_provider.php?actionn=get_table_data" + "&filtername=" + startList.name+"&filter=" + selectedObj+"&field="+endfield;
    //alert(requestUrl);
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = StateChangeHandler;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
}

// called from onChange or onClick event of the mail field
function mailOnChange(mail) 
{
	var mailObj = document.getElementById(mail);
	if (mailObj.value=='' || mailObj.value.length==0) 
	{
		return true;
	}
    // url of page that will send xml data back to client browser
    var requestUrl;
    requestUrl = "xml_data_provider.php?actionn=check_mail" + "&email=" + mailObj.value + "&emailfield=" + mail;
	CreateXmlHttpObj();

	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = StateChangeHandler;

		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);

		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
}

// called from onChange or onClick event of the password field
function pwdOnChange(pwdObj, user) 
{
	if (pwdObj.value=='' || pwdObj.value.length==0) 
	{
		return false;
	}
    // url of page that will send xml data back to client browser
    var requestUrl;
    requestUrl = "xml_data_provider.php?actionn=check_pwd" + "&pwd=" + pwdObj.value + "&user=" + user;
	CreateXmlHttpObj();

	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = StateChangeHandler;

		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);

		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
}

// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function StateChangeHandler()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{	
			var response = XmlHttpObj.responseXML.documentElement;
			var actionn = response.getAttribute("act");
			if (actionn=='get_table_data')
				PopulateList(response);
			else if (actionn=='check_mail')
					CheckMail(response);
			else if (actionn=='check_pwd')
					CheckPWD(response);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the dropdown list
function PopulateList(countryNode)
{
	var endfield = countryNode.getElementsByTagName('field')[0].getAttribute("name");
    var countryList = document.getElementById(endfield);
	// clear the list 
	for (var count = countryList.options.length-1; count >0; count--)
	{
		countryList.options[count] = null;
	}
	//make visible
	document.getElementById("s"+endfield).style.display="";

	var countryNodes = countryNode.getElementsByTagName('selection');
	var textValue; 
	var optionItem;
	
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < countryNodes.length; count++)
	{
   		textValue = GetInnerText(countryNodes[count]);
		optionItem = new Option( textValue, textValue,  false, false);
		countryList.options[countryList.length] = optionItem;
	}
}

function CheckMail(mailNode)
{
	var valid = mailNode.getElementsByTagName('valid')[0].getAttribute("response");
	if (valid=='yesValid') return;
	else 
	{
		errmsg = mailNode.getElementsByTagName('valid')[0].getAttribute("msg");
		alert(errmsg);
		field = mailNode.getElementsByTagName('valid')[0].getAttribute("field");
		mailObj = document.getElementById(field);
		mailObj.focus();
		return;
	}
}

function CheckPWD(pwdNode)
{
	var valid = pwdNode.getElementsByTagName('valid')[0].getAttribute("response");
	if (valid=='yesValid') 
	{
		document.frmaddnew.u_password.disabled = false;
		document.frmaddnew.u_password1.disabled = false; 
		document.frmaddnew.u_password.focus();
	}
	else
	{
		document.frmaddnew.u_password.disabled = true;
		document.frmaddnew.u_password1.disabled = true; 
		alert('Wrong password');
		document.frmaddnew.u_passwordo.focus();
	}
}
// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

function send_to_basket() 
{

	product=arguments[0];
    var requestUrl;
    requestUrl = "noxml_data_provider.php?actionn=upd_basket" + "&product=" + product;
   if (arguments.length>1) 
    {
	    requestUrl = requestUrl + "&qt=" + arguments[1];
    }
    
    CreateXmlHttpObj();
	if (XmlHttpObj.readyState == 4 || XmlHttpObj.readyState == 0) 
	{
		XmlHttpObj.open("GET",requestUrl, true);
		XmlHttpObj.onreadystatechange = function () 
		{
		   if (XmlHttpObj.readyState == 4)
		   {
				var rr = XmlHttpObj.responseText.split("@");
				var objDiv = document.getElementById('basket_small');
				objDiv.innerHTML = rr[0];
				setTimeout("objDiv.scrollTop = objDiv.scrollHeight", 1);
				objDiv = document.getElementById('basket_small1');
				objDiv.innerHTML = rr[1];
				objDiv = document.getElementById('show_multi_order');
				objDiv.style.display = rr[2];
				if (rr[2] == 'none') document.getElementById('order_1_1').checked = true;
				else document.getElementById('order_1_1').checked = false;
				
				document.getElementById('show_payment_cost').innerHTML = '';
				var radioObj=document.getElementsByName('payment_type');
			    for(var i = 0; i < radioObj.length; i++) 
			    {
					radioObj[i].checked=false;
				}
				document.getElementById('total_value_transport').innerHTML = '0.00';
				document.getElementById('total_value_products').innerHTML = rr[3];
				document.getElementById('total_value').innerHTML = rr[3];
				
		   }
		}
		XmlHttpObj.send(null);
	}
}

function upd_total() 
{

    var requestUrl;
    requestUrl = "noxml_data_provider.php?actionn=upd_total";
    var radioObj=document.getElementsByName('payment_cost_type');
    costvalue=0;
    for(var i = 0; i < radioObj.length; i++) 
    {
		if(radioObj[i].checked) 
		{
			costvalue=radioObj[i].value;
		}
	}
	requestUrl = requestUrl + "&payment_cost_id=" + costvalue + "&transportvalue=";
    radioObj=document.getElementsByName('order_1');
    costvalue=1;
    for(var i = 0; i < radioObj.length; i++) 
    {
		if(radioObj[i].checked) 
		{
			costvalue=radioObj[i].value;
		}
	}   
	requestUrl = requestUrl + costvalue;	
	
    CreateXmlHttpObj();
	if (XmlHttpObj.readyState == 4 || XmlHttpObj.readyState == 0) 
	{
		XmlHttpObj.open("GET",requestUrl, true);
		XmlHttpObj.onreadystatechange = function () 
		{
		   if (XmlHttpObj.readyState == 4)
		   {
				var rr = XmlHttpObj.responseText.split("@");
				document.getElementById('total_value_products').innerHTML = rr[0];
				document.getElementById('total_value_transport').innerHTML = rr[1];
				document.getElementById('total_value').innerHTML = rr[2];
		   }
		}
		XmlHttpObj.send(null);
	}
}

function send_order() 
{
	document.getElementById('basket_small').innerHTML = '<img src="img/ajax_loader.gif" border="0" />';
    var requestUrl;
    requestUrl = "noxml_data_provider.php?actionn=place_order";
    CreateXmlHttpObj();
	if (XmlHttpObj.readyState == 4 || XmlHttpObj.readyState == 0) 
	{
		XmlHttpObj.open("GET",requestUrl, true);
		XmlHttpObj.onreadystatechange = function () 
		{
		   if (XmlHttpObj.readyState == 4)
		   {
				var rr = XmlHttpObj.responseText;
				document.getElementById('basket_small').innerHTML = rr;
		   }
		}
		XmlHttpObj.send(null);
	}
}

function select_payment() 
{
    var requestUrl;
    var radioObj=document.getElementsByName('payment_type');
    costvalue=0;
    for(var i = 0; i < radioObj.length; i++) 
    {
		if(radioObj[i].checked) 
		{
			costvalue=radioObj[i].value;
		}
	}
    requestUrl = "show_paymant_cost.php?py_id=" + costvalue ;
    //alert(requestUrl);
    CreateXmlHttpObj();
	if (XmlHttpObj.readyState == 4 || XmlHttpObj.readyState == 0) 
	{
		XmlHttpObj.open("GET",requestUrl, true);
		XmlHttpObj.onreadystatechange = function () 
		{
		   if (XmlHttpObj.readyState == 4)
		   {
				var rr = XmlHttpObj.responseText.split("@");
				document.getElementById('show_payment_cost').innerHTML = rr[0];
				document.getElementById('total_value').innerHTML = rr[1];
				document.getElementById('total_value_transport').innerHTML = rr[2];
		   }
		}
		XmlHttpObj.send(null);
	}
}
