//THIS JS FILE CONTAINS CUSTOM VALIDATION FUNCTIONS USED IN DIFFERENT SCRIPTS WITHIN THE APPLICATION

// Initializes Get Rate page values (executed by page onLoad event)
// used with get_rate1.aspx
function initRatePage() {
    checkZip('s');
    checkZip('r');
    // checkServiceType(); !!!see comment below for this function!!!
}

// Determines if zip code is required for selected country
// If not, the message "Not Required" is displayed, and zip code text box is disabled.
// Otherwise, the zip code text box is enabled and cleared.
// used with get_rate1.aspx
function checkZip(p) {
	if (p == 's') {
		var oList = document.getElementById("lst_s_country");
		var oZip = document.getElementById("txt_s_zip");
	} else {
		var oList = document.getElementById("lst_r_country");
		var oZip = document.getElementById("txt_r_zip");
	}
	
	for (var i = 0; i < arrZipRequired.length; i++) {
		if (oList.options[oList.selectedIndex].value == arrZipRequired[i]) {
			oZip.value = '';
			oZip.text = '';
			oZip.readOnly = false;
		    // checkServiceType(); !!!see comment below for this function!!!
			return;
		}
	}

	oZip.value = 'Not Required';
	oZip.text = 'Not Required';
	oZip.readOnly = true;
    // checkServiceType(); !!!see comment below for this function!!!
}

// Determines if FDXG is supported for selected Country.
// If not, disable the FDXG option.
// used with get_rate1.aspx
// !!!!!THIS FUNCTION IS NOT NEEDED SINCE Skycap International DOES NOT PROVIDE FDXG SERVICE!!!!!
/* function checkServiceType() {
	if (document.getElementById("rblServiceType_1").checked) {
		var arrFDXG = new Array();
		arrFDXG[0] = 'CA';
		arrFDXG[1] = 'PR';
		arrFDXG[2] = 'US';

		var sValue = document.getElementById("lst_s_country").options[document.getElementById("lst_s_country").selectedIndex].value;
		var bSenderGround = false;
		
		for (var i = 0; i < arrFDXG.length; i++) {
			if (sValue == arrFDXG[i]) {
			    bSenderGround = true;
				break;
	        }
		}

		if (bSenderGround) {
			var sValue = document.getElementById("lst_s_country").options[document.getElementById("lst_s_country").selectedIndex].value;
			var bRecipientGround = false;
			
			for (var i = 0; i < arrFDXG.length; i++) {
				if (sValue == arrFDXG[i]) {
					bRecipientGround = true;
					break;
	            }
		    }
		}
		
		if (!(bSenderGround && bRecipientGround)) { // Select FDXE for user, thus disabling FDXG
			document.getElementById("rblServiceType_0").checked = true;
		}
	}
} */

// Checks if customer state/province is selected for US, CA, and MX
// used with customer_add.aspx and customer_edit.aspx
function checkCState(s, e) {
	var sState = document.getElementById("lst_state").options[document.getElementById("lst_state").selectedIndex].value;
	var sCountry = document.getElementById("lst_country").options[document.getElementById("lst_country").selectedIndex].value;
	if ((sCountry == 'US' || sCountry == 'CA' || sCountry == 'MX') && sState == '') {
		e.IsValid = false;
		return;
	}
	e.IsValid = true;
}

// Checks if sender's state/province is selected for US, CA, and MX
// used with s_r_info.aspx
function checkSState(s, e) {
	var sState = document.getElementById("lst_s_state").options[document.getElementById("lst_s_state").selectedIndex].value;
	var sCountry = document.getElementById("lst_s_country").options[document.getElementById("lst_s_country").selectedIndex].value;
	if ((sCountry == 'US' || sCountry == 'CA' || sCountry == 'MX') && sState == '') {
		e.IsValid = false;
		return;
	}
	e.IsValid = true;
}

// Checks if recipient's state/province is selected for US, CA, and MX
// used with s_r_info.aspx
function checkRState(s, e) {
	var sState = document.getElementById("lst_r_state").options[document.getElementById("lst_r_state").selectedIndex].value;
	var sCountry = document.getElementById("lst_r_country").options[document.getElementById("lst_r_country").selectedIndex].value;
	if ((sCountry == 'US' || sCountry == 'CA' || sCountry == 'MX') && sState == '') {
		e.IsValid = false;
		return;
	}
	e.IsValid = true;
}

