/***************************************************************************
*<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
*
* See also utils.js
* @author $Author: a_kryvasheyeu $
*
* @version $Revision: 1.35 $ $Date: 2005/04/08 15:54:19 $
*
* $Log: common.js,v $
* Revision 1.35  2005/04/08 15:54:19  a_kryvasheyeu
* 1. possibility to load images and JS from static site
* 2. possibility to manage discount programs in XML file
*
* Revision 1.33  2004/08/23 11:43:25  a_kryvasheyeu
* added support for dot symbol in forms - request from AeroMexico
*
* Revision 1.32  2004/08/10 18:00:50  pj_mckenna
* No longer converts Spanish accented vowels to Anglo.
*
* Revision 1.31  2004/05/06 14:19:45  m_veller
* no message
*
* Revision 1.30  2004/05/05 14:59:35  m_veller
* no message
*
* Revision 1.29  2004/05/05 13:13:38  m_veller
* no message
*
* Revision 1.28  2004/04/28 15:13:11  m_veller
* no message
*
* Revision 1.27  2004/04/22 10:53:42  d_razorionov
* no message
*
* Revision 1.26  2004/04/12 16:14:55  d_razorionov
* changed params of CreditCard(item, type) to ...(value,type)
*
* Revision 1.25  2004/03/31 13:22:59  m_veller
* no message
*
* Revision 1.24  2004/03/24 11:03:19  y_chyhir
* Changed js function checkEmail - allowed entering spanish symbols in email. Fixed bug #2033.
*
* Revision 1.23  2004/03/23 17:27:37  a_rubinau
* no message
*
* Revision 1.22  2004/03/23 13:49:43  a_rubinau
* no message
*
* Revision 1.21  2004/03/23 13:23:52  a_rubinau
* no message
*
* Revision 1.20  2004/03/18 13:27:15  m_veller
* no message
*
* Revision 1.19  2004/03/13 08:30:08  m_veller
* no message
*
* Revision 1.18  2004/03/09 11:18:52  m_veller
* no message
*
* Revision 1.17  2004/02/24 12:28:35  m_veller
* no message
*
* Revision 1.16  2004/02/23 13:10:34  a_rubinau
* changed
* function isAlpha(str)
* function openWindow
* function checkNameLength
*
* Revision 1.15  2004/02/11 11:08:05  a_rubinau
* fixed bug 1528  in function checkEmail
*
* Revision 1.14  2004/02/10 19:27:45  a_rubinau
* fixed bugs
*
* Revision 1.11  2004/02/10 17:15:25  a_rubinau
* fixed function checkField()
*
* Revision 1.10  2004/02/10 11:05:54  a_rubinau
* update "openWindow" function
*
* Revision 1.9  2004/02/09 16:59:08  m_veller
* window open for AOL 6.0
*
* Revision 1.8  2004/02/05 16:55:20  d_razorionov
* no message
*
* Revision 1.7  2004/02/05 13:02:09  m_veller
* Credit Card Checking
*
* Revision 1.6  2004/02/04 13:58:50  m_veller
* credit card checking
*
* Revision 1.5  2004/02/03 12:10:23  m_veller
* function isAlpha
*
* Revision 1.4  2004/01/29 15:37:54  m_veller
* no message
*
* Revision 1.3  2004/01/04 09:50:26  d_razorionov
* I've changed logic in checkEmail(...) function. now RegExp is used  to check variable for errors.
*
* Revision 1.2  2003/09/01 12:36:46  m_zenkevich
* change getYear to getFullYear (NN bug)
*
* Revision 1.1.1.1  2003/07/22 12:47:29  m_looby
* no message
*
* Revision 1.14.3.1 2002/03/13 12:29:39GMT emcneil 
* Duplicate revision
* Revision 1.13 2002/02/04 16:11:51GMT 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>
***************************************************************************/

/**
 * This function sets the location of the present window to the specified url.
 */
