//**************************************************************
// This file contains all javascript for the address pages
// This file contains js from following files:
// * 2.3.1.a.js
// * 2.3.1.b.js
// * 2.4.1.a.js
// * 2.4.1.b.js
//
// Messages from these have been moved to <msDir>/<locale>/errMsg

//**************************************************************

// These objects will hold information about an address that we read on load of page. 
// They can then be used to compare a value before submitting to see if it has been modified
var shipAddressAtLoad = new Object();
var	billAddressAtLoad = new Object();

// These objects will hold information about an address at time of submitting the page. Can then 
// be compared to the onLoad objects.
var shipAddressAtSubmit = new Object();
var billAddressAtSubmit = new Object();


/**
* The function will read address information from the form passed to it.
* billAddr and shipAddr are objects and the following fields will be 
* created and set on the object
* - city
* - state
* - zipCode
*/
function readAddresses(formName, billAddr, shipAddr) {

	theForm = document.forms[formName];

	// read the billing address, only if a billaddress object is passed
	if (billAddr != null) {
		if (theForm.city != null) {
			billAddr.city = theForm.city.value;
		}
		if (theForm.state != null) {
			billAddr.state = theForm.state.value;
		}
		if (theForm.zipCode != null) {
			billAddr.zipCode = theForm.zipCode.value;
		}
	}
	
	// read the shipping address, only if a shipaddress object is passed
	if (shipAddr != null) {
		if (theForm.ship_city != null) {
			shipAddr.city = theForm.ship_city.value;
		}
		if (theForm.ship_state != null) {
			shipAddr.state = theForm.ship_state.value;
		}
		if (theForm.ship_zipCode != null) {
			shipAddr.zipCode = theForm.ship_zipCode.value;
		}
	}
}

/**
* Function will read the address values in a page at load
*/
function getInitialValues() {
	readAddresses('signup', billAddressAtLoad, shipAddressAtLoad);
}

/**
* Function will read the address values on submit and compare them
*/
function getSubmitValues() {
	readAddresses('signup', billAddressAtSubmit, shipAddressAtSubmit);
}

function validateJurisdictionFields() {

	getSubmitValues();
	
	// compare the shipping values
	var shipChanged = compareJurisdictionFields(shipAddressAtLoad, shipAddressAtSubmit);
	var billChanged = compareJurisdictionFields(billAddressAtLoad, billAddressAtSubmit);
	
	var debugString;
	
	debugString = 
		'ShipAtLoad\n' + 
		'==========\n' + 
		shipAddressAtLoad.city + '\n' +
		shipAddressAtLoad.state + '\n' +
		shipAddressAtLoad.zipCode + '\n' +
		'\n' +
		'BillAtLoad\n' + 
		'==========\n' + 
		billAddressAtLoad.city + '\n' +
		billAddressAtLoad.state + '\n' +
		billAddressAtLoad.zipCode + '\n' +
		'\n====================\n' +
		'ShipAtSubmit\n' + 
		'============\n' + 
		shipAddressAtSubmit.city + '\n' +
		shipAddressAtSubmit.state  + '\n' +
		shipAddressAtSubmit.zipCode + '\n' +
		'\n' +
		'BillAtSubmit\n' +
		'============\n' +
		billAddressAtSubmit.city + '\n' +
		billAddressAtSubmit.state + '\n' +
		billAddressAtSubmit.zipCode + '\n' +
		'============\n' +
		'Compare ship: ' + shipChanged + '\n' +
		'Compare bill: ' + billChanged;

// Uncomment this line while debugging to display the values in a msg box
//		alert(debugString);
		
		// if the form contains the fields address3 and/or ship_address3, and 
		// one of the jurisdiction fields have changed, set address3/ship_address3
		// to blank.
		// This will ensure that the address is reevaluated by taxware
		if (billChanged && theForm.address3 != null) {
			theForm.address3.value = "";	
		}
		if (shipChanged && theForm.ship_address3 != null) {
			theForm.ship_address3.value = "";	
		}
}

/**
* Compare the fields used for determening jurisdiction. If one of the fields 
* have changed between pageload and submit, return true. Otherwise, false.
*/
function compareJurisdictionFields(initialValues, submitValues) {

	if ((initialValues.city != null && initialValues.city != "")) {
		if (initialValues.city != submitValues.city) {
			return true;
		}
	}
	if ((initialValues.state != null && initialValues.state != "")) {
		if (initialValues.state != submitValues.state) {
			return true;
		}
	}
	if ((initialValues.zipCode != null && initialValues.zipCode != "")) {
		if (initialValues.zipCode != submitValues.zipCode) {
			return true;
		}
	}
	return false;
}