// Checks if weight value is supplied for anything else but FedEx Envelope AND
// if the supplied value is within FedEx-defined range
// used with s_p_info_dom.aspx
function checkWeight(s, e) {
	var sPackaging = document.getElementById("lst_packaging").options[document.getElementById("lst_packaging").selectedIndex].value;
	var sWeight = document.getElementById("txt_weight").value;
	if (sPackaging != '06' && sWeight == '') {
		e.IsValid = false;
		return;
	}
	
	var sPackagingText = document.getElementById("lst_packaging").options[document.getElementById("lst_packaging").selectedIndex].text;
	if (sPackaging == '02' || sPackaging == '03' || sPackaging == '04') {
		if (sWeight < 0 || sWeight > 20) {
			e.IsValid = false;
			document.getElementById("cuv_txt_weight").errormessage = 'Weight for ' + sPackagingText + ' must be between 1 and 20.00 pounds';
			return;
		}
	}
	
	if (sPackaging == '01') {
		if (sWeight < 0 || sWeight > 150) {
			e.IsValid = false;
			document.getElementById("cuv_txt_weight").errormessage = 'Weight for ' + sPackagingText + ' must be between 1 and 150.00 pounds';
			return;
		}
	}
	
	if (sPackaging == '06' && sWeight > 10) {
		e.IsValid = false;
		document.getElementById("cuv_txt_weight").errormessage = 'Weight for ' + sPackagingText + ' cannot exceed 10.00 pounds';
		return;
	}
	
	e.IsValid = true;
}

// Checks if weight value is supplied for anything else but FedEx Envelope AND
// if the supplied value is within FedEx-defined range for international shipments
// used with s_p_info_int.aspx
function checkWeightInt(s, e) {
	var sPackaging = document.getElementById("lst_packaging").options[document.getElementById("lst_packaging").selectedIndex].value;
	var iUOM = document.getElementById("rbl_uom_1").checked; //LBS/IN option is checked when iUOM = 0
	var sWeight = document.getElementById("txt_weight").value;
	if (sPackaging != '06' && sWeight == '') {
		e.IsValid = false;
		return;
	}
	
	var sPackagingText = document.getElementById("lst_packaging").options[document.getElementById("lst_packaging").selectedIndex].text;
	if (sPackaging == '02' || sPackaging == '03' || sPackaging == '04') {
		if (((sWeight < 0 || sWeight > 20) && iUOM == 0) || ((sWeight < 0.5 || sWeight > 9) && iUOM == 1)) {
			e.IsValid = false;
			document.getElementById("cuv_txt_weight").errormessage = 'Weight for ' + sPackagingText + ' must be between 1 and 20.00 pounds (0.5 and 9 kilograms)';
			return;
		}
	}
	
	if (sPackaging == '01') {
		if (((sWeight < 0 || sWeight > 150) && iUOM == 0) || ((sWeight < 0.5 || sWeight > 68) && iUOM == 1)) {
			e.IsValid = false;
			document.getElementById("cuv_txt_weight").errormessage = 'Weight for ' + sPackagingText + ' must be between 1 and 150.00 pounds (0.5 and 68 kilograms)';
			return;
		}
	}
	
	if (sPackaging == '06') {
		if (((sWeight < 0 || sWeight > 10) && iUOM == 0) || ((sWeight < 0.5 || sWeight > 4.5) && iUOM == 1)) {
			e.IsValid = false;
			document.getElementById("cuv_txt_weight").errormessage = 'Weight for ' + sPackagingText + ' cannot exceed 10.00 pounds';
			return;
		}
	}
	
	e.IsValid = true;
}

