var cOver="#FFFFFF";
var cbOver="#999999";
var cNormal="#000000";
var cbNormal="#FFF8DC";

var cSearch="";
var cTimer;

var Validate=true;
var Valid=true;
var ValidMsg="";

var panelActive="";


function wordCount(t, wcid, ccid) {

  var wc=document.getElementById(wcid);
  if (wc==undefined) return;
  var cc=document.getElementById(ccid);
  if (cc==undefined) return;

  var l=t.length;
  var w=0;
  var v = t.split(/\s+/g);
  
  for (c=0; c<v.length; c++)
    if (v[c].length>1) w++;

  wc.value=w;
  cc.value=l;
}

function panelActivate(fid, pid) {
  panelActive=pid;
  var v=document.getElementById(fid+"#PANELS");
  var p=document.getElementById(pid);
  
  if (v==undefined) return;
  
  if (v.value!="")
  {
    var hid=v.value
    var h=document.getElementById(hid);
    if (h!=undefined)
    {    
      h.style.display="none";
      v.value=""
    } 
    if (p!=undefined) 
    {
      p.style.display="block"
      v.value=pid;
    }  
  }   
}

function panelSwitch(fid, pid, cid) {  
  
  var v=document.getElementById(fid+"#PANELS");
  if (v==undefined) return;
  
  if (pid=="") pid=panelActive;
  if (pid=="") pid=v.value;
  if (pid=="") 
  {    
    var c=document.getElementById(fid+"#"+cid);
    if (c!=undefined) pid="DETAILS"+c.value;
  }
  if (pid!="") panelActive=pid;
  
  var p=document.getElementById(pid);

  if (p==undefined) return;

  if (v.value==pid) 
  {
    p.style.display="none";
    v.value="";
  }
  else
  {
    p.style.display="block"
    v.value=pid;
  }    
}

function inputKeyDown(fid) {

}

function inputKeyFormat(fid) {
  
  var e=event.srcElement;
  if (e.phn!=undefined) formatphone(e);
  if (e.dat!=undefined) formatdate(e);
  if (e.num!=undefined) formatnumber(e);
  if (e.ssn!=undefined) formatssn(e);
  if (e.zip!=undefined) formatzip(e);  
}

function formatphone(v){var ValidChar = /[0123456789]/;var LastCode=event.keyCode; var LastChar=String.fromCharCode(LastCode);var val=v.value;if (LastChar.search(ValidChar) != 0 || val.length>13) window.event.keyCode=null;else { if(val.length==3) v.value="("+v.value+") ";if(val.length==9) v.value=v.value+"-";}} 
function formatssn(v){var ValidChar = /[0123456789]/;var LastCode=window.event.keyCode; var LastChar=String.fromCharCode(LastCode);var val=v.value;if (LastChar.search(ValidChar) != 0 || val.length>10) window.event.keyCode=null;else {if(val.length==3 || val.length==6) v.value=v.value+"-";}}
function formatdate(v){var ValidChar = /[0123456789]/;var ValidMonth = /[23456789]/;var LastCode=window.event.keyCode;var LastChar=String.fromCharCode(LastCode);var val=v.value;if (LastChar.search(ValidChar) != 0 || val.length>9){if (LastChar=="/"){ if (val.length==1) v.value="0"+val+"/";if (val.length==2 || val.length==5) v.value=val+"/";if (val.length==4) v.value=val.substr(0,2)+"/0"+val.substr(3,1)+"/";};window.event.keyCode=null;} else {if(val.length==1 && val.search(ValidMonth)==0) v.value="0"+v.value+"/"; if(val.length==2 || val.length==5) v.value=v.value+"/";}}
function formatnumber(v){var ValidChar = /[0123456789]/;var LastCode=window.event.keyCode; var LastChar=String.fromCharCode(LastCode);var val=v.value;if (LastChar.search(ValidChar) != 0) window.event.keyCode=null;}  
function formatzip(v){}

function formatyear() {
  var v=event.srcElement;
  var m=v.value.split("/");
  if (m[0]!=undefined && m[1]!=undefined && m[2]!=undefined) v.value=fs(m[0])+"/"+fs(m[1])+"/"+fy(m[2]);
}
function fs(s){var o= new String(s);if (o.length<2) o="0"+o;return o;}
function fy(y){if (y.length==1) y="200"+y;if (y.length==3) if (y.substr(0,1)=="0") y="2"+y; else y="1"+y;if (y.length==2){var x=parseInt(y);if (x>=0 && x<20) y="20"+y; else y="19"+y;};return y;}