function copyBilling()  {

  var theForm = document.forms.signup;

  if(theForm.shipToBillAddr.checked) {

    theForm.ship_firstName.value = "";
    theForm.ship_firstName.disabled = true;
    theForm.ship_lastName.value = "";
    theForm.ship_lastName.disabled = true;
    theForm.ship_address1.value = "";
    theForm.ship_address1.disabled = true;
    theForm.ship_address2.value = "";
    theForm.ship_address2.disabled = true;
    theForm.ship_city.value = "";
    theForm.ship_city.disabled = true;
    theForm.ship_state.value = "";
    theForm.ship_state.disabled = true;
    theForm.ship_zipCode.value = "";
    theForm.ship_zipCode.disabled = true;
	
		if (theForm.ship_organizationName != null) {
    	theForm.ship_organizationName.value = "";
    	theForm.ship_organizationName.disabled = true;
		}

    if (theForm.ship_zipCodeExt != null) {
    	theForm.ship_zipCodeExt.value = "";
    	theForm.ship_zipCodeExt.disabled = true;
		}

    theForm.ship_email1.value = "";
    theForm.ship_email1.disabled = true;

   	theForm.ship_email1retype.value = "";
   	theForm.ship_email1retype.disabled = true;

    theForm.ship_phone1.value = "";
    theForm.ship_phone1.disabled = true;


    if (theForm.ship_phone1ext != null) {
	    theForm.ship_phone1ext.value = "";
    	theForm.ship_phone1ext.disabled = true;
    }

    if (theForm.ship_phone2 != null) {
	    theForm.ship_phone2.value = "";
	    theForm.ship_phone2.disabled = true;
		}

		if (theForm.ship_phone2ext) {
	    theForm.ship_phone2ext.value = "";
	    theForm.ship_phone2ext.disabled = true;
		}

		if (theForm.ship_fax1) {
	    theForm.ship_fax1.value = "";
			theForm.ship_fax1.disabled = true;
		}
		
		if (theForm.ship_fax1ext) {
	    theForm.ship_fax1ext.value = "";
			theForm.ship_fax1ext.disabled = true;
		}

  } else {

    theForm.ship_firstName.value = "";
    theForm.ship_firstName.disabled = false;
    theForm.ship_lastName.value = "";
    theForm.ship_lastName.disabled = false;
    theForm.ship_address1.value = "";
    theForm.ship_address1.disabled = false;
    theForm.ship_address2.value = "";
    theForm.ship_address2.disabled = false;
    theForm.ship_city.value = "";
    theForm.ship_city.disabled = false;
    theForm.ship_state.value = "";
    theForm.ship_state.disabled = false;
    theForm.ship_zipCode.value = "";
    theForm.ship_zipCode.disabled = false;

    if (theForm.ship_zipCodeExt != null) {
    	theForm.ship_zipCodeExt.value = "";
    	theForm.ship_zipCodeExt.disabled = false;
		}
	
		if (theForm.ship_organizationName != null) {
    	theForm.ship_organizationName.value = "";
    	theForm.ship_organizationName.disabled = false;
		}

    theForm.ship_email1.value = "";
    theForm.ship_email1.disabled = false;

   	theForm.ship_email1retype.value = "";
   	theForm.ship_email1retype.disabled = false;

    theForm.ship_phone1.value = "";
    theForm.ship_phone1.disabled = false;


    if (theForm.ship_phone1ext != null) {
	    theForm.ship_phone1ext.value = "";
    	theForm.ship_phone1ext.disabled = false;
    }

    if (theForm.ship_phone2 != null) {
	    theForm.ship_phone2.value = "";
	    theForm.ship_phone2.disabled = false;
		}

		if (theForm.ship_phone2ext) {
	    theForm.ship_phone2ext.value = "";
	    theForm.ship_phone2ext.disabled = false;
		}

		if (theForm.ship_fax1) {
	    theForm.ship_fax1.value = "";
			theForm.ship_fax1.disabled = false;
		}
		
		if (theForm.ship_fax1ext) {
	    theForm.ship_fax1ext.value = "";
			theForm.ship_fax1ext.disabled = false;
		}
  }
}