// Checks if dimensions passed are valid (2x4x7 inches minimum and either all empty or all filled out)
// used with s_p_info_dom.aspx
function checkDimensions(s, e) {
	var sPackaging = document.getElementById("lst_packaging").options[document.getElementById("lst_packaging").selectedIndex].value;
	if (sPackaging == '01') {
		//var b1 = (document.getElementById("txt_length").value.length == 0 && document.getElementById("txt_width").value.length == 0 && document.getElementById("txt_height").value.length == 0);
		var b2 = (document.getElementById("txt_length").value.length > 0 && document.getElementById("txt_length").value > 0 && document.getElementById("txt_width").value.length > 0 && document.getElementById("txt_width").value > 0 && document.getElementById("txt_height").value.length > 0 && document.getElementById("txt_height").value > 0);
		//if (!(b1 || b2)) {
		//	e.IsValid = false;
		//	return;
		//} else if (b2) {
		if (b2) {
			var arr = [document.getElementById("txt_length").value, document.getElementById("txt_width").value, document.getElementById("txt_height").value];
			arr.sort(sortNumbers);
			if (arr[0] < 2 || arr[1] < 4 || arr[2] < 7) {
				e.IsValid = false;
				document.getElementById("cuv_txt_length1").errormessage = 'Package dimensions must be at least 7 inches for Length, 4 inches for Width, and 2 inches for Height for Your Packaging. If your package is smaller than these dimensions, place your package in a FedEx Pak for shipping.';
				return;
			}
		}
	}
	
	e.IsValid = true;
}

// Checks if dimensions passed are valid for international shipments (2x4x7 inches or 5x10x18 centimeters minimum 
// and either all empty or all filled out)
// used with s_p_info_int.aspx
function checkDimensionsInt(s, e) {
	var sPackaging = document.getElementById("lst_packaging").options[document.getElementById("lst_packaging").selectedIndex].value;
	var iUOM = document.getElementById("rbl_uom_1").checked; //LBS/IN option is checked when iUOM = 0
	if (sPackaging == '01') {
		//var b1 = (document.getElementById("txt_length").value.length == 0 && document.getElementById("txt_width").value.length == 0 && document.getElementById("txt_height").value.length == 0);
		var b2 = (document.getElementById("txt_length").value.length > 0 && document.getElementById("txt_length").value > 0 && document.getElementById("txt_width").value.length > 0 && document.getElementById("txt_width").value > 0 && document.getElementById("txt_height").value.length > 0 && document.getElementById("txt_height").value > 0);
		//if (!(b1 || b2)) {
		//	e.IsValid = false;
		//	return;
		//} else if (b2) {
		if (b2) {
			var arr = [document.getElementById("txt_length").value, document.getElementById("txt_width").value, document.getElementById("txt_height").value];
			arr.sort(sortNumbers);
			if ((arr[0] < 2 || arr[1] < 4 || arr[2] < 7) && iUOM == 0) {
				e.IsValid = false;
				document.getElementById("cuv_txt_length1").errormessage = 'Package dimensions must be at least 7 inches for Length, 4 inches for Width, and 2 inches for Height for Your Packaging. If your package is smaller than these dimensions, place your package in a FedEx Pak for shipping.';
				return;
			}
			if ((arr[0] < 5 || arr[1] < 10 || arr[2] < 18) && iUOM == 1) {
				e.IsValid = false;
				document.getElementById("cuv_txt_length1").errormessage = 'Package dimensions must be at least 18 centimeters for Length, 10 centimeters for Width, and 5 centimeters for Height for Your Packaging. If your package is smaller than these dimensions, place your package in a FedEx Pak for shipping.';
				return;
			}
		}
	}
	
	e.IsValid = true;
}

// Checks if length value is within FedEx-defined range
// used with s_p_info_dom.aspx
function checkLength(s, e) {
	var sPackaging = document.getElementById("lst_packaging").options[document.getElementById("lst_packaging").selectedIndex].value;
	var sLength = document.getElementById("txt_length").value;
	if (sPackaging == '01') {
		if (sLength > 119 || sLength < 0) {
			e.IsValid = false;
			return;
		}
	}
	
	e.IsValid = true;
}

// Checks if length value is within FedEx-defined range for international shipments
// used with s_p_info_int.aspx
function checkLengthInt(s, e) {
	var sPackaging = document.getElementById("lst_packaging").options[document.getElementById("lst_packaging").selectedIndex].value;
	var iUOM = document.getElementById("rbl_uom_1").checked; //LBS/IN option is checked when iUOM = 0
	var sLength = document.getElementById("txt_length").value;
	if (sPackaging == '01') {
		if (((sLength > 119 || sLength < 0) && iUOM == 0) || ((sLength > 274 || sLength < 0) && iUOM == 1)) {
			e.IsValid = false;
			return;
		}
	}
	
	e.IsValid = true;
}