function changeLocation(url)
{
	location.href=url;
}
/**
 * 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)
{
    var spSymbs = "\u00c1\u00e1\u00c9\u00e9\u00da\u00fa\u00d3\u00f3\u00cd\u00ed\u00d1\u00f1";
    var re = new RegExp("(^[A-Za-z0-9_"+spSymbs+"])([A-Za-z0-9_.-"+spSymbs+"]*)@((([A-Za-z0-9_-"+spSymbs+"]{2,})\\.){1,3})(([A-Za-z"+spSymbs+"]{2,4})$)");
	if (!re.test(FieldValue))
	{
		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);
		field.focus();
		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);
	}
	
	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" || isearchType == "MLFS") 
	{
	    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, 6, 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, middleName, surname, suffix, errorMessage)
{
	return true;	
  var nameLength = salutation.length + firstName.length + /*middleName*/ +surname.length + suffix.length;
 
    if(nameLength > 70)
    {
    	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.getFullYear() || (iYear==today.getFullYear() && iMonth<today.getMonth()) || (iYear==today.getFullYear() && 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,scrollbars,toolbar) {
	 var  sLeft = 0;
	 var sTop = 0;
	scrollbars = (scrollbars == 0)? "no": "yes"; 
	toolbar = (toolbar == 1)? "yes": "no"; 
	
	if(!(aol5 || aol6)){
	sLeft=(window.screen.availWidth/2)-(sWidth/2)
	if(window.screen.availHeight-32 < sHeight){
		sTop=0;
		sWidth+=17;
		scrollbars="yes";
		sHeight=window.screen.availHeight-32;
		}	
	else{
		sTop=(window.screen.availHeight/2)-(sHeight/2+16)
		}	
	}	
	
	var hWnd = window.open(href, "", 'toolbar='+toolbar+',location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=no,scrollbars='+scrollbars+',width='+sWidth+',height='+sHeight+',top='+sTop+',left='+sLeft);
	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=5;
var iImg=0;
var iCount=0;
var iCountMax=256;

var imgSpace=new Image();
var imgOn=new Image();

function progressBar()
{

	iCount++;
	var img;
	if(iCount==iCountMax)
	{
		return;
	}
	if(iCount==1 && !document.images.i6.complete)
	{
		document.images.i6.src="imagenes/processing.gif";
	}

	if(iImg < iImgMax)
	{
		iTemp = iImg+1;
		sTemp = iTemp.toString();
		
		eval("img=document.images.i"+sTemp);
	}
	else
	{
		for(i=1;i<=iImgMax;i++)
		{
			eval("document.images.i"+i+".src=imgSpace.src");
		}
		iImg=0;
		setTimeout("progressBar()",1000);
	}
	if(img!=null)
	{
		img.src=imgOn.src;
		iImg++;
		setTimeout("progressBar()",1000);
	}
}

function redir(imagePath, imageToLoad, url)
{
	imgOn.src=imagePath + imageToLoad;
	imgSpace.src=imagePath + "/space.gif";

	setTimeout("progressBar()",1000);	
	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();
}

/**
 * This function checks that the field contains only letters and ' '
 */
function isAlpha(str){
	var re = /[~`!@#$%^&*\(\)_=+\\|,<>\/?;:'\"{}\-\]\[\d]/;
	return !re.test(str);
}

function openCalendar(form,day,month,year){
	if(!opera5 && !ie5mac){
		var popup="width=230,height=170,status=no,scrollbars=no,toolbar=no,menubar=no,location=no,resizable=no";
 		if(oc!=null && !oc.closed) oc.close();
 		var prepath= isStaticSite ? rootPath+languagePath+"html/" : currentSite+"/"+languagePath+"html/";
		var oc=window.open(prepath+'calendar.html?form='+form+'&day='+day+'&month='+month+'&year='+year+'&nocache='+amxVersion,'viewCalendar',popup);
		oc.focus();
	}
}

function escapeEx(str){
	var ret='';
	for(i=0; i<str.length; i++){
		var code=str.charCodeAt(i);
		var symbol=str.substring(i,i+1);
		//alert(code);
		switch(code){

			case 191: // Upsidedown '?'
				ret+='';
				break;
			case 161: // Upsidedown '!'
				ret+='';
				break;

			default:
				ret+=symbol;
				break;	
		}
	}

return ret;
}

/***************************************************************************/
/*				Credit Card 											   */
/***************************************************************************/


function CheckCardNumber(val,type){
	if(val=="")
		return false;
 	switch(type){
	   case "AX" : 
	   		  var first = val.substring(0,2);
              if((!((first=="34")||(first=="37")))||(val.length!=15)) 
			  	return false;
              break;
	   case "CA" :
	   		  var first = parseInt(val.substring(0,2),10);
              if((first>55)||(first<51)||(val.length!=16))
			  	return false;
              break;
  		case "VI" : 
			  var first = val.substring(0,1);
              if((!((val.length==13)||(val.length==16)))||(first!="4"))
			  	return false;
              break;
  case "DC" : 
  case "CB" : var first=val.substring(0,2);
              var third = parseInt(val.substring(2,3),10);
              if((!(((first=="30")&&(third<6))||(first=="36")||(first=="38")))||(val.length!=14))
			  	return false;
              break;
  case "DS": var first = val.substring(0,4);
              if((val.length!=16)||(first!="6011"))
  			  	return false;
              break;
  default: 
 }
 return LuhnCheck(val);
}
 
function LuhnCheck(str){
	if(str=="")return false;
	var sum=0;
	var mul=1; 
	var strLen = str.length;
 	for (i = 0; i < strLen; i++){
	  var digit = str.substring(strLen-i-1,strLen-i);
	  var tproduct = parseInt(digit,10)*mul;
	  if (tproduct >= 10)
    	sum += (tproduct % 10) + 1;
	  else
    	sum += tproduct;
	  if(mul==1)
    	 mul++;
	   else
    	 mul--;
	 }
	if((sum%10)!=0)
	   return false;
	return true;   	
}

 
var clientTimeout = 0;
var warnWnd = null;
var IdTimer;
var siteId;
var languageId;
function startWatch(vSiteId,vLanguageId) {
	siteId = vSiteId ? vSiteId : 'mex';
	languageId = vLanguageId ? vLanguageId : 'spanish';
	IdTimer = setInterval('watchDog()', 60000);
}

function watchDog() {
	clientTimeout++;
	if(clientTimeout == sessionLimit) {
			clearInterval(IdTimer);
			warnWnd = window.open(warningPageUrl+"?site="+siteId+"&lang="+languageId, "session", "width=300,height=250,menubar=no,toolbar=no,scrollbars=no,resizable=no,statusbar=no");
			//warnWnd.focus();				
			location = "sessionExpired_AM.jsp?site="+siteId+"&lang="+languageId;//homePage;
	}
}

