function getHTTPData(url){
	var myRequest = null;

	try {
		if(window.XMLHttpRequest) {
			// Gecko (Firefox, Moz), KHTML (Konqueror, Safari), Opera, Internet Explorer 7
			myRequest = new XMLHttpRequest();
		} else if(window.ActiveXObject) {  
			myRequest = new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer 4,5,5.5,6,7
		} 
	} catch(e) {
		return 'Error in detecting browser capabilities.';
	}
	
	try {
		myRequest.open("GET", url, false);
		myRequest.send(null);
	}
	catch(e) {
		return 'Error in XMLHttpRequest.';
	}
	return myRequest.responseText;
}

function AddItemToBasket(productID, productNumber, language){
	myURL = '/global/xmlrequest/basket_summary.php?glbLanguage='+language+'&Add=' + productID;
	myResponse = getHTTPData(myURL);
	if(myResponse.substring(0,3) == 'Err'){
		alert(myResponse);
	}
	else if(myResponse){
		document.getElementById('basketsummary').innerHTML = myResponse;
		document.getElementById('basketsummary').style.visibility = 'visible';
		document.getElementById('basketsummary').style.display = 'block';

		if(language == 'fr'){
			alert(productNumber + ' était ajouté a votre panier.');
		}
		else{
			alert(productNumber + ' was added to your basket.');
		}
	}
}

function AddItemToOrder(productID, productNumber, language){

	var quantity = prompt('Enter Quantity: '); 
	myURL = '/global/xmlrequest/basket_summary.php?glbLanguage='+language+'&Add=' + productID +'&Quantity=' + quantity;
	myResponse = getHTTPData(myURL);
	if(myResponse){
		document.getElementById('basketsummary').innerHTML = myResponse;
		document.getElementById('basketsummary').style.visibility = 'visible';
		document.getElementById('basketsummary').style.display = 'block';
	}
}

function AddSearchToOrder(searchID){

	myURL = '/global/xmlrequest/bulk_to_order.php?searchID=' + searchID;
	myResponse = getHTTPData(myURL);
	
	if(myResponse){
		document.getElementById('basketsummary').innerHTML = myResponse;
		document.getElementById('basketsummary').style.visibility = 'visible';
		document.getElementById('basketsummary').style.display = 'block';
	}
}


function UpdateBasket(language){
	myURL = '/global/xmlrequest/basket_summary.php?glbLanguage='+language;
	myResponse = getHTTPData(myURL);
	if(myResponse){
		document.getElementById('basketsummary').innerHTML = myResponse;
		document.getElementById('basketsummary').style.visibility = 'visible';
		document.getElementById('basketsummary').style.display = 'block';
	}
}


function UpdateProvinceCodeSelect(selectName, divID, language, countryID, selectedID, otherID, otherName){
	myURL = '/global/xmlrequest/province_code_select_for_country.php?countryID=' + countryID + '&glbLanguage=' + language + '&otherID=' + otherID + '&selectedID=' + selectedID + '&selectName=' + selectName +'&otherName=' +otherName;
	myResponse = getHTTPData(myURL);
	document.getElementById(divID).innerHTML = myResponse;
}

/*
	formats date selection list/input for the frontpage item expiry/insert date. 
	intervalType is one of:
	0 = now()
	1 = days
	2 = months
	3 = years
	4 = specific date.
*/

function UpdateDateIntervalSelect(fieldName, divID, intervalType, interval, dateYear, dateMonth, dateDay){
	myResponse = '';	
	
	switch(intervalType){
	
		case '0':	//now()
			break;
		
		case '1':	//days
		case '2':	//months
		case '3':	//years
			myResponse += '<input type="text" name="' + fieldName +'" value="' + interval + '" size="5" />';
			break;
	
		case '4': //specific date 
			
			if(!(dateYear || dateMonth || dateDay )){
				dateYear = 'YYYY';
				dateMonth = 'MM';
				dateDay = 'DD';
			}
			myResponse += '<span>Year: <input type="text" name="' + fieldName +'Year" value="' + dateYear + '" size="4" maxlength="4" /></span>';		
			myResponse += '<span> Month: <input type="text" name="' + fieldName +'Month" value="' + dateMonth + '" size="2" maxlength="2" /></span>';		
			myResponse += '<span> Day: <input type="text" name="' + fieldName +'Day" value="' + dateDay + '" size="2" maxlength="2" /></span>';		
			break;	
	}
	
	document.getElementById(divID).innerHTML = myResponse;
} 

