/*
checkform.js    v. 1.0.1
	Tweaked isValid to include NULL values

checkform.js    v. 1.0.0

Library of functions used to do stuff
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

//	Check for "enter" key in form fields
//	put "document.onkeypress = checkCR;" on page to use
function checkCR(evt) {
	var evt  = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    if ((evt.keyCode == 13) && ((node.type=="text") || (node.type=="checkbox") || (node.type=="password"))) {
		return false;
	}
}

//	Parse fields and highlight if blank, return false if any do not pass
//	Field IDs as arguments, e.g. isValid('name','address')
function isValid(){
	var items = isValid.arguments.length;
	var count = 0;
	for (i = 0;i < items;i++){
		if (document.getElementById(isValid.arguments[i]).value == '' || document.getElementById(isValid.arguments[i]).value == 'NULL'){
			count++;
			document.getElementById(isValid.arguments[i]).style.backgroundColor = '#FFA6AE';
		} else {
			document.getElementById(isValid.arguments[i]).style.backgroundColor = '';
		}
	}
	if (count > 0){
		return false;
	} else {
		return true;
	}
}

//	Check for numbers only, highlight accordingly and return false
function isNumeric(element_id){
	var el = document.getElementById(element_id);
	var numericExpression = /^[0-9]+$/;
	if (el.value != '' && el.value.match(numericExpression)){
		el.style.backgroundColor = '';
		return true;
	} else {
		el.style.backgroundColor = '#FFA6AE';
		return false;
	}
}

//	Check for proper date format, highlight accordingly, return false on fail
function isDate(element_id){
	var el = document.getElementById(element_id);
	var dateExpression = /^([0-9][0-9])\-([0-9][0-9])\-([0-9][0-9][0-9][0-9])$/;
	if (el.value != '' && !(el.value.match(dateExpression))){
		el.style.backgroundColor = '#FFA6AE';
		return false;
	} else if (el.value.match(dateExpression)){
		el.style.backgroundColor = '';
		return true;
	}
}

//	Check email field for proper format, highlight accordingly, return false on fail
function isEmail(element_id){
	var el = document.getElementById(element_id);
	var emailExpression = /^([a-zA-Z0-9_\.\+])+\@(([a-zA-Z0-9_\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (el.value != '' && !(el.value.match(emailExpression))){
		el.style.backgroundColor = '#FFA6AE';
		return false;
	} else if (el.value == '' || el.value.match(emailExpression)){
		el.style.backgroundColor = '';
		return true;
	}
}

//	Hide/Unhide
//	Pass field IDs as arguments, e.g. toggle_hidden('div_id_1','span_id_4')
function toggle_hidden(){
	var items = toggle_hidden.arguments.length;
	for (i = 0;i < items;i++){
		el = document.getElementById(toggle_hidden.arguments[i]);
		if (el.style.display == 'none'){
			el.style.display = 'block';
		} else if (el.style.display == 'block' || el.style.display == ''){
			el.style.display = 'none';
		} else if (el.style.visibility == 'visible' || el.style.visibility == ''){
			el.style.visibility = 'hidden';
		} else if (el.style.visibility == 'hidden'){
			el.style.visibility = 'visible';
		}
	}
}

//	Check menu for "other" value, and hide/unhide accordingly
function swap_field(element_id,swap_field){
	var el = document.getElementById(element_id);
	if (el.value == 'other'){
		toggle_hidden(element_id,swap_field);
		return true;
	} else {
		return false;	
	}
}

//	Open a new pop-up and print to the window in order listed.
//	Function takes at least 2 arguments: PrintContent('element id','copy type')
//	'clone' for copy type will copy entire element including atributes, otherwise only innerHTML is copied
function PrintContent() {
	var WindowObject = window.open("", "PrintWindow","width=750,height=650,top=50,left=50,menubar=1,scrollbars=1,status=1,resizable=1");
	var items = PrintContent.arguments.length;
	WindowObject.document.writeln("<input type=\"button\" id=\"cmdPrint\" name=\"cmdPrint\" value=\"Print\" onclick=\"this.style.display='none';window.print();return false;\" />");
	for (i = 0;i < items;i++){
		var DocumentContainer = document.getElementById(PrintContent.arguments[i]);
		i++;
		var DocumentContainerCopyType = PrintContent.arguments[i];
		//alert(DocumentContainerCopyType);
		if (DocumentContainerCopyType == "clone") {
			clone = DocumentContainer.cloneNode(true);
			WindowObject.document.body.appendChild(clone);
		} else {
			WindowObject.document.writeln(DocumentContainer.innerHTML);
		}
		
	}
	WindowObject.document.close();
	WindowObject.focus();
}