// popup an image
function imagePop(url, pic_width, pic_height, autodie) {
	var text, preview, windowprops, scrn_w, scrn_h, calc_left;
	scrn_w = screen.width;
	scrn_h = screen.height;
	if (navigator.appVersion.indexOf('Mac') != -1) {
		pic_width = pic_width + 5;
		pic_height = pic_height + 5;
	}
	calc_left = scrn_w-pic_width-50; // used to place window on right side...
	if (scrn_w < pic_width + 50 && scrn_h < pic_height + 50) {
		windowprops = "scrollbars=yes,resizable=yes,left="+ calc_left +",top=50,width=" + (scrn_w-100) + ",height=" + (scrn_h-150);
	} else if (scrn_w < pic_width + 50) {
		windowprops = "scrollbars=yes,resizable=yes,left=" + calc_left + ",top=50,width=" + (scrn_w-100) + ",height=" + (pic_height);
	} else if (scrn_h < pic_height + 50) {
		windowprops = "scrollbars=yes,resizable=yes,left=" + calc_left + ",top=50,width=" + (pic_width) + ",height=" + (scrn_h-150);	
	} else { // is small enough
		windowprops = "resizable=yes,left=" + calc_left + ",top=50,width=" + (pic_width) + ",height=" + (pic_height);
	} 
	text = "<html><head><title>" + url + "</title></head><body style='margin: 0; padding: 0; background-color: #000;'";
	if (autodie) text += " onblur='self.close();'";
	text += "><center><img src='" + url + "'>";
	text += "</center></body></html>";
	preview = window.open("", "p" + (Math.round(Math.random()*1000000)), windowprops); // different window id each time...
	preview.document.open();
	preview.document.write(text);
	preview.document.close();
}

/*
 * VALIDATE A FORM - WORKS WITH FORMPRO3
 */
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function hasXRQD(s) {
	var xpattern = /XRQD/;
	return xpattern.test(s);
}

function leftOrRightOfXRQD(s) {
	// returns left if exists
	var i = s.indexOf('XRQD');
	if (i == -1) alert('Error XQRD01. Please call us if you see this error.');
	if (i == 0) {
		// nothing to the left, return right
		return s.substr(4);
	} else {
		// something to left, return left
		return s.substr(0, i);
	}
}

function validate(f) {
	/*
	 * RULES
	 * 1. The characters right of the XRQD in the name must match the value in unrequired inputs,
          else the default value will show up in the email.
	 * 2. Each group of required checkboxes or radio buttons must end with a hidden input
	 *    that has a value of "CheckOrRadioEnd". Remember to name the hidden inputs uniquely.
	 * 3. Each required form element must be named "XRQDxxxxx" or "xxxxXRQDxxxxx.
	 * 4. Anything prepending the "XRQD" becomes the alert box's name for the element.
	 	  (This is true for the last element of a group and all other elements.)
	 * 5. Required email must have a name which ends with a case insensitive "email" to be tested.
	 */
	var emailValuePattern = new RegExp("^[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})$","i");
	var emailNamePattern = /email/i;
	var keepname;
	var onePlusChosenInGrp = false;
	var sel;
	for (var i = 0; i < f.length; i++) {
		var e = f.elements[i];
		// Checkbox and Radio button test
		if (e.value == "CheckOrRadioEnd") {
			if (!onePlusChosenInGrp) { // must have to prepare 2nd group once 1st group is okay.
				alert("'" + leftOrRightOfXRQD(keepname) + "' is required.");
				return false;
			} else {
				// set up for next group
				onePlusChosenInGrp = false;
			}
		}
		if (hasXRQD(e.name)) {
			if (e.type == "checkbox" || e.type == "radio") {
				keepname = e.name;
				if (e.checked) onePlusChosenInGrp = true;
			}
			if (e.type == "select-one" || e.type == "select-multiple") {
				sel = false;
				for (var j = 0; j < e.options.length; j++) {
					if (e.options[j].selected) sel = true;
				}
				if (!sel) {
					alert("'" + leftOrRightOfXRQD(e.name) + "' is required.");
					return false;
				}
			}
			if (e.type == "text" || e.type == "textarea" || e.type == "password") {
				if (e.value == e.defaultValue || trim(e.value) == "") { 
					alert("'" + leftOrRightOfXRQD(e.name) + "' is required.");
					return false;
				}
			}
			
			if (emailNamePattern.test(e.name)) {
			//if (e.name.substr(-5) == "email") {
				if (!emailValuePattern.test(e.value)) {
					alert("The email address '" + e.value + "' does not seem valid. If it is please call us.");
					return false;
				}
			}
		}
	}
	return true;
}



