// JavaScript Document
var issueNote = new Array();
var aryIssuesDisplayed = new Array();
var arySubmitIssue = new Array();

function checkDangerousGoods(oSelect){
	if(oSelect != null){
		if(oSelect.value == 'yes'){
			alert('Smart Send does not carry dangerous goods.');
			oSelect.value = 'no';
			return false;
		}
	}

	return true;
}

function checkAllDimensions(){
	for(increment = 1; increment < 21; increment++)
	{
		if (document.getElementById('height_' + increment) != undefined)
			{
			checkForIssues(document.getElementById('height_'+increment), 'invalidCharacters|maxLength|decimals', true);
			checkForIssues(document.getElementById('length_'+increment), 'invalidCharacters|maxLength|decimals', true);
			checkForIssues(document.getElementById('depth_'+increment), 'invalidCharacters|maxLength|decimals', true);
			}
	}
}

function customQuoteCheck(oForm){
	var alert_message = "";
	// check postcode from and to
	if(document.getElementById('postcodeFrom').value == '') {
		alert_message += 'From (postcode)\n';
	}
	if(document.getElementById('postcodeTo').value == '') {
		alert_message += 'To (postcode)\n';
	}
	for(increment = 1; increment < 21; increment++)
	{
		if (document.getElementById('description_' + increment))
			{
				if (document.getElementById('height_1') == undefined) {
					if(oForm.description_1.value == '') {
						alert_message += 'Item Description\n';
					}
					if(oForm.weight_1.value == '') {
						alert_message += 'Item Weight\n';
					}
					if(oForm.height_1.value == '') {
						alert_message += 'Item Height\n';
					}
					if(oForm.length_1.value == '') {
						alert_message += 'Item Length\n';
					}
					if(oForm.depth_1.value == '') {
						alert_message += 'Item Depth\n';
					}
				}
				else if (increment == 1 ||
					document.getElementById('description_'+increment).value != '' ||
					document.getElementById('weight_'+increment).value != '' ||
					document.getElementById('height_'+increment).value != '' ||
					document.getElementById('depth_'+increment).value != '' ||
					document.getElementById('length_'+increment).value != ''
					)
				{
					// check all 5 values
					if(document.getElementById('description_'+increment).value == '') {
						alert_message += 'Item #'+increment+' Description\n';
					}
					if(document.getElementById('weight_'+increment).value == '') {
						alert_message += 'Item #'+increment+' Weight\n';
					}
					if(document.getElementById('height_'+increment).value == '') {
						alert_message += 'Item #'+increment+' Height\n';
					}
					if(document.getElementById('length_'+increment).value == '') {
						alert_message += 'Item #'+increment+' Length\n';
					}
					if(document.getElementById('depth_'+increment).value == '') {
						alert_message += 'Item #'+increment+' Depth\n';
					}
				}
			}
	}
	
	if (alert_message) { 
		alert ("Your form is incomplete.\n" +
			   "You must supply the following information:\n" + 
			   alert_message )
		return false;
	} else { 
		return true;
	}	
		
		
}

function checkSubmitIssue(oForm){

	//Need to check if the user has chosen furniture as one of the descriptions
	selectList = oForm.getElementsByTagName('SELECT');
	for(i=0; i < selectList.length; i++){
		
		//Check to see if the user has chosen furniture
		if(selectList[i].value == 13){
			alert("Smart Send does not transport assembled furniture or bedding.  We can however transport furniture if it is disassembled and well packaged inside a flat pack or strong sturdy box.");
			return false;
		}
	}

	checkAllDimensions();

	var returnValue = true;
	var userTypeLength;
	
	var alertMessage = '';

	if(oForm.userType == null){
		userTypeLength = undefined;
	}else{
		userTypeLength = oForm.userType.length;
	}
	

		if(!isPostcode(oForm.postcodeTo.value)){
			alertMessage = alertMessage+ 'Invalid To postcode \n';
			returnValue = false;
		}
		if(!isPostcode(oForm.postcodeFrom.value)){
			alertMessage = alertMessage+ 'Invalid From postcode \n';
			returnValue = false;
		}
		

	if(userTypeLength != undefined){
		for(increment = 0; increment < userTypeLength; increment++){
			if(oForm.userType[increment].checked){
				switch(oForm.userType[increment].value){
					case 'ebay':
						if(oForm.ebayUserID.value.length < 1){
							alert("Please supply a User ID");
							return false;
						}
						break;
					case 'corporate':
						if(oForm.corporateUserID.value.length < 1){
							alert("Please supply a User ID");
							return false;
						}
						break;
				}
			}
		}
	}
	
	if(oForm.description_1.value == 13){
		alert("Smart Send does not transport assembled furniture or bedding.  We can however transport furniture if it is disassembled and well packaged inside a flat pack or strong sturdy box.");
		return false;
		
	}

	for(key in arySubmitIssue){
		if(typeof arySubmitIssue[key] == 'function'){continue;}
		if(key == "extend"){continue;}
		if(arySubmitIssue[key] == null){continue;}

		if(arySubmitIssue[key] === true){
			returnValue = false;
		}
	}

	if(!checkDangerousGoods(oForm.dangerousGoods)){
		alertMessage = alertMessage+ 'We cannot transport dangerous goods. \n';
		return false;
	}

	if(returnValue){
		return customQuoteCheck(oForm);
	}else{

		alert(alertMessage);
		return false;
	}
}