function AddInputField(divID, inputName, inputSize){
	var parentDiv = document.getElementById(divID);

	var newID = divID+ parentDiv.childNodes.length;	
	
	var newDiv = document.createElement("div");	
	newDiv.id = newID;

	var input= document.createElement("input");	
	input.type="text";
	input.value="";
	input.name = inputName;
	input.size = inputSize;
	input.maxLength = inputSize;

	newDiv.appendChild(input);
		
	var removeButton = document.createElement("input");		
	removeButton.type="button";
	removeButton.value="Remove"
	removeButton.name = "remove"+newID;
	removeButton.onclick = function() { removeElement(newID, divID, inputName, inputSize); };
	
	newDiv.appendChild(removeButton);

	parentDiv.appendChild(newDiv);
	
}

function removeElement(divID, parentID, inputName, inputSize) {  
  	var d = document.getElementById(parentID);
  	
	if(inputSize>0 && d.childNodes.length == 1){
		AddInputField(parentID, inputName, inputSize);	
	}  

  	var olddiv = document.getElementById(divID);
  	d.removeChild(olddiv);
}

function barcodeUpdate(parentID, newQuantity, upc, sessionID){
	document.getElementById('upcNotice').innerHTML ='';	
	upc = trim(upc);
	var myURL = '/global/xmlrequest/barcode_lookup.php?upc=' + upc +'&quantity=' +newQuantity+'&inventorySessionID=' +sessionID;
	var myResponse = getHTTPData(myURL);

	if(myResponse){
		document.getElementById(parentID).innerHTML= myResponse;
		document.newBarcodeList.upc.value='';
		document.newBarcodeList.upc.focus();
	}
	else{
		document.getElementById(parentID).innerHTML= '<p>Error in XMLHTTP request</p>';
		document.newBarcodeList.upc.value='';
		document.newBarcodeList.upc.focus();
	}
}

function removeBarcode(upc){
	removeElement('product_'+upc, 'productList',-1, -1);

	if(!document.getElementById('productList').childNodes.length){
		document.getElementById('saveSession').style.display='none';
	}
}

function barcodeItemQuantityUpdate(productID, quantity, upc){

	if(quantity.length<7){
		document.forms['newBarcodeList'].elements["productArray["+productID+"]"].value=quantity;
		var quantitySpan = 'quantity_'+productID;
		document.getElementById(quantitySpan).innerHTML=quantity;
		var subtotalSpan = 'subtotal_'+productID;
		cost = document.forms['newBarcodeList'].elements['wholesale_cost_'+productID].value;
		document.getElementById(subtotalSpan).innerHTML= cost*quantity;
		document.newBarcodeList.upc.focus();
	}
	else{
	//read another upc into the quantity field...ignore it and reset quantity field to empty, then keep focus on quantity
		document.forms["newBarcodeList"].elements["changeQuantity_"+upc].value='';
		document.forms["newBarcodeList"].elements["changeQuantity_"+upc].focus();
	}

}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}


/*note: sending toggle=true hides the element*/
function toggleDisplay(elementID, toggle){
	if(document.getElementById(elementID)){
		if(toggle){
				document.getElementById(elementID).style.display='none';
			}
			else{
		 		document.getElementById(elementID).style.display='block';
			}
	}
}

function UpdateAddress(updateDiv, hideDiv, addressID){

	if(addressID > 0){
		var myURL = '/global/xmlrequest/display_single_address.php?addressID=' + addressID;
		var myResponse = getHTTPData(myURL);
		
		if(myResponse){
			
			document.getElementById(updateDiv).innerHTML = myResponse;
			document.getElementById(hideDiv).style.display='none';
		}
	}
	else{
		document.getElementById(hideDiv).style.display='block';
		document.getElementById(updateDiv).innerHTML = '';
	}
}

function toggleShippingAddressClone(hideDiv, updateDiv, cloneToggle, isAuthenticated, savedAddress){
	toggleDisplay(hideDiv, cloneToggle); 

	if(isAuthenticated){ 	
		toggleDisplay(updateDiv, cloneToggle);
	
		if(!cloneToggle){
			UpdateAddress('savedBillingAddressDiv', hideDiv, savedAddress);
		}
	}
}

