var msg=""
var count=1
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
var winCust=null

function checkLength(minl, maxl, stval, fieldname){
if(stval.length<minl || stval.length>maxl){
if(minl!=maxl){
msg=msg+count+". The "+fieldname+" must be "+minl+" to "+ maxl+" characters length.\n"
}else{
msg=msg+count+". The "+fieldname+" must be "+minl+" characters length.\n"
}
count++
return false
}else{
return true
}
}

function IsEmpty(stValue) {
if ((stValue.length==0) || (stValue==null)) {
return true;
}else { 
return false; 
}}

function validateZIP(field) {
var valid = "0123456789-";
var hyphencount = 0;

if (field.length!=5 && field.length!=10) {
msg=msg+count+". Please enter your 5 digit or 5 digit+4 zip code.\n"
count++
return false;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
msg=msg+count+". Invalid characters in your zip code.\n"
count++
return false;
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
msg=msg+count+". The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.\n"
count++
return false;
}
}
return true;
}

function isInteger(s){
var i;
for (i = 0; i < s.length; i++){   
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
return true;
}

function stripCharsInBag(s, bag){
var i;
var returnString = "";
for (i = 0; i < s.length; i++){   
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}
 
function checknumber(nvalue){
var anum=/(^\d+$)|(^\d+\.\d+$)/
if (anum.test(nvalue))
testresult=true
else{
testresult=false
}
return (testresult)
}

function format (expr, decplaces) {
var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces))
while (str.length <= decplaces) {
	str = "0" + str
}
var decpoint = str.length - decplaces
return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}

function dollarize (expr) {
return format(expr,2)
}


function emailCheck (emailStr) {
var checkTLD=1;
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);

if (matchArray==null) {
msg=msg+count+". Email address seems incorrect (check @ and .'s).\n"
count++
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
msg=msg+count+". Email contains invalid characters.\n"
count++
return false;
}}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
msg=msg+count+". Email contains invalid characters.\n"
count++
return false;
}}

if (user.match(userPat)==null) {
msg=msg+count+". Email contains invalid characters.\n"
count++
return false;
}

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
msg=msg+count+". Destination IP address of the email is invalid.\n"
count++
return false;
}}
return true;
}
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
msg=msg+count+". The email address does not seem to be valid.\n"
count++
return false;
}}


if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
msg=msg+count+". The email address must end in a well-known domain or two letter " + "country.\n"
count++
return false;
}

if (len<2) {
msg=msg+count+". This email address is missing a hostname!\n"
count++
return false;
}

return true;
}


var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 10;

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (checknumber(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidatePhone(Phone){
if ((Phone==null)||(Phone=="")){
msg=msg+count+". Phone number can not be blank.\n"
count++
return false
}
if (checkInternationalPhone(Phone)==false){
msg=msg+count+". Please enter a valid phone number.\n"
count++
return false
}
return true
}

function getObj(name){
if (document.getElementById){
this.obj = document.getElementById(name);
this.style = document.getElementById(name).style;
}else if (document.all){
this.obj = document.all[name];
this.style = document.all[name].style;
}else if (document.layers){
this.obj = getObjNN4(document,name);
this.style = this.obj;
}
}

function getObjNN4(obj,name){
var x = obj.layers;
var thereturn;
for (var i=0;i<x.length;i++){
if (x[i].id == name)
thereturn = x[i];
else if (x[i].layers.length)
var tmp = getObjNN4(x[i],name);
if (tmp) thereturn = tmp;
}
return thereturn;
}

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_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) { //v4.01
var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>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);
if(!x && d.getElementById) x=d.getElementById(n); 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];}
}


function fnPreview(page,w,h,scroll,pos,res){
if(winCust!=null){
if(!winCust.closed){
winCust.close()
}
winCust=null
}
if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=yes,menubar=no,toolbar=no,resizable='+res;
winCust=window.open(page,'',settings,false);
winCust.creator=self
}

function fnClosePreWin(){
if(winCust!=null){
if(!winCust.closed){
winCust.close()
}
winCust=null
}
}