function checkForIssues(oItem, issueType, popup){
	var setCellBorder;
	aryIssues = issueType.split('|');
	/* Clear message for provided field */
	aryIssuesDisplayed[oItem.name] = null;
	arySubmitIssue[oItem.name] = false;

	/* Check for issues in supplied field */
	$A(aryIssues).each(
		function(oNodes){
			switch(oNodes){
				case 'maxLength':
					if(oItem.value.length > 0){
						intRow = oItem.name.split('_')[1];
						intRowType = $('description_' + intRow).value;

						var checkItem = aryItemMaxLength[intRowType];

						if(document.getElementById("ServiceType").value == "Express")
							checkItem = 240;

						if(checkItem){
							if(oItem.value > checkItem){
								setCellBorder = true;
								arySubmitIssue[oItem.name] = true;

								if(document.getElementById("ServiceType").value == "Express")
									aryIssuesDisplayed[oItem.name] = 'We cannot accept items over 2.4 metres on the EXPRESS FREIGHT service';
								else
									aryIssuesDisplayed[oItem.name] = 'Maximum length of ' + checkItem + 'cm';
							}else{
								setCellBorder = false;
							}
						}else{
							setCellBorder = false;
						}
					}else{
						setCellBorder = false;
					}
					break;
				case 'secondPerson':
					if(oItem.value.length > 0){
						intRow = oItem.name.split('_')[1];
						intRowType = $('description_' + intRow).value;

						if($('description_' + intRow).options[intRowType].text == "Carton <17kgs"){
							itemDescription = "Carton";
						}else if($('description_' + intRow).options[intRowType].text == "Heavy Carton >17kgs"){
							itemDescription = "Heavy Carton";
						}else{
							itemDescription = $('description_' + intRow).options[intRowType].text;
						}

						if(aryItemSecondPerson[intRowType]){
							if(oItem.value > aryItemSecondPerson[intRowType]){
								setCellBorder = true;

								switch(intRowType)
								{
									case '2':
									case '5':
									case '6':
									case '7':
									case '8':
									case '10':
										aryIssuesDisplayed[oItem.name] = itemDescription + "s over " + aryItemSecondPerson[intRowType] + "kgs require a 2nd person to help the driver load and unload the goods. If you cannot guarantee someone will be available for pickup or delivery, you should consider adding a 'Tail-Lift Truck' service to your booking to ensure there are no issues with pickup or delivery.";
										break;
									default:
										aryIssuesDisplayed[oItem.name] = itemDescription + 's over ' + aryItemSecondPerson[intRowType] + 'kgs require a 2nd person to help the driver load and unload the goods. Pickup will be refused by driver otherwise.';
										break;
								}
							}else{
								setCellBorder = false;
							}
						}else{
							setCellBorder = false;
						}
					}else{
						setCellBorder = false;
					}
					break;
				case 'forkLift':
					if(oItem.value.length > 0){
						intRow = oItem.name.split('_')[1];
						intRowType = $('description_' + intRow).value;
						
						if($('description_' + intRow).options[intRowType].text == "Carton <17kgs"){
							itemDescription = "Carton";
						}else if($('description_' + intRow).options[intRowType].text == "Heavy Carton >17kgs"){
							itemDescription = "Heavy Carton";
						}else{
							itemDescription = $('description_' + intRow).options[intRowType].text;
						}
						
						if(aryItemForkLift[intRowType]){
							if(oItem.value > aryItemForkLift[intRowType]){
								setCellBorder = true;

								switch(intRowType)
								{
								case '10':
									aryIssuesDisplayed[oItem.name] = "Maximum weight for '" + itemDescription + "' description is " + aryItemMaxWeight[intRowType] + "kgs. Please strap the Length to a SKID or PALLET and choose the correct description from the drop down list. You should also consider adding a 'Tail-Lift Truck' service to your booking to ensure there are no issues with pickup or delivery.";
									break;
								case '5':
								case '6':
								case '7':
									aryIssuesDisplayed[oItem.name] = itemDescription + "s over " + aryItemForkLift[intRowType] + "kgs require a forklift to load & unload the goods.  If you cannot arrange a forklift you must add a 'Tail-Lift Truck' service to your booking. Otherwise drivers may refuse to pickup and/or deliver the goods, resulting in additional charges to you.";
									break;
								default:
									aryIssuesDisplayed[oItem.name] = itemDescription + 's over ' + aryItemForkLift[intRowType] + 'kgs require a fork lift to load and unload the goods. Pickup will be refused by driver otherwise.';
									break;
								}
							}else{
								setCellBorder = false;
							}
						}else{
							setCellBorder = false;
						}
					}else{
						setCellBorder = false;
					}
					break;
				case 'invalidCharacters':
					if(oItem.value.length > 0){
						if(/[^\d\.]/.test(oItem.value) || (oItem.value.length == 1 && oItem.value == '0')){
							setCellBorder = true;
							arySubmitIssue[oItem.name] = true;

							aryIssuesDisplayed[oItem.name] = 'Invalid Character';
						}else{
							setCellBorder = false;
						}
					}
					break;
				case 'decimals':
					if(oItem.value.length > 0){
						if(!(/[^\d\.]/.test(oItem.value))){
							oItem.value = Math.ceil(oItem.value);
						}
					}
					break;
				case 'selectTyre':
					if(oItem.value.length > 0 && oItem.value == 12){
						aryIssuesDisplayed[oItem.name] = 'Tyres/Wheels should preferably be packaged in sturdy cartons/boxes to provide protection and security during transport. If you do not have suitable boxes available, layers of shrinkwrap (pallet plastic wrapping) or bubblewrap around the individual items should be sufficient but ensure they are tightly wrapped with no loose wrapping.';
					}
					break;
					
				case 'selectFurniture':
					if(oItem.value.length > 0 && oItem.value == 13){
						aryIssuesDisplayed[oItem.name] = 'Smart Send does not transport assembled furniture or bedding.  We can however transport furniture if it is disassembled and well packaged inside a flat pack or strong sturdy box.';
					}
					break;
				
				case 'maxWeight':
					if(oItem.value.length > 0){
						intRow = oItem.name.split('_')[1];
						intRowType = $('description_' + intRow).value;
						
						if($('description_' + intRow).options[intRowType].text == "Carton <17kgs"){
							itemDescription = "Carton";
						}else if($('description_' + intRow).options[intRowType].text == "Heavy Carton >17kgs"){
							itemDescription = "Heavy Carton";
						}else{
							itemDescription = $('description_' + intRow).options[intRowType].text;
						}

						if(aryItemMaxWeight[intRowType]){
							if(oItem.value > aryItemMaxWeight[intRowType]){
								setCellBorder = true;
								arySubmitIssue[oItem.name] = true;

								switch(intRowType){
									case '1':
										aryIssuesDisplayed[oItem.name] = "Maximum weight for '" + itemDescription + "' description is " + aryItemMaxWeight[intRowType] + "kgs. Please choose 'HEAVY CARTON' as the description";
										break;
									case '2':
										aryIssuesDisplayed[oItem.name] = "Maximum weight for '" + itemDescription + "' description is " + aryItemMaxWeight[intRowType] + "kgs. Please strap the carton/box to a SKID or PALLET and choose the correct description from the drop down list. You should also consider adding a 'Tail-Lift Truck' service to your booking to ensure there are no issues with pickup or delivery.";
										break;
									case '10':
									case '8':
										aryIssuesDisplayed[oItem.name] = "Maximum weight for '" + itemDescription + "' description is " + aryItemMaxWeight[intRowType] + "kgs. Please strap the " + $('description_' + intRow).options[intRowType].text + " to a SKID or PALLET and choose the correct description from the drop down list. You should also consider adding a 'Tail-Lift Truck' service to your booking to ensure there are no issues with pickup or delivery.";
										break;
									default:
										aryIssuesDisplayed[oItem.name] = 'Maximum weight of ' + aryItemMaxWeight[intRowType] + 'kg';
										break;
								}
							}else{
								setCellBorder = false;
							}
						}else{
							setCellBorder = false;
						}
					}else{
						setCellBorder = false;
					}
					break;
			}
		}
	);

	/* Set field border if issue found */
	if(setCellBorder == true){
		oItem.style.border = '1px solid #FF0000';
		if(document.all){
			oItem.style.padding = '2px 2px';
		}else{
			oItem.style.padding = '2px 1px';
		}
	}else if(setCellBorder == false){
		oItem.style.border = '1px solid #7F9DB9';
		if(document.all){
			oItem.style.padding = '2px 2px';
		}else{
			oItem.style.padding = '2px 1px';
		}
	}
	displayIssues(oItem, popup);
}

