/*CFC Ajax Proxy component declares in the header.cfm file*/
var myProxy = new UAproxy();

function serverLookupShipState(thisCountry) {

	myProxy.setCallbackHandler(serverLookupShipStateSuccess);
	myProxy.lookupState(thisCountry);
}

function serverLookupShipStateSuccess(result) {

	AutoComplete_Create('ShipState', result);
}

function serverLookupBillState(thisCountry) {

	myProxy.setCallbackHandler(serverLookupBillStateSuccess);
	myProxy.lookupState(thisCountry);
}

function serverLookupBillStateSuccess(result) {

	AutoComplete_Create('BillState', result);
}

function serverCheckEmailAddress(thisAddress) {

	myProxy.setCallbackHandler(serverUsernameSuccess);
	myProxy.checkUsername(thisAddress);
}

function serverUsernameSuccess(result) {
	if (result == 'Found')
	{
		alert('I am sorry.<br/><br/>Your email address is found in our system.<br/><br/>Please login or enter a different<br/>email address.');
		document.getElementById('EmailAddress').value = '';
		document.formBilling('EmailAddress').value = '';
	}
}

function serverCheckShipState() {
	var stateInput = document.getElementById('ShipState').value;
	var countryInput = document.getElementById('ShipCountry').value;
	
	myProxy.setCallbackHandler(serverShipStateSuccess);
	myProxy.checkStateSpelling(stateInput,countryInput);
}

function serverShipStateSuccess(result) {
	if (result == 'InvalidInput')
	{
		if (document.getElementById('ShipCountry').value == 'US')
		{
			alert('The State is not valid for the<br/>Country of United States.<br/><br/>Please try again.<br/><br/>It may be necessary to choose your Country first so the State/Province<br/>is properly validated.');
		}
		else
		{
			if (document.getElementById('ShipCountry').value == 'CA')
			{
				alert('The Province is not valid for the<br/>Country of Canada.<br/><br/>Please try again.<br/><br/>It may be necessary to choose your Country first so the State/Province<br/>is properly validated.');
			}
			else
			{
				if (document.getElementById('ShipCountry').value == 'DE')
				{
					alert('The Province is not valid for the<br/>Country of Germany.<br/><br/>Please try again.<br/><br/>It may be necessary to choose your Country first so the State/Province<br/>is properly validated.');
				}
				else
				{
				alert('The State/Province is not valid for the Country.<br/><br/>Please try again.<br/><br/>It may be necessary to choose your Country first so the State/Province<br/>is properly validated.');
				}
			}
		}
		document.getElementById('ShipState').value = '';
		document.getElementById('ShipState').focus();
		return false;
	}
	else
	{
		document.getElementById('ShipState').value = result;
		return true;
	}
}

function serverCheckBillState() {
	var stateInput = document.getElementById('BillState').value;
	var countryInput = document.getElementById('BillCountry').value;
	
	myProxy.setCallbackHandler(serverBillStateSuccess);
	myProxy.checkStateSpelling(stateInput,countryInput);
}

function serverBillStateSuccess(result) {
	if (result == 'InvalidInput')
	{
		if (document.getElementById('BillCountry').value == 'US')
		{
			alert('The State is not valid for the<br/>Country of United States.<br/><br/>Please try again.<br/><br/>It may be necessary to choose your<br/>Country first so the State/Province<br/>is properly validated.');
		}
		else
		{
			if (document.getElementById('BillCountry').value == 'CA')
			{
				alert('The Province is not valid for the<br/>Country of Canada.<br/><br/>Please try again.<br/><br/>It may be necessary to choose your<br/>Country first so the State/Province<br/>is properly validated.');
			}
			else
			{
				if (document.getElementById('BillCountry').value == 'DE')
				{
					alert('The Province is not valid for the<br/>Country of Germany.<br/><br/>Please try again.<br/><br/>It may be necessary to choose your<br/>Country first so the State/Province<br/>is properly validated.');
				}
				else
				{
					alert('The State/Province is not valid for the Country.<br/><br/>Please try again.<br/><br/>It may be necessary to choose your<br/>Country first so the State/Province<br/>is properly validated.');
				}
			}
		}
		document.getElementById('BillState').value = '';
		document.getElementById('BillState').focus();
		return false;
	}
	else
	{
		document.getElementById('BillState').value = result;
		return true;
	}
}

