﻿function CountryChange(countryDropDownID, stateDivID, stateValidatorID, provinceDivID, provinceValidatorID) {
	if ($("#" + countryDropDownID).val() == "United States") {
		$("#" + stateDivID).show();
		$("#" + provinceDivID).hide();
		ValidatorEnable($("#" + stateValidatorID)[0], true);
		ValidatorEnable($("#" + provinceValidatorID)[0], false);
	} else {
		$("#" + stateDivID).hide();
		$("#" + provinceDivID).show();
		ValidatorEnable($("#" + stateValidatorID)[0], false);
		ValidatorEnable($("#" + provinceValidatorID)[0], true);
	}
}

function CopyAddressHandler(checkBox,
											fromAddress1Id,
											fromAddress2Id,
											fromCountryId,
											fromStateId,
											fromProvinceId,
											fromCityId,
											fromPostalId,
											toAddress1Id,
											toAddress2Id,
											toCountryId,
											toStateId,
											toProvinceId,
											toCityId,
											toPostalId) {
	if ($(checkBox).attr("checked")) {
		CopyAndDisable(fromAddress1Id, toAddress1Id);
		CopyAndDisable(fromAddress2Id, toAddress2Id);
		CopyAndDisable(fromCountryId, toCountryId);
		CopyAndDisable(fromStateId, toStateId);
		CopyAndDisable(fromProvinceId, toProvinceId);
		CopyAndDisable(fromCityId, toCityId);
		CopyAndDisable(fromPostalId, toPostalId);

		$("#" + toCountryId).change();
	} else {
		EnableAfterUnselect(toAddress1Id);
		EnableAfterUnselect(toAddress2Id);
		EnableAfterUnselect(toCountryId);
		EnableAfterUnselect(toStateId);
		EnableAfterUnselect(toProvinceId);
		EnableAfterUnselect(toCityId);
		EnableAfterUnselect(toPostalId);
	}
}

function CopyNameHandler(checkBox,
										fromFirstNameId,
										fromLastNameId,
										fromEmailId,
										fromPhoneId,
										toFirstNameId,
										toLastNameId,
										toEmailId,
										toPhoneId) {
	if ($(checkBox).attr("checked")) {
		CopyAndDisable(fromFirstNameId, toFirstNameId);
		CopyAndDisable(fromLastNameId, toLastNameId);
		CopyAndDisable(fromEmailId, toEmailId);
		CopyAndDisable(fromPhoneId, toPhoneId);
	} else {
		EnableAfterUnselect(toFirstNameId);
		EnableAfterUnselect(toLastNameId);
		EnableAfterUnselect(toEmailId);
		EnableAfterUnselect(toPhoneId);
	}
}

function CopyAndDisable(fromId, toId) {
	var from = $("#" + fromId);
	var to = $("#" + toId);

	to.val(from.val());

	// not doing this, but keeping it here just in case
	//to.attr("readonly", "true");
}

function EnableAfterUnselect(id) {
	// not doing this, but keeping it here just in case
	//$("#" + id).removeAttr("readonly");
}

//http://www.delphifaq.com/faq/javascript/f1038.shtml

function addOption(theSel, theText, theValue) {
    var newOpt = new Option(theText, theValue);
    var selLength = theSel.length;
    theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex) {
    var selLength = theSel.length;
    if (selLength > 0) {
        theSel.options[theIndex] = null;
    }
}

function moveOptions(theSelFrom, theSelTo, hiddenfield, selected) {
    var selLength = theSelFrom.length;
    var selectedText = new Array();
    var selectedValues = new Array();
    var selectedCount = 0;

    var i;

    // Find the selected Options in reverse order
    // and delete them from the 'from' Select.
    for (i = selLength - 1; i >= 0; i--) {
        if (theSelFrom.options[i].selected) {
            selectedText[selectedCount] = theSelFrom.options[i].text;
            selectedValues[selectedCount] = theSelFrom.options[i].value;
            deleteOption(theSelFrom, i);
            selectedCount++;
        }
    }

    // Add the selected text/values in reverse order.
    // This will add the Options to the 'to' Select
    // in the same order as they were in the 'from' Select.
    for (i = selectedCount - 1; i >= 0; i--) {
        addOption(theSelTo, selectedText[i], selectedValues[i]);
    }

    var lb = theSelTo;
    arrTexts = new Array();
    arrValues = new Array();
    arrOldTexts = new Array();

    for (i = 0; i < lb.length; i++) {
        arrTexts[i] = lb.options[i].text.toLowerCase();
        arrValues[i] = lb.options[i].value;
        arrOldTexts[i] = lb.options[i].text;
    }

    arrTexts.sort();

    for (i = 0; i < lb.length; i++) {

        for (j = 0; j < lb.length; j++) {
            if (arrTexts[i] == arrOldTexts[j].toLowerCase()) {
                lb.options[i].text = arrOldTexts[j];
                lb.options[i].value = arrValues[j];
                j = lb.length;
            }
        }
    }

    hiddenfield.value = "";

    // populate the hidden field value
    for (i = 0; i < selected.length; i++) {
        if (hiddenfield.value == "") {
            hiddenfield.value = selected.options[i].value;
        } else {
            hiddenfield.value += "," + selected.options[i].value;
        }
    }
}