// Checks if width value is within FedEx-defined range
// used with s_p_info_dom.aspx
function checkWidth(s, e) {
	var sPackaging = document.getElementById("lst_packaging").options[document.getElementById("lst_packaging").selectedIndex].value;
	var sWidth = document.getElementById("txt_width").value;
	if (sPackaging == '01') {
		if (sWidth > 119 || sWidth < 0) {
			e.IsValid = false;
			return;
		}
	}
	
	e.IsValid = true;
}

// Checks if width value is within FedEx-defined range for international shipments
// used with s_p_info_int.aspx
function checkWidthInt(s, e) {
	var sPackaging = document.getElementById("lst_packaging").options[document.getElementById("lst_packaging").selectedIndex].value;
	var iUOM = document.getElementById("rbl_uom_1").checked; //LBS/IN option is checked when iUOM = 0
	var sWidth = document.getElementById("txt_width").value;
	if (sPackaging == '01') {
		if (((sWidth > 119 || sWidth < 0) && iUOM == 0) || ((sWidth > 274 || sWidth < 0) && iUOM == 1)) {
			e.IsValid = false;
			return;
		}
	}
	
	e.IsValid = true;
}

// Checks if height value is within FedEx-defined range
// used with s_p_info_dom.aspx
function checkHeight(s, e) {
	var sPackaging = document.getElementById("lst_packaging").options[document.getElementById("lst_packaging").selectedIndex].value;
	var sHeight = document.getElementById("txt_height").value;
	if (sPackaging == '01') {
		if (sHeight > 119 || sHeight < 0) {
			e.IsValid = false;
			return;
		}
	}
	
	e.IsValid = true;
}

// Checks if height value is within FedEx-defined range for international shipments
// used with s_p_info_int.aspx
function checkHeightInt(s, e) {
	var sPackaging = document.getElementById("lst_packaging").options[document.getElementById("lst_packaging").selectedIndex].value;
	var iUOM = document.getElementById("rbl_uom_1").checked; //LBS/IN option is checked when iUOM = 0
	var sHeight = document.getElementById("txt_height").value;
	if (sPackaging == '01') {
		if (((sHeight > 119 || sHeight < 0) && iUOM == 0) || ((sHeight > 274 || sHeight < 0) && iUOM == 1)) {
			e.IsValid = false;
			return;
		}
	}
	
	e.IsValid = true;
}

// Checks if total length and girth value is within FedEx-defined range
// used with s_p_info_dom.aspx
function checkLengthGirth(s, e) {
	var sPackaging = document.getElementById("lst_packaging").options[document.getElementById("lst_packaging").selectedIndex].value;
	if (sPackaging == '01') {
		var arr = new Array();
		arr[0] = document.getElementById("txt_length").value;
		arr[1] = document.getElementById("txt_width").value;
		arr[2] = document.getElementById("txt_height").value;
		arr.sort();
		if (arr[2]*1 + arr[0]*2 + arr[1]*2 > 165) {
			e.IsValid = false;
			return;
		}
	}
	
	e.IsValid = true;
}

// Checks if total length and girth value is within FedEx-defined range for international shipments
// used with s_p_info_int.aspx
function checkLengthGirthInt(s, e) {
	var sPackaging = document.getElementById("lst_packaging").options[document.getElementById("lst_packaging").selectedIndex].value;
	var iUOM = document.getElementById("rbl_uom_1").checked; //LBS/IN option is checked when iUOM = 0
	if (sPackaging == '01') {
		var arr = new Array();
		arr[0] = document.getElementById("txt_length").value;
		arr[1] = document.getElementById("txt_width").value;
		arr[2] = document.getElementById("txt_height").value;
		arr.sort();
		if ((arr[2]*1 + arr[0]*2 + arr[1]*2 > 130 && iUOM == 0) || (arr[2]*1 + arr[0]*2 + arr[1]*2 > 330 && iUOM == 1)) {
			e.IsValid = false;
			return;
		}
	}
	
	e.IsValid = true;
}

