// global variables //
var TIMER = 5;
var SPEED = 10;
var WRAPPER = 'content';

// calculate the current window width //
function pageWidth() {
  return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}
// calculate the current window height //
function pageHeight() {
  return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}
// calculate the current window vertical offset //
function topPosition() {
  return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}
// calculate the position starting at the left of the window //
function leftPosition() {
  return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}
// build/show the dialog box, populate the data and call the fadeDialog function //
function showDialog(title,message,type,autohide) {
  if(!type) {
    type = 'error';
  }
  errormessage= '<br><table align="center" border="0" cellpadding="5" cellspacing="0"><tr><td align="center" class="largered">Please complete all required fields correctly.</td></tr></table>'

  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  if(!document.getElementById('dialog')) {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogtitlefooter = document.createElement('div');
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);    
    dialogheader.appendChild(dialogtitle);
	dialogheader.appendChild(dialogclose);  
    dialog.appendChild(dialogcontent);
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;
  } else {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
    dialogmask = document.getElementById('dialog-mask');
    dialogmask.style.visibility = "visible";
    dialog.style.visibility = "visible";
  }
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;
  var width = pageWidth();
  var height = pageHeight();
  var left = leftPosition();
  var top = topPosition();
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
  var topposition = top + (height / 3) - (dialogheight / 2);
  var leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
 
  if(type== 'error') {
  	dialogcontent.innerHTML = errormessage;
  }
  if(type== 'prompt') {
 	dialogcontent.innerHTML = message;
  
  	dialogcontent.innerHTML = message + '<table align="center" border="0" cellpadding="5" cellspacing="0"><tr><td align="center"><input type="image" src="dialogboximages/yes.jpeg" onclick="javascript: AllDone();"></td><td align="center"><input type="image" src="dialogboximages/no.jpeg" onclick="hideDialog();"></td></tr></table>';
  }
  
  var content = document.getElementById(WRAPPER);
  dialogmask.style.height = content.offsetHeight + 'px';
  dialog.timer = setInterval("fadeDialog(1)", TIMER);
  if(autohide) {
    dialogclose.style.visibility = "hidden";
    window.setTimeout("hideDialog()", (autohide * 1000));
  } else {
    dialogclose.style.visibility = "visible";
  }
}
function AllDone()
{
  document.login.submit();
}
function hideDialog() {
  var dialog = document.getElementById('dialog');
  clearInterval(dialog.timer);
  dialog.timer = setInterval("fadeDialog(0)", TIMER);
}
function fadeDialog(flag) {
  if(flag == null) {
    flag = 1;
  }
  var dialog = document.getElementById('dialog');
  var value;
  if(flag == 1) {
    value = dialog.alpha + SPEED;
  } else {
    value = dialog.alpha - SPEED;
  }
  dialog.alpha = value;
  dialog.style.opacity = (value / 100);
  dialog.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(dialog.timer);
    dialog.timer = null;
  } else if(value <= 1) {
    dialog.style.visibility = "hidden";
    document.getElementById('dialog-mask').style.visibility = "hidden";
    clearInterval(dialog.timer);
  }
}

//Form Validation Options Via Regular Expressions
//urlvalid: to check for valid urls
//password: must contain at least 1 number and 1 letter and from 6 to 15 characters long
//alpha
//alphaspace
//alphanum
//alphanumspace
//alphanumchar
//ipmatch
//decimal
//unsigned
//integer
//real
//email
//date
//phone

var TV0=/^(\d{1,2})\-(\d{1,2})\-(\d{4})$/;
var TV1=/^(\d{1,2})\:(\d{1,2})\:(\d{1,2})$/;
var TV2={'urlvalid':/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/,'password':/(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,15})$/,'alpha':/^[a-zA-Z\.\-]*$/,'alphaspace':/^[a-zA-Z\s\.\-]*$/,'alphanumchar':/^[0-9a-zA-Z\s\.\:\;\,\!\(\)\?\+\=\-]*$/,'alphanumspace':/^[0-9a-zA-Z\s\.\-]*$/,'alphanum':/^\w+$/,'ipmatch':/\b(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\b/,'decimal':/^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$/,'unsigned':/^\d+$/,'integer':/^[\+\-]?\d*$/,'real':/^[\+\-]?\d*\.?\d*$/,'email':/^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/,'phone':/^[\d\.\s\-]+$/,'date':function(TV3){if(!TV0.test(TV3))return false;if(RegExp.$1>31||RegExp.$2>12)return false;var TV4=new Date(RegExp.$3,Number(RegExp.$2-1),RegExp.$1);if(TV4.getMonth()!=Number(RegExp.$2-1))return false;return true;},'time':function(TV6){if(!TV1.test(TV6))return false;if(RegExp.$1>23||RegExp.$2>59||RegExp.$3>59)return false;return true;}};