function displayIssues(oItem, popup){
	/* Display issues in side note */
	if(!popup){
		$('issueCell').innerHTML = '';
		issueHTML = '<ul>';
		for(key in aryIssuesDisplayed){
			if(typeof aryIssuesDisplayed[key] == 'function'){continue;}
			if(key == "extend"){continue;}
			if(aryIssuesDisplayed[key] == null){continue;}

			issueHTML += '<li>' + aryIssuesDisplayed[key] + '</li>';
		}

		issueHTML += '</ul>';
		$('issueCell').innerHTML = issueHTML;
	}else{
		if(aryIssuesDisplayed[oItem.name]){
			alert(aryIssuesDisplayed[oItem.name]);
		}
	}
}

function CheckExpressZones()
{
	var postcodeFrom = document.getElementById("postcodeFrom").value;
	var postcodeTo = document.getElementById("postcodeTo").value;

	if ((postcodeFrom.length > 0) && (postcodeTo.length >  0))
	{
		if (document.getElementById("ServiceType").value == "Express")
			ajaxFunction('','Alert','/cfc/courier_pricing.cfc?method=checkExpressService&PostCodeTo=' + postcodeTo + '&PostCodeFrom=' + postcodeFrom);
	}
}

function CheckPostCode(postcodeInput)
{

	var postcode = document.getElementById(postcodeInput).value;

	if (postcode.length > 0)
	{
		ajaxFunction('' + postcodeInput + '','Alert','/cfc/courier_pricing.cfc?method=checkPostcode&PostCode=' + postcode);
	}
}

