function openTaC(url) {
	window.open(url, 'TaC', 'width=450,height=300,toolbars=no,menubar=no,resizeable=no,scrollbars=yes');
}

function getPage(requestedPage) {
    document.digiSHOP2.m.value = "<?=PRODUCT_LIST_CMD?>";
    document.digiSHOP2.target = "_self";
    document.digiSHOP2.pageNumber.value = requestedPage;

    document.digiSHOP2.submit();
    return false;
}

function checkReturnCustomerOption(){
    document.digiSHOP.elements[3].checked = true;
    if(document.digiSHOP.cEmail2.value){
        document.digiSHOP.cEmail1.value = document.digiSHOP.cEmail2.value;
        document.digiSHOP.cPass1.value = document.digiSHOP.cPass2.value;
    }

    document.digiSHOP.cEmail2.value = "";
    document.digiSHOP.cPass2.value = "";
}

function checkNewCustomerOption(){
    document.digiSHOP.elements[6].checked = true;
    if(document.digiSHOP.cEmail1.value){
        document.digiSHOP.cEmail2.value = document.digiSHOP.cEmail1.value;
        document.digiSHOP.cPass2.value = document.digiSHOP.cPass1.value;
    }
    document.digiSHOP.cEmail1.value = "";
    document.digiSHOP.cPass1.value = "";
}

function forgotPassword(page){
    var cEmail = document.digiSHOP.cEmail1.value;
    window.location.href=page+'?m=password&cEmail='+cEmail;
}

function forgotEmail(page){    
    window.location.href=page+'?m=emailaddress';
}

function sendReminderEmail(page){
    var cEmail = document.digiSHOP.cEmail2.value;
    window.location.href=page+'?m=password&submit=true&cEmail='+cEmail;
}

function returnToLogin(page){
    var cEmail = document.digiSHOP.cEmail.value;
    window.location.href=page+'?m=client_login&cEmail2='+cEmail;
}

function validatePasswords() {
	var firstPass = document.getElementById('pass1');
	var secondPass = document.getElementById('pass2');

	if (firstPass.value != secondPass.value) {
		alert("Your password confirmation doesn't match. Please try again.");
		secondPass.value = '';
		secondPass.focus();
		return false;
	}

	return true;
}

function doOnLoadBilling(strState) {
    initializeBilling();

    /* We need to go back and reset to our database values */
	if(document.address.caProvinceBilling) {
		for (i = 0; i < document.address.caProvinceBilling.options.length; i++) {
			if (document.address.caProvinceBilling.options[i].value == strState) {
				document.address.caProvinceBilling.selectedIndex = i;
			}
		}
	}
	for (i = 0; i < document.address.usStateBilling.options.length; i++) {
		if (document.address.usStateBilling.options[i].value == strState) {
			document.address.usStateBilling.selectedIndex = i;
		}
	}
}

function doOnLoadShipping(strState, strCounty) {
    /* We need to go back and reset to our database values */
	if(document.address.caProvinceShipping) {	
		for (i = 0; i < document.address.caProvinceShipping.options.length; i++) {
			if (document.address.caProvinceShipping.options[i].value == strState) {
				document.address.caProvinceShipping.selectedIndex = i;
			}
		}
	}
	for (i = 0; i < document.address.usStateShipping.options.length; i++) {
		if (document.address.usStateShipping.options[i].value == strState) {
			document.address.usStateShipping.selectedIndex = i;
		}
	}

	var curCounty = eval("document.address.usCountiesShipping" + document.address.usStateShipping.options[document.address.usStateShipping.selectedIndex].value);

	for (i = 0; i < curCounty.options.length; i++) {
		if (curCounty.options[i].value == strCounty) {
			curCounty.selectedIndex = i;
		}
	}
    initializeShipping();
}

function confirmMsg(msg){
  if(!confirm(msg)){
      return false;
  } else {
      return true;
  }
}

function submitForm(fieldId, fieldValue) {
    digiShopObj = document.getElementById(fieldId);

    try {
        digiShopObj.value = fieldValue;
        digiShopObj.form.submit();
        return false;
    } catch (e) {
        alert('There was an error updating your shopping cart. Please contact us with this error.');
        return false;
    }

}

function openChangeRId() {
	var winOpen = window.open('crid.htm', 'newRealtorId', 'width=500,height=75,status=yes');
}

function doOnLoadPayment(ccNumber, ccName, ccExpiry, ccCvv2) {
	ccEnableDisable(true, ccNumber, ccName, ccExpiry, ccCvv2);
}

