var ns4 = navigator.appName.indexOf("Netscape") != -1 && navigator.appVersion.charAt(0) >= 3
var ie4 = navigator.appName.indexOf("Microsoft Internet Explorer") != -1 && navigator.appVersion.charAt(0) >= 4

function fnSuppressEnterKey() {

	//if (ie4) alert ("ie"); else alert ("not ie");
	/*
	if (ie4) {
		if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {return false;}} else {return true}; 
	} else {
		if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {return false;}} else {return true}; 
		var eventChooser = (isNetscape) ? keyStroke.which : event.keyCode;
	}
	*/
	
if(event.which || event.keyCode || window.event.keyCode){if ((event.which == 13) || (event.keyCode == 13) || (window.event.keyCode == 13)) {return false;}} else {return true}; 	
	
	
}



function fnValidateInput(aform, ctrlName, mandatory, regexStr, mandatoryMsg, errMsg) {
	var result = true;
	
  for(var i=0; i < aform.elements.length; i++){
    if (aform.elements[i].type == "text") {			
			if (aform.elements[i].name.indexOf(ctrlName) > 0) {
				
				//Mandatory checking
				if (mandatory) {
					if (fnTrim(aform.elements[i].value) == '') {
						alert (mandatoryMsg);
						return (false);
					}
				}
				
				//Regex checking
				if (fnTrim(aform.elements[i].value) != '') {
					var re = new RegExp(regexStr, "g");
					if (!re.test(aform.elements[i].value)) {
						alert (errMsg);
						return (false);
					}					
				}
				
			}  
    }
  }
  return (result);	
}


function fnTrim(strInput) {
	return strInput.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");	
}

function fnADVdt(aform) {
	if (fnSameValue(aform, "cboFrom", "cboTo")) {
		alert ("Origin and Destination cannot be the same!");
		return (false);
	}	
}

function fnFltPlanVdt(aform, astrFrDate, astrToDate, astrDayCID, astrMthYrCID, astrDisplayFrDate) {
	var strTemp="";

	if (fnSameValue(aform, "cboOrigin", "cboDest")) {
		alert ("Origin and Destination cannot be the same!");
		return (false);
	}	
	
	if (!fnValidInputDate(aform, "cboStartDay", "cboStartMonth")) {
		alert ("Please input a valid date.");
		return (false);		
	}
	
	strTemp = eval(aform.name + "." + astrMthYrCID + ".value") + "-" + eval(aform.name + "." + astrDayCID + ".value");
	if (strTemp < astrFrDate || strTemp> astrToDate) {
		alert ("Invalid Arrival or Departure date!\nDate should fall between " + astrDisplayFrDate + " and one year ahead");
		return (false);			
	}
	
	return (true);	
}

function fnFltSchVdt(aform, astrFrDate, astrToDate, astrDayCID, astrMthYrCID, astrDisplayFrDate) {
	var strTemp="";

	if (fnSameValue(aform, "cboOrigin", "cboDest")) {
		alert ("Origin and Destination cannot be the same!");
		return (false);
	}	
	
	if (!fnValidInputDate(aform, "cboStartDay", "cboStartMonth")) {
		alert ("Please input a valid date.");
		return (false);		
	}
	
	strTemp = eval(aform.name + "." + astrMthYrCID + ".value") + "-" + eval(aform.name + "." + astrDayCID + ".value");
	if (strTemp < astrFrDate || strTemp> astrToDate) {
		alert ("Invalid Arrival or Departure date!\nDate should fall between " + astrDisplayFrDate + " and one year ahead");
		return (false);			
	}
	
	return (true);	
}

function fnSameValue(aform, aCtrlVal1, aCtrlVal2) {
	var pVal1 = "";
	var pVal2 = "";
	for (var i=0; i < aform.elements.length; i++) {	
	  if (aform.elements[i].type == "select-one") {			
			if (aform.elements[i].name.indexOf(aCtrlVal1) > 0) pVal1 = aform.elements[i].value;
			if (aform.elements[i].name.indexOf(aCtrlVal2) > 0) pVal2 = aform.elements[i].value;
		}
	}
	if (pVal1 == pVal2) return (true); else return (false);
}


