var noErrors = true;
var errorMessage = "";
var focusOnError = false;

var browser = navigator.appName;
var version = navigator.appVersion;
var ie = "Microsoft Internet Explorer";
var nn = "Netscape";
var mac = "MacPPC";
var os = navigator.platform;

function launchBrowser(url, name, width, height) {
  var x = 0;
  var y = 0;
  if (navigator.appVersion.length > 0
      && navigator.appVersion.charAt(0) > '3'
      && navigator.appVersion.charAt(0) <= '9') {
    if (width > screen.availWidth - 12) {
      width = screen.availWidth - 12;
    }
    if (height > screen.availHeight - 48) {
      height = screen.availHeight - 48;
    }
    x = (screen.availWidth - 12 - width) / 2;
    y = (screen.availHeight - 48 - height) / 2;
  }
  var params = "resizable=yes,scrollbars=yes,location=yes,status=yes,toolbar=yes,menubar=yes,directories=yes,screenX=" + x + ",screenY=" + y + ",width=" + width + ",height=" + height + ")";
  var windowvar = window.open(url, name, params);
  windowvar.focus();
}

function checkInput(control,error) {
  if (control.value.length < 1) {
    setFocusOnError(control);
    addError(error);
  }
}

function checkInputValue(control,value,error) {
  if (control.value == value) {
    setFocusOnError(control);
    addError(error);
  }
}

function addError(error) {
  errorMessage += error + "\n";
  noErrors = false;
}

function initErrors() {
  noErrors = true;
  errorMessage = "";
  focusOnError = false;
}

function showErrors() {
  if (noErrors) {
    return true;
  } else {
    alert("The following errors were found:\n\n" + errorMessage);
    if (focusOnError) {
      //focusOnError.focus();
    }
    return false;
  }
}

function setFocusOnError(ctrl) {
  if (focusOnError == false) {
    focusOnError = ctrl;
  }
}

