/* Behavior for www.freylutz.com */

window.onload = function() {
	formFocus();
	assignFormVal();
}

/* -- checker function - checks to see if variables in each function exist -- */

function checkVars(e) {
	var x = e.split(",");
	var pass = true;
	checkerInvalid = new Array;

	for(i=0; i<x.length; i++) {
		
		if(!eval(x[i])) {
		pass = false; 
		checkerInvalid[checkerInvalid.length] = x[i];
		}
		
	}
	
	return pass;

}

/* -- on contact page, places cursor focus on name field on page load -- */

function formFocus() {
	
	if(!checkVars('document.getElementById("focus")')) {
		return;
	}
	
	var form_focus = document.getElementById("focus");
	
	form_focus.focus();
}

function assignFormVal() {
	if(checkVars('document.getElementById("freeform")')) {
		document.getElementById("freeform").onsubmit = function () {return formValidate(this)};
	}
}

function formValidate(f) {
	var firstField = "";
	var msg = "";
	for(var i=0;i<f.length;i++) {
		if (f.elements[i].className == "req" && f.elements[i].value == "") {
			msg = msg + "   - " + f.elements[i].title + "\n";
			if(firstField == "") {
				firstField = f.elements[i];
			}
		}
	}
	
	if (msg == "") {
		return true;
	}
	else {
		msg = "The following fields must be completed: \n" + msg;
		alert(msg);
		firstField.focus();
		return false;
	}
}

function launchPopUp(linkObject) {
	// This closes a pop-up window by the same name if it is already open.
	var winName = linkObject.href;
	if (linkObject.name) {
		winName = linkObject.name;
		if (eval("window." + winName) != null) {
			eval("window." + winName + ".close()");
		}
	}
	
	var h = 400;
	if (linkObject.getAttribute("h")) {h = linkObject.getAttribute("h")}
	
	var w = 400;
	if (linkObject.getAttribute("w")) {w = linkObject.getAttribute("w")}
	
	var r = "yes";
	if (linkObject.getAttribute("r")) {r = linkObject.getAttribute("r")}
	
	var s = "yes";
	if (linkObject.getAttribute("s")) {s = linkObject.getAttribute("s")}
	
	var newWin = window.open(linkObject.href,"","width=" + w + ",height=" + h + ",resizable=" + r + ",scrollbars=" + s);

	newWin.focus();
	
	return newWin;

}