
/* VALIDATES FORM VERIFYING THAT REQUIRED FIELDS HAVE BEEN FILLED IN */
/* This function is called on form submit and passes the index of form to check
   and also the index of field on which to begin validation. */
function validateForm($frmI,$fldI) {
	var $fl = document.forms[$frmI].length - 1;  //Determines the form length less one (to acct for Submit button)
	var $msg = "The following fields are required:\n";
	var $fv = "";
	for($i=$fldI; $i<$fl; $i++) {  //Loops thru all fields in form starting with field passed from function call
		if(document.forms[$frmI].elements[$i].className == "reqField" && document.forms[$frmI].elements[$i].value == "") {  //Checks if field has class of "reqField" and has no value
			$fv += "- " + document.forms[$frmI].elements[$i].title + "\n";  //Concatenates empty element's title to $fv
		}
	}
	if($fv) {  //If $fv variable has a value after loop execution, then
		alert($msg + $fv);  //alert is displayed showing fields to be filled in
		return false;
	} else {   //else form is submitted
		return true;
		//alert("SUCCESS");
		//return false;
	}
}

/* HIDES EMAIL ADDRESS FROM SPAM SPIDERS */
function dispEmlAdr(emlPrf,emlDom,emlTld,cls,disp) {
	/* example: nobody@nowhere.com
			emlPrf = email prefix (e.g., nobody)  REQUIRED
			emlDom = email domain (e.g., nowhere)  REQUIRED
			emlTld = email top-level domain (e.g., com)  REQUIRED
		cls = class for link's CSS style  [optional]
		disp = text displayed for link  [optional; otherwise defaults to email addres]
	*/
	var q = '"';  //quote (") character (eliminates need to escape quotes)
	var s = '/';  //slash (/) character (eliminates need to escape slashes)
	document.write("<a href=" + q + "&#109;&#97;" + "&#105;&#108;" + "&#116;&#111;&#58;");
	document.write(emlPrf + "&#64;" + emlDom + "&#46;" + emlTld);
	document.write(q + " class=" + q + cls + q + ">");
	if (disp) {
		document.write(disp); //if "disp" has a value, "disp" is displayed
	} else {
		document.write(emlPrf + "&#64;" + emlDom + "&#46;" + emlTld);
		// otherwise, the actual email address is displayed on-screen
	}
	document.write("<" + s + "a>");
}

/* VALIDATES EMAIL ADDRESS TO ENSURE SYNTAX IS CORRECT */
function check_email(e) {
    ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-+=_QWERTYUIOPASDFGHJKLZXCVBNM";
    for(i=0; i < e.length ;i++){
        if(ok.indexOf(e.charAt(i))<0){
            return (false);
        }
    }

    if (document.images) {
        re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
        re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
        if (!e.match(re) && e.match(re_two)) {
            return (-1);
        }
    }
}

/* SELECTS A RADIO BUTTON WHEN ADJACENT FIELD IS ENTERED OR AN ADJACENT IMAGE OR ELEMENT IS CLICKED */
function checkRadioBtn(id) {
	//insert name of form and radio buttons where indicated by angle brackets
	//(be sure to remove brackets) the uncomment the code
	//document.<formName>.<radioName>[id].checked = true;
}
//onfocus="checkRadioBtn(id);"  //use to select button on field entry

/* FORMATS A NUMBER TO SPECIFIED NUMBER OF DECIMAL PLACES; CAN BE USED TO FORCE CURRENCY DISPLAY */
Number.prototype.toDecimals = function(n){
	//
	n=(isNaN(n))?
		2:
		n;
	var
		nT = Math.pow(10,n);
	function pad(s) {
			s = s || '.';
			return (s.length>n)?
				s:
				pad(s+'0');
	}
	return (isNaN(this))?
		this:
		(new String(
			Math.round(this*nT)/nT
		)).replace(/(\.\d*)?$/,pad);
}

//cost.toDecimals(2); //example of call to the method, where 'cost' is a previously defined variable


/* DETECTS ENTER KEY PRESS IN FORM FIELD AND PREVENT FORM SUBMISSION */
function detectEnterKey(e){ //event object passed from function invocation
	var characterCode; //stores ascii character code
	if(e && e.which){ //if which property detects Netscape
		e = e;
		characterCode = e.which; //captures ascii code for Netscape
	}else{
		e = event;
		characterCode = e.keyCode; //captures ascii code for IE
	}
	if(characterCode == 13){ //tests for ascii code for enter key
		alert("Click the \"Update\" button to modify the estimate values.");
		return false;
	}else{
		return true;
	}
}


/* TOGGLES CONTENT OF A BOX (BY ID) FROM VISIBLE TO HIDDEN */
function toggleBox(id) {
	if(!document.getElementById) return;
	var box = document.getElementById(id);
	if(box.className = 'box_hidden') {
		box.className = 'box_visible';
	}else{
		box.className = 'box_hidden';
	}
}


/* DISPLAYS ERROR MSG FOR NON-ACTIVE (NULL) LINKS */
function nullink(flag,msg) {
	if(flag == 1) {
		if(msg) {
			alert(msg);
		} else {
			//default msg if no custom msg is passed
			alert("This link is not yet active.");
		}
		return false;
	} else {
		return;
	}
}


/* DISPLAYS POPUP WINDOW */
function openPopupWindow(compID,windowWidth,windowHeight) {
	var scrnHt = screen.height;  //Ascertains user's screen height
	var scrnWd = screen.width;  //Ascertains user's screen width
	// Modify the values in quotation marks to control window characteristics.
	var windowURL = "compPopup.php?compID=" + compID;  // URL of content to display in window
	var windowName = "popup";  // Name for new window
	var windowTop = (scrnHt-windowHeight)/2;  // Window y-coordinate origin (auto centers; or set to pixels)
	var windowLeft = (scrnWd-windowWidth)/2;  // Window x-coordinate origin (auto centers; or set to pixels)
	var windowLocation = "no";  // Boolean; enables input field for entering URLs
	var windowMenubar = "no";  // Boolean; enables window Menubar
	var windowStatus = "no";  // Boolean; enables window Status line
	var windowToolbar = "no";  // Boolean; enables window Toolbar (e.g. Back, Forward buttons)
	var windowResizable = "no";  // Boolean; enables window resizing
	var windowScrollbars = "no";  // Boolean; enables window scrollbars

	// DO NOT MODIFY ANY OF THE CODE BELOW THIS LINE
	var winHt = "height=" + windowHeight, winWd = "width=" + windowWidth, winLft = "left=" + windowLeft, winTop = "top=" + windowTop, winLoc = "location=" + windowLocation, winMenu = "menubar=" + windowMenubar, winSts = "status=" + windowStatus, winBar = "toolbar=" + windowToolbar, winSiz = "resizable=" + windowResizable, winScrl = "scrollbars=" + windowScrollbars;
	var windowFeatures = winHt + "," + winWd + "," + winLft + "," + winTop + "," + winLoc + "," + winMenu + "," + winSts + "," + winBar + "," + winSiz + "," + winScrl;

	window.open(windowURL,windowName,windowFeatures);
}


