/*
checkform.js	v.  1.0.2
	Added more functions - getcss, cookie actions, instagate mobile

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();
}

//	load alternate CSS
function getcss(cssfile){
	loadcss = document.createElement('link')
	loadcss.setAttribute("rel", "stylesheet")
	loadcss.setAttribute("type", "text/css")
	loadcss.setAttribute("href", cssfile)
	document.getElementsByTagName("head")[0].appendChild(loadcss)
}

//	Cookie Actions
function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function instagate_mobile() {
	if (!Get_Cookie('fabco_mobile_disable')) {
		if (navigator.userAgent.match(/midp|blackberry|netfront|nokia|panasonic|portalmmm|sharp|sie-|sonyericsson|symbian|windows ce|benq/i) || navigator.userAgent.match(/mda|mot-|opera mini|philips|pocket pc|sagem|samsung|sda|sgh-|vodafone|xda|palm|iphone|ipod|android/i)) {
			if (screen.width < '1024' || screen.height < '768') {
				toggle_hidden('header','body_container','mobile_content');
				temp_body = document.getElementById('body_content').cloneNode(true);
				document.getElementById('body_content').innerHTML = '';
				getcss('/style_handheld.css');
				document.getElementById('mobile_content').appendChild(temp_body);				
				document.getElementsByName('viewport')[0].setAttribute('content', 'initial-scale=.40;');
			}
		}
	}	
}

function disable_mobile(){
	if (confirm("Mobile redirection will be disabled until your web browser is restarted!\n\nContinue?")){
		Set_Cookie('fabco_mobile_disable', 'true', '', '/','','');
		window.location.reload();
	}
	return false;
}
