// common.js

	// This is hiding the link url from status bar 
	   function hidestatus()
	   {
	      window.status='';
	      return true
	   }
   
	   if (document.layers)
	   {
	      document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT)
	   }
       document.onmouseover=hidestatus
       document.onmouseout=hidestatus
	//-------------------------------------------------------------------------------
	//function to disable right click
	   function clickIE4()
	   {
	   		if (event.button==2)
	   		{
	   			return false;
	   		}
	   }
	   
	   function clickNS4(e)
	   {
	   		if (document.layers||document.getElementById&&!document.all)
	   		{
	   			if (e.which==2||e.which==3)
	   			{
	   				return false;
	   			}
	   		}
	   	}
		if (document.layers)
		{
			document.captureEvents(Event.MOUSEDOWN);
			document.onmousedown=clickNS4;
		}else if (document.all&&!document.getElementById)
		{
			document.onmousedown=clickIE4;
		}
		document.oncontextmenu=new Function("return false")
	 //-------------------------------------------------------------------------
	  document.onkeydown = mykeyhandler; //to disable backspace button
      
      function mykeyhandler() 
      {                      
          try
          {     //we can backspace in a textbox 
            if(window.event.srcElement.readOnly == false)
            {
               if((window.event.srcElement.type.match("text")) || (window.event.srcElement.type.match("password")))                
               {
                  return true;
               }  
            }
          }catch(e){}
          if(window.event && window.event.keyCode == 8) // try to cancel the backspace
          {
             window.event.cancelBubble = true;
             window.event.returnValue = false;
             return false;
          }          
      }
    //-------------------------------------------------------------------------
	//added for populating err messages in a common place
	var msgArray;
	function errMsgStructure(errCode,errMsg)
	{
		this.errCode = errCode;
		this.errMsg = errMsg;
	}
	//----------------------------------------------------------------------
	function loadErrMessages()
	{
		msgArray = new Array(
					new errMsgStructure("J0001", "Please key in %1."),
					new errMsgStructure("J0002", "%1 entered is invalid. Please key in again."),
					new errMsgStructure("J0003", "Please select %1."),
					new errMsgStructure("J0004", "%1 entered is not the same as %2. Please key in again."),
					new errMsgStructure("J0005", "%1 entered is greater than %2. Please key in again."),
					new errMsgStructure("J0006", "Invalid User Id / Password. Please key in again."),
					new errMsgStructure("J0007", "Changes Saved."),
					new errMsgStructure("J0008", "Updation failed."),
					new errMsgStructure("J0009", "%1 %2 already exist."),
					new errMsgStructure("J0010", "Your password has been sent to your email account."),
					new errMsgStructure("J0011", "The email Id %1 is not registered with rewards system."),
					new errMsgStructure("J0012", "Email sent to the administrator."),
					new errMsgStructure("J0013", "Not able to %1. Please try again later."),
					new errMsgStructure("J0014", "Thank you for applying to join MILLENNIUM rewards the 'bookers' loyalty programme brought to you by Millennium & Copthorne Hotels.  Your application is being processed and we will be in touch shortly."),
					new errMsgStructure("J0015", "%1 %2 already exists. Please key in a unique value."),
					new errMsgStructure("J0016", "Do you want to generate excel report?"),
					new errMsgStructure("","")					
		);
	}	
//----------------------------------------------------------------------
//returns error message for the given error code
	function getJSErrMessage(errCode,param1,param2,param3)
	{
		loadErrMessages();
		for(i=0; i<msgArray.length; i++)
		{
			errMsgObj = msgArray[i];
			msgInCode = errMsgObj.errCode;
			errorMsg = errMsgObj.errMsg;
			var msgBfr = "";
			var msgAft = "";
			
			if(msgInCode == errCode)
			{
				if(errorMsg.length>0)
				{
					if (errorMsg.indexOf("%1")>=0)
					{
						msgBfr = errorMsg.substring(0,errorMsg.indexOf("%1"));  
						msgAft = errorMsg.substring(errorMsg.indexOf("%1")+2,errorMsg.length);
						errorMsg = msgBfr + param1 + msgAft;
					}  
					if (errorMsg.indexOf("%2")>0)
					{                      
						msgBfr = errorMsg.substring(0,errorMsg.indexOf("%2"));   
						msgAft = errorMsg.substring(errorMsg.indexOf("%2")+2,errorMsg.length);
		                if (param2 == "")
		                {
		                  msgBfr = msgBfr.substring(0,(msgBfr.length-1));
		               	}
						errorMsg = msgBfr + param2 + msgAft;
					}
					if (errorMsg.indexOf("%3")>0)
					{ 
						msgBfr = errorMsg.substring(0,errorMsg.indexOf("%3"));  
						msgAft = errorMsg.substring(errorMsg.indexOf("%3")+2,errorMsg.length);
						errorMsg = msgBfr + param3 + msgAft;      
					} 
			    } 
				return errorMsg;
			}//end of error code check
		}  //end of for loop
	}	