function validateNewAccount() {
	var invalid = " "; // Invalid character is a space
	var minLength = 6; // Minimum length
	var illegalChars = /\W/;
	var alertString = "";
	var returnBoolean = true;
	// check for minimum length
	/*if (document.form1.Username.value.length < minLength) {
		alert('Your Username must be at least<br/>' + minLength + ' characters long.<br/><br/>Please try again.');
		document.form1.Username.value = "";
		document.form1.Username.focus();
		returnBoolean = false;
	}
	if (document.form1.Username.value.indexOf(invalid) > -1) {
		alert("Spaces are not allowed in Username.<br/><br/>Please try again.");
		document.form1.Username.value = "";
		document.form1.Username.focus();
		returnBoolean = false;
	}
	if (illegalChars.test(document.form1.Username.value)) {
		alert("Illegal characters are not allowed in Username.<br/><br/>Please try again.");
		document.form1.Username.value = "";
		document.form1.Username.focus();
		returnBoolean = false;
	}*/
	if (document.newCustomer.newPassword.value.length < minLength) {
		alertString = alertString + 'Your password must be at least<br/>' + minLength + ' characters long.<br/><br/>';
		document.newCustomer.confirmPassword.value = "";
		document.newCustomer.newPassword.value = "";
		document.newCustomer.newPassword.focus();
		returnBoolean = false;
	}
	// check for spaces
	if (document.newCustomer.newPassword.value.indexOf(invalid) > -1) {
		alertString = alertString + 'Spaces are not allowed in password.<br/><br/>';
		document.newCustomer.confirmPassword.value = "";
		document.newCustomer.newPassword.value = "";
		document.newCustomer.newPassword.focus();
		returnBoolean = false;
	}
	if (document.newCustomer.newPassword.value != document.newCustomer.confirmPassword.value) {
		alertString = alertString + 'You did not enter the same password twice.<br/><br/>';
		document.newCustomer.confirmPassword.value = "";
		document.newCustomer.newPassword.value = "";
		document.newCustomer.newPassword.focus();
		returnBoolean = false;
	}
	/*if (document.newCustomer.SecurityAnswer.value.indexOf(invalid) > -1) {
		alertString = alertString + 'Your Security Answer must be one word.<br/><br/>';
		document.newCustomer.SecurityAnswer.value = "";
		document.newCustomer.SecurityAnswer.focus();
		returnBoolean = false;
	}*/
	if (returnBoolean == false)
	{
		alert(alertString + 'Please try again.');
		return false;
	}
	else
	{
		var emailInput = document.newCustomer.EmailAddress.value;
		var passwordInput = document.newCustomer.newPassword.value;
		var questionInput = document.newCustomer.SecurityQuestion.value;
		var answerInput = document.newCustomer.SecurityAnswer.value;		

		document.newCustomer.EmailAddress.value = '';
		document.newCustomer.newPassword.value = '';
		document.newCustomer.confirmPassword.value = '';
		document.newCustomer.SecurityQuestion.value = '';
		document.newCustomer.SecurityAnswer.value = '';
		document.getElementById('MyUsername').value = emailInput;
		document.getElementById('MyPassword').value = passwordInput;
		
		myProxy.setCallbackHandler(serverNewCustomerSuccess);
		myProxy.addNewCustomer(emailInput,passwordInput,questionInput,answerInput);
		return false;
	}
}

function validateNewEmailAccount() {
	var newEmailInput = document.newEmail.newEmail.value;
	var confirmEmailInput = document.newEmail.confirmEmail.value;
	
	if(newEmailInput != confirmEmailInput)
	{
		document.newEmail.newEmail.value = "";
		document.newEmail.confirmEmail.value = "";
		document.newEmail.newEmail.focus();
		alert('Your new and confirm email addresses<br/>do not match.<br/><br/>Please try again.');
		return false;
	}
	else
	{
		return true;
	}

}