function formValid(fid, mid) {
  
  if (!Validate)
  {
    Validate=true;
    return true;
  }
  
  var F=document.getElementById(fid); 
  if (F==undefined) return;
  
  var M=document.getElementById(mid);
  ValidClear(F,M);

  $(F).find(":input").each(function() {
    if (IsVisible(this) == 1) {
      if ($(this).attr("req") != undefined) ValidReq(this, M);
      if ($(this).attr("num") != undefined) ValidNum(this, M);
      if ($(this).attr("max") != undefined) ValidMax(this, M);
      if ($(this).attr("min") != undefined) ValidMin(this, M);
      if ($(this).attr("dat") != undefined) ValidDat(this, M);
      if ($(this).attr("eml") != undefined) ValidEmail(this, M);
      if ($(this).attr("ssn") != undefined) ValidSSN(this, M);
      if ($(this).attr("phn") != undefined) ValidPhone(this, M);
      if ($(this).attr("zip") != undefined) ValidZip(this, M);
      if ($(this).attr("ccn") != undefined) ValidCCN(this, M);

    }
  });

  if (ValidMsg!="") if (M!=null) M.innerText=ValidMsg; else alert(ValidMsg);

  if (Valid)
    $("button").attr("disabled", "disabled");
    
  return Valid;  
}

function IsVisible(e) {
  if (e.style.visibility=="hidden" || e.style.display=="none") return 0;
  return 1;  
}

function ValidClear(F,M)
{
  Valid=true;
  ValidMsg="";
  if (M!=null) M.className="msgI";

  $(".labelError").removeClass("labelError");
  
}

function InValid(I, M)
{
  Valid=false;
  var lab=document.getElementById(I.id.replace(/_TEXT/,"")+"_LABEL"); 
  if (lab!=null) lab.className="labelError";
  
  if (ValidMsg=="")
  { 
    if (M!=undefined) M.className="msgE";
    if (I.texterr!=undefined) ValidMsg=I.texterr; else if (lab!=null) ValidMsg="Invalid "+lab.innerText; else ValidMsg=I.id;   
    I.focus();   
  }   
}
 
function ValidReq(I, M)
{
  var v=document.getElementById(I.id.replace(/_TEXT/,""));
  var T = v.value.replace(/\s+/g, "");
  if (T.length==0 || (T=="0" && v.type=="hidden")) InValid(I,M);
}

function ValidNum(I, M)
{
  var p = I.value.search("[^0-9]"); 
  if (I.value.length > 0 && p >= 0) InValid(I,M);
}
function ValidMin(I, M)
{
  var m=0;
  m=I.min;
  var T = I.value

  if (I.num!=undefined) 
    {if (T<m) InValid(I,M);}
  else
	  {if (T.length<m) InValid(I,M);}
}

function ValidMax(I, M)
{
  var m=0;
  m=I.max;
  var T = I.value

  if (I.num!=undefined)
    if (T>m) InValid(I,M);
  else
	  if (T.length>m) InValid(I,M);
}
  
function ValidDat(I, M)
{
  var StartDate = 1900;
  var EndDate = 2100;
  var m=I.value.split("/");
 
  if (I.value=="") return; 
  if (!Date.parse(I.value) && I.value!="") {InValid(I,M); return;}
  if (m[0] < 1 || m[0] > 12) {InValid(I,M); return;}
  if (m[1] < 1 || m[1] > 31) {InValid(I,M); return;}    
  if (m[2].length<2 || m[2].length==3) {InValid(I,M); return;}    
  if (m[2].length==2) if (m[2] < 20) m[2]="20"+m[2]; else m[2]="19"+m[2];
  if (m[2] < StartDate || m[2] > EndDate) {InValid(I,M); return;}
    
  I.value=m[0]+"/"+m[1]+"/"+m[2];
}
  
