/* ########### AJAX ENGINE TO SEND/RECEIVE REQUEST  ###############
*  The script which will send and receiver the customer related data
*  to php(fetcher.php). Mainly used in customer validation, verification
*  code send, smsstatus. etc
*
*/
// define global variables.
var xmlHttp		= RequestObject();
var codeRequestCounter = 1;
// define global variables
var HOST = "fetcher.php"; // the php script which will fetch and return data



// create xmlHTTP request for different browsers. 
function RequestObject()
{
var xmlHttp;
	
	
	
	if(window.ActiveXObject)
	{
		var tryPossibleVersions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHttp"];
		
		for(i=0;i<tryPossibleVersions.length;i++) {
			try
			{
				xmlHttp = new ActiveXObject(tryPossibleVersions[i]);
				break;
			}
			catch (e)
			{
			xmlHttp = false;
			}
		}
	}
	else
	{
		try
		{
		xmlHttp = new XMLHttpRequest();
		}
		catch (e)
		{
		xmlHttp = false;
		}
	}
	if (!xmlHttp)
	alert("Error creating the XMLHttpRequest object.");
	else
	return xmlHttp;

	

}

// function to call fetcher.php to validate the form fields with cellnumber
function validateForm2(vType) {
	
	if(xmlHttp) {
		
		
		try
		{
			
			if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
				
				// make objects
				// read the form content
				var oCompanyName = document.getElementById('idCompanyName');
				var oEin = document.getElementById('idEin');
				var oAddress1 = document.getElementById('idAddress1');
				var oAddress2 = document.getElementById('idAddress2');
				var oZip = document.getElementById('idZip');
				var oCity = document.getElementById('idCity');
				var oPhone = document.getElementById('idPhone');
				var oFirstName = document.getElementById('idFirstName');
				var oLastName = document.getElementById('idLastName');
				var oPosition = document.getElementById('idPosition');
				var oCellphone = document.getElementById('idCellphone');
				var oEmail = document.getElementById('idEmail');
				var oCompanyEmail = document.getElementById('idCompanyEmail');

				// get validation method (sms, email)
				oValMethod = "";
				for( i = 0; i < document.frmsales.rd_verification.length; i++ ) {
					if( document.frmsales.rd_verification[i].checked == true )
					oValMethod = document.frmsales.rd_verification[i].value;
				}
								
				
				// if the function calls through form
				
				if(vType=='Form') {
					
					show_processing_image();
					
					myVar = "action=customerDetails&companyName="+escape(oCompanyName.value)+"&companyEin="+escape(oEin.value)+"&address1="+escape(oAddress1.value)+"&address2="+escape(oAddress2.value)+"&zip="+escape(oZip.value)+"&city="+escape(oCity.value)+"&phone="+escape(oPhone.value)+"&firstName="+escape(oFirstName.value)+"&lastName="+escape(oLastName.value)+"&position="+escape(oPosition.value)+"&cellphone="+escape(oCellphone.value)+"&email="+escape(oEmail.value)+"&company_email="+escape(oCompanyEmail.value)+"&verification_method="+escape(oValMethod);
					
					xmlHttp.open("POST", HOST,true);
					xmlHttp.setRequestHeader ("Content-Type","application/x-www-form-urlencoded");
					xmlHttp.onreadystatechange = readValidationStatus;
					xmlHttp.send(myVar);
				
				}
				
			}
			

		
		}catch (e) {
			alert(e.toString());
		}

	}
	
}

function validateForm3() {
	if(xmlHttp) {
		
		
		try
		{
			
			if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
				
					show_processing_image();
					var oCode = document.getElementById('id_code');
					var oTos  = document.getElementById('iChkTos');
					var tosStatus = 0;
					if(oTos.checked==true) {
						tosStatus = 1;
					}

					myVar = "action=vcode&code="+escape(oCode.value)+"&tos="+tosStatus;
					xmlHttp.open("POST", HOST,true);
					xmlHttp.setRequestHeader ("Content-Type","application/x-www-form-urlencoded");
					xmlHttp.onreadystatechange = readCodeStatus;
					xmlHttp.send(myVar);
			
			}
			

		
		}catch (e) {
			alert(e.toString());
		}

	}
	
	
}
/* @Descriptio: Function reads the server response and display errors
*  @Return : display the errors
*/

function readValidationStatus() {
	
	try
	{
		if (xmlHttp.readyState == 4)
		{
			if (xmlHttp.status == 200)
			{
				xmlResponse = xmlHttp.responseText;
				
				hide_processing_image();
				
				if(xmlResponse == "") {
					document.frmsales.action="order_confirmation.php";
					document.frmsales.submit();
				}
				else {
					alert(xmlResponse);
					makeFieldGray();
					//return false;
				}
				
				
			}
			else
			{
			alert("There was a problem accessing the server: " +
			xmlHttp.statusText);
			}
		}
	}
	catch (e)
	{
		alert("Error in server response pls try after some time"+e.toString());
	}
}

// function to read sms status (ok= send successfully, failed)
function readSmsStatus() {
	
	try
	{
		if (xmlHttp.readyState == 4)
		{
			if (xmlHttp.status == 200)
			{
				xmlResponse = xmlHttp.responseText;
				
				hide_processing_image();
				
				// send on confirmation page
				document.frmsales.action="order_confirmation.php";
				document.frmsales.submit();

			}
			else
			{
			alert("There was a problem accessing the server: " +
			xmlHttp.statusText);
			}
		}
	}
	catch (e)
	{
		alert("Error in server response pls try after some time"+e.toString());
	}
}