function DisableTaillift()
{
	if (getObject("ServiceType").value == "Express" || getObject("ServiceType").value == "TNTRoad" || getObject("ServiceType").value == "TNTOvernight" || getObject("ServiceType").value == "TNTOvernightAM" || getObject("ServiceType").value == "TNTNextFlight" )
	{
		getObject("TailLift").options[0].selected = true;
		getObject("TailLift").disabled = true;

	}
	else
	{
		getObject("TailLift").disabled = false;
	}
}



function getObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId);
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId);
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject


function changeRequired(id) {
	if (document.getElementById('description_'+id).value.length || document.getElementById('weight_'+id).value.length || document.getElementById('height_'+id).value.length || document.getElementById('depth_'+id).value.length || document.getElementById('length_'+id).value.length) {
		document.getElementById('inputFields_description_'+id).value = document.getElementById('inputFields_description_'+id).value.replace("optional","required");
		document.getElementById('inputFields_weight_'+id).value = document.getElementById('inputFields_weight_'+id).value.replace("optional","required");
		document.getElementById('inputFields_height_'+id).value = document.getElementById('inputFields_height_'+id).value.replace("optional","required");
		document.getElementById('inputFields_length_'+id).value = document.getElementById('inputFields_length_'+id).value.replace("optional","required");
		document.getElementById('inputFields_depth_'+id).value = document.getElementById('inputFields_depth_'+id).value.replace("optional","required");
	} else if (id != 1) {  // 1 is always required.
		document.getElementById('inputFields_description_'+id).value = document.getElementById('inputFields_description_'+id).value.replace("required","optional");
		document.getElementById('inputFields_weight_'+id).value = document.getElementById('inputFields_weight_'+id).value.replace("required","optional");
		document.getElementById('inputFields_height_'+id).value = document.getElementById('inputFields_height_'+id).value.replace("required","optional");
		document.getElementById('inputFields_length_'+id).value = document.getElementById('inputFields_length_'+id).value.replace("required","optional");
		document.getElementById('inputFields_depth_'+id).value = document.getElementById('inputFields_depth_'+id).value.replace("required","optional");
	}
}

function checkMainFreight() {
	//Main Freight do not provide transport assurance
	if (getObject("ServiceType").value == "MainFreightRoad") {
		getObject("wholeValue").value = 'N/A';	
		getObject("wholeValue").disabled = true;	
	} else {
		
		getObject("wholeValue").disabled = false;	
		getObject("wholeValue").value = '';	
	}	
}
