// ************************************************************************
// This function parses the location URL by & and = signs.
// ************************************************************************
function getParm(string,parm) {
	// returns value of parm from string
	var startPos = string.indexOf(parm + "=");
	if (startPos > -1) {
		startPos = startPos + parm.length + 1;
		var endPos = string.indexOf("&",startPos);
	if (endPos == -1)
		endPos = string.length;
	return unescape(string.substring(startPos,endPos));
	}
	return '';
}

// ******************************************************************************
// This function checks if the input fields are empty on the netfare signup page.  
// If so, the system will prompt the user to input the missing fields.
// April 25, 2005
// It also validates username / password inputs
// ******************************************************************************
function checkSignupFields(){
	x = document.netfareSignup;
	
	if (isAcceptableInput(x.agencyUser.value) == false){		// Checks if the username field contains invalid chars
		alert("Username must not have spaces , ' or `");
		x.agencyUser.focus();
		return false;
	}
	if (x.password.value == ""){		// Checks if the password field is empty
		alert("Please enter a password");
		x.password.focus();
		return false;
	}
	if (x.password.value.length < 5 || x.password.value.length > 10) {	// Check if the input is 5 - 10 chars
		alert("Password must be 5-10 characters");
		x.password.focus();
		return false;
	}
	if (isAcceptableInput(x.password.value) == false){			// Checks if the password field contains invalid chars
		alert("Password must not have spaces , ' or `");
		x.password.focus();
		return false;
	}
	if (x.rePassword.value == ""){		// Checks if the re password field is empty
		alert("Please re-enter the password");
		x.rePassword.focus();
		return false;
	}
	if (isFieldMatch(x.password.value, x.rePassword.value) == false){
		alert("Password entries does not match.  Please re-enter the password");
		x.password.focus();
		return false;
	}
	if (x.agencyName.value == ""){		// Checks if the agency field is empty
		alert("Please enter your agency name");
		x.agencyName.focus();
		return false;
	}
	if (x.lastName.value == ""){	// Checks if the contact person field is empty
		alert("Please enter the last name of the contact person for this agency");
		x.lastName.focus();
		return false;
	}
	if (x.firstName.value == ""){	// Checks if the contact person field is empty
		alert("Please enter the first name of the contact person for this agency");
		x.firstName.focus();
		return false;
	}
	if (x.address.value == ""){			// Checks if the address field is empty
		alert("Please enter the address of this agency");
		x.address.focus();
		return false;
	}
	if (x.city.value == ""){			// Checks if the city field is empty
		alert("Please enter the city of this agency");
		x.city.focus();
		return false;
	}
	if (x.province.value == ""){		// Checks if the province field is empty
		alert("Please enter the province of this agency");
		x.province.focus();
		return false;
	}
	if (x.country.value == ""){		// Checks if the country field is empty
		alert("Please enter the Country of this agency");
		x.country.focus();
		return false;
	}
	if (x.postalCode.value.length < 5){		// Checks if the postalCode field is empty
		alert("Please enter the postal code of this agency");
		x.postalCode.focus();
		return false;
	}
	if (x.areaCode.value.length != 3){		// Checks if the phone field is 3 digit
		alert("Please enter a proper telephone number for this agency");
		x.areaCode.focus();
		return false;
	}
	if (x.phone2.value.length != 3){		// Checks if the phone field is 3 digit
		alert("Please enter a proper telephone number for this agency");
		x.phone2.focus();
		return false;
	}
	if (x.phone3.value.length != 4){		// Checks if the phone field is 4 digit
		alert("Please enter a proper telephone number for this agency");
		x.phone3.focus();
		return false;
	}
	if (x.email.value == ""){			// Checks if the email field is empty
		alert("Please specify the email address for this agency");
		x.email.focus();
		return false;
	}
	if (!isEmail(x.email.value)){		// Checks if the email syntax is valid
		alert("Please enter a proper E-mail Address");
		x.email.focus();
		return false;
	}
	if (x.reEmail.value == ""){			// Checks if the email field is empty
		alert("Please retype the email address");
		x.reEmail.focus();
		return false;
	}
	if (isFieldMatch(x.email.value, x.reEmail.value) == false){
		alert("Email entries does not match.  Please re-enter the email");
		x.email.focus();
		return false;
	}
	if (x.iata.value.length == ""){		// Checks if the iata field is empty
		alert("Please enter an IATA number for this agency");
		x.iata.focus();
		return false;
	}
	if (isFieldMatch(x.strName.value, x.varName.value) == false){
		alert("Please enter the characters as shown on the picture!");
		x.strName.focus();
		return false;
	}
	return true;					// Test passed
}