/* check the verification code status. if code is ok(valid) redirect user
*  to order_processed.php page otherwise show an alert message.
*/

function readCodeStatus() {
	try
	{
		if (xmlHttp.readyState == 4)
		{
			if (xmlHttp.status == 200)
			{
				xmlResponse = xmlHttp.responseText;
				hide_processing_image();
				if(xmlResponse == "ok") {
					document.frmsale.action="order_processed.php";
					document.frmsale.submit();
				}
				else {
					alert(xmlResponse);
					return false;
				}
					
								
				
			}
			else
			{
			alert("There was a problem accessing the server: " +
			xmlHttp.statusText);
			}
		}
	}
	catch (e)
	{
		alert("Error in server response pls try after some time"+e.toString());
	}

}
// function to change the color of fields (errors)
function makeFieldGray() {
	
		var oCompanyName = document.getElementById('idCompanyName');
		var oEin = document.getElementById('idEin');
		var oAddress1 = document.getElementById('idAddress1');
		//var oAddress2 = document.getElementById('idAddress2');
		var oZip = document.getElementById('idZip');
		var oCity = document.getElementById('idCity');

		var oPhone = document.getElementById('idPhone');
		var oFirstName = document.getElementById('idFirstName');
		var oLastName = document.getElementById('idLastName');
		var oPosition = document.getElementById('idPosition');
		var oCellphone = document.getElementById('idCellphone');
		var oCompanyEmail = document.getElementById('idCompanyEmail');
		
		if(oCompanyName.value=="") {
			oCompanyName.style.backgroundColor = "silver";
		}
		if(oEin.value=="") {
			oEin.style.backgroundColor = "silver";
		}
		if(oAddress1.value=="") {
			oAddress1.style.backgroundColor = "silver";
		}
		
		if(oCompanyEmail.value=="") {
			oCompanyEmail.style.backgroundColor = "silver";
		}
		if(oZip.value=="") {
			oZip.style.backgroundColor = "silver";
		}
		if(oCity.value=="") {
			oCity.style.backgroundColor = "silver";
		}
		if(oPhone.value=="") {
			oPhone.style.backgroundColor = "silver";
		}
		if(oFirstName.value=="") {
			oFirstName.style.backgroundColor = "silver";
		}
		if(oLastName.value=="") {
			oLastName.style.backgroundColor = "silver";
		}
		if(oPosition.value=="") {
			oPosition.style.backgroundColor = "silver";
		}
		if(oCellphone.value=="") {
			oCellphone.style.backgroundColor = "silver";
		}


}

/* function that will increment the value of hidden field.
*  mainly used to check how many times user has tried to send.
*/

function incrementSmsFailedCounter(vCounterValue) {
	var oSmsCounter = document.getElementById('iSmsFailedCounter');
	oSmsCounter.value = parseInt(vCounterValue)+1;

}

/* function to show busy image at the top of form. This function will execute each time
* when a request take place.
*/

function show_processing_image() {
	
	var o_proc_image_div = document.getElementById('waiting_image');
	o_proc_image_div.style.display="inline";
}

/* This function will execute each time when a response will occure.
*  hide the processing image showen by "show_processing_image" function.
*/
function hide_processing_image() {
	
	var o_proc_image_div = document.getElementById('waiting_image');
	o_proc_image_div.style.display="none";
}

// Function to send the verification code used in summary page

function resend_code(order_id, name, cell_number, email) {
	
	if(xmlHttp) {
		
		
		try
		{
			
			if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
				
					show_processing_image();
					
					myVar = "action=resend_code&order_id="+escape(order_id)+"&name="+escape(name)+"&cell_number="+escape(cell_number)+"&email="+escape(email)+"&reqCounter="+codeRequestCounter;
					xmlHttp.open("POST", HOST,true);
					xmlHttp.setRequestHeader ("Content-Type","application/x-www-form-urlencoded");
					xmlHttp.onreadystatechange = resend_code_status;
					xmlHttp.send(myVar);
					// Increase the code request counter
					codeRequestCounter++;
			
			}
			

		
		}catch (e) {
			alert(e.toString());
		}

	}

}

// read the response of resend verification code and update the browser

function resend_code_status() {
	
	try
	{
		if (xmlHttp.readyState == 4)
		{
			if (xmlHttp.status == 200)
			{
				xmlResponse = xmlHttp.responseText;
				hide_processing_image();
				alert(xmlResponse);
			}
			else
			{
			alert("There was a problem accessing the server: " +
			xmlHttp.statusText);
			}
		}
	}
	catch (e)
	{
		alert("Error in server response pls try after some time"+e.toString());
	}
}

// function to show hide cellnumber box ( for verification method)
function showHideBox(tag) {
	
	var oSms = document.getElementById("code_via_sms");
	var oEmail = document.getElementById("code_via_email");

	if(tag == 'sms') {
		oSms.style.display = "inline";
		oEmail.style.display = "none";
	}
	else if(tag == 'email') {
		oSms.style.display = "none";
		oEmail.style.display = "inline";
	}
}