// Checks if both Saturday Pickup and Saturday Delivery selected (these two values are mutually exclusive)
// used with s_p_info_int.aspx and s_p_info_dom.aspx
function checkSaturdayOptions(s, e) {
	var sSatPickup = document.getElementById("chk_service_options_0").checked;
	var sSatDelivery = document.getElementById("chk_service_options_1").checked;
	if (sSatPickup && sSatDelivery) {
		e.IsValid = false;
		return;
	}
	
	e.IsValid = true;
}

// Checks if both Deliver without Signature and C.O.D. options are selected (these two values are mutually exclusive)
// used with s_p_info_dom.aspx
function checkOtherOptions(s, e) {
	if (document.getElementById("chk_service_options_3") != null) {
		var sDWOS = document.getElementById("chk_service_options_2").checked;
		var sCOD = document.getElementById("chk_service_options_3").checked;
		if (sDWOS && sCOD) {
			e.IsValid = false;
			return;
		}
	}
	e.IsValid = true;	
}

// Checks if HAL state/province is selected for US, CA, and MX
// used with options_dom.aspx and options_int.aspx
function checkHALState(s, e) {
	var sState = document.getElementById("lst_hal_state").options[document.getElementById("lst_hal_state").selectedIndex].value;
	var sCountry = document.getElementById("lst_hal_country").options[document.getElementById("lst_hal_country").selectedIndex].value;
	if ((sCountry == 'US' || sCountry == 'CA' || sCountry == 'MX') && sState == '') {
		e.IsValid = false;
		return;
	}
	e.IsValid = true;
}

// Checks if COD state/province is selected for US, CA, and MX
// used with options_dom.aspx
function checkCODState(s, e) {
	var sState = document.getElementById("lst_cod_state").options[document.getElementById("lst_cod_state").selectedIndex].value;
	var sCountry = document.getElementById("lst_cod_country").options[document.getElementById("lst_cod_country").selectedIndex].value;
	if ((sCountry == 'US' || sCountry == 'CA' || sCountry == 'MX') && sState == '') {
		e.IsValid = false;
		return;
	}
	e.IsValid = true;
}

// Checks if pickup time value is valid
// used with options_dom.aspx and options_int.aspx
function checkPickupTime(s, e) {
	var arr = new Array();
	var sTime = document.getElementById("txt_pickup_time").value;
	arr = sTime.split(":");
	if (arr[0] > 23 || arr[1] > 59) {
		e.IsValid = false;
		return;
	}
	if (arr[0] > 12 && arr[1] > 0) {
		e.IsValid = false;
		document.getElementById("cuv_pickup_time").errormessage = 'Earliest Pickup Time must be before noon (FedEx cutoff time)';
		return;
	}
	e.IsValid = true;
}

// Checks if close time value is valid
// used with options_dom.aspx and options_int.aspx
function checkCloseTime(s, e) {
	var arr = new Array();
	var sTime = document.getElementById("txt_close_time").value;
	arr = sTime.split(":");
	if (arr[0] > 23 || arr[1] > 59) {
		e.IsValid = false;
		return;
	}
	e.IsValid = true;
}

// Checks if Document Description is entered for document shipments
// used with s_p_info_int.aspx
function checkDocumentDesc(s, e) {
	var iShipmentType = document.getElementById("rbl_shipment_type_1").checked; //"Documents only" option is checked when iShipmentType = 0
	var sDocumentDesc = document.getElementById("txt_document_description").value;
	if (iShipmentType == 0 && sDocumentDesc == '') {
		e.IsValid = false;
		return;
	}
	e.IsValid = true;
}

// Checks if Declared Value is entered and valid for document shipments
// used with s_p_info_int.aspx
function checkDeclaredValue(s, e) {
	var iShipmentType = document.getElementById("rbl_shipment_type_1").checked; //"Documents only" option is checked when iShipmentType = 0
	var dDeclaredValue = document.getElementById("txt_declared_value").value;
	
	if (iShipmentType == 0 && dDeclaredValue > 0) {
		e.IsValid = false;
		return;
	}
	e.IsValid = true;
}

// Provides rules for proper sorting the arrays of numbers
// used with JS sort() function for arrays, particularly
// for validating package dimensions
function sortNumbers(num1, num2) {
	if (parseInt(num1) < parseInt(num2)) {
		return -1;
	} else if (parseInt(num1) > parseInt(num2)) {
		return 1;
	} else {
		return 0;
	}
}