// ******************************************************************************
// This function checks if the email address is a valid one
// ******************************************************************************
function isEmail(inStr){
	temp=inStr.toLowerCase();		// change the email address into lower case
	len=temp.length-1;				// set temp variable for length of email

	// Checks 3 things
	// 1. @ with nothing in front or without the sign
	// 2. missing .
	// 3. nothing after .  (ie. .com, .ca etc)
	if (temp.indexOf("@")<1 || temp.indexOf(".") == -1 || temp.indexOf(".")==len)
		return false;
	
	// Checks if underscore is in the domain name (cannot happen)
	if (temp.indexOf("_")>temp.indexOf("@")-1)
		return false;
		
	// otherwise it is correct email.
	return true;
}

// *************************************************************************************
// This function checks if input field is valid
// *************************************************************************************
function isAcceptableInput(inStr){
	temp=inStr;						
	len=temp.length-1;				// set temp variable for length of input
	i = 0;

	// Check for correct ASCII Range, excluded spaces	
	while (i <= len){
		if (temp.substring(i,i+1) < '!' || temp.substring(i,i+1) > '~'){
			return false;
		}
		i++;
	}

	// Checks 3 things
	// 1. no commas
	// 2. no single quotes
	// 3. no single backquotes
	if (temp.indexOf(",") >= 0 || temp.indexOf("'") >= 0 || temp.indexOf("`") >= 0)
		return false;
	// otherwise it is correct email.
	return true;
}

// ******************************************************************************
// This function is used to retrieve cookie information using JavaScript.
// ******************************************************************************
function getCookie(name) {
	cookie = " " + document.cookie;
	search = " " + name + "=";
	setStr = null;
	offset = 0;
	end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

// ******************************************************************************
// This function checks if the SID had expired.  If so, you must re-login.
// It also keeps people from jumping right into the main page without loggin in.
// ******************************************************************************
function checkPassword(){
	if (!getCookie('SID') || !getCookie('agency')){
		top.location="http://www.jadetours.com";
		return false;
	}
	return true;
}

// ******************************************************************************
// This function logouts the user by setting the cookie password to expire.
// ******************************************************************************
function setCookie (name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

// ******************************************************************************
// This function is used to click all mails to be deleted automatically.
// ******************************************************************************
function chkAll() {
	for (i = 0; i <= document.messageListForm.numMail.value; i++) {
	    document.messageListForm.elements[i+1].checked=document.messageListForm.chkAllMail.checked;
	}
}


function numbersonly(myfield, e, dec) {
	var key;
	var keychar;

	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
		
	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
		return true;

	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
		return true;

	// decimal point jump
	else if (dec && (keychar == ".")) {
		myfield.form.elements[dec].focus();
		return false;
	}
	else
		return false;
}

function checkChangeFields(){
	x = document.netfareSignup;
	if (x.login.value == ""){		// login field empty?
		alert("Please enter your login name");
		x.login.focus();
		return false;
	}
	if (x.password.value == ""){	// password field empty?
		alert("Please enter your password");
		x.password.focus();
		return false;
	}
	if (x.newPassword.value != ""){	// new password exist
		if (x.newPassword.value.length < 5 || x.newPassword.value.length > 10) {	// Check if the input is 5 - 10 chars
			alert("Password must be 5-10 characters");
			x.newPassword.focus();
			return false;
		}
		if (isAcceptableInput(x.newPassword.value) == false){			// Checks if the password field contains invalid chars
			alert("Password must not have spaces , ' or `");
			x.newPassword.focus();
			return false;
		}
		if (x.reNewPassword.value == ""){		// Checks if the re password field is empty
			alert("Please re-enter the password");
			x.reNewPassword.focus();
			return false;
		}
		if (isFieldMatch(x.newPassword.value, x.reNewPassword.value) == false){
			alert("New password doesn't match. Please re-enter your NEW password");
			x.newPassword.focus();
			return false;
		}
	}	
	return true;					// Test passed
}

function noComma(){
	var key;
	var keychar;

	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
		
	keychar = String.fromCharCode(key);

	// control keys
	if ( (",").indexOf(keychar) > -1 )
		return false;

	else
		return true;
}

function isFieldMatch(inputField1, inputField2)
{
	if (inputField1 == (inputField2))
		return true;
	else
		return false;
}