/*
Changes 5/19/09
- IE 8 does not handle the Google GeoEncode correctly when we are on a secure connection.  So we need to wait until after 
	processing the credit card to GeoEncode.
	CHANGES MADE
	1.  Line 111 Commented Out The CheckAddress() function.
	2.  Removed  onblur="CheckAddress();" from ZipCode on SignUp.asp Line 203
	3.  We will make sure the address is sent to the AcceptedContent.asp and from there we will call CheckAddress()
	4.  Line 64 and 67 Commentted out Google Code.
*/


	window.onload = function() {
		document.getElementById("SignUpForm").reset()
	}


//  SignUp
	
	//  Object For Keeping Track of the Number of 
	//  categories selected under each package.	
	var oNumberCats = new Object();
	oNumberCats['TowingCat'] = 0;	
	oNumberCats['RepairCat'] = 0;
	oNumberCats['RepoCat'] = 0;
	oNumberCats['StorageCat'] = 0;
		
	var iTotalCats = 0;


	function AddOrRemoveCat(oCheckBox) {
		mSubMonthlyCost = 0;
		if (oCheckBox.checked) { oNumberCats[oCheckBox.id]++; } else { oNumberCats[oCheckBox.id]--; }
	
		iTotalCats = parseInt(oNumberCats['TowingCat']) + parseInt(oNumberCats['RepairCat'])  + parseInt(oNumberCats['RepoCat']) + parseInt(oNumberCats['StorageCat'])	
	
		if (iTotalCats > 0) { document.getElementById("spListingStatus").innerHTML = ''}
		//StatusText = 'Number of Cats:' + iTotalCats + '<br>\n Name:' + oCheckBox.name + '<br>\n Value:' + oCheckBox.value + '<br>\n Checked:' + oCheckBox.checked + '<hr>'
		//document.getElementById("ItemizedCosts").innerHTML = document.getElementById("ItemizedCosts").innerHTML + StatusText

	
		CalculateCosts();		
		
		oCheckBox.focus();
		
	};

		/* 
		1.  The first single service is $25.  
			a.  Every service after increases the price.
			b.  ALERT the user that the price will be the package price when second service is added.
		2.  If all services are under the same category then charge the package price.
			a.  Set the other category prices to the add on price.
		3.  If they add another category add the category add-on category price.
			a.  ALERT the user of the additional charge.
		*/	
		
	function CalculateCosts() {
		
		//  PRICING IS SET AT THE BOTTOM OF THE SIGNUP.asp PAGE
	
		if (iTotalCats == 1) { 
			mSubMonthlyCost = mSingleCatCost; //  1.  The first service is $25.
		} else {
			if(parseInt(oNumberCats['TowingCat']) > 0) { mSubMonthlyCost = mTRPackageCost; }
			if(parseInt(oNumberCats['RepairCat']) > 0) { 
				if(mSubMonthlyCost > 0) { mSubMonthlyCost = mSubMonthlyCost + mAddOnPackageCost; } else { mSubMonthlyCost = mPackageOnlyCost; }
			}
			if(parseInt(oNumberCats['StorageCat']) > 0) { 
				if(mSubMonthlyCost > 0) { mSubMonthlyCost = mSubMonthlyCost + mAddOnPackageCost; } else { mSubMonthlyCost = mPackageOnlyCost; }
			}
			if(parseInt(oNumberCats['RepoCat']) > 0) { 
				if(mSubMonthlyCost > 0) { mSubMonthlyCost = mSubMonthlyCost + mAddOnPackageCost; }  // Base will always be set by this point.
			}			
		}
		
		document.getElementById("spSubTotalMonthlyPrice").innerHTML = formatCurrency(mSubMonthlyCost);
		document.getElementById("spAnnualDiscount").innerHTML = formatCurrency(mSubMonthlyCost);
		document.getElementById("spSubTotalAnnualPrice").innerHTML = formatCurrency((mSubMonthlyCost - Discount) * 12);
		document.getElementById("spTotalMonthlyPrice").innerHTML = formatCurrency(mSubMonthlyCost - Discount);
		document.getElementById("spTotalAnnualPrice").innerHTML = formatCurrency((mSubMonthlyCost - Discount) * 11);			
		
		document.getElementById("inMonthlyTotalAmount").value = mSubMonthlyCost - Discount
		document.getElementById("inAnnualTotalAmount").value = (mSubMonthlyCost - Discount) * 11
		document.getElementById("spTermsMonthlyAmount").innerHTML = mSubMonthlyCost - Discount
		document.getElementById("spTermsAnnualAmount").innerHTML = (mSubMonthlyCost - Discount) * 11
	}

	function EnterCompanyInformation() {
		if(iTotalCats == 0) {
			document.getElementById("spListingStatus").innerHTML = 'You must first choose services from the above lists.  If you are having trouble please ask a representative for assistance.<br>'
		} else {
			document.getElementById("divChooseListings").style.visibility = 'hidden';
			document.getElementById("divChooseListings").style.display = 'none';
			document.getElementById("divCompanyInformation").style.visibility = 'visible';
			document.getElementById("divCompanyInformation").style.display = 'inline';
			document.getElementById("divPaymentInformation").style.visibility = 'hidden';
			document.getElementById("divPaymentInformation").style.display = 'none';			
		}
	}	
	
	function EnterChooseListings() {
		document.getElementById("divChooseListings").style.visibility = 'visible';
		document.getElementById("divChooseListings").style.display = 'inline';
		document.getElementById("divCompanyInformation").style.visibility = 'hidden';
		document.getElementById("divCompanyInformation").style.display = 'none';
		document.getElementById("divPaymentInformation").style.visibility = 'hidden';
		document.getElementById("divPaymentInformation").style.display = 'none';	
	}

	function EnterPaymentInformation() {
	 	
		//CheckAddress();
		
		if(iTotalCats == 0) { // VALIDATE Choose Listings Form.
			document.getElementById("spListingStatus").innerHTML = 'You must first choose services from the above lists.  If you are having trouble please ask a representative for assistance.<br>'
		} else if (CheckCompanyInformationForm()){ 			// VALIDATE Company Information Form.
			document.getElementById("spCompanyInfoStatus").innerHTML = '';
			oForm = document.SignUpForm;
			document.getElementById("txtConfirmationEmail").value = oForm.ContactEmail.value;
			document.getElementById("divCompanyInformation").style.visibility = 'hidden';
			document.getElementById("divCompanyInformation").style.display = 'none';
			document.getElementById("divPaymentInformation").style.visibility = 'visible';
			document.getElementById("divPaymentInformation").style.display = 'inline';
			
//			alert(document.getElementById("CompanyId").value + ' ' + document.getElementById("CompanyId").value.length)
			if(document.getElementById("CompanyId").value.length == 0) { AddCompanyInfoAJAX(); }
			
		}
	}
	
	function CheckCompanyInformationForm(){
		 var missinginfo = '';
		var co = window.document.SignUpForm
		if(co.ContactName.value == '') {
		missinginfo += "\n     -  Contact Name";
		}
		if(co.ContactPhone.value == '') {
		missinginfo += "\n     -  Contact Phone Number";
		}
		//if(co.ContactEmail.value == '') {
		//missinginfo += "\n     -  Contact Email Address";
		//}
		if(co.Username.value.length < 6) {
		missinginfo += "\n     -  Username (Min. Length 6 Letters)";
		}
		//if(co.Password.value != co.ConfirmPassword.value) {
		//missinginfo += "\n     -  Password and Confirm Password Do Not Match";
		//}
		if(co.Password.value.length < 6) {
		missinginfo += "\n     -  Password (Min. Length 6 Letters)";
		}
		if(co.CompanyName.value == '') {
		missinginfo += "\n     -  Company Name";
		}
		if(co.AreaCode.value == '' || co.PhonePrefix.value == '' || co.PhoneNumber.value == '' || (co.PhonePrefix.value.length + co.PhoneNumber.value.length) < 7) {
		missinginfo += "\n     -  Phone Number";
		}	
		if(document.getElementById("City").value == '') {
		missinginfo += "\n     -  City";
		}	
		if(document.getElementById("StateCode").value == '') {
		missinginfo += "\n     -  State";
		}			
		if(document.getElementById("ZipCode").value == '') {
		missinginfo += "\n     -  ZipCode";
		}
		
		if(missinginfo.length > 1) {
			document.getElementById("spCompanyInfoStatus").innerHTML = '<strong>Action Required:</strong> ' + missinginfo + '<br>';
			missinginfo = '';
			return false;
		} else {
			return true;
		}
		
	}	
	
	function SubmitFormToProcess() {
		return VerifyPaymentForm();
	}	
	
	function VerifyPaymentForm() {
		missinginfo = ''
		var co = window.document.SignUpForm

		if(co.CCardType.value == '') {
		missinginfo += "\n     -  Credit Card Type";
		}
		if(co.CCNumber.value == '') {
		missinginfo += "\n     -  Credit Card Number";
		}
		if(co.ExpirationMonth.selectedIndex == '') {
		missinginfo += "\n     -  Expiration Date";
		}
		if(co.ExpirationYear.selectedIndex == '') {
		missinginfo += "\n     -  Expiration Date";
		}
		
		if(missinginfo.length > 1) {
			document.getElementById("spPaymentInfoStatus").innerHTML = '<strong>Action Required:</strong> ' + missinginfo + '<br>';		
			missinginfo = '';
			ToggleVisible('divProc');
			return false;		
		} else {
			if (isValidCreditCard(co.CCardType.value, co.CCNumber.value)) { 
				co.CCNumber.value = RemoveBad(co.CCNumber.value);	
				return true;
			} else {
				document.getElementById("spPaymentInfoStatus").innerHTML = '<strong>Action Required:</strong> The credit card number you entered does not appear to be valid.<br>';
				ToggleVisible('divProc');				
				return false;
			}
		} 
	}
	
	
