// JavaScript Document
function stripSpaces(form_item){
	var mytext = new String("");
	
	mytext = form_item.value;
	while (mytext.charAt(0) == ' ') {
		mytext = mytext.substring(1,mytext.length);
	}
	while (mytext.charAt(mytext.length - 1) == ' ') {
		mytext = mytext.substring(0, (mytext.length - 1));
	}
	form_item.value = mytext;
};
function checkForum(form_item){
	if(form_item.value == 1){
		document.forms[0].f_status.value = '';
		//form_item.checked = true;
	}
	if(document.forms[0].f_status.value != 'pen' && document.forms[0].f_status.value != 'inv' && document.forms[0].f_status.value != 'rej' && document.forms[0].f_status.value != 'dec' && 	document.forms[0].f_status.value != 'can' == false && document.forms[0].f_status.value != 'can7' && document.forms[0].f_status.value != 'wait' && document.forms[0].f_status.value != 'conf'){
		document.forms[0].f_status.value == 'pen';
	}
};
function checkURL(myItem){
	if((myItem.value.search(/http[s]?:\/\/[A-Za-z0-9_-]*/gi) == -1) && (myItem.value + "" != "n/a")){
		alert("Please check your URL.");
		myItem.focus();
		myItem.value = "http://www.";
		return false;
	}else{
		return true;
	}
};

function checkEmail(myItem){
	if((myItem.value.search(/[A-Za-z0-9_-]*@[A-Za-z0-9_-]*\.[A-Za-z0-9_-]*/gi) == -1) && (myItem.value + "" != "n/a") && (myItem.value + "" != "")){
		alert("Please check your email address.");
		myItem.focus();
		myItem.value = "";
		return false;
	}else{
		return true;
	}
};

