var $b = jQuery.noConflict();

var errorTextArr = {
						"firstname": {
										"required": "The first name field cannot be left blank",
										"min": "This first name is too short (2 characters minimum)",
										"max": "This first name is too long. (255 characters maximum)"
									 },
					    "lastname":  {
										"required": "The last name field cannot be left blank",
										"min": "This last name is too short (2 characters minimum)",
										"max": "This last name is too long. (255 characters maximum)"
									 },
						"email":     {
							            "required": "The email field cannot be left blank",
										"check": "Check your email"
									 },
						"feedback":  {
										"required": "The feedback field cannot be left blank",
										"min": "This feedback is too short (2 characters minimum)",
										"max": "This feedback is too long. (6000 characters maximum)"
									 },
						"from_city": {
							            "required": "From city must be selected"
									 },
						"to_city": {
							            "required": "To city must be selected"
									 },
						"date_of_shipment":  {
										"required": "The Date of Shipment field cannot be left blank",
										"min": "Date of Shipment is not valid (use select-button)",
										"max": "Date of Shipment is not valid (use select-button)"
									 },
						"total_number_of_packages":  {
										"required": "The Total Number of Packages field cannot be left blank",
										"min": "This Total Number of Packages is too short (1 characters minimum)",
										"max": "This Total Number of Packages is too long. (255 characters maximum)"
									 },
						"total_weight":  {
										"required": "The Total Weight field cannot be left blank",
										"min": "This Total Weight is too short (1 characters minimum)",
										"max": "This Total Weight is too long. (255 characters maximum)"
									 },
						"delivered_to_cargo":  {
										"required": "The To be delivered to Finnair Cargo field cannot be left blank",
										"min": "To be delivered to Finnair Cargo is not valid (use select-button)",
										"max": "To be delivered to Finnair Cargo is not valid (use select-button)"
									 },
					    "package_contents":  {
										"required": "The Contents field cannot be left blank",
										"min": "This Contents is too short (2 characters minimum)",
										"max": "This Contents is too long. (6000 characters maximum)"
									 },
						"package_weight":  {
										"required": "The Package Weight field cannot be left blank",
										"min": "This Package Weight is too short (1 characters minimum)",
										"max": "This Package Weight is too long. (255 characters maximum)"
									 },
						"package_measures":  {
										"required": "The Package Measures field cannot be left blank",
										"min": "This Package Measures is too short (2 characters minimum)",
										"max": "This Package Measures is too long. (255 characters maximum)"
									 },
						"shipper_type": {
							            "required": "Shipper type must be selected"
									 },
						"shipper_firstname":  {
										"required": "The Shipper first name field cannot be left blank",
										"min": "This Shipper first name is too short (2 characters minimum)",
										"max": "This Shipper first name is too long. (255 characters maximum)"
									 },
						"shipper_lastname":  {
										"required": "The Shipper last name field cannot be left blank",
										"min": "This Shipper last name is too short (2 characters minimum)",
										"max": "This Shipper last name is too long. (255 characters maximum)"
									 },
						"shipper_email":  {
										"required": "The Shipper email field cannot be left blank",
										"check": "Check your Shipper email"
									 },
						"shipper_phone":  {
										"required": "The Shipper phone field cannot be left blank",
										"min": "This Shipper phone is too short (2 characters minimum)",
										"max": "This Shipper phone is too long. (255 characters maximum)"
									 },									 
						"awb_number":  {
										"required": "The awb number field cannot be left blank",
										"min": "This awb number is too short (2 characters minimum)",
										"max": "This awb number is too long. (255 characters maximum)"
									 },
						"charges_payer": {
							            "required": "Charges payer must be selected"
									 },
						"address_of_consignee":  {
										"required": "The Address of Consignee field cannot be left blank",
										"min": "This Address of Consignee is too short (2 characters minimum)",
										"max": "This Address of Consignee is too long. (6000 characters maximum)"
									 }
					};
					
var alertString = "";

var finlandCityArr = [];

var packageCount=1;

function validRequestForm(submitUrl)
{
	clearValidErrors();
	alertString="";
	
	alertString+=stringValid('firstname',2,255);
	alertString+=stringValid('lastname',2,255);
	alertString+=emailValid('email');
	alertString+=stringValid('feedback',2,6000);
	
	if(alertString=="")
	{
		document.forms.requestForm.action=submitUrl;
		document.forms.requestForm.submit();
	}
	else
	{
		$b.prompt(alertString);
	}
	
}

function validBookingForm(submitUrl)
{
	clearValidErrors();
	var formType=$b("#form_type").val();
	
	alertString="";
	alertString+=selectValid('from_city','');
	alertString+=selectValid('to_city','to_city_other');
	
	if(formType==1)
	{
		alertString+=stringValid('date_of_shipment',9,11);
	}
	
	alertString+=stringValid('total_number_of_packages',1,255);
	alertString+=stringValid('total_weight',1,255);
	
	alertString+=stringValidPackage('contents',2,6000,'package_contents');
	
	if(formType==1)
	{
		alertString+=stringValid('delivered_to_cargo',9,11);
	}
	
	alertString+=radioButtonValid('shipper_type');
	
	alertString+=stringValid('shipper_firstname',2,255);
	alertString+=stringValid('shipper_lastname',2,255);
	alertString+=emailValid('shipper_email');
	alertString+=stringValid('shipper_phone',2,255);
	
	if(checkAwd())
	{
		alertString+=stringValid('awb_number',2,255);
	}
	
	alertString+=radioButtonValid('charges_payer');
	
	alertString+=validConsigneeAddress();
	
	if(alertString=="")
	{
		document.forms.bookingForm.action=submitUrl;
		document.forms.bookingForm.submit();
	}
	else
	{
		$b.prompt(alertString);
	}
}