//  PAYMENT INFORMATION

function SameAsCompanyAddress() {
	oForm = document.SignUpForm;
	document.getElementById("txtBillingAddress1").value = oForm.Street1.value;
	document.getElementById("txtBillingAddress2").value = oForm.Street2.value;
	document.getElementById("txtBillingCity").value = oForm.City.value;
	document.getElementById("txtBillingZipCode").value = oForm.ZipCode.value;
	SelectFromDropDown('BillingStateCode', oForm.CompanyState.value)
}



function isValidCreditCard(type, ccnum) {
   	if (type == "VS") {
      	var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   	} else if (type == "MC") {
      	var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   	} else if (type == "DV") {
      	var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   	} else if (type == "AX") {
    	var re = /^3[4,7]\d{13}$/;
	}
   if (!re.test(ccnum)) return false;
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}

function CheckPaymentForm() {
	var co = window.document.CheckOut
	document.getElementById("CCNumber").value = RemoveBad(document.getElementById("CCNumber").value);
return true;
}


function AddCompanyInfoAJAX() {

		var co = window.document.SignUpForm
		
		var RequestVars
		
		document.getElementById("divProcessStatus").innerHTML = document.getElementById("divProcessStatus").innerHTML + "<br>Adding Company & Getting CompanyId..."

		RequestVars = 'CompanyId=' + escape(document.getElementById("CompanyId").value)
		RequestVars += '&ContactName=' + escape(co.ContactName.value)
		RequestVars += '&ContactPhone=' + escape(co.ContactPhone.value)
		RequestVars += '&ContactEmail=' + escape(co.ContactEmail.value)
		RequestVars += '&Username=' + escape(co.Username.value)
		RequestVars += '&Password=' + escape(co.Password.value)
		RequestVars += '&CompanyName=' + escape(co.CompanyName.value)
		RequestVars += '&AreaCode=' + escape(co.AreaCode.value)
		RequestVars += '&PhonePrefix=' + escape(co.PhonePrefix.value)
		RequestVars += '&PhoneNumber=' + escape(co.PhoneNumber.value)
		RequestVars += '&PhoneExt=' + escape(co.PhoneExt.value)
		RequestVars += '&CompanyEmail=' + escape(co.CompanyEmail.value)
		RequestVars += '&Referrer=' + escape(co.Referrer.value)
		RequestVars += '&SalesRep=' + escape(co.SalesRep.value)
		RequestVars += '&Street1=' + escape(document.getElementById("Street1").value)
		RequestVars += '&Street2=' + escape(document.getElementById("Street2").value)
		RequestVars += '&City=' + escape(document.getElementById("City").value)
		RequestVars += '&StateCode=' + escape(document.getElementById("StateCode").value)
		RequestVars += '&ZipCode=' + escape(document.getElementById("ZipCode").value)
		RequestVars += '&CouponCode=' + escape(document.getElementById("CouponCode").value)
		RequestVars += '&Latitude=' + escape(document.getElementById("Latitude").value)
		RequestVars += '&Longitude=' + escape(document.getElementById("Longitude").value)


			var oXMLHttp = zXmlHttp.createRequest();
			var url = "AddCompanyData.asp?" + RequestVars
			//			alert(url)
			oXMLHttp.open("GET", url + '&rnd=' + Math.random(), true);
			oXMLHttp.onreadystatechange = function() {
				if(oXMLHttp.readyState == 4) {
					if(oXMLHttp.status == 200){
						var Response = oXMLHttp.responseText;
						HandleAddCompanyResponse(Response)
					} else alert("There was an error while processing this screen.  The document was not ready to process. (Code: ACD-" + oXMLHttp.status + ")  Please try again.");
				}
			};
			oXMLHttp.send(null);
}

function HandleAddCompanyResponse(sRes) {
		document.getElementById("divProcessStatus").innerHTML = document.getElementById("divProcessStatus").innerHTML + "Company Id: " + sRes
		document.getElementById("CompanyId").value = sRes;
}


