var GLOBAL_SESSION_FIRSTNAME = '';
function showCustData()
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert (ajaxErrMsg);
		return;
	}
	loadAccPage(true, null);
	var url = 'account_BillShipping.php?purpose=showCustData&'+Math.random();
	xmlHttp.onreadystatechange = stateChanged_showCustData;
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);
}
function stateChanged_showCustData()
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete')
	{
		if (xmlHttp.responseText.indexOf('E~X~P~I~R~E~D') != -1)
			window.location = 'login.php?m=2';
		else
		{
			loadAccPage(false, xmlHttp.responseText);
			setCountryValue('CountryB', 'billingCountry');
			setCountryValue('CountryS', 'ShippingCountry');			
			setShipToSame();
		}
	}
}

function validNSaveCustAccountForm(cldFrm)
{
	/*=====================
	cldFrm = 1, means called from Account
	cldFrm = 2, means called from signup
	=====================*/	
	var form = document.getElementById('AccountForm');
	if (!IsAddressInfoOk(form)) return false;
/*	if(cldFrm == 1)
	{
		if (!areEmailsSame(form)) return false;
	}*/
	saveCustAccountForm(form, cldFrm);
}
function saveCustAccountForm(form, cldFrm)
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert (ajaxErrMsg);
		return false;
	}
	var postVars = makeCustAccPostVars(form);
	document.getElementById('statusSaveAccForm').innerHTML = '&nbsp;';
	document.getElementById('btnSaveAccount').disabled = true;	
	document.getElementById('statusLoader').style.display = '';
	var url = 'account_BillShipping.php?cldFrm='+cldFrm+'&purpose=saveCustAccountForm&'+Math.random();
	//var url = 'account_BillShipping.php?purpose=saveCustAccountForm&'+Math.random();
	xmlHttp.onreadystatechange = stateChanged_saveCustAccountForm;
	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);
}
function stateChanged_saveCustAccountForm()
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete')
	{
		document.getElementById('btnSaveAccount').disabled = false;
		document.getElementById('statusLoader').style.display = 'none';
		if (xmlHttp.responseText.indexOf('E~R~R~O~R') != -1)
			document.getElementById('statusSaveAccForm').innerHTML = '<font color="#FF0000">' + xmlHttp.responseText.split('HAM~JAM')[1] + '</font>';
		else
		{
			var arrResponse = xmlHttp.responseText.split('~');
			if(document.getElementById('sessionFName'))
				document.getElementById('sessionFName').innerHTML = GLOBAL_SESSION_FIRSTNAME;
			document.getElementById('statusSaveAccForm').innerHTML = arrResponse[0];
		}
	}
}
function makeCustAccPostVars(form)
{
	var postVars = '';
	var shipToSame = form.ShipToSame.checked ? 1 : 0;
	var subscribe = document.getElementById('subscribe1').checked ? 1 : 0;
	postVars += '&FirstNameB='+form.FirstNameB.value;
	GLOBAL_SESSION_FIRSTNAME = form.FirstNameB.value;
	postVars += '&LastNameB='+form.LastNameB.value;
	//postVars += '&Email='+form.username.value;
	postVars += '&Phone='+form.Phone.value;
	postVars += '&Mobile='+form.Mobile.value;
	postVars += '&CompanyB='+form.CompanyB.value;
	postVars += '&Address1B='+form.Address1B.value;
	postVars += '&Address2B='+form.Address2B.value;
	postVars += '&CityB='+form.CityB.value;
	postVars += '&StateB='+form.StateB.value;
	postVars += '&ZipB='+form.ZipB.value;
	postVars += '&CountryB='+form.CountryB.value.split('|')[1];
	postVars += '&shipToSame='+shipToSame;
	postVars += '&FirstNameS='+form.FirstNameS.value;
	postVars += '&LastNameS='+form.LastNameS.value;
	postVars += '&CompanyS='+form.CompanyS.value;
	postVars += '&Address1S='+form.Address1S.value;
	postVars += '&Address2S='+form.Address2S.value;
	postVars += '&CityS='+form.CityS.value;
	postVars += '&StateS='+form.StateS.value;
	postVars += '&ZipS='+form.ZipS.value;
	postVars += '&CountryS='+form.CountryS.value.split('|')[1];
	postVars += '&subscribe='+subscribe;
	//postVars += '&custId='+form.custId.value;
	if(form.Email)
		postVars += '&Email='+form.Email.value;		
	if(form.custId)
		postVars += '&custId='+form.custId.value;
	if(form.username)
		postVars += '&username='+form.username.value;
	if(form.NewPw)
		postVars += '&pwd='+form.NewPw.value;
	
	return postVars;
}
function setShipToSame()
{
	var form = document.getElementById('AccountForm');
	if (form.ShipToSame.checked)
	{				
		form.FirstNameS.value = form.FirstNameB.value;
		form.LastNameS.value = form.LastNameB.value;
		form.CompanyS.value = form.CompanyB.value;
		form.Address1S.value = form.Address1B.value;
		form.Address2S.value = form.Address2B.value;
		form.CityS.value = form.CityB.value;
		form.StateS.value = form.StateB.value;
		form.ZipS.value = form.ZipB.value;
		form.CountryS.value = form.CountryB.value;
		//form.CountryS.value.split('|')[1] = form.CountryB.value.split('|')[1];		
	}
}

function uncheckSameB()
{
	document.getElementById('AccountForm').ShipToSame.checked = false;
}
function setCountryValue(countryDropDown, countryId)
{
	var list = document.getElementById(countryDropDown);
	var id = document.getElementById(countryId).value;
	for (var i=1; i < list.options.length; i++)
	{		
		if(list.options[i].value.split('|')[1] == id)
		{
			list.options[i].selected = true;
			break;
		}
	}
}
