var xmlhttp;

function loadXMLDoc(url)
{
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange=state_Change;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		if (xmlhttp)
		{
			xmlhttp.onreadystatechange=state_Change;
			xmlhttp.open("GET",url,true);
			xmlhttp.send();
		}
	}
}

function state_Change()
{
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4)
	{
		// if "OK"
		if (xmlhttp.status==200)
			buildArray();			
		else
			alert("Problem retrieving XML data:" + xmlhttp.statusText);
	}
}

var discounts = new Array(2);

function buildArray() {
	var dom = xmlhttp.responseXML;			
	var collection = dom.getElementsByTagName('discounts');
	var discTypes =  collection.length;
	discounts[0] = new Array(discTypes);
	discounts[1] = new Array(discTypes);
	
	for(i=0;i<discTypes;i++){
		disccode = collection[i].getElementsByTagName('Code');
		discounts[0][i] = disccode[0].firstChild.nodeValue;
		
		discamount = collection[i].getElementsByTagName('Amount');
		discounts[1][i] = discamount[0].firstChild.nodeValue;
	}
}

/*------------------------------------------------------------------------------------------------
     isCodeValid is called by discount textbox on OrderForm.aspx & RegistrationForm.aspx 
     This will check if the code exist in XML discount array
     if exist, it will take give the amount passed and applied the discount percentage to b
     passed to targetID.
  ------------------------------------------------------------------------------------------------*/
function isCodeValid( senderID, targetID, amount ){
	code = trim(document.getElementById(senderID).value);
	document.getElementById(targetID).value = 0; // reset the value
	if( discounts[0] != null && discounts[1] != null && ( discounts[0].length == discounts[1].length ) ) {		  
		  // search for array value		
		for( i=0; i<discounts[0].length; i++ ){		
			if( discounts[0][i] == code ){
				document.getElementById(targetID).value = discounts[1][i] * (amount /100);
				break;
			}
		}
	}
}

/* Trimming white space */
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}