function stripPhone(form_item){
	var mytext = new String(""),
		mytemp = new String(""),
		mylength = 0,
		i = 0;
	
	mytext = form_item.value;
	mylength = mytext.length;
	if(mytext != "n/a"){		
		while(mytext.charAt(0).search(/[\d+(]/gi) == -1){
			mytext = mytext.substring(1,mytext.length);
			if(mytext.length == 0){
				break;
			}
		};
		for(var i = 1; i < mylength; i++){
			if(mytext.charAt(i).search(/[\d\s()]/gi) == -1){
				mytemp = mytext.substring(0,i);
				mytext = mytext.substring(i+1,mytext.length);
				mytext = mytemp + " " + mytext;
				mylength;
				i;
			}
		};
		if(mytext.charAt(mylength-1).search(/[\d\s()]/gi) == -1){
			mytext = mytext.substring(0,mylength-1);
		}
		form_item.value = mytext;
	}
	if(mytext == ""){
		restoreDialingCode(form_item);
	}
};

function dialingCode(){
	var myForm = document.forms[0],
		tempCode = "",
		myIndex = 0;
	
	for(var i = 0; i < myForm.dialing_id.length; i++){
		if(myForm.dialing_id[i].value == myForm.f_id_country.options[myForm.f_id_country.selectedIndex].value){
			myIndex = i;
		}
	};
	if(myForm.f_id_country.selectedIndex == 0){
		if("" + myForm.f_tel.value == "+" + myForm.code.value){
			myForm.f_tel.value = "";
		}
		if("" + myForm.f_fax.value == "+" + myForm.code.value){
			myForm.f_fax.value = "";
		}
		if(myForm.f_tel_pers){
			if("" + myForm.f_tel_pers.value == "+" + myForm.code.value){
				myForm.f_tel_pers.value = "";
			}
			if("" + myForm.f_fax_pers.value == "+" + myForm.code.value){
				myForm.f_fax_pers.value = "";
			}
			if("" + myForm.f_mobile.value == "+" + myForm.code.value){
				myForm.f_mobile.value = "";
			}
		}
		if(myForm.f_as_tel_pers){
			if("" + myForm.f_as_tel_pers.value == "+" + myForm.code.value){
				myForm.f_as_tel_pers.value = "";
			}
			if("" + myForm.f_as_fax_pers.value == "+" + myForm.code.value){
				myForm.f_as_fax_pers.value = "";
			}
			if("" + myForm.f_as_mobile.value == "+" + myForm.code.value){
				myForm.f_as_mobile.value = "";
			}
		}
		myForm.code.value = "";
	}else{
		tempCode = "" + myForm.code.value;
		myForm.code.value = myForm.dialing_code[myIndex].value;
		if("" + myForm.f_tel.value == "+" + tempCode || "" + myForm.f_tel.value == "" || "" + myForm.f_tel.value == "+"){
			restoreDialingCode(myForm.f_tel);
		}
		if("" + myForm.f_fax.value == "+" + tempCode || "" + myForm.f_fax.value == "" || "" + myForm.f_fax.value == "+"){
			restoreDialingCode(myForm.f_fax);
		}
		if(myForm.f_tel_pers){
			if("" + myForm.f_tel_pers.value == "+" + tempCode || "" + myForm.f_tel_pers.value == "" || "" + myForm.f_tel_pers.value == "+"){
				restoreDialingCode(myForm.f_tel_pers);
			}
			if("" + myForm.f_fax_pers.value == "+" + tempCode || "" + myForm.f_fax_pers.value == "" || "" + myForm.f_fax_pers.value == "+"){
				restoreDialingCode(myForm.f_fax_pers);
			}
			if("" + myForm.f_mobile.value == "+" + tempCode || "" + myForm.f_mobile.value == "" || "" + myForm.f_mobile.value == "+"){
				restoreDialingCode(myForm.f_mobile);
			}
		}
		if(myForm.f_as_tel_pers){
			if("" + myForm.f_as_tel_pers.value == "+" + tempCode || "" + myForm.f_as_tel_pers.value == "" || "" + myForm.f_as_tel_pers.value == "+"){
				restoreDialingCode(myForm.f_as_tel_pers);
			}
			if("" + myForm.f_as_fax_pers.value == "+" + tempCode || "" + myForm.f_as_fax_pers.value == "" || "" + myForm.f_as_fax_pers.value == "+"){
				restoreDialingCode(myForm.f_as_fax_pers);
			}
			if("" + myForm.f_as_mobile.value == "+" + tempCode || "" + myForm.f_as_mobile.value == "" || "" + myForm.f_as_mobile.value == "+"){
				restoreDialingCode(myForm.f_as_mobile);
			}
		}
	}
};

function restoreDialingCode(form_item){
	var myForm = form_item.form;
	
	if(myForm.code){
		form_item.value = "+" + myForm.code.value;
	}else{
		form_item.value = "+" + myForm.at_code.value;
	}
};

function checkNumeric(myItem){
	var mytext;
	
	mytext = "" + myItem.value;
	if((isNaN(myItem.value) && mytext.toLowerCase() != "n/a" && mytext != "") || mytext.length > 18){
		alert("This field should contain a number.");
		myItem.focus();
		myItem.value = "";
		return false;
	}else{
		return true;
	}	
};

function checkInt(myItem){
	var mytext;
	
	mytext = "" + myItem.value;
	if((isNaN(myItem.value) && mytext.toLowerCase() != "n/a" && mytext != "") || mytext.length > 9){
		alert("This field should contain a number.");
		myItem.focus();
		myItem.value = "";
		return false;
	}else if(mytext != "" && mytext.toLowerCase() != "n/a" && mytext.length <= 9){
		myItem.value = parseInt(mytext);
		return true;
	}else{
		return true;
	}
};

function countSMS(myItem, myIndex){
	myLength = 0,
	myString = "",
	counter = 0;
	
	myString = "" + myItem.value;
	myLength = myString.length;

	counter = myLength;
	if(myIndex >= 0){
		document.forms[0].counterSMS[myIndex].value = counter;
	} else{
		document.forms[0].counterSMS.value = counter;
	}
};

function countChar150(myItem, myIndex){
	myLength = 0,
	myString = "",
	counter = 0;
	
	myString = "" + myItem.value;
	myLength = myString.length;

	if (myLength > 150){
		myItem.value = myString.substring(0, 150);
		alert("Maximum size is 150 characters.");	
	}
	counter = myLength;
	if(myIndex >= 0){
		document.forms[0].counter150[myIndex].value = counter;
	} else{
		document.forms[0].counter150.value = counter;
	}
};

function countChar200(myItem, myIndex){
	myLength = 0,
	myString = "",
	counter = 0;
	
	myString = "" + myItem.value;
	myLength = myString.length;

	if (myLength > 200){
		myItem.value = myString.substring(0, 200);
		alert("Maximum size is 150 characters.");	
	}
	counter = myLength;
	if(myIndex >= 0){
		document.forms[0].counter200[myIndex].value = counter;
	} else{
		document.forms[0].counter200.value = counter;
	}
};

function countChar255(myItem, myIndex){
	myLength = 0,
	myString = "",
	counter = 0;
	
	myString = "" + myItem.value;
	myLength = myString.length;

	if (myLength > 255){
		myItem.value = myString.substring(0, 255);
		alert("Maximum size is 255 characters.");	
	}
	counter = myLength;
	if(myIndex >= 0){
		document.forms[0].counter255[myIndex].value = counter;
	} else{
		document.forms[0].counter255.value = counter;
	}
};

function countChar500(myItem, myIndex){
	myLength = 0,
	myString = "",
	counter = 0;
	
	myString = "" + myItem.value;
	myLength = myString.length;

	if (myLength > 500){
		myItem.value = myString.substring(0, 500);
		alert("Maximum size is 500 characters.");	
	}
	counter = myLength;
	if(myIndex >= 0){
		document.forms[0].counter500[myIndex].value = counter;
	} else{
		document.forms[0].counter500.value = counter;
	}
};

function countChar750(myItem, myIndex){
	myLength = 0,
	myString = "",
	counter = 0;
	
	myString = "" + myItem.value;
	myLength = myString.length;

	if (myLength > 750){
		myItem.value = myString.substring(0, 750);
		alert("Maximum size is 750 characters.");	
	}
	counter = myLength;
	if(myIndex >= 0){
		document.forms[0].counter750[myIndex].value = counter;
	} else{
		document.forms[0].counter750.value = counter;
	}
};

function countChar1000(myItem, myIndex){
	myLength = 0,
	myString = "",
	counter = 0;
	
	myString = "" + myItem.value;
	myLength = myString.length;

	if (myLength > 1000){
		myItem.value = myString.substring(0, 1000);
		alert("Maximum size is 1000 characters.");	
	}
	counter = myLength;
	if(myIndex >= 0){
		document.forms[0].counter1000[myIndex].value = counter;
	} else{
		document.forms[0].counter1000.value = counter;
	}
};

function countChar1500(myItem, myIndex){
	myLength = 0,
	myString = "",
	counter = 0;
	
	myString = "" + myItem.value;
	myLength = myString.length;

	/*if (myLength > 1500){
		myItem.value = myString.substring(0, 1500);
		alert("Maximum size is 1500 characters.");	
	}*/
	counter = myLength;
	if(myIndex >= 0){
		document.forms[0].counter1500[myIndex].value = counter;
	} else{
		document.forms[0].counter1500.value = counter;
	}
};
function countChar2000(myItem, myIndex){
	myLength = 0,
	myString = "",
	counter = 0;
	
	myString = "" + myItem.value;
	myLength = myString.length;

	if (myLength > 2000){
		myItem.value = myString.substring(0, 2000);
		alert("Maximum size is 2000 characters.");	
	}
	counter = myLength;
	if(myIndex >= 0){
		document.forms[0].counter2000[myIndex].value = counter;
	} else{
		document.forms[0].counter2000.value = counter;
	}
};

function checkPresentation(myItem){
	if((myItem.value.search(/[A-Za-z0-9_-]*\.pdf|ppt|pps/gi) == -1) && (myItem.value + "" != "n/a") && (myItem.value + "" != "")){
		alert("Presentation file should be a Powerpoint or a PDF file.");
		myItem.focus();
		return false;
	}else{
		return true;
	}
};
function checkPresentation_pdf(myItem){
	if((myItem.value.search(/[A-Za-z0-9_-]*\.pdf/gi) == -1) && (myItem.value + "" != "n/a") && (myItem.value + "" != "")){
		alert("Presentation file should be a PDF file.");
		myItem.focus();
		return false;
	}else{
		return true;
	}
};

function checkLogo(myItem){
	if((myItem.value.search(/[A-Za-z0-9_-]*\.eps|tiff|tif|jpeg|jpg|jpe|ai|ai5|ai4|ai3|ps/gi) == -1) && (myItem.value + "" != "n/a") && (myItem.value + "" != "")){
		alert("Logo file should be an EPS, a TIFF or a JPEG file.");
		myItem.focus();
		return false;
	}else{
		return true;
	}
};

function checkLogoWeb(myItem){
	if((myItem.value.search(/[A-Za-z0-9_-]*\.gif|png|jpeg|jpg|jpe/gi) == -1) && (myItem.value + "" != "n/a") && (myItem.value + "" != "")){
		alert("Logo file should be an a PNG, GIF or a JPEG file.");
		myItem.focus();
		return false;
	}else{
		return true;
	}
};

function checkDateMonth(myItem){
	var myArrayDate, myMonth, myYear, myString, myYearDigit;
	
	myString = myItem.value + "";
	
	myArrayDate = myString.split("/");
	myMonth = Math.round(parseFloat(myArrayDate[0]));
	myYear = Math.round(parseFloat(myArrayDate[1]));
	myString = myArrayDate[1] + "";
	myYearDigit = myString.length;
	if (myItem.value + "" != "mm/aaaa" && myItem.value + "" != "mm/yyyy" && myItem.value + "" != "n/a" && (isNaN(myMonth) || isNaN(myYear) || (myYear < 1) || (myMonth < 1) || (myMonth > 12) || (myYearDigit != 4))){
		alert("Please check date format (mm/yyyy).");
		myItem.focus();
		myItem.value = "mm/yyyy";
		return false;
	}else{
		return true;
	}
};

function restoreDateMonth(myItem){
	if ("" + myItem.value == "" ){
		myItem.value = "mm/yyyy";
	}
};

function restoreDate(myItem){
	if ("" + myItem.value == "" ){
		myItem.value = "dd/mm/yyyy";
	}
};

function restoreTime(myItem){
	if ("" + myItem.value == "" ){
			myItem.value = "hh:mm";
	}
};

function emptyMe(myItem) {
	if ("" + myItem.value == "mm/yyyy" || "" + myItem.value == "hh:mm" || "" + myItem.value == "dd/mm/yyyy"){
			myItem.value = "";
	}
};

function checkHour(myItem){
	var myArrayHour, mytext, myHour, myMinute;
	mytext = "" + myItem.value;
	
	myArrayHour = mytext.split(":");
	myHour = parseInt(myArrayHour[0]);
	myMinute = parseInt(myArrayHour[1]);
	if((mytext != "hh:mm") && (isNaN(myHour) || isNaN(myMinute) || (myHour < 0) || (myHour > 23) || (myMinute < 0) || (myMinute > 59)))
	{
		alert("Please check your the format of the time (hh:mm)");
		myItem.focus();
		myItem.value = "hh:mm";
		return false;
	}
	else
	{
		return true;
	}
};

function checkCardNumber(myItem){
	var myStr;
	myStr = "" + myItem.value;
	
	if(isNaN(myItem.value) || (myStr.length != 16 && myStr.length != 15)){
		alert("Please check your credit card number, it should be 15 or 16 digits long without spaces.");
		myItem.focus();
		myItem.value = "";
	}
};

function checkExpiration(myItem){
	var myArrayDate, myMonth, myYear, myString, myYearDigit;
	myString = myItem.value + "";
	
	myArrayDate = myString.split("/");
	myMonth = parseInt(myArrayDate[0]);
	myYear = parseInt(myArrayDate[1]);
	myString = myArrayDate[1] + "";
	myYearDigit = myString.length;
	if (isNaN(myMonth) || isNaN(myYear) || (myMonth < 0) || (myMonth > 12) || (myYearDigit < 4))
	{
		alert("Please check your expiration date format (mm/yyyy)");
		myItem.focus();
		myItem.value = "mm/yyyy";
		return false;
	}
	else
	{
		return true;
	}
};

function checkAndCompareDate(FirstDate,SecondDate,type){
	if(checkDate(FirstDate) && checkDate(SecondDate)) return true;
	if(compareDates(FirstDate, SecondDate, type)) return true;
	else {
		SecondDate.value = "dd/mm/yyyy";
		alert("Arrival date should be before departure date.");
		return false;
	}
};

function checkDate(myItem){
	var myArrayDate, myDay, myMonth, myYear, myString, myYearDigit;
	myString = myItem.value + "";
	if (myString == "" || myString == "dd/mm/yyyy"){
		myItem.value = "dd/mm/yyyy";
		return true;
	}
	myArrayDate = myString.split("/");
	
	myDay = Math.round(parseFloat(myArrayDate[0]));
	myMonth = Math.round(parseFloat(myArrayDate[1]));
	myYear = Math.round(parseFloat(myArrayDate[2]));
	myString = myYear + "";
	myYearDigit = myString.length;
	
	if (isNaN(myDay) || isNaN(myMonth) || isNaN(myYear) || (myYear < 1) || (myDay < 1) || (myMonth < 1) || (myMonth > 12) || (myYearDigit != 4) || (myDay > days_in(myMonth, myYear)))
	{
		alert("Please check your date format. (dd/mm/yyyy)");
		myItem.value = "dd/mm/yyyy";
		return true;
	}
	else
	{
		return false;
	}
};

function isLeap(year){
	if(year % 400 == 0){
		return true;
	}
	else if((year % 4 == 0) && (year % 100 != 0)){
		return true
	}
	else return false;
};
	
function days_in(month, year){
	if(month == 4 || month == 6 || month == 9 || month == 11){
		return 30;
	}
	else if(!isLeap(year) && month == 2){
		return 28;
	}
	else if(isLeap(year) && month == 2){
		return 29;
	}
	else return 31;
};

function compareDates(myDate, myDate2, type){
	
	var myArrayDate, myArraydate2, myDay, myMonth, myYear, myString, myString2, myYearDigit, theDate = new Date(), theDate2 = new Date(), debut = new Date, fin = new Date();
	
	debut.setDate(10);
	debut.setMonth(03);
	debut.setYear(2002);
	fin.setDate(12);
	fin.setMonth(03);
	fin.setYear(2002);
	myString = myDate.value + "";
	myString2 = myDate2.value + "";
	myArrayDate = myString.split("/");
	myArrayDate2 = myString2.split("/");
	theDate.setDate(myArrayDate[0]);
	theDate.setMonth(myArrayDate[1]-1);
	theDate.setYear(myArrayDate[2]);
	theDate2.setDate(myArrayDate2[0]);
	theDate2.setMonth(myArrayDate2[1]-1);
	theDate2.setYear(myArrayDate2[2]);
	
	if (myString == "" || myString2 == "" || myString == "jj/mm/aaaa" || myString2 == "jj/mm/aaaa" || myString == "dd/mm/yyyy" || myString2 == "dd/mm/yyyy"){
			return true;
	}
	
	if(theDate2.valueOf() - theDate.valueOf() < 0){
		return false;
	}
	else{
		return true;
	}
};
function checktel(myItem, myCode){
	if((myItem.value.search(/^\+[0-9]+$/gi) == -1) || (myItem.value + "" == "") || (myItem.value + "" == "+" + myCode.value)){
		return false;
	}else{
		return true;
	}
};