//----------------------------------------------------------------------
// this is to check all the entered value is zero or not
   function checkZero(str)
   {
      var digitsCheck ="123456789";
      if (trim(str).length == 0)
      {
         return false;
      }
      if (isNaN(str))
      {
         return false;
      }
      for (i=0; i < str.length; i++)
      {
         if (digitsCheck.indexOf(str.substring(i,i+1)) >= 0)
         {
            return false;               
         }
      } 
      return true; //all are zero
   }
//--------------------------------------------------------------------------------   
//this is to trim the string
   function trim(str)
   {
      if(str.length ==0) return "";
      while(str.substr(0,1)==" ")
      {
         if(str.length ==1) return "";
         else str=str.substr(1,str.length);
      }
      while(str.substr(str.length-1,str.length)==" ")
      {
         if(str.length ==1) return "";
         else str=str.substr(0,str.length-1);
      }
      return str;
   }
//--------------------------------------------------------------------------------
	//this will remove Unnecessary blank space
   function trimExtraSpace(str)
   {
      if(str.length ==0) return "";
      var returnVal = "";
      var strCheck = "";
      var check = true;
      str = trim(str);
      var arr = str.split(' ');
      for (i=0; i<arr.length;i++)
      {
        returnVal += trim(arr[i]);   
        if (i < arr.length)
        {
            if (returnVal.substr(returnVal.length-1,returnVal.length) != " ")
            {
               returnVal += " ";
            }
        }
      }
      return returnVal;
   } 
