/***************************************************************************
*<PRE>
* Copyright (c) 1999 by Datalex Communications USA, Inc. AllRights Reserved.
*
* Synopis:  This utility file contains javascript methods that are common to a number of jsp pages.
*           These include:
		- changeLocation
		- checkEmail
		- checkField
		- checkFrmAir
		- checkLength
		- checkLoginForm
		- checkValidDate
		- initScreen
		- openWindow
		- removeSpaces
		- saveDefault
		- setFormAction
		- isEmpty
		- isWhitespace
*
* See also utils.js
* @author $Author: mkilleen $
*
* @version $Revision: 1.11 $ $Date: 2003/12/12 11:59:50EST $
*
* $Log: LVjsp/std/common.js  $
* Revision 1.11 2003/12/12 11:59:50EST mkilleen 
* PR 12,368 - Allows 4 word passwords to be valid on login in the check length. 
* Is in patch 17 for Dec 03.
* Revision 1.10 2002/10/09 14:05:35EDT adunne 
* 
* Revision 1.9 2002/10/08 21:12:29GMT+01:00 adunne 
* 
* Revision 1.8 2002/09/23 16:43:45GMT+01:00 mlooby 
* pr8694 - the checkEmail address was not checking for a second '.' if it already found 1. Some email addresses contai\n  >1 '.'
* Revision 1.7 2002/09/19 15:01:17GMT+01:00 mlooby 
* Added the following functions which are useful when validating user input.
* Revision 1.6 2002/09/10 16:11:37GMT+01:00 adunne 
* 
* Revision 1.5 2002/09/03 10:41:23GMT+01:00 adunne 
* 
* Revision 1.4 2002/08/29 19:55:46GMT+01:00 adunne 
* 
* Revision 1.3 2002/08/20 14:24:26GMT+01:00 mlooby 
* Added two new function isWhiteSpace and isEmpty
* Revision 1.2 2002/06/25 18:29:09GMT+01:00 mlooby 
* got rid of the suffix and the middle initial in the checkNameLength method. Not used by LV.com
* Revision 1.1 2002/03/27 12:56:12GMT+00:00 mlooby 
* Initial revision
* Member added to project d:/Source/LVCOM_BIC_2_4.pj
* Revision 1.13 2002/02/04 16:11:51GMT+00:00 mloughnane 
* Edited method checkLength so it could be used to check for a minimum length only 
* or a maximum length only.  (PR 6724)
* Revision 1.12 2002/02/01 17:15:52GMT mloughnane 
* Added method removeSpaces. (PR 6683)
* Revision 1.11 2002/01/30 16:52:17GMT jenglish 
* Fixes PR 6,626 - added new method checkDirectAccess
* Revision 1.10 2002/01/17 10:39:22GMT mloughnane 
* Added function checkValidDate to check that dates are not in the past. (PR 6438)
* Revision 1.9 2002/01/09 12:16:45GMT mloughnane 
* Removed alerts that were used for debugging - should have been removed from 
* previous version.
* Revision 1.8 2002/01/09 12:15:17GMT mloughnane 
* Added a function checkNameLength which checks if the total length of the name is 
* not longer than 60 characters. (PR 6352)
* Revision 1.7 2002/01/08 10:18:18GMT mloughnane 
* Increased minimum length of password from 4 to 6.
* Revision 1.6 2001/12/03 16:09:09GMT mloughnane 
* Removed AddToNewOrCurrentItinerary method.
* Revision 1.5 2001/11/23 14:34:00GMT award 
* Modified saveDefault to deal with case where there is only one option.
* Revision 1.4 2001/11/14 11:07:14GMT mloughnane 
* Added code for processing pages.
* Revision 1.3 2001/10/22 14:04:55IST maevel 
* Added some functions.
* Revision 1.2  2001/10/19 11:14:11  maevel
* Added some functions.
* Revision 1.1  2001/10/18 16:49:18  maevel
* Initial revision
*
*</PRE>
***************************************************************************/

// whitespace characters
var whitespace = " \t\n\r";

var images = new Array();