function validateCreateAccount() {
	var invalid = " "; // Invalid character is a space
	var minLength = 6; // Minimum length
	var illegalChars = /\W/;
	var alertString = "";
	var returnBoolean = true;
	// check for minimum length
	/*if (document.form1.Username.value.length < minLength) {
		alert('Your Username must be at least<br/>' + minLength + ' characters long.<br/><br/>Please try again.');
		document.form1.Username.value = "";
		document.form1.Username.focus();
		returnBoolean = false;
	}
	if (document.form1.Username.value.indexOf(invalid) > -1) {
		alert("Spaces are not allowed in Username.<br/><br/>Please try again.");
		document.form1.Username.value = "";
		document.form1.Username.focus();
		returnBoolean = false;
	}
	if (illegalChars.test(document.form1.Username.value)) {
		alert("Illegal characters are not allowed in Username.<br/><br/>Please try again.");
		document.form1.Username.value = "";
		document.form1.Username.focus();
		returnBoolean = false;
	}*/
	if (document.newCustomer.newPassword.value.length < minLength) {
		alertString = alertString + 'Your password must be at least<br/>' + minLength + ' characters long.<br/><br/>';
		document.newCustomer.confirmPassword.value = "";
		document.newCustomer.newPassword.value = "";
		document.newCustomer.newPassword.focus();
		returnBoolean = false;
	}
	// check for spaces
	if (document.newCustomer.newPassword.value.indexOf(invalid) > -1) {
		alertString = alertString + 'Spaces are not allowed in password.<br/><br/>';
		document.newCustomer.confirmPassword.value = "";
		document.newCustomer.newPassword.value = "";
		document.newCustomer.newPassword.focus();
		returnBoolean = false;
	}
	if (document.newCustomer.newPassword.value != document.newCustomer.confirmPassword.value) {
		alertString = alertString + 'You did not enter the same password twice.<br/><br/>';
		document.newCustomer.confirmPassword.value = "";
		document.newCustomer.newPassword.value = "";
		document.newCustomer.newPassword.focus();
		returnBoolean = false;
	}
	/*if (document.newCustomer.SecurityAnswer.value.indexOf(invalid) > -1) {
		alertString = alertString + 'Your Security Answer must be one word.<br/><br/>';
		document.newCustomer.SecurityAnswer.value = "";
		document.newCustomer.SecurityAnswer.focus();
		returnBoolean = false;
	}*/
	if (returnBoolean == false)
	{
		alert(alertString + 'Please try again.');
		return false;
	}
	else
	{
		return true;
	}
}

function cleanCreditCard()
{
	var thisCC = document.getElementById('CCNum').value;
	thisCC = thisCC.replace(/[^0-9]+/g,'');
	document.getElementById('CCNum').value = thisCC;
	
	/*4= Visa, 5= Master Card, 6= Discover, 3= American Express*/
}

function validatePOBox()
{
	var checkPOBox = new RegExp("[pP]{1}[.]*[oO]{1}[.]*[ ]*[bB]{1}[oO]{1}[xX]{1}"); 
	var thisShip1 = document.formBilling.ShipAddress1.value;
	var thisShip2 = document.formBilling.ShipAddress2.value;
		
	if (thisShip1.match(checkPOBox) != null || thisShip2.match(checkPOBox) != null)
	{
		var answer = confirm("You have entered a Post Office Box for the shipping address. Your order will be mailed using USPS Priority. If you wish to ship using UPS or Fedex, you will need to enter a physical shipping address. Continue using the Post Office Box address?")
		if (answer)
		{
			document.formBilling.ShipPOBox.value = 1;
			return true;
		}
		else
		{
			document.formBilling.ShipPOBox.value = 0;
			return false;
		}
	}
	else
		return true;
}

function ckPOBox(strAddress)
{
	var checkPOBox = new RegExp("[pP]{1}[.]*[oO]{1}[.]*[ ]*[bB]{1}[oO]{1}[xX]{1}"); 
		
	if (strAddress.match(checkPOBox) != null)
		return true;
	else
		return false;
}

function serverNewCustomerSuccess(result) 
{
	if (result == 'Success')
	{
		alert('Welcome to the Earthy Delights!<br/><br/>You may now login using your<br/>email address and password.');
		return false;
	}
	else
	{
		alert('I am sorry.<br/><br/>The was an issue with the system that prevents from creating your new account.<br/><br/>Please try again.');
		return false;
	}
}

function setNotSameShip() 
{
	document.formBilling.ShipSame[1].checked = true;
}

function SetBillToShip(sVal) 
{
	if(sVal == 'Yes') {
		document.formBilling.ShipFirstName.value = document.formBilling.BillFirstName.value;
		document.formBilling.ShipLastName.value = document.formBilling.BillLastName.value;
		document.formBilling.ShipBusinessName.value = document.formBilling.BillBusinessName.value;
		document.formBilling.ShipAddress1.value = document.formBilling.BillAddress1.value;
		document.formBilling.ShipAddress2.value = document.formBilling.BillAddress2.value;
		document.formBilling.ShipCity.value = document.formBilling.BillCity.value;
		document.formBilling.ShipState.value = document.formBilling.BillState.value;
		document.formBilling.ShipZip.value = document.formBilling.BillZip.value;
		document.formBilling.ShipCountry.value = document.formBilling.BillCountry.value;
	}
	else
	{
		document.formBilling.ShipFirstName.value = "";
		document.formBilling.ShipFirstName.style.required = "Yes";
		document.formBilling.ShipLastName.value = "";
		document.formBilling.ShipBusinessName.value = "";
		document.formBilling.ShipAddress1.value = "";
		document.formBilling.ShipAddress2.value = "";
		document.formBilling.ShipCity.value = "";
		document.formBilling.ShipState.value = "";
		document.formBilling.ShipZip.value = "";
		document.formBilling.ShipCountry.value = document.formBilling.BillCountry.value;
		document.formBilling.ShipFirstName.focus();
	}
}