function changePaymentType(paymentType, ccNumber, ccName, ccExpiry, ccCvv2) {
	if (paymentType != 'Credit Card') {
		var txtCcType = document.getElementById('cc_type');
		txtCcType.selectedIndex = 0;
		ccEnableDisable(false, ccNumber, ccName, ccExpiry, ccCvv2);
	}
}

function ccEnableDisable(changePaymentType, ccNumber, ccName, ccExpiry, ccCvv2) {
	if (changePaymentType == null) {
		changePaymentType = true;
	}

	var lblCcType = document.getElementById('lbl_cc_type');
	var lblCcNumber = document.getElementById('lbl_cc_number');
	var lblCcHolder = document.getElementById('lbl_cc_holdersname');
	var lblCcExpiration = document.getElementById('lbl_cc_expiration');
	var lblCcExpirationNote = document.getElementById('lbl_cc_expiration_note');
	var lblCcCVV2 = document.getElementById('lbl_cc_cvv2');
	var lblCcCVV2Note = document.getElementById('lbl_cc_cvv2_note');

	var txtCcType = document.getElementById('cc_type');
	var txtCcNumber = document.getElementById('cc_number');
	var txtCcHolder = document.getElementById('cc_holdersname');
	var txtCcExpiration = document.getElementById('cc_expiration');
	var txtCcCVV2 = document.getElementById('cc_cvv2');

	var rwCcNumber = document.getElementById('cc_number_row');
	var rwCcHolder = document.getElementById('cc_holdersname_row');
	var rwCcExpiration = document.getElementById('cc_expiration_row');
	var rwCcCVV2 = document.getElementById('cc_cvv2_row');

	var txtSelectedCcType = txtCcType.options[txtCcType.selectedIndex];
	var objPaymentType = document.getElementById('idCreditCard');

	if ((changePaymentType == true) && (objPaymentType.checked == false)) {
		objPaymentType.checked = true;
	}

	if (txtSelectedCcType.value != 'Existing') {
		txtCcNumber.value = '';
		txtCcHolder.value = '';
		txtCcExpiration.value = '';
		txtCcCVV2.value = '';
	}
	else {
		txtCcNumber.value = ccNumber;
		txtCcHolder.value = ccName;
		txtCcExpiration.value = ccExpiry;
		txtCcCVV2.value = ccCvv2;
	}

	if (txtSelectedCcType.value == '' || txtSelectedCcType.value == 'Existing') {
		lblCcNumber.style.color = 'silver';
		txtCcNumber.disabled = true;

		lblCcHolder.style.color = 'silver';
		txtCcHolder.disabled = true;

		lblCcExpiration.style.color = 'silver';
		lblCcExpirationNote.style.color = 'silver';
		txtCcExpiration.disabled = true;

		lblCcCVV2.style.color = 'silver';
		lblCcCVV2Note.style.color = 'silver';
		txtCcCVV2.disabled = true;
	} else {
		lblCcNumber.style.color = 'black';
		txtCcNumber.disabled = false;

		lblCcHolder.style.color = 'black';
		txtCcHolder.disabled = false;

		lblCcExpiration.style.color = 'black';
		lblCcExpirationNote.style.color = 'black';
		txtCcExpiration.disabled = false;

		lblCcCVV2.style.color = 'black';
		lblCcCVV2Note.style.color = 'black';
		txtCcCVV2.disabled = false;
	}

	if (txtSelectedCcType.value == 'Existing') {
		rwCcNumber.style.display = 'block';
		rwCcHolder.style.display = 'none';
		rwCcExpiration.style.display = 'none';
		rwCcCVV2.style.display = 'none';
	}
	else if (txtSelectedCcType.value == '') {
		rwCcNumber.style.display = 'none';
		rwCcHolder.style.display = 'none';
		rwCcExpiration.style.display = 'none';
		rwCcCVV2.style.display = 'none';
	}
	else {
		rwCcNumber.style.display = 'block';
		rwCcHolder.style.display = 'block';
		rwCcExpiration.style.display = 'block';
		rwCcCVV2.style.display = 'block';
	}
}

function applyDisplay(element, value) {
    document.getElementById(element).style.display = value;
}

function validCCExp() {
	var ccexp = document.getElementById('cc_expiration');
	var ccfield = document.getElementById('typeCC');

	if (ccfield.checked == true) {

    	var expDate = ccexp.value.split('/');

    	if (expDate[0] == ccexp.value) {
    		alert("You must enter the credit card expiration date as mm/yy");
    		ccexp.select();
    		ccexp.focus();
    		return false;
    	}
	}

	return true;
}