function fnValidInputDate(aform, aCtrlDay, aCtrlMthYr) {
	var pDay = "";
	var pMthYr = "";
	for (var i=0; i < aform.elements.length; i++) {
	  if (aform.elements[i].type == "select-one") {			
			if (aform.elements[i].name.indexOf(aCtrlDay) > 0) pDay = aform.elements[i].value;
			if (aform.elements[i].name.indexOf(aCtrlMthYr) > 0) pMthYr = aform.elements[i].value;
		}
	}
	if (fnValidDate(pMthYr + '-' + pDay, '-')) return (true); else return (false);
}


function fnValidDate (myDate,sep) {
// checks if date passed is in valid yyyy/mm/dd format
    if (myDate.length == 10) {
        if (myDate.substring(4,5) == sep && myDate.substring(7,8) == sep) {
            var date  = myDate.substring(8,10);
            var month = myDate.substring(5,7);
            var year  = myDate.substring(0,4);

            var test = new Date(year,month-1,date);

            if (year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) {
                reason = '';
                return true;
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }
    }
    else {
        return false;
    }
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }


function fnShow(NAME){
	if (ns4) {
		document.layers[NAME].visibility = "show";
	}
	if (ie4) {
		document.getElementById("mnuFrame").style.top = document.getElementById(NAME).style.top;
		document.getElementById("mnuFrame").style.height = document.getElementById(NAME).style.height;
		document.getElementById("mnuFrame").style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
		document.getElementById("mnuFrame").style.display = "inline";
		document.all[NAME].style.visibility = "visible";	
		
		var pintPopUpMenuLeft = fnFindPos(document.getElementById("leftnav")) + 155;		
		document.getElementById("mnuFrame").style.left = pintPopUpMenuLeft;
		document.all[NAME].style.left = pintPopUpMenuLeft;	
		
	}
}

function fnHide(NAME){
	if (ns4) {
		document.layers[NAME].visibility = "hide";
	}
	if (ie4) {
		document.getElementById('mnuFrame').style.display = "none";
		document.all[NAME].style.visibility = "hidden";
	}
}

function fnFindPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	//return [curleft,curtop];
	return curleft;
}



function fnTrackVdt(aform) {	
	var pstrErr = "";
	var pstrAWBPrefix = "";
	var pstrAWBSuffix = "";
	var pintAWBPrefixLen;	
	var pintAWBSuffixLen;	
	var pintTotalAWBSuffixLen = 0;	
	
  for(var i=0; i < aform.elements.length; i++){
    if (aform.elements[i].type == "text") {			    
    	if (aform.elements[i].name.indexOf("AWBPrefix") > 0)  {
    		pstrAWBPrefix = fnTrim(aform.elements[i].value);
    		pintAWBPrefixLen = pstrAWBPrefix.length;
    		if (pintAWBPrefixLen > 0) {
	    		if (!fnIsNum(pstrAWBPrefix)) {
	    			pstrErr = "errAlp";
	    			break;
	    		}
    			if (pintAWBPrefixLen != 3) {
	    			pstrErr = "errLen";
	    			break;
	    		}    		
	    	}
	    }

    	if (aform.elements[i].name.indexOf("AWBSuffix") > 0)  {
    		pstrAWBSuffix = fnTrim(aform.elements[i].value);
    		pintAWBSuffixLen = pstrAWBSuffix.length;
    		pintTotalAWBSuffixLen += pintAWBSuffixLen;
    		if (pintAWBSuffixLen > 0) {
	    		if (!fnIsNum(pstrAWBSuffix)) {
	    			pstrErr = "errAlp";
	    			break;
	    		}	    			    		
    			if (pintAWBSuffixLen != 8) {
	    			pstrErr = "errLen";
	    			break;
	    		}	    		
	    	}	    	
	    }
	    
	    if (pintAWBSuffixLen > 0 && pintAWBPrefixLen <= 0) {
  			pstrErr = "errLen";
  			break;
  		}
	  }
	}
	
	if (pintTotalAWBSuffixLen == 0) pstrErr = "errEmpty";
	
	switch (pstrErr) {
		case "errLen":
			alert ("Invalid AWB number!\nA valid AWB number consists of a 3 digits airline code number and a 8 digits reference number.");
			break;
		case "errAlp":
			alert ("Invalid AWB number!\nNo alphabet allowed in a valid AWB number.");
			break;	
		case "errEmpty":
			alert ("Please input at least one AWB number.");
			break;				
	}
	if (pstrErr == "") return (true); else return (false);
}