function isValidDate(myDate) {
// checks if date passed is in valid mm/dd/yyyy format

  if (myDate.length==0) {return true;}

  if (myDate.length == 10) {
      if (myDate.substring(2,3) == "/" && myDate.substring(5,6) == "/") {
          var month  = myDate.substring(0,2);
          var date = myDate.substring(3,5);
          var year  = myDate.substring(6,10);

          var test = new Date(year,month-1,date);

          year = y2k(test.getYear());
          if (year>=1900 && (month-1 == test.getMonth()) && (date == test.getDate())) {
              return true;
          }
      }
  }
  return false;
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function isValidZip(zip,mode) {
  if (zip.length<1) return true;
  switch (mode) {
    default:
      re = /^\d{5}$/
      if(zip.search(re)==-1) {
        return false;
      } else {
        return true;
      }
  }
}

function isValidCCNum(s) {
  if (s.length<1) return true;
  re = /^\d{10,20}$/
  if (s.search(re)==-1) {
    return false;
  } else {
    return true;
  }
}

function isValidCCExpDate(ExpMonth,ExpYear) {
  if (ExpMonth.length<1 || ExpYear.length<1) return true;
  re = /^\d+$/
  if (ExpMonth.search(re)==-1 || ExpYear.search(re)==-1) {
    return false;
  } else {
    if (ExpMonth.substring(0,1)=='0') {
      m = parseInt(ExpMonth.substring(1));
    } else {
      m = parseInt(ExpMonth);
    }
    if (ExpYear.substring(0,1)=='0') {
      y = parseInt(ExpYear.substring(1));
    } else {
      y = parseInt(ExpYear);
    }

    if (m<1 || m>12 || y<3) {
      return false;
    } else {
      return true;
    }
  }
}

function isValidCCSecCode(s) {
  if (s.length<1) return true;
  re = /^\d{2,6}$/
  if (s.search(re)==-1) {
    return false;
  } else {
    return true;
  }
}

function RegExpTest(TestString,re) {
  if (TestString.length<1) return true;
  if (TestString.search(re)==-1) {
    return false;
  } else {
    return true;
  }
}

function formatCurrency(myNum){
 var prefix="";
 var wd;

 wd="w";
 var tempnum=myNum.toString();
 for (i=0;i<tempnum.length;i++) {
   if (tempnum.charAt(i)==".") {
     wd="d"
     break
   }
 }
 if (wd=="w") {
  return prefix+tempnum+".00";
 } else {
  if (tempnum.charAt(tempnum.length-2)==".") {
    return prefix + tempnum + "0";
  } else {
    tempnum = Math.round(tempnum*100)/100;
    if (tempnum == Math.round(tempnum)) {tempnum=formatCurrency(tempnum);}
    if (tempnum.toString().charAt(tempnum.length-2)==".") {
      return prefix+tempnum+"0";
    } else {
      return prefix+tempnum;
    }
  }
 }
}

function helpWindow(URL) {
//  features = "height=,width=,status=yes,toolbar=no,menubar=yes,location=yes, left=240, top=30";
  window.open(URL,"","");
}

function getFilename(pathname) {
  for (i=pathname.length; i>=1; i--){
    if (pathname.charAt(i)=="/"){
      output = pathname.substr(i+1);
      break;
    }
  }
  return output;
}

btnPre = "/images/btn/btn";

function loadBtnImg (src) {
  img = new Image();
  img.src = src;
}

function btnImg(name) {
  img = btnPre + name + ".gif";
  img_down = btnPre + name + "_down.gif";
  document.write("<input id=btn" + name + " type=image src=" + img + " onmouseup=\"this.src='" + img + "'\" onmouseout=\"this.src='" + img + "'\" onmousedown=\"this.src='" + img_down + "'\" alt=" + name + " style=\"cursor:default\">");
  //load the mouse-down image
  loadBtnImg(img_down);
}

function btnImgCustom(name,onclickAction) {
  img = btnPre + name + ".gif";
  img_down = btnPre + name + "_down.gif";
  document.write("<img id=btn" + name + " onclick=\"" + onclickAction + "\" src=" + img + " onmouseup=\"this.src='" + img + "'\" onmouseout=\"this.src='" + img + "'\" onmousedown=\"this.src='" + img_down + "'\" alt=" + name + ">");
  //load the mouse-down image
  loadBtnImg(img_down);
}

function btnImgBack(name) {
  img = btnPre + name + ".gif";
  img_down = btnPre + name + "_down.gif";
  document.write("<img id=btn" + name + " src=" + img + " onclick=\"history.back();\" onmouseup=\"this.src='" + img + "'\" onmouseout=\"this.src='" + img + "'\" onmousedown=\"this.src='" + img_down + "'\" alt=" + name + ">");
  //load the mouse-down image
  loadBtnImg(img_down);
}

function split(str,pos,ch) {
  if (str.length == 0) return str;
  if (str.indexOf(ch) < 0) {
    return "";
  }
  var count = 0, index = 0, last = 0;
  index = str.indexOf(ch);
  while (index >=0) {
    if (count == pos) {
      return str.substring(0,index);
    }
    str = str.substring(index+1, str.length);
    count++;
    index = str.indexOf(ch);
  }
  if (count == pos) return str;
  else return "";
}

function rtrim(str) {
  var strLength = str.length;
  var returnStr = "";
  var ch;
  for (var i=strLength-1; i > -1; i--) {
    if (strLength == 1)	ch = str;
    else ch = str.substring(i, i+1);
    if (ch != " ") {
      returnStr = str.substring(0, i+1);
      i =  0;
    }
  }
  return returnStr;
}

function ltrim(str) {
  var strLength = str.length;
  var ch;
  var returnStr = "";
  for (var i=0; i < strLength; i++) {
    if (strLength == 1) ch = str;
    else ch = str.substring(i, i+1);
    if (ch != " ") {
      returnStr = str.substring(i, strLength);
      i =  strLength + 1;
    }
  }
  return returnStr;
}

function trim(str)   {
  return rtrim(ltrim(str));
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_displayStatusMsg(msgStr)  { //v3.0
  status=msgStr; document.MM_returnValue = true;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("^/index.htm.htm"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