// copies variables that needs to be merged and
// set based on other variables
function copyVariables(form) {

	var theForm = form;

	// the sourounding if for all of these is to check if there
	// exists an extension to merge. If there isn't, the page will
	// be sent with the values in the main field

	// merge phone-main and phone-ext into phone1,phone2.....
	if (theForm.phone1ext != null) {
		if (theForm.phone1 != null && theForm.phone1ext.value != '') {
			theForm.phone1.value = theForm.phone1.value + 'ext' + theForm.phone1ext.value;
		}
	}

	if (theForm.phone2ext != null) {
		if (theForm.phone2 != null && theForm.phone2ext.value != '') {
			theForm.phone2.value = theForm.phone2.value + 'ext' + theForm.phone2ext.value;
		}
	}

	if (theForm.ship_phone1ext != null) {
		if (theForm.ship_phone1 != null && theForm.ship_phone1ext.value != '') {
			theForm.ship_phone1.value = theForm.ship_phone1.value + 'ext' + theForm.ship_phone1ext.value;
		}
	}

	if (theForm.ship_phone2ext != null) {
		if (theForm.ship_phone2 != null && theForm.ship_phone2ext.value != '') {
			theForm.ship_phone2.value = theForm.ship_phone2.value + 'ext' + theForm.ship_phone2ext.value;
		}
	}

	// merge the two parts of the zipcodes if zipcode exist on the form
	if (theForm.zipCodeExt != null) {
		if (theForm.zipCode != null && theForm.zipCodeExt.value != '') {
			theForm.zipCode.value = theForm.zipCode.value + '-' + theForm.zipCodeExt.value;
		}
	}

	if (theForm.ship_zipCodeExt != null) {
		if (theForm.ship_zipCode != null && theForm.ship_zipCodeExt.value != '') {
			theForm.ship_zipCode.value = theForm.ship_zipCode.value + '-' + theForm.ship_zipCodeExt.value;
		}
	}
}

/**
* Purpose of this function is to make sure that the variables that
* needs to be sent to IrwAddressApply
*/
function setAddressControlVariables() {

	var mode;
	// get the pageMode
	if (theForm.validationMode != null) {
		mode = theForm.validationMode.value;

		// if pageMode is newCustomer, set variables based on value of billingToShipping
		if (mode == 'newCustomer') {
			var shipBilling = theForm.shipToBillAddr.checked;

			// make sure that addShipAddr is false if shipToBill is true
			if(shipBilling) {
				theForm.addShipAddr.value = 'false';
				theForm.updateShipAddr.value = 'false';
			} else {
				//theForm.addShipAddr.value = 'true';
			}
		} // end of newCustomer

		// always set addShipAddr to false in this mode
		if (mode == 'editBillAddr') {
			theForm.addShipAddr.value = 'false';
		}
	} // end check for pagemode != null
}

/**
* The method validates that the field has a value and only
* contains characters a-z, A-Z [-] and [space]
*/
function validateUserName(validationIndex) {

	var val = theForm[validation[validationIndex][0]].value.trim();

	if (val == "" || !isLegal(val, true) ) {
    	setErrorText(validation[validationIndex][0]);
    	return validation[validationIndex][1];
    }
  return null;
}


//Check that a string contains only valid letters
//Valid letters are a-z,A-Z, country specific chars.' and -
//
//string string = the string to validate
//ignoreWhiteSpace boolean = value that indicate if blanks are allowed or not
function isLegal(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^a-zA-Z'.\x2d\xc0-\xd6\xd8-\xf6\xf8-\xff\xe4-\xe6\s]/) != -1)
                || (!ignoreWhiteSpace && string.search(/[^a-zA-Z'\xe4-\xe6]/) != -1)){

                return false;
		}
	}
	return true;
}

/**
* function validates that the input only contains
* characters that are valid in a phonenumber.
* These are: 0-9, (, ), +, - and (space)
*/
function validatePhoneNumber(validationIndex) {

	var regEx = new RegExp("[^0-9 \+ () -]");
	var val = theForm[validation[validationIndex][0]].value.trim();

	if (val == "" || regEx.test(val)) {
    	setErrorText(validation[validationIndex][0]);
    	return validation[validationIndex][1];
    }
  return null;
}

/**
* The function will only validate a field based on the rules for
* the phonenumber if the field has a value
*/
function validateOptionalPhoneNumber(validationIndex) {

	if (theForm[validation[validationIndex][0]].value == "") {
		return null;
	} else {
		return validatePhoneNumber(validationIndex);
	}
}

/**
* Function validates that the IKEA card (family or business) is digits and
* have the exact length of 13 digits
*/
function validateIkeaCard(validationIndex) {

	var value = theForm[validation[validationIndex][0]].value;
	var fieldLength = value.length;

	if (fieldLength != 13 || validateNumber(validationIndex) != null) {
		setErrorText(validation[validationIndex][0]);
		return validation[validationIndex][1];
	}
    return null;
}

function deleteAddress(link) {

	if (confirmDelete()) {
		window.location.href=link;
	}
}

function validateZipCode(validationIndex){
    var value = theForm[validation[validationIndex][0]].value;
    theForm[validation[validationIndex][0]].value = value;
    
    var regEx = new RegExp("^\\d{5}$");
    if (!regEx.test(value)) {
        setErrorText(validation[validationIndex][0]);
        return validation[validationIndex][1];
    }

    return null;
}

function simpleValidateZipCode(zipCode, errorText) {
    var regEx = new RegExp("^\\d{5}$");
    if (!regEx.test(zipCode)) {
      alert(errorText);
        return false;
    }
}