function fnIsNum(astrInput) {
	var re = new RegExp("^[0-9]*$", "g");
	if (!re.test(astrInput)) return (false); else return (true);
}


function fnAllotListVdt(aform, aCtrlOrig, aCtrlDest) {
	/* check on submit for search allotment list */
	var err = "";
	var pOrig = "";
	var pDest = "";
	for (var i=0; i < aform.elements.length; i++) {
	  if (aform.elements[i].type == "text") {			
			if (aform.elements[i].name.indexOf(aCtrlOrig) > 0) pOrig = aform.elements[i].value;
			if (aform.elements[i].name.indexOf(aCtrlDest) > 0) pDest = aform.elements[i].value;
		}
	}
	err = fnValidAllotAirports(pOrig, pDest);
	if (err.length == 0) {
		return(true);
	} else {
		alert(err);
		return(false);		
	}
}

function fnAllotStatusVdt(aform, aCtrlAllotID, aCtrlDay, aCtrlMthYr) {
	/* check on submit for display allotment status */
	var err = "";
	var pAllotID = "";
	var pDay = "";
	var pMthYr = "";
	for (var i=0; i < aform.elements.length; i++) {
		if (aform.elements[i].type == "text") {			
			if (aform.elements[i].name.indexOf(aCtrlAllotID) > 0) pAllotID = aform.elements[i].value;
		}
		if (aform.elements[i].type == "select-one") {			
			if (aform.elements[i].name.indexOf(aCtrlDay) > 0) {
				pDay = aform.elements[i].value;
				if (pDay.length == 1) pDay = "0" + pDay;
			}
			if (aform.elements[i].name.indexOf(aCtrlMthYr) > 0) pMthYr = aform.elements[i].value;
		}
	}
	if (fnValidDate(pMthYr.substring(0,4) + "/" + pMthYr.substring(4,6) + "/" + pDay, "/")) {
		err = fnValidAllotID(pAllotID);
		if (err.length == 0) {
			return(true);
		} else {
			alert(err);
			return(false);		
		}
	} else {
		alert("The Date is not valid.");
		return(false);				
	}
}

function fnAllotViewVdt(aform, aCtrlDay, aCtrlMthYr) {
	/* check on submit for view allotment status */
	var pDay = "";
	var pMthYr = "";
	if (document.getElementById)  {			// Netscape 6.0+ and Internet Explorer 5.0+
		pDay = document.getElementById(aCtrlDay).value; 
		if (pDay.length == 1) pDay = "0" + pDay;
		pMthYr = document.getElementById(aCtrlMthYr).value;
		if (fnValidDate(pMthYr.substring(0,4) + "/" + pMthYr.substring(4,6) + "/" + pDay, "/")) {
			return(true);
		} else {
			alert("The Date is not valid.");
			return(false);				
		}
	}
}

function fnValidAllotID(aAllotID) {
	/* check allotment ID */
	var err = "";
	if (aAllotID.length == 0) {
		err = "Allotment ID must be entered.";
	} else if (!fnIsAlphanumeric(aAllotID)) {
		err = "The Allotment ID is not valid.";
	}
	return err;
}

function fnValidAllotAirports(aOrig, aDest) {
	/* check origin & destination airports */	
	aOrig = aOrig.toUpperCase();
	aDest = aDest.toUpperCase();
	var err = "";
	if (aOrig != "") {
		if (aOrig.length > 3 || !fnIsLetter(aOrig)) {
			err = err + "The Origin is not valid.\n";
		}
	}
	if (aDest != "") {
		if (aDest.length > 3 || !fnIsLetter(aDest)) {
			err = err + "The Destination is not valid.\n";
		}
	}
	if (aOrig.length > 0 && aOrig != "ALL") {
		if (aOrig == aDest) {
			err = err + "Origin and Destination must not be the same.\n";
		}
	}
	return err;
}