function submitform()
{
	if(document.form1.onsubmit())
		{
			document.form1.submit();
		}
}

/*PositionX = 100;
PositionY = 100;

defaultWidth  = 500;
defaultHeight = 500;

var AutoClose = true;

if (parseInt(navigator.appVersion.charAt(0))>=4){
var isNN=(navigator.appName=="Netscape")?1:0;
var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;
function popImage(imageURL,imageTitle){
if (isNN){imgWin=window.open('Card_Member_ID','',optNN);}
if (isIE){imgWin=window.open('Card_Member_ID','',optIE);}
with (imgWin.document){
writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(300,300);');
writeln('width=300-(document.body.clientWidth-document.images[0].width);');
writeln('height=300-(document.body.clientHeight-document.images[0].height);');
writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
if (!AutoClose) writeln('</head><body bgcolor=ffffff scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
else writeln('</head><body bgcolor=ffffff scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
close();		
}}*/	

function toggledetailsMouseOver(id) {
   var e = document.getElementById(id);
   e.style.display = 'block';
}
function toggledetailsMouseOut(id) {
   var e = document.getElementById(id);
   e.style.display = 'none';
}

/*function scrollIt(myH,myV){
	window.scrollTo(myH,myV);
}

function setCoords(id){
	var scrH = 0;
	var scrV = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrV = window.pageYOffset;
		scrH = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrV = document.body.scrollTop;
		scrH = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrV = document.documentElement.scrollTop;
		scrH = document.documentElement.scrollLeft;
	}
	var e = document.getElementById(id);
	e.myScrollH.value = scrH;
	e.myScrollV.value = scrV;
}*/

function setShipAmount(thisMethod,thisCost) {
	document.getElementById('myShipMethod').value = thisMethod;	
	document.getElementById('myShipAmount').value = thisCost;
	document.getElementById('lblShipping').innerHTML = '$' + CurrencyFormatted(thisCost);
	var t = document.getElementById('CheckoutTotal').value;
	var tTotal = thisCost*1 + t*1;
	document.getElementById('lblTotal').innerHTML = '$' + CurrencyFormatted(tTotal);
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function checkShipSelected() {
	if (document.getElementById('myShipMethod').value == '')
	{
		alert('Please choose your shipping.');
		return false;
	}
	else
	{
		return true;
	}
}

function checkExpDate()
{
	var CCTodayDate = new Date();
	var CCTodayYear = CCTodayDate.getFullYear();
	var CCTodayMonth = CCTodayDate.getMonth();
	CCTodayMonth = CCTodayMonth + 1;
	var CCExpDateYear = document.form1.CCExpYear.value;
	var CCExpDateMonth = document.form1.CCExpMonth.value;
	CCExpDateMonth = stripZeros(CCExpDateMonth);
	
	if (CCTodayYear == CCExpDateYear)
	{
		if (CCExpDateMonth < CCTodayMonth)
		{
			alert('The expiration date has expired.<br/><br/>Please try again.');
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}
function Right(str, n)
{
      if (n <= 0)
          return "";
      else if (n > String(str).length)
          return str;
      else
   {
          var iLen = String(str).length;
          return String(str).substring(iLen, iLen - n);
      }
}
function Left(str, n)
{
   if (n <= 0)
         return "";
   else if (n > String(str).length)
         return str;
   else
         return String(str).substring(0,n);
}
function stripZeros(inputStr) {
	var result = inputStr
	while (result.substring(0,1) == "0") {
		result = result.substring(1,result.length)
	}
	return result
}

function divShowHideCheckout(thisID)
{
	document.getElementById('divLogin').style.display='none';
	document.getElementById('divCreate').style.display='none';
	document.getElementById('divSkip').style.display='none';
	if (thisID == 'login')
		document.getElementById('divLogin').style.display='block';
	if (thisID == 'create')
		document.getElementById('divCreate').style.display='block';
	if (thisID == 'skip')
		document.getElementById('divSkip').style.display='block';
}

function divShowHideDetails(thisID)
{
	if (document.getElementById(thisID).style.display == 'none')
		document.getElementById(thisID).style.display='block';
	else
		document.getElementById(thisID).style.display='none';
}

