
function openWindow(url) {
   var left = Math.floor( (screen.width - url.width) / 2);
   var top = Math.floor( (screen.height - url.height) / 2);
   var winParms = "top=" + (top-10) + ",left=" + left + ",height=" + height + ",width=" + width;
   winParms += "," + "status=yes,scrollbars=yes,resizable=yes"; 
   var win = window.open(url, 'HQ Preview', winParms);
   if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
   //return win;
}

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 clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
}
}
function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
}
}

function KW_s(r, g, b, el) {
    d=document;hr=r.toString(16);hg=g.toString(16);hb=b.toString(16);
	hr=(hr.length==1)?"0"+hr:hr;hg=(hg.length==1)?"0"+hg:hg;hb=(hb.length==1)?"0"+hb:hb;
	if (d.getElementById) d.getElementById(el).style.color="#"+hr+hg+hb;
}

function KW_c(a,b,s,i) { return Math.floor(a*((s-i)/s)+b*(i/s)) }

function fadein(o){
    var r=0;g=51;b=153;e=21;n=98;l=255;s=20;
    for(i=0;i<=s;i++)setTimeout("KW_s("+KW_c(r,e,s,i)+","+KW_c(g,n,s,i)+","+KW_c(b,l,s,i)+",'"+o+"');",i*s);
}

function fadeout(o){
    var r=21;g=98;b=255;e=0;n=51;l=153;s=20;
    for(i=0;i<=s;i++)setTimeout("KW_s("+KW_c(r,e,s,i)+","+KW_c(g,n,s,i)+","+KW_c(b,l,s,i)+",'"+o+"');",i*s);
}

// returns true if the string is empty
function isEmpty(str){
	return (str == null) || (str.length == 0);
}

// returns true if the string is a valid email
function isEmail(str){
	if(isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}

// returns true if the string only contains characters A-Z or a-z
function isAlpha(str){
	var re = /[^a-zA-Z]/g
	if (re.test(str)) return false;
	return true;
}

// returns true if the string only contains characters 0-9
function isNumeric(str){
	var re = /[\D]/g
	if (re.test(str)) return false;
	return true;
}

// returns true if the string only contains characters A-Z, a-z or 0-9
function isAlphaNumeric(str){
	var re = /[^a-zA-Z0-9]/g
	if (re.test(str)) return false;
	return true;
}

// returns true if the string's length equals "len"
function isLength(str, len){
	return str.length == len;
}

// returns true if the string's length is between "min" and "max"
function isLengthBetween(str, min, max){
	return (str.length >= min)&&(str.length <= max);
}

// returns true if the string is a US phone number formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhoneNumber(str){
	var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
	return re.test(str);
}

// returns true if the string is a valid date formatted as...
// mm dd yyyy, mm/dd/yyyy, mm.dd.yyyy, mm-dd-yyyy
function isDate(str){
	var re = /^(\d{1,2})[\s\.\/-](\d{1,2})[\s\.\/-](\d{4})$/
	if (!re.test(str)) return false;
	var result = str.match(re);
	var m = parseInt(result[1]);
	var d = parseInt(result[2]);
	var y = parseInt(result[3]);
	if(m < 1 || m > 12 || y < 1900 || y > 2100) return false;
	if(m == 2){
		var days = ((y % 4) == 0) ? 29 : 28;
	}else if(m == 4 || m == 6 || m == 9 || m == 11){
		var days = 30;
	}else{
		var days = 31;
	}
	return (d >= 1 && d <= days);
}

// returns true if "str1" is the same as the "str2"
function isMatch(str1, str2){
	return str1 == str2;
}

// returns true if the string contains only whitespace
// cannot check a password type input for whitespace
function isWhitespace(str){ // NOT USED IN FORM VALIDATION
	var re = /[\S]/g
	if (re.test(str)) return false;
	return true;
}

// removes any whitespace from the string and returns the result
// the value of "replacement" will be used to replace the whitespace (optional)
function stripWhitespace(str, replacement){// NOT USED IN FORM VALIDATION
	if (replacement == null) replacement = '';
	var result = str;
	var re = /\s/g
	if(str.search(re) != -1){
		result = str.replace(re, replacement);
	}
	return result;
}

