
var xmlHttp;
var ajaxErrMsg = 'Your Browser does not support HTTP Requests!';

function GetXmlHttpObject()
{ 
	var objXMLHttp = null;
	if (window.XMLHttpRequest)
		objXMLHttp = new XMLHttpRequest();
	else if (window.ActiveXObject)
		objXMLHttp = new ActiveXObject('Microsoft.XMLHTTP');
	return objXMLHttp;
}

function sendMsg(custEmail, OrderId)
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert (ajaxErrMsg);
		return false;
	}
	var subject = document.getElementById('subject').value;
	if (subject == '')
	{
		alert ('Please provide Subject!');
		document.getElementById('subject').focus();
		return false;
	}
	var message = document.getElementById('message').value;
	if (message == '')
	{
		alert ('Please provide Message!');
		document.getElementById('message').focus();
		return false;
	}
	document.getElementById('btnSendMsg').disabled = true;
	document.getElementById('statusLoaderSendMsg').style.display = '';
	document.getElementById('Status').innerHTML = '';
	var url = 'ajax.php?custEmail='+custEmail+'&OrderId='+OrderId+'&subject='+subject+'&message='+message+'&purpose=sendMsg&'+Math.random();
	xmlHttp.onreadystatechange = stateChanged_sendMsg;
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);
}
function stateChanged_sendMsg() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete')
	{
		document.getElementById('subject').value = '';
		document.getElementById('message').value = '';
		document.getElementById('btnSendMsg').disabled = false;
		document.getElementById('statusLoaderSendMsg').style.display = 'none';
		document.getElementById('Status').innerHTML = '<b>' + xmlHttp.responseText + '</b>';
	}
}

function proceedLogin()
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert (ajaxErrMsg);
		return;
	}
	var postVars = '&username='+document.getElementById('txtEmail').value;
	postVars += '&pwd='+document.getElementById('pwd').value;
	document.getElementById('statusLoader').style.display = '';
	document.getElementById('tdError').innerHTML = '&nbsp;';
	document.getElementById('btnLogin').disabled = true;
	xmlHttp.onreadystatechange = stateChanged_proceedLogin;	
	var url = 'ajax.php?purpose=proceedLogin&'+Math.random();
	xmlHttp.open('POST', url, true);
	xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; text/xml; charset=utf-8');
	xmlHttp.setRequestHeader('Content-length', postVars.length);
	xmlHttp.setRequestHeader('Connection', 'close');
	xmlHttp.send(postVars);
	
}
function stateChanged_proceedLogin()
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete')
	{
		document.getElementById('statusLoader').style.display = 'none';
		if (xmlHttp.responseText.indexOf('E~R~R~O~R') != -1)
		{
			document.getElementById('pwd').value = '';
			document.getElementById('pwd').focus();
			document.getElementById('btnLogin').disabled = false;
			var arrResponse = new Array();
			arrResponse = xmlHttp.responseText.split('HAM~JAM');
			document.getElementById('tdError').innerHTML = '<font class="errMsg">' + arrResponse[1] + '</font>';
		}
		else
			window.location = xmlHttp.responseText;
	}
}