//---------------------------------------------------------------------------------
//this is to check the date is valid or not
   function isDate(dtStr)
   {     
      var dtCh= "/";
      var daysInMonth = DaysArray(12);
      var pos1 = dtStr.indexOf(dtCh);
      var pos2 = dtStr.indexOf(dtCh,pos1+1);
      var strDay = dtStr.substring(0,pos1);
      var strMonth = dtStr.substring(pos1+1,pos2);
      var strYear = dtStr.substring(pos2+1);
      var strYr = strYear;
      var month = "";
      var day = "";
      var year = "";
      if (strDay.charAt(0)=="0" && strDay.length>1) 
      {
      	strDay = strDay.substring(1);
      }
      if (strMonth.charAt(0)=="0" && strMonth.length>1) 
      {
      	strMonth = strMonth.substring(1);
      }
      for (var i = 1; i <= 3; i++) 
      {
         if (strYr.charAt(0)=="0" && strYr.length>1) 
         {
         	strYr=strYr.substring(1);
         }
      }      
      if (pos1==-1 || pos2==-1)
      {         
         return false; //invalid date
      }
      month = parseInt(strMonth);
	  day = parseInt(strDay);
	  year = parseInt(strYr);
      if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
      {         
         return false; //invalid date
      }
      if (strMonth.length<1 || month<1 || month>12)
      {         
         return false; //invalid date
      }
      if (strYear.length != 4 || year==0 || year<1900 || year>2100)
      {         
         return false; //invalid date
      }
      if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
      {         
         return false; //invalid date
      }
   	  return true; //valid date
   }
   //-----------------------------------------------------------------------------
   //if the given date is future date then returns true else returns false
   function checkForFutureDate(str)
   {
      var strDate = "";
      var strMonth = "";
      
      curDate = new Date();
      givDate = new Date();
      var strDateArray = str.split('/');
      
      strDate = strDateArray[0];
      strMonth = strDateArray[1]; 
      
      
      if(strDate.indexOf("0") == 0) 
      { 
        strDate = strDate.substring(strDate.indexOf("0")+1);
      } 
      
      if(strMonth.indexOf("0") == 0) 
      { 
        strMonth = strMonth.substring(strMonth.indexOf("0")+1);
      }
      
      givDate.setYear(strDateArray[2]);
      givDate.setMonth(parseInt(strMonth)-1);
      givDate.setDate(strDate);
      if(givDate>curDate)
      {
         return true;
      }else
      {
         return false;
      }
   }
   //-----------------------------------------------------------------------------
   function DaysArray(n) 
   {
      for (var i = 1; i <= n; i++) 
      {
         this[i] = 31
         if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
         if (i==2) {this[i] = 29}
      }
      return this
   }
   //-----------------------------------------------------------------------------
   // February has 29 days in any year evenly divisible by four,
   // EXCEPT for centurial years which are not also divisible by 400.
   function daysInFebruary (year)
   {      
       return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
   }
   //-----------------------------------------------------------------------------
   function stripCharsInBag(s, bag)
   {
      var i;
       var returnString = "";
       // Search through string's characters one by one.
       // If character is not in bag, append to returnString.
       for (i = 0; i < s.length; i++)
       {
           var c = s.charAt(i);
           if (bag.indexOf(c) == -1) returnString += c;
       }
       return returnString;
   }
   //-----------------------------------------------------------------------------
   function isInteger(s)
   {
      var i;
       for (i = 0; i < s.length; i++)
       {
           // Check that current character is number.
           var c = s.charAt(i);
           if (((c < "0") || (c > "9"))) return false;
       }
       // All characters are numbers.
       return true;
   }
   //-----------------------------------------------------------------------------
   //returns true if date1 is less than date2 else returns false
   function datecompare(str1, str2)
   {
      var aa = str1;
      var bb = str2;
      var strDate = "";
      var strDate2 = "";
      var strMonth = "";
      var strMonth2 = "";
      var computeDate1 = 0;
      var computeDate2 = 0;      
      var computeMonth1 = 0;
      var computeMonth2 = 0;      
      var computeYear1 = 0;
      var computeYear2 = 0;      
      var dte1 = new Date();
      var arr1 = str1.split('/');
      
      strDate = arr1[0];
      strMonth = arr1[1]; 
      
      if(strDate.indexOf("0") == 0) 
      { 
        strDate = strDate.substring(strDate.indexOf("0")+1);
      } 
     
      if(strMonth.indexOf("0") == 0) 
      { 
        strMonth = strMonth.substring(strMonth.indexOf("0")+1);
      }
      
      dte1.setYear(arr1[2]);
   
      compute1 = parseInt(strMonth);
      compute1 = compute1 -1;
      
      computeDate1 = parseInt(strDate);
      computeMonth1 = parseInt(strMonth);
      computeYear1 = parseInt(arr1[2]);
      
      dte1.setMonth(compute1);
      dte1.setDate(strDate);
     
      var dte2 = new Date();
      var arr2 = str2.split('/');
      
      strDate2 = arr2[0];
      strMonth2 = arr2[1]; 
      
      if(strDate2.indexOf("0") == 0) 
      { 
        strDate2 = strDate2.substring(strDate2.indexOf("0")+1);
      } 
      
      if(strMonth2.indexOf("0") == 0) 
      { 
        strMonth2 = strMonth2.substring(strMonth2.indexOf("0")+1);
      }
      dte2.setYear(arr2[2]);
      dte2.setMonth(parseInt(strMonth2)-1);
      dte2.setDate(strDate2);
    
      computeDate2 = parseInt(strDate2);
      computeMonth2 = parseInt(strMonth2);
      computeYear2 = parseInt(arr2[2]);
      //check
      if(computeYear1 > computeYear2) 
      { 
        return false;
      }
      
      if(computeYear1 < computeYear2) 
      { 
        return true;
      }
      if(computeYear1 == computeYear2) 
      { 
        if(computeMonth1 < computeMonth2) 
        { 
          return true;
        }
        if(computeMonth1 > computeMonth2) 
        { 
          return false;
        }        
        if(computeMonth1 == computeMonth2) 
        { 
          if(computeDate1 <= computeDate2) 
          {
            return true;
          }
          if(computeDate1 > computeDate2) 
          {
            return false;
          }
        }        
      }
   }
   //--------------------------------------------------------------------------
   //this is to insert date mask dd/mm/yyyy
   function insertDateMask(obj,eve)
   {
      var dateCheck = "0123456789";      
      var result = "";
      var part1 = "";  
      var part2 = "";
      var count = 0;
      //8-Back space,9-Tab,35-End,36-Home,37-left arrow,39-right arrow
      if ((eve.keyCode == 8) || (eve.keyCode == 9) || (eve.keyCode == 35) || (eve.keyCode == 36) || (eve.keyCode == 37) || (eve.keyCode == 39))
      {
         return;
      }

      if (obj.value.length > 0)
      {
         if(!dateMask(obj))
            return;
         for (i=0; i < obj.value.length;i++)
         {
            if (dateCheck.indexOf(obj.value.substring(i,i+1)) >= 0)
            {
               result += obj.value.substring(i,i+1);
            }
            if (obj.value.substring(i,i+1) == "/")
            {
               count += 1;
            }
            if (((count == 1) && (i < 2)) || ((count == 2) && (i <5)) || 
               ((count == 2) && (obj.value.length < 10)))
            {      
               return;
            }
            if ((count ==1) && (obj.value.length > 5) && (obj.value.length < 10))
            {   
               return;
            }
         }
      }else
      {
         result = obj.value;
      }
      if (result.length > 8)
      {
         result = result.substring(0,8);
      }
      if (result.length >= 2)
      { 
         part1 = result.substring(0,2);
         part2 = result.substring(2,result.length);               
         result = part1 +"/"+part2;
      }
      if (result.length >= 5)
      {  
         part1 = result.substring(0,5);
         part2 = result.substring(5,result.length);    
         result = part1 +"/"+part2;
      }
      if(navigator.appName == "Netscape") 
      {
         obj.value = "";
      }      
      obj.value = result; 
   }
   //-----------------------------------------------------------------------------
   // misc input mask for date field
   function dateMask(obj)
   {
      var oVal = obj.value;
      var oLen = oVal.length;
      // remove entered space
      if(oVal.substring(oLen-1, oLen)==' ')
      {
         obj.value = oVal.substring(0,oLen-1);
         return false;
      }
      // remove 2nd immediate '/'
      if(oVal.substring(oLen-1, oLen)=="/")
      {
         if (oLen==1)
         {
            obj.value = '';
            return false;
         }else if (obj.value.substring(oLen-2,oLen-1) == "/")
         {
            obj.value = obj.value.substring(0, oLen-1);
            return false;
         }
      }
      // if user enter 1 digit followed by '/', insert '0' ahead
      if(obj.value.length==2)
      {
         if(obj.value.substring(1,2)=='/')
         {
            obj.value = '0'+obj.value;
         }
      }else if(obj.value.length==5)
      {
         if(obj.value.substring(4,5)=='/')
         {
            obj.value = obj.value.substring(0,3) +'0'+obj.value.substring(3);
         }
      }
      return true;
   }
   //--------------------------------------------------------------------------------
   function insertNumberMask(obj, eve)
   {
		//8-Back space,9-Tab,35-End,36-Home,37-left arrow,39-right arrow
		if ((eve.keyCode == 8) || (eve.keyCode == 9) || (eve.keyCode == 35) || (eve.keyCode == 36) || (eve.keyCode == 37) || (eve.keyCode == 39))
		{
			return;
		}
		var dateCheck = "0123456789";
		var result = '';
		var resultDec = '';
		var pos = '';
		for (i=0; i < obj.value.length;i++)
		{
			if (dateCheck.indexOf(obj.value.substring(i,i+1)) >= 0)
			{
			   result += obj.value.substring(i,i+1);
			}
		}
			    
		obj.value = result;
   }
   //------------------------------------------------------------------------------
   function insertDecimalMask(obj, eve)
   {
		//8-Back space,9-Tab,35-End,36-Home,37-left arrow,39-right arrow
		if ((eve.keyCode == 8) || (eve.keyCode == 9) || (eve.keyCode == 35) || (eve.keyCode == 36) || (eve.keyCode == 37) || (eve.keyCode == 39))
		{
			return;
		}
		var dateCheck = "0123456789.";
		var result = '';
		var resultDec = '';
		var pos = '';
		for (i=0; i < obj.value.length;i++)
		{
			if (dateCheck.indexOf(obj.value.substring(i,i+1)) >= 0)
			{
			   result += obj.value.substring(i,i+1);
			}
		}
		//this to restrict 2 decimal places
		if (result.indexOf('.') >= 0)
		{
			resultDec = obj.value.substring(result.indexOf('.')+1,result.length);
			result = obj.value.substring(0,result.indexOf('.')+1)
			if (resultDec.length > 2)
			{
				resultDec = resultDec.substring(0,2);
			}
			result += resultDec;
		}	    
		obj.value = result;
   }
   //--------------------------------------------------------------------------------
   //this is used to round the number eg: roundTo(5,2) ==> 5.00
   function roundTo(num,pow)
   {
	  num *= Math.pow(10,pow);
	  num = (Math.round(num)/Math.pow(10,pow))+ "" ;
	  if(num.indexOf(".") == -1)
	    num += "." ;
	  while(num.length - num.indexOf(".") - 1 < pow)
	    num += "0" ;
	  return num ;
   }
   //--------------------------------------------------------------------------------
   function goLite(FRM,BTN)
   {
	   window.document.forms[FRM].elements[BTN].style.color = "#a30010";
	   window.document.forms[FRM].elements[BTN].style.backgroundColor = "#FFEEDD";
	   window.document.forms[FRM].elements[BTN].style.borderColor = "#a30010";
	}
	
	function goDim(FRM,BTN)
	{
	   window.document.forms[FRM].elements[BTN].style.color = "#330000";
	   window.document.forms[FRM].elements[BTN].style.backgroundColor = "#DDDDDD";
	   window.document.forms[FRM].elements[BTN].style.borderColor = "#330000";
	}