// validate the form
function validateForm(f){
	var errors = '';
	var i,e,t,n,v;
	for(i=0; i < f.elements.length; i++){
		e = f.elements[i];
		if(e.optional) continue;
		t = e.type;
		n = e.name;
		v = e.value;
		if(t == 'text' || t == 'password' || t == 'textarea'){
			if(isEmpty(v)){
				errors += n+' cannot be empty.\n'; continue;
			}
			if(v == e.defaultValue){
				errors += n+' cannot use the default value.\n'; continue;
			}
			if(e.isAlpha){
				if(!isAlpha(v)){
					errors += n+' can only contain characters A-Z a-z.\n'; continue;
				}
			}
			if(e.isNumeric){
				if(!isNumeric(v)){
					errors += n+' can only contain characters 0-9.\n'; continue;
				}
			}
			if(e.isAlphaNumeric){
				if(!isAlphaNumeric(v)){
					errors += n+' can only contain characters A-Z a-z 0-9.\n'; continue;
				}
			}
			if(e.isEmail){
				if(!isEmail(v)){
					errors += v+' is not a valid email.\n'; continue;
				}
			}
			if(e.isLength != null){
				var len = e.isLength;
				if(!isLength(v,len)){
					errors += n+' must contain only '+len+' characters.\n'; continue;
				}
			}
			if(e.isLengthBetween != null){
				var min = e.isLengthBetween[0];
				var max = e.isLengthBetween[1];
				if(!isLengthBetween(v,min,max)){
					errors += n+' cannot contain less than '+min+' or more than '+max+' characters.\n'; continue;
				}
			}
			if(e.isPhoneNumber){
				if(!isPhoneNumber(v)){
					errors += v+' is not a valid US phone number.\n'; continue;
				}
			}
			if(e.isDate){
				if(!isDate(v)){
					errors += v+' is not a valid date.\n'; continue;
				}
			}
			if(e.isMatch != null){
				if(!isMatch(v, e.isMatch)){
					errors += n+' does not match.\n'; continue;
				}
			}
		}
		if(t.indexOf('select') != -1){
			if(isEmpty(e.options[e.selectedIndex].value)){
				errors += n+' needs an option selected.\n'; continue;
			}
		}
		if(t == 'file'){
			if(isEmpty(v)){
				errors += n+' needs a file to upload.\n'; continue;
			}
		}
	}
	if(errors != '') alert(errors);
	return errors == '';
}

/*
The following elements are not validated...

button   type="button"
checkbox type="checkbox"
hidden   type="hidden"
radio    type="radio"
reset    type="reset"
submit   type="submit"

All elements are assumed required and will only be validated for an
empty value or defaultValue unless specified by the following properties.

isEmail = true;          // valid email address
isAlpha = true;          // A-Z a-z characters only
isNumeric = true;        // 0-9 characters only
isAlphaNumeric = true;   // A-Z a-z 0-9 characters only
isLength = number;       // must be exact length
isLengthBetween = array; // [lowNumber, highNumber] must be between lowNumber and highNumber
isPhoneNumber = true;    // valid US phone number. See "isPhoneNumber()" comments for the formatting rules
isDate = true;           // valid date. See "isDate()" comments for the formatting rules
isMatch = string;        // must match string
optional = true;         // element will not be validated
*/

// All of the previous JavaScript is coded to process
// any form and should be kept in an external file if
// multiple forms are being processed.

// This function configures the previous
// form validation code for this form.
function configureValidation(f){
 f.name.isAlphaNumeric = true;
 f.email.isEmail = true;
 f.url.optional = true;
 return validateForm(f);
}

function configureReqValidation(f){
 f.name.isAlphaNumeric = true;
 f.email.isEmail = true;
 f.url.optional = true;
 f.phone.optional = true;
 f.phone.isPhoneNumber = true;
 f.company.optional = true;
 f.city.isAlphaNumeric = true;
 f.country.isAlphaNumeric = true;
 f.fcms.optional = true;
 f.fnews.optional = true;
 f.ffaq.optional = true;
 f.fcommerce.optional = true;
 f.fcart.optional = true;
 f.fgallery.optional = true;
 f.fflash.optional = true;
 f.fpoll.optional = true;
 f.fforum.optional = true;
 f.fstat.optional = true;
 f.fsearch.optional = true;
 f.fcalendar.optional = true;
 f.fmore.optional = true;
 f.xhosting.optional = true;
 f.xmaintenance.optional = true;
 f.xseo.optional = true;
 f.xgum.optional = true;
 f.file.optional = true;
 f.comments.optional = true;
 return validateForm(f);
}

