// 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 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;

	if(oForm.userType == null){
		userTypeLength = undefined;
	}else{
		userTypeLength = oForm.userType.length;
	}

	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.  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)){
		return false;
	}

	if(returnValue){
		return checkFormComplete(oForm);
	}else{
		alert('There are some outstanding form issues');
		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(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] = $('description_' + intRow).options[intRowType].text + "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] = $('description_' + intRow).options[intRowType].text + '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(aryItemForkLift[intRowType]){
							if(oItem.value > aryItemForkLift[intRowType]){
								setCellBorder = true;

								switch(intRowType)
								{
								case '10':
									aryIssuesDisplayed[oItem.name] = "Maximum weight for '" + $('description_' + intRow).options[intRowType].text + "' 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] = $('description_' + intRow).options[intRowType].text + "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] = $('description_' + intRow).options[intRowType].text + '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] = 'Must be sent in a carton/box (packaging with shrinkwrap/commercial pallet wrap alone will not be accepted).';
					}
					break;
					
				case 'selectFurniture':
					if(oItem.value.length > 0 && oItem.value == 13){
						aryIssuesDisplayed[oItem.name] = 'Smart Send does not transport assembled furniture.  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(aryItemMaxWeight[intRowType]){
							if(oItem.value > aryItemMaxWeight[intRowType]){
								setCellBorder = true;
								arySubmitIssue[oItem.name] = true;

								switch(intRowType){
									case '1':
										aryIssuesDisplayed[oItem.name] = "Maximum weight for '" + $('description_' + intRow).options[intRowType].text + "' description is " + aryItemMaxWeight[intRowType] + "kgs. Please choose 'HEAVY CARTON' as the description";
										break;
									case '2':
										aryIssuesDisplayed[oItem.name] = "Maximum weight for '" + $('description_' + intRow).options[intRowType].text + "' 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 '" + $('description_' + intRow).options[intRowType].text + "' 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){
		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 DisableTaillift()
{
	if (getObject("ServiceType").value == "Express")
	{
		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");
	}
}

