//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
var mmm = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

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;
}

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 daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
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
}

function isDate(dtStr){

	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)
	strYr=strYear
	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)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month " + strMonth )
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}


// Evgeni Pantchev (c) 2004


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function OnFocusTime (obj)
{
if (obj.value.length  = 5) 
	{
		obj.value =  obj.value.substring(0,2) + obj.value.substring(3)
	}

}

function ValidateTime(time) {
 	if (time.value.length == 4) 	
{
if (eval(time.value.slice(0, 2)) < 24 && eval(time.value.slice(2, 4)) < 60 )
{return true} else {time.value = '';alert("Invalid Time. Ignored!"); return false}
	}
else
{return false}
}


function OnBlurTime (obj)
{
	if (obj.value.length == 2) {obj.value = obj.value + "00"}

	if (obj.value.length  == 4 && ValidateTime(obj) )
	{
		obj.value = obj.value.substring(0, 2) + ':' +  obj.value.substring(2)
	}
	else {obj.value = '' }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


function OnFocusDate (obj)
{	
	
	if (obj.value.length  == 11) 
	{
		var mm =  obj.value.slice(3,6);
		var mm1;
		for (var i = 0; i <= 11; i++) 
			{ if (mm == mmm[i]) { mm1 = i+1}
			}
		if (mm1 < 10) { mm1 = '0' + mm1}
		obj.value =  obj.value.slice(0, 2) + mm1 + obj.value.slice(9,11)  	
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function FilterNumeric(obj) 
{
    if (window.event.keyCode < 48 || window.event.keyCode > 57)
        window.event.keyCode = 0
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function CallLookupDate(URL) 
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
if(keycode == 113){window.open(URL, '','width=200,height=210,top=250,left=250') ;}
else {CrToTab();}

} 

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function CallFLDLookup(fname, txtfld) 
{

var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
var URL;
URL='../FieldLookup.aspx?fname=' + fname + '&txtfld=' + txtfld
if(keycode == 113) {window.open(URL, '', 'width=690,height=410,top=250,left=250') ;}
else {CrToTab();}

} 

function CallFLDLookupNoCR(fname, txtfld) 
{

var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
var URL;
URL='../FieldLookup.aspx?fname=' + fname + '&txtfld=' + txtfld + '&mode=M'
if(keycode == 113) {window.open(URL, '', 'width=690,height=410,top=250,left=250') ;}
} 

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function CallLookup(URL) 
{

var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
if(keycode == 113) {window.open(URL, '', 'width=700,height=410,top=250,left=250') ;}
//else if(keycode == 118) {window.open('../system/LangTransl.aspx?NetFrtName='+URL, '', 'width=690,height=410,top=250,left=250') ;}
else {CrToTab();}

} 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function CallLookupJob(URL) 
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
if(keycode == 113){
window.open(URL, '','width=900,height=600,top=50,left=50,resizable=0') ;}
} 

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function OnBlurDate (obj)
{	

//	alert (daysInFebruary(2008));

	var curdate = new Date();
	var objmonth = curdate.getMonth() ;
	var objyear = curdate.getFullYear()	;


//~~~~~~~~~~~  dd  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	var dd	;


	if (obj.value.length  == 1 ) 
	{		
		dd = "0" + obj.value;
	}

	else
	{
		dd =  obj.value.slice(0, 2);
	}
	
	var daysInMonth = DaysArray(12);

//~~~~~~~~~~~  mmm  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	var month = objmonth

	if (obj.value.length  == 3 ) 
	{
		month = obj.value.slice(2,3);
		month = month-1
	};

	if (obj.value.length  > 3 ) 
	{
		month = obj.value.slice(2,4);
		month = month-1
	};


	var yy = new String(objyear);
	yy = yy.slice(2,4);

	if (obj.value.length  == 6 ) 
	{		
		yy =	obj.value.slice(4,6)
	};

	var yyyy ;
	if ( yy < 50 ) {yyyy = 20 + yy} else {yyyy = 19 + yy}
//	alert (yyyy +  ' days in Feb = ' +  daysInFebruary(yyyy));
	if (month==1 && dd > daysInFebruary(yyyy)) dd=0;

	if (obj.value.length  > 0  &&  month < 12 && dd > 0  && dd <= daysInMonth[month+1])
	{
		obj.value =  dd + '-' + mmm[month] + '-' + yyyy
	}
	else
	{
		if (obj.value.length > 0)
		{ alert ('Invalid Date Entered. Ignored!'); obj.value= '' }
	}


}

// -----------  callback ---------------------

   function ClientCallbackError(result, context){
      alert(result);
   }

    var popupmsg;
    
    function ClientCallback_1(result){
               
        popupmsg = '';
               
        if (result=='(^)') {return false};
        
        result = result + '****'
        var splitString = result.split("*")        
    
        document.forms[0].elements[splitString[0] + '_textBox2'].title= splitString[1]; 
        document.forms[0].elements[splitString[0] + '_textBox2'].value = splitString[2].replace('<','(');
        if (splitString[2] == '<Invalid Entry>' ) {                 
        document.forms[0].elements[splitString[0] + '_textBox2'].style.background = '#FF6633' }
        else
        {document.forms[0].elements[splitString[0] + '_textBox2' ].style.background = 'white'}
        
        if (splitString[3] == 'Y') { document.forms[0].elements[splitString[0] + '_textBox1'].value = splitString[1] }
        
               
        //specific
        
        
        if (splitString[0] == 'ep_IssueCarrier' ) { document.forms[0].elements['ep_JOB_AIR_AWB_PREFX_textBox1'].value = splitString[5] }  
        //if (splitString[0] == 'ep_IssueCarrier' && document.forms[0].elements['ep_ARLN_1_textBox1'].value == '') { document.forms[0].elements['ep_ARLN_1_textBox1'].value = splitString[1] }

        if (splitString[0] == 'ep_JOB_AIR_ARLN_1' ) { document.forms[0].elements['ep_JOB_AIR_AWB_PREFX_textBox1'].value = splitString[5] }  
        if (splitString[0] == 'ep_JOB_AIR_ARLN_2' && document.forms[0].elements['ep_JOB_AIR_AWB_PREFX_textBox1'].value == '') { document.forms[0].elements['ep_JOB_AIR_AWB_PREFX_textBox1'].value = splitString[5] }


        if (splitString[0] == 'ep_JOB_AIR_SHIP_TERMS_MAWB' ) 
           {
           if (splitString[5] == 'C') {splitString[5]='COLLECT'};
           if (splitString[5] == 'P') {splitString[5]='PRE-PAID'};
           if (splitString[6] == 'C') {splitString[6]='COLLECT'};
           if (splitString[6] == 'P') {splitString[6]='PRE-PAID'};           
           document.forms[0].elements['lblFrt'].value = splitString[5];
           document.forms[0].elements['lblOther'].value = splitString[6];
           }  

        
        //if called from air import job - will put the country code in the box ep_ovs_cntry
        if (splitString[0] == 'ep_APT_DEPT_AI') { document.forms[0].elements['ep_OVS_CNTRY_textBox1'].value = splitString[5];document.forms[0].elements['ep_OVS_CNTRY_textBox2'].value = splitString[6] }
        //if called from air export job - will put the country code in the box ep_ovs_cntry
        if (splitString[0] == 'ep_APT_ARR_AE') { document.forms[0].elements['ep_OVS_CNTRY_textBox1'].value = splitString[5];document.forms[0].elements['ep_OVS_CNTRY_textBox2'].value = splitString[6] }
                
        if (splitString[0] == 'ep_INV_CUSTOMER') { document.forms[0].elements['ep_INV_CRNCY_textBox1'].value = splitString[5] }
        if (splitString[0] == 'ep_COST_SUPPLIER') { document.forms[0].elements['ep_COST_CRNCY_textBox1'].value = splitString[5]}
        
               
        //splitString[4] - will holds the reminder message - this is popup message
        //if (splitString[4] !== '') { document.forms[0].elements[splitString[0] + '_imgLu'].title = splitString[4];};
        
        if (splitString[4] !== '') {popupmsg  = splitString[4]} ;
    }
    
    function Onblr(CtrlID){
      var txt1 = CtrlID + '_textBox1';
      var txt2 = CtrlID + '_textBox2';

        if (document.forms[0].elements[txt2].style.background == '#ff6633' ) //invalid entry
           {
           document.forms[0].elements[txt2].style.background = 'white';
           document.forms[0].elements[txt1].value = ''; 
           document.forms[0].elements[txt2].value = '';              
           }     
         else
         {
            var rez1 = document.forms[0].elements[txt1].value;
            var rez2 = document.forms[0].elements[txt2].title;
            var rez3 = document.forms[0].elements[txt2].value;
            if (rez1 !== rez2 && rez2 !== '') {document.forms[0].elements[txt1].value =  document.forms[0].elements[txt2].title ; };     
         } 

          //popup message
          //var txt3 = CtrlID + '_imgLu';
          //var txt3 = CtrlID + '_textBox2';
          //if (document.forms[0].elements[txt3].title !== '{F2}' && document.forms[0].elements[txt3].title !== '' ) {alert(document.forms[0].elements[txt3].title); document.forms[0].elements[txt3].title = '{F2}'}
         
          if (popupmsg !== undefined && popupmsg  !== '' ) {alert(popupmsg);}


   }
// -----------  end callback ---------------------

//---------------new cr to tab
function CrToTab() 
       {if(event.keyCode == 13){event.keyCode=9;}; return true;}  

// -----------  convert CRLF key to tab ----------
    function CRTAB_old_version(evt) 
    {
       var browserName = navigator.appName;
              
       if (browserName=='Microsoft Internet Explorer')
       {
           var keyCode = (evt) ? evt.keyCode : ((window.event) ? event.which : null);
           if(keyCode == 13)
           {
            evt.keyCode=9;
            return true;
           }
           else return true;
       }
       
       else 
       {
         if (browserName=='Opera')
         {
            
            var code; 
            if (!e) var e = window.event; // set var e for ie 
            if (e.keycode) code = e.keycode; // ie and mozilla/gecko 
            else if (e.which) code = e.which; // ns4 and opera 
            
            if(code == 13)  {
                              
               return false; 
            }
           
            else return true;
                      
         }
       };
     }      
     
// //-----------------------------------------------
//    var highlightRowBgColor = "yellow";
//    var SelRowColour;  
//    function highlightRow(row)  {
//       SelRowColour = document.getElementById("<%=SelBgColor.ClientID %>");
//       SelRowColour.value = row.style.backgroundColor;
//       row.style.backgroundColor = highlightRowBgColor;
//    }
//    function unhighlightRow(row) {
//        row.style.backgroundColor = SelRowColour.value
//    }