function fnIsLetter(aInput){
	/* validate the letter input */
	var alphaExp = /^[a-zA-Z]+$/;
	if (aInput.match(alphaExp)) {
		return true;
	} else {
		return false;
	}
}

function fnIsAlphanumeric(aInput){
	/* validate the alphanumeric input */
	var alphanumExp = /^[a-zA-Z0-9]+$/;
	if (aInput.match(alphanumExp)) {
		return true;
	} else {
		return false;
	}
}

//-----------------------------------------------------------
// Text Box Styling
//
function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+, NS6 and Mozilla
// By Scott Andrew
{
	if (elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} 
	else if (elm.attachEvent){
		var r = elm.attachEvent("on"+evType, fn);
		return r;
	} 
	else {
		alert("Handler could not be removed");
	}
} 

function prepareTextBoxes(){
if (!document.getElementsByTagName) return;
	var oi=0;
	var thisObj;
	var objs = document.getElementsByTagName("input");
	for (oi=0;oi<objs.length;oi++) {
		thisObj = objs[oi];
		if(thisObj.getAttribute('type') == 'text'){
			thisObj.className = 'text ' + thisObj.className;
		}
		else if(thisObj.getAttribute('type') == 'password'){
			thisObj.className = 'text ' + thisObj.className;
		}
	}
}
addEvent(window, "load", prepareTextBoxes);
//
//-----------------------------------------------------------

// ---------------------------------------------------------------
// Booking Landing Page
function fnCheckAWB ( AWBprefix , AWBsuffix){
	//var f = this.form;
	if (AWBprefix.length < 1 || AWBsuffix.length < 1) {
		alert("Air Waybill must be entered");
		return false;
	} else if (AWBprefix.length != 3 || AWBsuffix.length != 8) {
		alert("Air Waybill is not valid");
		return false;
	} else if (!j_isInt(AWBprefix) || !j_isInt(AWBsuffix)) {
		alert("Air Waybill is not valid");
		return false;
	} else {
		//f.submit();			
		return true;
	}		
}
function j_isInt(num) {
	for (var i = 0; i < num.length; i++) {
     		if ((num.charAt(i) < '0') || (num.charAt( i ) > '9')) {
			return false;
		}
	}
       return true;
}	   	
//
// ---------------------------------------------------------------

// ---------------------------------------------------------------
// Preliminary Claim Page
function fnCheckAWBSuffix (AWBsuffix){
	if (AWBsuffix.length < 1) {
		//alert("Airway Bill Number must be entered.");
		//return false;
	} else if (AWBsuffix.length != 8) {
		//alert("The Airway Bill Number is not valid.");
		//return false;
	} else if (!j_isInt(AWBsuffix)) {
		//alert("The Airway Bill Number is not valid.");
		//return false;
	} else if (!fnCheckValidAWBSuffix(AWBsuffix.substring(0,AWBsuffix.length-1), AWBsuffix.charAt(AWBsuffix.length-1))) {
		alert("The Air Waybill Number is not valid.");
		return false;		
	} else {		
		return true;
	}	
	return true;	
}

function fnCheckValidAWBSuffix(num1, num2) {
	if (Mod(num1,7) == num2) {
			return true;
	}
       return false;
}	  

function Mod(X, Y) { 
	return X - Math.floor(X/Y)*Y 
} 	

// Set the maxlength for textarea
function fnTextLimit(field, maxlimit) {
	if (field.value.length > maxlimit) // if the charceters is too long, then trim it.
		field.value = field.value.substring(0, maxlimit);
}
//
// ---------------------------------------------------------------

function PopupWindow(url)
{
	window.open(url,'_blank','location=no,scrollbars=yes,resize=no,status=no');
}

function checkACT(divACTId,SHC1,SHC2)
{
	var divACT=document.getElementById(divACTId);
	if (divACT)
	{
		var SHC1Box=document.getElementById(SHC1);
		var SHC2Box=document.getElementById(SHC2);
		if (SHC1Box.value.toUpperCase()=='ACT' || SHC2Box.value.toUpperCase()=='ACT')
		{
			divACT.style.visibility='visible';
		}
		else
		{
			divACT.style.visibility='hidden';
		}
	}
}
