/***********************
 Main Javascript functions
 *************************/
 
 function setRealPass()
{
	document.getElementById('mockpass').style.display='none'; 
	document.getElementById('realpass').style.display=''; 
	document.getElementById('realpass').focus();
}

function setMockPass()
{
	if(this.value=='') 
	{
		document.getElementById('mockpass').style.display=''; 
		document.getElementById('realpass').style.display='none';
	}
}

// Only allow numeric fields
function num(field)
{
    field.value=field.value.toUpperCase().replace(/([^0-9\/.\/,\/-])/g,"");
}

//  check for valid numeric strings
function isNumeric(field)
{
    var checkstr = "0123456789";
    var checkvar;
    var result = true;

    if (field.length == 0) return false;

    //  test strString consists of valid characters listed above
    for (i = 0; i < field.length && result == true; i++)
    {
        checkvar = field.charAt(i);

        if (checkstr.indexOf(checkvar) == -1)
        {
            result = false;
        }
    }

    return result;
}

function checkDate(day, month, year)
{
    var dateValue = day+month+year;
    var leap = 0;
    var result = true;
   
   
   /* Date must be 8 digits */
   if (dateValue.length != 8) {
      result = false;}
  
   /* Year is wrong if year = 0000 */
   if (year == 0) {
      result = false;
   }
   /* Validation of month*/
   if ((month < 1) || (month > 12)) {
      result = false;
   }
   /* Validation of day*/
   if (day < 1) {
     result = false;
   }
   
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      result = false;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      result = false;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      result = false;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      result = false;
   }
   
   return result;
}

function explode( delimiter, string, limit ) 
{
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function'
        || typeof delimiter == 'object'
        || typeof string == 'function'
        || typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) 
    {
        delimiter = '1';
    }
    
    if (!limit) 
    {
        return string.toString().split(delimiter.toString());
    } 
    else 
    {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}

function ri(img_name,img_src) 
{
    document.images[img_name].src=img_src;
}

function ro(img_name,img_src) 
{
    document.images[img_name].src=img_src;
}

var win=null;

function newWindow(mypage,myname,w,h,pos,infocus) 
{
    if(pos=="center") 
    {
        myleft=(screen.width)?(screen.width-w)/2:100;mytop=(screen.height)?(screen.height-h)/2:100;
    }
    else if(pos==null) 
    {
        myleft=0;mytop=20
    }
    settings="width="+ w +",height="+ h +",top=" + mytop + ",left=" + myleft + ",toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1";
    win=window.open(mypage,myname,settings);
    win.focus();
}

function popup(mypage,myname,w,h,pos,infocus) 
{
    if(pos=="center") 
    {
        myleft=(screen.width)?(screen.width-w)/2:100;mytop=(screen.height)?(screen.height-h)/2:100;
    }
    else if(pos==null) 
    {
        myleft=0;mytop=20
    }
    settings="width="+ w +",height="+ h +",top=" + mytop + ",left=" + myleft + ",toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1";
    win=window.open(mypage,myname,settings);
    win.focus();
}

function popup2(mypage,myname,w,h,pos,infocus) 
{
    if(pos=="center") 
    {
        myleft=(screen.width)?(screen.width-w)/2:100;mytop=(screen.height)?(screen.height-h)/2:100;
    }
    else if(pos==null) 
    {
        myleft=0;mytop=20
    }
    settings="width="+ w +",height="+ h +",top=" + mytop + ",left=" + myleft + ",toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1";
    win=window.open(mypage,myname,settings);
    win.focus();
}

var downStrokeField;

function autojump(formName, fieldName,nextFieldName,fakeMaxLength)
{
    var myForm=document.forms[formName];
    var myField=myForm.elements[fieldName];
    
    myField.nextField=myForm.elements[nextFieldName];

    if (myField.maxLength == null)
       myField.maxLength=fakeMaxLength;

    myField.onkeydown=autojump_keyDown;
    myField.onkeyup=autojump_keyUp;
}

function autojump_keyDown()
{
    this.beforeLength=this.value.length;
    downStrokeField=this;
}

function autojump_keyUp()
{
  if (
     (this == downStrokeField) && 
     (this.value.length > this.beforeLength) && 
     (this.value.length >= this.maxLength)
     )
     this.nextField.focus();
    downStrokeField=null;
}

function _redirect(url, msg) 
{
   var TARG_ID = "redirect";
   var DEF_MSG = "";


   if(!url) 
   {
      throw new Error('You didn\'t include the "url" parameter');
   }

   var e = document.getElementById(TARG_ID);

   if(!e) 
   {
      throw new Error('"redirect" element id not found');
   }

   var cTicks = parseInt(e.innerHTML);
   var timer = setInterval(function() 
   {
      if(cTicks) 
      {
         e.innerHTML = --cTicks;
      }
      else
      {
         clearInterval(timer);
         location = url;	  
      }
   }, 1000);
}