//field validators-------------------------------------------------
function selectValid(inputId,otherField)
{
	if($b("#"+inputId).val()!="")
	{
		return "";
	}
	else if(otherField!="" && $b("#"+otherField).val().replace(/^\s+|\s+$/g, '')!="")
	{
		return "";
	}
	else
	{
		addRedBordersById(inputId);
		return errorTextArr[inputId]["required"]+'<br/>';
	}
	
}

function stringValid(inputId, min, max)
{
	var temp = $b("#"+inputId).val().replace(/^\s+|\s+$/g, '');
	
	if(temp.length==0)
	{
		addRedBordersById(inputId);
		return errorTextArr[inputId]["required"]+'<br/>';
	}
	else if(temp.length<min)
	{
		addRedBordersById(inputId);
		return errorTextArr[inputId]["min"]+'<br/>';
	}
	else if(temp.length>max)
	{
		addRedBordersById(inputId);
		return errorTextArr[inputId]["max"]+'<br/>';
	}
	else
	{
		return "";
	}
}

function stringValidPackage(inputId, min, max, errorId)
{
	var temp = $b("#"+inputId).val().replace(/^\s+|\s+$/g, '');
	
	if(temp.length==0)
	{
		addRedBordersById(inputId);
		return errorTextArr[errorId]["required"]+'<br/>';
	}
	else if(temp.length<min)
	{
		addRedBordersById(inputId);
		return errorTextArr[errorId]["min"]+'<br/>';
	}
	else if(temp.length>max)
	{
		addRedBordersById(inputId);
		return errorTextArr[errorId]["max"]+'<br/>';
	}
	else
	{
		return "";
	}
}

function emailValid(inputId)
{
	var temp = $b("#"+inputId).val().replace(/^\s+|\s+$/g, '');
	if(temp.length==0)
	{
		addRedBordersById(inputId);
		return errorTextArr[inputId]["required"]+'<br/>';
	}
	else if(emailCheck(temp)==false)
	{
		addRedBordersById(inputId);
		return errorTextArr[inputId]["check"]+'<br/>';
	}
	else
	{
		return "";	
	}		
}

function emailCheck(str) 
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function radioButtonValid(inputId)
{
	if($b("input[@name='" + inputId + "']:checked").val())
	{
		return "";
	}
	else
	{
		addRedBordersToRadio(inputId);
		return errorTextArr[inputId]["required"]+'<br/>';
	}
}

function validConsigneeAddress()
{
	var city = $b("#to_city").val();
	if(jQuery.inArray(city,finlandCityArr)!=-1)
	{
		return stringValid('address_of_consignee',2,6000);
	}
	else if($b("#charges_payer_Consignee").attr("checked"))
	{
		return "";
	}
	else
	{
		return "";
	}
}

// ui --------------------------------------------------------

function clearValidErrors()
{
	$b(".highlight").removeClass("highlight");
}

function addRedBordersById(inputId)
{
	$b("#" + inputId).addClass("highlight");
}
function addRedBordersToRadio(inputId)
{
	$b("#" + inputId + "_td").addClass("highlight");
}

function checkCity()
{
	var city = $b("#to_city").val();
	if(jQuery.inArray(city,finlandCityArr)!=-1)
	{
		showConsigneeAddress(1,1);
	}
	else if($b("#charges_payer_Consignee").attr("checked"))
	{
		showConsigneeAddress(1,0);
	}
	else
	{
		showConsigneeAddress(0,0);
	}
}

function checkAwdCompulsory()
{
	changeCompulsory('awb_number',checkAwd());
}

function checkAwd()
{
	var city = $b("#to_city").val();
	if(jQuery.inArray(city,finlandCityArr)!=-1)
	{
		return false;
	}
	else if($b("#shipper_type_Forwarding_Agent").attr("checked"))
	{
		return true;
	}
	else
	{
		return false;
	}
	
}

function changeCompulsory(field,typeBool)
{
	var compulsoryStr="";
	if(typeBool==1) compulsoryStr="*";
	
	$b("#"+field+"_compulsory").html(compulsoryStr);
}

function showConsigneeAddress(typeBool,compulsoryBool)
{
	if(typeBool==1)
	{
		$b("#address_of_consignee_tr").show();
		if(compulsoryBool==1)
		{
			changeCompulsory('address_of_consignee',1);
		}
		else
		{
			changeCompulsory('address_of_consignee',0);
		}
	}
	else
	{
		$b("#address_of_consignee_tr").hide();
	}
	
}

function enableDatePickers()
{
	document.getElementById('trigger_date_of_shipment').disabled = false;
	Calendar.setup({
		  inputField : 'date_of_shipment',
		  ifFormat : '%d.%m.%Y',
		  daFormat : '%d.%m.%Y',
		  button : 'trigger_date_of_shipment'
	});
	
	document.getElementById("trigger_delivered_to_cargo").disabled = false;
	Calendar.setup({
	      inputField : "delivered_to_cargo",
	      ifFormat : "%d.%m.%Y",
	      daFormat : "%d.%m.%Y",
	      button : "trigger_delivered_to_cargo"
	});
	packageCount=1;
}

function newPacketAdded()
{
	packageCount=packageCount+1;
	$b("#removePacketButton").show();
}

function removeLastPacket()
{
	$b("#singlePacketContainer"+packageCount+"first").remove();
	$b("#singlePacketContainer"+packageCount+"second").remove();
	$b("#singlePacketContainer"+packageCount+"third").remove();
	$b("#singlePacketContainer"+packageCount+"forth").remove();
	packageCount=packageCount-1;
	if(packageCount==1)
	{
		$b("#removePacketButton").hide();
	}
}