function Couponvalidation()
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert (ajaxErrMsg);
		return false;
	}
	
	
	var sub_total=document.getElementById('sub_total').value;
	if(sub_total=='' || sub_total == "0")
	{
		alert('Please Fill Your Cart !');
		return false;
	}
	var coupon=document.getElementById('txtCoupon').value;
	if(coupon=='')
	{
		alert('Please write Coupon Code!');
		document.getElementById('txtCoupon').focus();
		return false;
	}
	
	var postVars = '&couponcode='+coupon;
	document.getElementById('waitcoupon').innerHTML  = '<img src="include/images/ajax-loader_saas.gif" border="0" />';
	document.getElementById('btnvalidation').disabled = true;
	document.getElementById('couponresult').innerHTML = '';
	xmlHttp.onreadystatechange = stateChanged_CouponValidation;
	var url = 'ajax.php?purpose=couponvalidation&'+Math.random();
	xmlHttp.open('POST', url, true);
	xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; text/xml; charset=utf-8');
	xmlHttp.setRequestHeader('Content-length', postVars.length);
	xmlHttp.setRequestHeader('Connection', 'close');
	xmlHttp.send(postVars);
}
function stateChanged_CouponValidation() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete') {
		document.getElementById('waitcoupon').innerHTML = '';
		document.getElementById('btnvalidation').disabled = false;
		if (xmlHttp.responseText.indexOf('E~R~R~O~R') != -1)
		{
			var arrResponse = new Array();
			arrResponse = xmlHttp.responseText.split('HAM~JAM');
			document.getElementById('couponresult').innerHTML  = '<font class="errMsg" style="font-weight:bold; font-style:italic;">'+arrResponse[1]+'</font>';
		}
		else
		{
			var sub_total=document.getElementById('sub_total').value;
			var discount_val= (sub_total)*(xmlHttp.responseText/100);
			var diffval = sub_total-discount_val.toFixed(2);
			document.getElementById('discount').value=xmlHttp.responseText;//discount_val.toFixed(2);
			document.getElementById('couponresult').innerHTML = '<font style="font-weight:bold; font-style:italic;"> Congratulations! You saved '+xmlHttp.responseText+' % off your Merchandise Subtotal';
			document.getElementById('tblcoupendetail').style.display= '';
			////////////assign value to table
			//document.getElementById('mamount').innerHTML='USD' + ' ' + sub_total;
			//document.getElementById('dpercent').innerHTML= xmlHttp.responseText;
			document.getElementById('damount').innerHTML= discount_val.toFixed(2);
			document.getElementById('udmamount').innerHTML= (diffval).toFixed(2);
			
			document.getElementById('sub_total_h').value=(diffval).toFixed(2);
			document.getElementById('sub_total_actual').value=(diffval).toFixed(2);
			document.getElementById('CurrConversion').value = CURRENCY_ABV + ' ' + Currency((diffval).toFixed(2));
			
			if(document.getElementById('GrandTotal').value!='' && document.getElementById('GrandTotal').value!=0)
			{
				calculateTotal();
			}
				
		}
	}
}

function loginUser()
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert (ajaxErrMsg);
		return false;
	}
	var postVars = '&username='+document.getElementById('username').value;
	postVars += '&pwd='+document.getElementById('pwd').value;
	document.getElementById('statusLoginUser').style.display  = '';
	document.getElementById('btnContUser').disabled = true;
	document.getElementById('errorLogin').innerHTML = '';
	xmlHttp.onreadystatechange = stateChanged_loginUser;
	var url = 'ajax.php?purpose=loginUser&'+Math.random();
	xmlHttp.open('POST', url, true);
	xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; text/xml; charset=utf-8');
	xmlHttp.setRequestHeader('Content-length', postVars.length);
	xmlHttp.setRequestHeader('Connection', 'close');
	xmlHttp.send(postVars);		
}
function stateChanged_loginUser() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete') {
		document.getElementById('btnContUser').disabled = false;
		document.getElementById('statusLoginUser').style.display = 'none';
		if (xmlHttp.responseText.indexOf('E~R~R~O~R') != -1)
		{
			var arrResponse = new Array();
			arrResponse = xmlHttp.responseText.split('HAM~JAM');
			if(xmlHttp.responseText.indexOf('blocked') != -1)
			{
				document.getElementById('errorLogin').innerHTML  = '<font class="errMsg">'+arrResponse[1]+'</font>';
			}
			else
			{
				document.getElementById('errorLogin').innerHTML  = '<font class="errMsg">'+arrResponse[1]+'</font><a href="unlock.php" target="_blank" style="padding-left:55px;" class="menutext">Forgot your password?</a>';
			}
			resetLoginForm();
		}
		else
		{
			document.getElementById('remAreas').innerHTML = xmlHttp.responseText;
			doRemActions();
		}
	}
}

