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 Email2Friend(ctype) {
	var l = (screen.width-470)/2;
	var t = (screen.height-300)/2;
	window.open("email2friend.asp?type="+ctype, "email", "width=470, height=300, left="+l+",top="+t+",scrollbars=no,resizable=no");
}

function EmailSite2Friend() {
	var l = (screen.width-470)/2;
	var t = (screen.height-300)/2;
	window.open("emailsite2friend.asp", "email", "width=470, height=300, left="+l+",top="+t+",scrollbars=no,resizable=no");
}

function isBlank(s) {
		for (var i=0; i < s.length; i++) {
			c = s.charAt(i);
			if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
		}
		if (s != "") return false;
		return true;
}

function isEmail(s) {
	if (s != null) {
		var pos1 = s.indexOf('@');
		var pos2 = s.indexOf('.');
		invalid_ch = "\/#?!, ";

			for (var i=0;i<s.length;i++) {  // check for invalid chars
			if(invalid_ch.indexOf(s.charAt(i)) != -1) {
				return false;				
			}
		}
		if ((pos1 == -1) || (pos2 == -1) || (s.indexOf('@', pos1+1) != -1)) {
			return false;
		}
		return true;
	}
}

function StripMarker(str) {
	str = str.replace("__", "");
	str = str.replace("#", "");
	return str;
}

function validate(f) {
	var msg = "";
	var errors = "";
	var emptyFields = "";
	var error1 = -1;
	for (var i=0; i<f.length; i++) {
		var e = f.elements[i];
		if (((e.type == "text") || (e.type == "textarea") || (e.type == "password")) && (e.name.indexOf("__") != -1)) {
			if ((e.value == null) || (e.value == "") || isBlank(e.value)) {
				emptyFields += "\n- "+ StripMarker(e.name);
				if (error1 == -1) {
					error1 = i;
				}
				continue;
			}
		}
		if ((e.name.indexOf("#") != -1) && (!isBlank(e.value))) {
			var v = e.value;
			if (isNaN(v)) {
				errors += "-- Please enter a valid numeric "+ StripMarker(e.name) +"!\n";
				if (error1 == -1) {
					error1 = i;
				}
				continue;
			}
		}
		if ((e.name.indexOf("date") != -1) && (!isBlank(e.value))) {
			var fdate = e.value;
			if (!Date.parse(fdate)) {
				errors += "-- Please enter a valid "+ StripMarker(e.name)+"!\n";
				if (error1 == -1) {
					error1 = i;
				}
				continue;
			}
		}
		if ((e.name.indexOf("email") > -1) && (!isBlank(e.value))) {
			if (!isEmail(e.value)) {
				errors += "-- Please enter a valid email address!\n";
				if (error1 == -1) {
					error1 = i;
				}
				continue;
			}
		}
	}
	if (emptyFields) {
		msg += "The following required field/s is/are empty: "+ emptyFields + "\n\n";
	}
	if (errors) {
		msg += "Please check the ff. error(s):\n"+errors;
	}
	if ((errors) || (emptyFields)) {
		alert(msg);
		if ((f.elements[error1].type == "text") || (f.elements[error1].type == "password") || (f.elements[error1].type == "textarea")) {
			f.elements[error1].select();
			f.elements[error1].focus();
		}
		else {
			f.elements[error1].focus();
		}
		return false;
	}
	if (!emptyFields && !errors) {
		return true;
	}
}