function TV7(){this.alert=0;this.alert_class=[];this.to_disable=[];this.messages={'setup':['No form name passed to validator construction routine','No array of "%form%" form fields passed to validator construction routine','Form "%form%" can not be found in this document','Can not find area for error message box (id="error_%form%")','Incomplete "%n%" form field descriptor entry. Attribute "%attr%" is missing','Can not find form field "%n%" in the form "%form%"','Can not find label tag (id="%t%")','Can not verify match. Field "%m%" was not found'],'fill':['"%l%" is a required field','"%v%" is not valid value for "%l%".<br>Certain characters like apostrophes or quotations are not allowed.','Value for "%l%" must be %mn% characters or more','Value for "%l%" must be no longer than %mx% characters','"%l%" must match "%ml%"'],'boxes':['<table cellpadding="0" cellspacing="0" border="0" width="100%">'+'<tr><td bgcolor="#CCCC33"><table cellpadding="15" cellspacing="1" border="0" width="100%">'+'<tr><td bgcolor="#FFFFCC" style="color: red;">%error%</td></tr>'+'</table></td></tr><tr><td height="10">&nbsp;</td></tr></table>','<table cellpadding="0" cellspacing="0" border="0" width="100%">'+'<tr><td bgcolor="#FFFFFF"><table cellpadding="15" cellspacing="1" border="0" width="100%">'+'<tr><td align="center" bgcolor="#FFFFFF" style="color: blue; font-weight: bold;">Submitting...</td></tr>'+'</table></td></tr><tr><td height="10">&nbsp;</td></tr></table>'],'confirm':['The form is to be submitted. Are you sure?']}}

function validator(TV8,TV9,TVA){
this.TVB=TVC;
var TVD=new TV7();
TVE(TVA,TVD);
this.TVA=TVD;
this.TVF=TVD.alert;
this.TVG=this.TVF&4?function(TVH){showDialog('Error',TVH,'error',3);return false}:function(){return false};this.TVI=TVD.alert_class;if(!TV8)return this.TVG(this.TVB('setup',0));this.TV8=TV8;if(!TV9||typeof(TV9)!='object')return this.TVG(this.TVB('setup',1));this.TV9=TV9;this.TVJ=TVD.to_disable;this.exec=TVK;}

