var win;
var msie3=false;
browserVer=parseInt( navigator.appVersion );
if( browserVer == 2 && navigator.appName == "Microsoft Internet Explorer" ) { browserVer++; msie3=true; }

function winclose()
{
  if( browserVer <= 2 ) { return true; }
  if( msie3 != true && typeof( win ) == 'object' && !win.closed ) { win.close(); } return true;
}
function dhl_alert( name, text, height )
{
  if( typeof( height) == "undefined" ) { height = 200; }
  winclose();
  win=open( "", "dhl_alert", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=300,height=" + height);
  if( msie3 == true ) while( typeof( win.document ) == "undefined" );
  win.document.write( "<HTML><HEAD><TITLE>"+ name +"</TITLE></HEAD>\n<BODY BGCOLOR=\"#FFFFFF\" MARGINWIDTH=0 MARGINHEIGHT=0>\n" );
  win.document.write( "<TABLE CELLPADDING=6 WIDTH=\"100\%\" HEIGHT=\"100\%\" BORDER=0><TR><TD BGCOLOR=\"#951314\"><FONT FACE=\"arial,helvetica\" COLOR=\"#FFFFFF\" SIZE=\"+1\"><B>" + name +"</B></FONT></TD></TR>" );
  win.document.write( "<TR><TD><FONT FACE=\"arial,helvetica\" SIZE=2>"+ text +"</FONT></TD></TR>" );
  win.document.write( "<TR><TD ALIGN=CENTER><FORM onSubmit=\"self.close();return false\"><INPUT TYPE=SUBMIT VALUE=\"&nbsp;&nbsp;OK&nbsp;&nbsp;\"></FORM></TD></TR></TABLE>" );
  win.document.write( "</BODY></HTML>\n" );
  win.document.close();
}
function strip( instring )
{
  var outstring="";
  var bit="";
  var founddigit=false;

  for( j=0;j<instring.length;j++ )
  {
    c=instring.charAt(j);
    if( c != " " && c !="\t" && c !="\n"  && c !="\r" )
    {
      if( founddigit == true )
        outstring+=bit;
      bit="";
      outstring+=c;
      founddigit=true;
    }
    else if( founddigit == true )
      bit+=c;
  }
  return outstring;
}
function checkForm(form)
{
  if( !form.docheck.checked || browserVer <= 2 )
  {
    remember();
    return true;
  }
  awb=strip( form.AWB.value );

  var awbs=new Array;
  var tmp="";
  var curr=0;
  for(i=0;i<awb.length;i++)
  {
    c=awb.charAt( i );
    d=parseInt( c );
    if( ( msie3==true && !( d==0 && c != "0" ) ) || ( msie3==false && !isNaN( d ) ) )
    {
      tmp+=c;
    }
    else
    {
      if( tmp != "" )
      {
        awbs[curr]=tmp;
        curr++;
        tmp="";
      }
    }
  }
  if( tmp != "" )
  {
  awbs[curr]=tmp;
  }

  num=awbs.length;

  if( num == 0 )
  {
     dhl_alert( "No Airwaybills", "You have not entered any Airwaybill numbers.<P>Please enter up to 10 Airwaybill numbers.", 180 );
     return false;
  }

  errors="";
  off=0;
  for( i=0 ; i< num ; i++ )
  {
    awbs[i]=strip( awbs[i] );
    if( awbs[i].length == 3 && awbs[i+1] && awbs[i+1].length == 4 && awbs[i+2] && awbs[i+2].length == 3 )
    {
      if( errors == "" )
        errors=""+(i+1);
      else
        errors+=(  ", " + (i+1-off) );
      off+=2;
    }
  }
  if( errors != "" )
  {
    if( msie3 == false )
      numbad=errors.split( ", " );
    else
      numbad=new Array(2);
    if( numbad.length == 1 )
      dhl_alert( "Invalid Airwaybill number", "Airwaybill number " + numbad[0] + " has been split into 3 groups of digits.<BR>Airwaybill numbers should be an unbroken string of 10 digits.<P>Please correct or remove this Airwaybill number", 230 );
    else
      dhl_alert( "Invalid Airwaybill numbers", "Airwaybill numbers " + errors + " have all been split into 3 groups of digits.<BR>Airwaybill numbers should be an unbroken string of 10 digits.<P>Please correct or remove these Airwaybill numbers", 230 );
    return false;
  }

  if( 10 < num )
  {
     dhl_alert( "Too many Airwaybills", "Please enter 10 or fewer airwaybill numbers. Each airwaybill number should be 10 digits long and each one should be on a separate line." );
     return false;
  }

  errors="";
  for( i=0 ; i< num ; i++ )
  {
    awbs[i]=strip( awbs[i] );
    if( awbs[i].length != 10 && !isNaN( awbs[i] ) )
    {
      if( errors == "" )
        errors=""+(i+1);
      else
        errors+=(  ", " + (i+1) );
    }
  }
  if( errors != "" )
  {
    if( msie3 == false )
      numbad=errors.split( ", " );
    else
      numbad=new Array(2);
    if( numbad.length == 1 )
      dhl_alert( "Invalid Airwaybill number", "Airwaybill number " + numbad[0] + " is not a 10 digit number.<P>Please correct or remove this Airwaybill number" );
    else
      dhl_alert( "Invalid Airwaybill numbers", "Airwaybill numbers " + errors + " are not 10 digit numbers.<P>Please correct or remove these Airwaybill numbers" );
    return false;
  }

  errors="";
  for( i=0 ; i< num ; i++ )
  {
    awbs[i]=strip( awbs[i] );
    check1=awbs[i].substring(9,10);
    tocheck=awbs[i].substring(0,9);
    check2=tocheck % 7;
    if( check1 != check2  && awbs[i] != "" )
    {
      if( errors == "" )
        errors=""+(i+1);
      else
        errors+=(  ", " + (i+1) );
    }
  }
  if( errors != "" )
  {
    if( msie3 == false )
      numbad=errors.split( ", " );
    else
      numbad=new Array(2);
    if( numbad.length == 1 )
      dhl_alert( "Invalid Airwaybill number", "The Airwaybill number on row " + numbad[0] + " is not a valid airwaybill number.<P>Please correct or remove this Airwaybill number" );
    else
      dhl_alert( "Invalid Airwaybill numbers", "The Airwaybill numbers on rows " + errors + " are not valid airwaybill numbers.<P>Please correct or remove these Airwaybill numbers" );
    return false;
  }

  remember(form);
  return true;
}
function remember(form)
{
  dd=new Date();
  ms=dd.getTime()+45*86400000;
  dd.setTime(ms);
  days=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
  mons=new Array("Jan","Tue","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  if( typeof( dd.getFullYear ) != "undefined" ) year=dd.getFullYear();
  else { year=dd.getYear(); if( year < 1900 ) year+=1900; }
  year+="";
  expire=days[dd.getDay()]+", "+zpad(dd.getDate())+"-"+mons[dd.getMonth()]+"-"+year.substring(2,year.length)+" 00:00:00 GMT";
  setaCookie( "AWBS", form.AWB.value, expire );
}
function zpad(num)
{
  return ((num<10)?"0"+num:num);
}
function getaCookie(Name)
{
  var search = Name + "=";
  if (0 < document.cookie.length)
  {
    offset = document.cookie.indexOf(search) 
    if (offset != -1)
    {
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) 
        end = document.cookie.length;
      return unescape(document.cookie.substring(offset, end))
    } 
  }
  return "";
}
function setaCookie(name, value, expire)
{
  document.cookie = name + "=" + escape(value)
    + ((expire == null) ? "" : ("; expires=" + expire))
}
function initForm(form)
{
  if(typeof(form)=="undefined"||typeof(form.elements)=="undefined") form=document.forms[0];
  if( document.location.search == "" )
  {
    form.AWB.value=getaCookie("AWBS");
  }
  else
  {
    s=document.location.search.indexOf("awb=");
    e=document.location.search.indexOf("&",s);
    if( e == -1 ) { e=document.location.search.length; }
    form.AWB.value=document.location.search.substring(s+4,e);
  }
  if( typeof( setForm ) != "undefined" )
  {
    setForm(form,'','','','FIRST_DB');
  }
}