function checkUserValidity()
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert (ajaxErrMsg);
		return false;
	}
	document.getElementById('statusLoginUser').style.display  = '';
	document.getElementById('btnContUser').disabled = true;
	document.getElementById('errorLogin').innerHTML = '';
	var username = document.getElementById('username').value;
	//var emailId = document.getElementById('emailId').value;
	var url = 'ajax.php?username='+username+'&purpose=checkUserValidity&'+Math.random();
	xmlHttp.onreadystatechange = stateChanged_checkUserValidity;
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);
}
function stateChanged_checkUserValidity()
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete') {
		document.getElementById('btnContUser').disabled = false;
		document.getElementById('statusLoginUser').style.display = 'none';
		if (xmlHttp.responseText.indexOf('E~R~R~O~R') != -1)
		{
			var arrResponse = new Array();
			arrResponse = xmlHttp.responseText.split('HAM~JAM');
			document.getElementById('errorLogin').innerHTML  = '<font class="errMsg">'+arrResponse[1]+'</font>';
			resetLoginForm();
		}
		else
		{
			document.getElementById('remAreas').innerHTML = xmlHttp.responseText;
			doRemActions();
		}
	}
}
function checkUserAvailability(username)
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert (ajaxErrMsg);
		return
	}
	document.getElementById('status_usrAvbLoader').style.display = '';
	document.getElementById('Status_ChkUsr').innerHTML = '&nbsp;';
	var url = 'ajax.php?username='+username+'&purpose=checkUserAvailability&'+Math.random();
	xmlHttp.onreadystatechange = stateChanged_checkUserAvailability;
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);
}
function stateChanged_checkUserAvailability()
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete')
	{		
		document.getElementById('status_usrAvbLoader').style.display = 'none';
		document.getElementById('Status_ChkUsr').innerHTML = xmlHttp.responseText;		
	}
}
function checkCouponAvailablity()
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert (ajaxErrMsg);
		return false;
	}
	var CouponCode = document.getElementById('CouponCode').value;
	var sub_total  = document.getElementById('sub_total_actual').value;
	document.getElementById('btnCouponCode').disabled = true;
	document.getElementById('errorCouponCode').innerHTML = '';
	var url = 'ajax.php?CouponCode='+CouponCode+'&sub_total='+sub_total+'&purpose=checkCouponAvailablity&'+Math.random();
	xmlHttp.onreadystatechange = stateChanged_checkCouponAvailablity;
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);
}
function stateChanged_checkCouponAvailablity() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete')
	{
		document.getElementById('btnCouponCode').disabled = false;
		document.getElementById('statusCouponCode').innerHTML  = '&nbsp;';
		var arrResponse = new Array();
		arrResponse = xmlHttp.responseText.split('HAM~JAM');
		if (xmlHttp.responseText.indexOf('E~R~R~O~R') != -1)	// ERROR case...
		{
			document.getElementById('errorCouponCode').innerHTML  = '<font class="errMsg">'+arrResponse[1]+'</font>';
			document.getElementById('CouponCode').select();
		}
		else
		{
			document.getElementById('errorCouponCode').innerHTML  = '<font class="errMsg">'+arrResponse[0]+'</font>';
			document.getElementById('sub_total').value     = Currency(arrResponse[1]);
			document.getElementById('sub_total_h').value   = arrResponse[1];
			document.getElementById('CouponDetails').value = arrResponse[2];
			document.getElementById('CouponId').value = arrResponse[3];
			document.getElementById('CouponCode_h').value = document.getElementById('CouponCode').value;
			document.getElementById('CouponCode').value = '';
			calculateTotal();
		}
	}
	else
		document.getElementById('statusCouponCode').innerHTML  = '<img src="include/images/ajax-loader_saas.gif" border="0" />';
}

function sendCartInfo(arrCart, cartLength)
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert (ajaxErrMsg);
		return false;
	}
	var SSurcharge = document.getElementById('FreeShipping').value == 1 ? document.getElementById('default_SSurcharge').value : parseFloat(document.getElementById('SSurcharge').value);
	var priority = 'No';
	if (document.getElementById('ExpShipping').value == 1 || document.getElementById('ExpShipping').value == 3)
		priority = 'Yes';
	var postVars = '';
	for (var i = 0; i < cartLength; i++)
	{
		postVars += '&itemStore'+i+'='+arrCart['itemStore'+i];
		postVars += '&itemDesc'+i+'='+arrCart['itemDesc'+i];
		postVars += '&itemProduct'+i+'='+arrCart['itemProduct'+i];
		postVars += '&itemSize'+i+'='+arrCart['itemSize'+i];
		postVars += '&itemColor'+i+'='+arrCart['itemColor'+i];
		postVars += '&itemQuantity'+i+'='+arrCart['itemQuantity'+i];
		postVars += '&itemPrice'+i+'='+arrCart['itemPrice'+i];
		postVars += '&itemTotal'+i+'='+arrCart['itemTotal'+i];
	}
	postVars += '&sub_total='+document.getElementById('sub_total').value;
	postVars += '&domShipping='+document.getElementById('DomShipping').value;
	postVars += '&intHandling='+document.getElementById('IntHandling').value;
	postVars += '&shippingCountry='+document.getElementById('Zone').value.split('|')[1];
	postVars += '&SSurcharge='+SSurcharge;
	postVars += '&PPI='+document.getElementById('PPI').value;
	postVars += '&Priority='+priority;
	postVars += '&intShipping='+document.getElementById('IntShipping').value;
	postVars += '&fees='+document.getElementById('fees').value;
	postVars += '&shipping='+document.getElementById('Shipping').value;
	postVars += '&cartLength='+cartLength;
	var url = 'ajax.php?purpose=sendCartInfo&'+Math.random();
	xmlHttp.open('POST', url, true);
    xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xmlHttp.setRequestHeader('Content-length', postVars.length);
    xmlHttp.setRequestHeader('Connection', 'close');
	xmlHttp.send(postVars);
}