images[0] = new Image();
images[0].src = "lvimages/search_air1.gif";
images[1] = new Image();
images[1].src = "lvimages/search_air2.gif";
images[2] = new Image();
images[2].src = "lvimages/search_air3.gif";


/**
 * This function sets the location of the present window to the specified url.
 */
function changeLocation(url)
{
	location.href=url;
}

/*
*Check whether string s is empty.
*/
function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}

// Returns true if character c is an English letter 
// (A .. Z, a..z).
//
function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

// isAlphabetic (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) only.
//
//
function isAlphabetic (s, errorMessage) 
{ 
	var i;
    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphabetic character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is letter.
        var c = s.charAt(i);
        if (!isLetter(c))
        {
			alert(errorMessage);
			return false;
		}
    }
    // All characters are letters.
    return true;
}

// isAlphanumeric (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) and numbers only.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.
function isAlphanumeric (s)
{
	var i;
    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }

    // All characters are numbers or letters.
    return true;
}

/*
*Returns true if string s is empty or 
*whitespace characters only.
*/
function isWhitespace (s)
{ 
	var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

/**
 * This function checks that the email is the correct format and if it isn't an appropriate
 * error message is displayed.
 */
function checkEmail(FieldValue, errorMessage)
{
	if (FieldValue.length<1 || FieldValue.indexOf(" ")>-1 || FieldValue.indexOf("@")<1 || FieldValue.indexOf(".")<1)
	{
		if (errorMessage != null)
		{
			alert(errorMessage);
		}
		return false;
	}
	else
	{
		return true;
	}
}

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
function isEmailOK(s, errorMessage)
{
    // is s whitespace?
    if (isWhitespace(s))
    { 
		if (errorMessage != null)
		{
			alert(errorMessage);
		}
		return false;
	}	
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    {
		i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) 
    {
		if (errorMessage != null)
		{
			alert(errorMessage);
		}
		return false;
	}	
    else
    {
		i += 2;
	}	

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { 
		i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) 
    {
		if (errorMessage != null)
		{
			alert(errorMessage);
		}
		return false;
	}	
    else
    {
		return true;
	}		
}

/**
 * This function checks that the field passed in is blank and if it is, displays an appropriate
 * error message.
 */
function checkField(field, errorMessage)
{
	if (field.value=="")
	{
		alert(errorMessage);
		return false;
	}
	return true;
}
/**
  * This function checks that a departure city and an arrival city have both been entered in the
  * appropriate fields and that the length of the city is between 1 and 50 characters.
  * If either city length doesn't comply false, this function returns a value of false.
  * The parameters passed in are:
  * 	- depLen - the field containing the departure city value
  *	- arrLen - the field containing the arrival city value
  *	- errorMessage1 - the error message to display if the departure city is missing
  *	- errorMessage2 - the error message to display if the arrival city is missing
  */
function checkFrmAir(depField, arrField, errorMessage1, errorMessage2)
{
	var bValidInput = true;
	
	bValidInput = checkLength(depField, 1, -1, errorMessage1);
	if (bValidInput)
	{
		bValidInput = checkLength(arrField, 1, -1, errorMessage2);
	}
	if (bValidInput)
	{
		if ((parseInt(document.forms['airRes'].elements['PTypeValue1'].value) + parseInt(document.forms['airRes'].elements['PTypeValue2'].value) + parseInt(document.forms['airRes'].elements['PTypeValue5'].value) + parseInt(document.forms['airRes'].elements['PTypeValue4'].value)) > 4)	
		{
			alert("Maximum of 4 passengers allowed. Please re-select");		
			bValidInput = false;
		}
	}
	return bValidInput;
}
/**
 * This function checks that if we are doing an availability and direct access is checked that we specified
 * at leaset one airline is selected. If not an error message is displayed.
 */
function checkDirectAccess(searchType,directAccess,PrefAirline1,PrefAirline2,PrefAirline3,errorMessage)
{
	var isearchType = searchType.value;
        var idirectAccess = directAccess.checked;
        var iPrefAirline1 = PrefAirline1.value;
        var iPrefAirline2 = PrefAirline2.value;
        var iPrefAirline3 = PrefAirline3.value;

	if(isearchType == "FS") 
	{
	    if(idirectAccess) 
            {
                    if( (iPrefAirline1 == "") && (iPrefAirline2 == "") && (iPrefAirline3 == "") )
                    {
                        if (errorMessage != null)
	                {
		            alert(errorMessage);
                            return false;
                        }
                    }
             }
        }

        return true;
	
}

/**
 * This function checks the length of the fieldvalue passed in. 
 * If the length of the field is less that the minimum length (iMinLength) or greater 
 * than the maximum length (iMaxLength), then an alert box with an appropriate message (errorMessage)
 * is displayed.
 */
function checkLength(FieldValue, iMinLength, iMaxLength, errorMessage)
{	
	if(iMinLength != -1 && iMaxLength != -1)
	{
		if (FieldValue.value.length < iMinLength || FieldValue.value.length > iMaxLength)
		{
			if (errorMessage != null)
				{alert(errorMessage); }
			FieldValue.focus();
			return false;
		}
	}
	else if(iMinLength == -1)
	{
		if (FieldValue.value.length > iMaxLength)
		{
			if (errorMessage != null)
				{alert(errorMessage); }
			FieldValue.focus();
			return false;
		}		
	}
	else if(iMaxLength == -1)
	{
		if (FieldValue.value.length < iMinLength )
		{
			if (errorMessage != null)
				{alert(errorMessage); }
			FieldValue.focus();
			return false;
		}	
	}
	return true;
}
/**
  * This function checks that the username and password entered in the login form are 
  * the appropriate length i.e. between 4 and 20 characters.
  * If either length doesn't comply false, this function returns a value of false.
  * The parameters passed in are:
  * 	- userLen - the field containing the username value
  *	- passLen - the field containing the password value
  *	- errorMessage1 - the error message to display if the username is missing
  *	- errorMessage2 - the error message to display if the password is missing
  */
function checkLoginForm(user, password, errorMessage1, errorMessage2)
{
    var bValidInput = true; 
  
    bValidInput = checkLength(user, 4, 20, errorMessage1); 
    if (bValidInput)
    {
        bValidInput = checkLength(password, 4, 20, errorMessage2); 
    }
    return bValidInput;
}
/**
  * This function checks that the full length of the traveler/passenger/client name
  * is not more than 60 characters.
  */
function checkNameLength(salutation, firstName, surname, errorMessage)
{
  var nameLength = salutation.length + firstName.length + surname.length;
 
    if(nameLength > 60)
    {
    	alert(errorMessage);
    	return false;
    }
    return true;
}
/**
  * This function checks that the date is not in the past.
  */
function checkValidDate(sDay, sMonth, sYear, sErrorMessage)
{
	var iDay = sDay.value;
	var iMonth = sMonth.value;
	var iYear = sYear.value;
	
	today = new Date();
	if (iYear<today.getYear() || (iYear==today.getYear() && iMonth<today.getMonth()) || (iYear==today.getYear() && iMonth==today.getMonth() && iDay < today.getDate()) )
	{
		alert(sErrorMessage);
		return false;
	}
	return true;
}
/** 
 * This function sets the focus to the specified window.
 */
function initScreen(wind)
{
	wind.focus();
}

/*
 * This function opens a window with the following variables passed in.
 *	- href- the url of the window to open
 *	- sWidth - the width of the window
 *	- sHeight - the height of the window
 */
function openWindow(href, sWidth, sHeight) {
	hWnd = window.open(href, "", 'toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=no,scrollbars=yes,width='+sWidth+',height='+sHeight+',top=150,left=100');
	return(false);
}
/*
 * This function removes spaces from the parameter passed in
 *	- string - the string to remove spaces from
 */
function removeSpaces(string) {
	var temp = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
		temp += splitstring[i];
	return temp;
}

/*
 * The following code and two functions (progressBar and redir) are used to display the images for
 * the processing pages.
 */

var iImgMax=2;
var iCount=0;

function progressBar2()
{
	if(iCount>iImgMax)
		iCount = 0;
	if(document.images.i1.complete)
		document.images.i1.src = images[iCount++].src;

	setTimeout("progressBar2()",200);
}

function redir2(url)
{
	setTimeout("progressBar2()",200);
	window.location.replace(url);
}

function progressBar()
{
	if(iCount>iImgMax)
		iCount = 0;
	if(document.images.i1.complete)
		eval("document.images.i1.src=" + images[iCount++].src);

	setTimeout("progressBar()",200);
}

function redir(imagePath, imageToLoad, url)
{
	setTimeout("progressBar()",200);
	window.location.replace(url);
}


/**
 * This function sets the default of the radio set to whichever option is currently selected.
 * It then sets the location of the present page to process this changed default option.
 */
function saveDefault(radio, url)
{
	var selected=0;

	if (radio!=null)
	{
		if (radio.length>0)
		{
			for (var i=0;i<radio.length;i++)
			{
				if (radio[i].checked)
					selected=radio[i].value;
			}
		}
		else
		{
			selected=radio.value;
		}
	}
	location.href=url + '&id=' + selected;

}
/**
 * This function sets the action property of a form to a specific url and then submits
 * that form.
 */
function setFormAction(formToSubmit, url)
{
	formToSubmit.action=url;
	formToSubmit.submit();
}

function showWindow( m_url, winname, winwidth, winheight, wndparams)
{
	if ((winname == null) || (winname == "")) winname = "lvs";

	winleft = (screen.availWidth / 2) - (winwidth / 2);
	wintop = (screen.availHeight / 2) - (winheight / 2);

	if (wndparams == null) wndparams = "scrollbars=no,resizable=no";
	oWinInfo=(window.open(m_url, winname,"left=" + winleft + ",top=" + wintop + ",width=" + winwidth + ",height=" + winheight + "," + wndparams));
//	registerWindow(oWinInfo);
	if (!Browser.ie) {
		if(oWinInfo.self != null)  oWinInfo.focus();
	}
}

var Browser = new Object();
with (Browser) {
    Browser.b = Browser;
    b.a = navigator.userAgent.toLowerCase();
    b.v = navigator.appVersion;
    b.version = parseFloat(v);
    b.major = parseInt(v);
	b.opera = (a.indexOf('opera') != -1)?true:false;
    b.hotjava = (a.indexOf('hotjava') != -1)?true:false;
    b.webtv = (a.indexOf('webtv') != -1)?true:false;
    b.nav = (a.indexOf('mozilla') != -1 && a.indexOf('spoofer') == -1 && 
		a.indexOf('compatible') == -1 && !opera && !webtv && !hotjava)?true:false;
    b.nav4 = (nav && major == 4)?true:false;
    b.nav6 = (nav && major == 5)?true:false;
    b.nav6up = (nav && major >= 5)?true:false;
    b.aol = (a.indexOf('aol') != -1)?true:false;
	b.ie = (!opera && a.indexOf('msie') != -1)?true:false;
    b.ie4 = (ie && major == 4 && a.indexOf('msie 4') != -1)?true:false;
    b.ie4up = (ie && major >= 4)?true:false;
    b.ie5 = (ie4up && a.indexOf('msie 5.0') != -1)?true:false;
    b.ie5up = (ie4up && !ie4)?true:false;
	b.macie5=(ie4 && v.indexOf("macintosh")!=-1)?true:false;
    b.ie55 = (ie5up && a.indexOf('msie 5.5') != -1)?true:false;
    b.ie55up = (ie5up && !ie5)?true:false;
    b.ie6 = (ie4up && a.indexOf('msie 6.') != -1)?true:false;
    b.ie6up = (ie55up && !ie55)?true:false;
    b.win95 = (a.indexOf('win95') != -1 || a.indexOf('windows 95') != -1)?true:false;
    b.win98 = (a.indexOf('win98') != -1 || a.indexOf('windows 98') != -1)?true:false;
    b.mac = (a.indexOf('macintosh') != -1)?true:false;
    b.win9x = (win95 || win98)?true:false;
    b.sun = (a.indexOf('sunos') != -1)?true:false;
}