function TVK(){var TVL=document.forms[this.TV8];if(!TVL)return this.TVG(this.TVB('setup',2));var TVM=TVN('error_'+this.TV8),TVO=document.body&&document.body.innerHTML;if((this.TVF&2)||(this.TVF&8)){if(TVO&&!TVM)return this.TVG(this.TVB('setup',3));if(TVO){TVM.style.display='none';TVM.innerHTML='';}}var TVP=['l'];for(var TVQ in this.TV9){this.TV9[TVQ]['n']=TVQ;for(TVR in TVP)if(!this.TV9[TVQ][TVP[TVR]])return this.TVG(this.TVB('setup',4,this.TV9[TVQ],{'attr':TVP[TVR]}));TVS=TVL.elements[TVQ];if(!TVS)return this.TVG(this.TVB('setup',5,this.TV9[TVQ]));this.TV9[TVQ].TVS=TVS;}if(TVO)for(var TVQ in this.TV9)if(this.TV9[TVQ]['t']){var TVT=this.TV9[TVQ]['t'],TVU=TVN(TVT);if(!TVU)return this.TVG(this.TVB('setup',6,this.TV9[TVQ]));this.TV9[TVQ].TVV=TVU;TVU.className=this.TVI[1];}for(var TVQ in this.TV9){TVS=this.TV9[TVQ].TVS;this.TV9[TVQ]['v']=null;if(TVS.type=='checkbox'){if(TVS.checked&&TVS.value)this.TV9[TVQ]['v']=TVS.value;}else if(TVS.value)this.TV9[TVQ]['v']=TVS.value;else if(TVS.options&&TVS.selectedIndex>-1)this.TV9[TVQ]['v']=TVS.options[TVS.selectedIndex].value;else if(TVS.length>0)for(var TVW=0;TVW<TVS.length;TVW++)if(TVS[TVW].checked){this.TV9[TVQ]['v']=TVS[TVW].value;break;}}var TVX=0,TVY,TVZ,TV_,TVa;for(var TVQ in this.TV9){TVb=this.TV9[TVQ];TV_=TVb['f']?(TV2[TVb['f']]?TV2[TVb['f']]:TVb['f']):null;TVa=(TV_+'').indexOf('function')>-1;TVb.TVc=null;if(TVb['r']&&!TVb['v']){TVb.TVc=1;TVX++;}else if(TVb['mn']&&String(TVb['v']).length<TVb['mn']){TVb.TVc=3;TVX++;}else if(TVb['mx']&&String(TVb['v']).length>TVb['mx']){TVb.TVc=4;TVX++;}else if(TV_&&((TVa&&(TVY=TV_(TVb['v'],TVL))!=true)||(!TVa&&TVb['v']&&!TV_.test(TVb['v'])))){TVb.TVc=typeof(TVY)!='string'?2:TVY;TVX++;}else if(TVb['m']){for(var TVd in this.TV9)if(TVd==TVb['m']){TVZ=TVd;break;}if(TVZ==null)return this.TVG(this.TVB('setup',7,TVb));if(this.TV9[TVZ]['v']!=TVb['v']){TVb['ml']=this.TV9[TVZ]['l'];TVb.TVc=5;TVX++;}}}var TVe='',TVf='',TVg;if(TVX){for(var TVQ in this.TV9){var TVh=this.TV9[TVQ].TVc,TVi='';if(TVh){TVi=this.TVB('fill',TVh-1,this.TV9[TVQ]);if(!TVi)TVi=TVh;}if(TVi){if(!TVg)TVg=TVL.elements[TVQ];TVe+=TVi+'<br>';TVf+=TVi+"\n";if(TVO&&this.TVI&&this.TV9[TVQ].TVV)this.TV9[TVQ].TVV.className=this.TVI[0];}}TVe=this.TVB('boxes',0,{'error':TVe});if(typeof(this.TVF)!='function'){if(this.TVF&1)showDialog('Error',TVf,'error',3);if(TVO&&(this.TVF&2)){TVM.innerHTML=TVe;TVM.style.display='block';}if(TVg.focus&&TVg.type!='hidden')TVg.focus();return false;}}if(typeof(this.TVF)=='function')return this.TVF(TVf,TVe,TVM);else{if((this.TVF&16)&&!showDialog('Confirmation',this.TVB('confirm',0),'prompt'))return false;if(TVO&&(this.TVF&8)){TVM.innerHTML=this.TVB('boxes',1);TVM.style.display='block';}for(TVQ in this.TVJ){var TVj=TVN(this.TVJ[TVQ]);if(TVj&&TVj.disabled!=null)TVj.disabled=true;}}return true;}function TVE(TVk,TVl){for(var TVm in TVk)if(TVk[TVm]!=null){if(typeof(TVk[TVm])=='object'){if(typeof(TVl[TVm])!='object')alert("Type mismatch ("+TVm+"). Array in place of scalar.");TVE(TVk[TVm],TVl[TVm]);}else{if(typeof(TVl[TVm])=='object')alert("Type mismatch ("+TVm+"). Scalar in place of array.");TVl[TVm]=TVk[TVm];}}}function TVC(TVn,TVW){var TVm=this.TVA.messages[TVn][TVW],TVo=2,TVp;if(!TVm)return false;if(typeof(TVm)=='function')TVm=TVm(this.TV8);for(;TVo<arguments.length;TVo++)for(TVp in arguments[TVo])TVm=TVm.replace('%'+TVp+'%',arguments[TVo][TVp]);TVm=TVm.replace('%form%',this.TV8);return TVm}function TVN(TVq){return(document.all?document.all[TVq]:(document.getElementById?document.getElementById(TVq):null));}