function ValidEmail(I, M)
{
  if (!/^[A-Za-z0-9\'\.\_\-]+\@[A-Za-z0-9\.\_\-]+(\.[A-Za-z]+)+$/.test(I.value) && I.value!="") InValid(I,M)
}
  
function ValidSSN(I, M)
{
  if (!/^(\d{3}-)?(\d{2})-(\d{4})$/.test(I.value) && I.value!="") InValid(I, M)
} 
  
function ValidPhone(I, M)
{
  if (!/^(\()(\d{3})(\))(\s)(\d{3})-(\d{4})$/.test(I.value) && I.value!="") InValid(I, M)
}

function ValidZip(I, M)
{
  if (I.ZIP=="CANADA")
    { if (!/^(\S{1})(\d{1})(\S{1})(\s)(\d{1})(\S{1})(\d{1})$/.test(I.value) && I.value!="") InValid(I, M);}
  else
    { if (!/^(\d{5})+(-)*(\d{4})*$/.test(I.value) && I.value!="") InValid(I, M);}
}

function ValidCCN(i, M) {
  if (i.value.length==0) return;
  
  var v=new String(i.value);
  var sum = 0; 
  var mul = 1; 
  var vl = v.length;
  
  for (z = 0; z<vl; z++) {
    var digit = v.substring(vl-z-1,vl-z);
    var tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10) sum += (tproduct % 10) + 1; else sum += tproduct;
    if (mul == 1) mul++; else mul--;
  }
  if ((sum % 10) != 0) InValid(i,M);
}

function inputFocus(cid){
  
  var i=document.getElementById(cid);
  
  if (!i) return;
  if (i.className=="readonly") return; 
  
  i.className="inputSelected";
  i.select();
}

function inputBlur(cid){  
  
  var i=document.getElementById(cid);
  
  if (!i) return;  
  if (i.className=="readonly") return;
   
  i.className="";
}

function buttonClick(cid, aid, bval) {
  var b=document.getElementById(cid);
  var a=document.getElementById(aid);

  if (b.type.toUpperCase()=='SUBMIT') {
    Validate=($(cid).attr('val')!="N");  
    a.value=bval;
  }  
}

function calendarClick(cid, fid){
  
  var i=document.getElementById(cid);
  var b=document.getElementById(cid+"_BUTTON");
  var fr=document.getElementById(fid);
  
  if (i==undefined || b==undefined) return;   
  if (i.className=="readonly") return; 
  
  var c=document.getElementById("qCalendar");
  
  if (c.style.display == "block") {
    c.style.display="none"; 
    activeObject=null;
    activeDown=null;
    i.focus();
  }  
  else 
  {
    showObject(c, b, "B", "R", "T", "R",-2,0);
    activeObject=c;
    activeDown=b;
    
    if (i.value!="" && Date.parse(i.value)) 
      var s=new Date(i.value); 
    else 
      var s=new Date();  
    
    window.frames.qCalendar.CalSet(i, s.getDate(), s.getMonth(), s.getFullYear());
    window.frames.qCalendar.CalInit();
    i.focus();
  }
}



function classifiedGet(fid,vid) {
  var v=document.getElementById(fid+"#"+vid);
  if (v==undefined) return;
  var f=v.form;
  
  var getnames = " P2 P3 P4 P5 P6 P7 P8 P9 N0 N1 N2 N3 N4 N5 "; 
  var t="";
  var fn="";
  var ex=""
  
  for (x=0; x<f.elements.length; x++)
  { 
    ex=""
    fn=f.elements(x).id;
    fn=fn.replace(/AD#/,"");
    if (getnames.indexOf(fn)>=0)      
      if (f.elements(x).value!="" && f.elements(x).value!="0")
      {
        var et=document.getElementById(f.elements(x).id+"_TEXT");
        if (et==undefined) et=f.elements(x);
        
        if (et.textshort!=undefined) {ex=et.textshort; if (ex!="X") t=t+et.textshort;};
        if (ex!="X") t=t+et.value+" ";
      }
  }
  if (t=="") t=document.getElementById("CAT").innerText;
  v.value=t;
  wordCount(v.value,fid+"#WORDCOUNT",fid+"#CHARCOUNT");
}

function classifiedSearch(c) {
  
  var f=document.getElementById("FORMSEARCH");
  var a=document.getElementById("SEARCH#ACTION");
  var q0=document.getElementById("SEARCH#Q0");
  var mc=c.toString().substr(0,1);

  q0.value=c;
  a.value="SEARCH";

  if (mc=="1") var sc=document.getElementById("SEARCH#W1");
  if (mc=="2") var sc=document.getElementById("SEARCH#E1");
  if (mc=="3") var sc=document.getElementById("SEARCH#A1");

  if (sc!=null) {if (c==mc+'000') sc.value='0'; else sc.value=c;}; 

  f.submit();  
}
