// JavaScript Document
//var xmlHttp;
//var xmlHttp1;
function GetXmlHttpObject(){
	var xmlHttp=null;
	try {
 		// Firefox, Opera 8.0+, Safari
 		xmlHttp=new XMLHttpRequest();
 	}
	catch (e) {
 		// Internet Explorer
 		try {
  			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  		}
 		catch (e){
  			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
 	}
	return xmlHttp;
}

function calculatorFormStateChanged(xmlHttp) { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
 		var element = document.getElementById("calculatorForm").innerHTML = xmlHttp.responseText ;
 	} 
}

function calculatorOutputStateChanged(xmlHttp) { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
 		var element = document.getElementById("calculatorOutput").innerHTML = xmlHttp.responseText ;
 	} 
	else {
		var element = document.getElementById("calculatorLoadingData").innerHTML = 'Calculating costs <img src="../images/loadingData.gif" border="0" alt="Loading Data">';
	}
}

function quoteFormStateChanged(xmlHttp) { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
 		var element = document.getElementById("quoteForm").innerHTML = xmlHttp.responseText ;
 	} 
}

function retrieveReqForQuoteStateChanged(xmlHttp) { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
 		var element = document.getElementById("artworkForm").innerHTML = xmlHttp.responseText ;
 	} 
}

function createQuoteStateChanged(xmlHttp) { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
 		var element = document.getElementById("quoteDetails").innerHTML = xmlHttp.responseText ;
 	} 
}

function quoteLineAdminStateChanged(xmlHttp) { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
 		var element = document.getElementById("manageData").innerHTML = xmlHttp.responseText ;
 	} 
}

function quoteLineAdminSubmitStateChanged(xmlHttp) { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
 		var element = document.getElementById("quoteDetails").innerHTML = xmlHttp.responseText ;
 	} 
}

function quoteLineAdminSubmitStateChanged(xmlHttp) { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
 		var element = document.getElementById("quoteDetails").innerHTML = xmlHttp.responseText ;
 	} 
}

function customerUpdateQStatusStateChanged(xmlHttp) { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
 		var element = document.getElementById("manageQuote").innerHTML = xmlHttp.responseText ;
 	} 
}


function changeCalculatorForm(productId, noOfItems, badgeFixingId) {		
	var xmlHttp = GetXmlHttpObject();
	if (xmlHttp==null) {
		document.getElementById("calculatorForm").innerHTML = "Your browser does not support HTTP Requests so the calculator cannot be loaded";
  		return
  	} 
	var url = "calculatorForm.php";
	url= url + "?productId=" + productId;
	url= url + "&sid=" + Math.random();
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = function() {calculatorFormStateChanged(xmlHttp)};
	xmlHttp.send(null);
	
	calculateCosts(productId, 1, 0); //Set default items to 1 and no badge fixing id
	
} 

function calculateCosts(productId, noOfItems, badgeFixingId) {
	var nItems = 0;
	var errorMsg = '';
	
	try {
		nItems = parseInt(noOfItems);
	} 
	catch(e) {}
	
	if (nItems < 1 || nItems > 100000000) {
		errorMsg = "Please enter a valid number";
		alert(errorMsg);
	}
	else {	
		var xmlHttp = GetXmlHttpObject();
		if (xmlHttp==null) {
			document.getElementById("calculatorOutput").innerHTML = "Your browser does not support HTTP Requests so the calculator cannot be loaded";
  			return
  		}
		var url = "calculatorOutput.php";
		url= url + "?productId=" + productId + "&noOfItems=" + noOfItems + "&badgeFixingId=" + badgeFixingId;
		url= url + "&sid=" + Math.random();	
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = function() {calculatorOutputStateChanged(xmlHttp)};
		xmlHttp.send(null); 
	}
}

function changeQuoteForm(str, fieldChanged) {
	var xmlHttp = GetXmlHttpObject();
	if (xmlHttp==null) {
		document.getElementById("quoteForm").innerHTML = "Your browser does not support HTTP Requests so the calculator cannot be loaded";
  		return
  	} 
	var url = "quoteForm.php";
	str = str + "&fc=" + fieldChanged;
	str = str + "&sid=" + Math.random();
	xmlHttp.open("POST", url, true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.onreadystatechange = function() {quoteFormStateChanged(xmlHttp)};
	xmlHttp.send(str);	
} 

function retrieveReqForQuote(str, source) {
	if (source > 0)
	{
		var xmlHttp = GetXmlHttpObject();
		if (xmlHttp==null) {
			document.getElementById("artworkForm").innerHTML = "Your browser does not support HTTP Requests so the calculator cannot be loaded";
			return
		} 
		var url = "artworkForm.php";
		str = str + "&sid=" + Math.random();
		xmlHttp.open("POST", url, true); 
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttp.onreadystatechange = function() {retrieveReqForQuoteStateChanged(xmlHttp)};
		xmlHttp.send(str);	
	}
	else {
		alert("Please select the request type to upload your artwork for");
	}
}	

/*function a(str) {
	alert(str);
}*/


function createNewQuote(str) {
	var xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
			/* error here */
			return;
	}
	
	var url = "createQuote.php";
	str = str + "&sid=" + Math.random();
	//alert(str);
	xmlHttp.open("POST", url, true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.onreadystatechange = function() {createQuoteStateChanged(xmlHttp)};
	xmlHttp.send(str);	
}

function quoteLineAdmin(str) {
	var xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
			/* error here */
			return;
	}
	
	var url = "quoteLineAdmin.php";
	str = str + "&sid=" + Math.random();
	//alert(str);
	xmlHttp.open("POST", url, true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.onreadystatechange = function() {quoteLineAdminStateChanged(xmlHttp)};
	xmlHttp.send(str);	
}

function quoteLineAdminSubmit(str) {
	var xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
			/* error here */
			return;
	}
	
	var url = "quoteLineAdmin.php";
	str = str + "&sid=" + Math.random();
	//alert(str);
	xmlHttp.open("POST", url, true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.onreadystatechange = function() {quoteLineAdminSubmitStateChanged(xmlHttp)};
	xmlHttp.send(str);	
}

function customerUpdateQStatus(str) {
	var xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
			/* error here */
			return;
	}
	
	var url = "manageStatus.php";
	str = str + "&sid=" + Math.random();
	//alert(str);
	xmlHttp.open("POST", url, true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.onreadystatechange = function() {customerUpdateQStatusStateChanged(xmlHttp)};
	xmlHttp.send(str);	
}