// ** 
// ** 
// **                 Common reusable functions.
// ** 
/*
	filterInput (input,the_type)
	limitInput(theobj,thesize)
	focus_form()
	enter_submit_form()
	
	test_email(the_str)
	test_phones(phone1,phone2)
	test_longPhone(thenum)
	check_password(theid,strength)
	
	isinlist(T_element,T_id)
	toggle_the_div(thediv)
	
	GEBI(theid): 	returns element
	$G(theid): 		returns value of element
	$G_DD(theid): 	returns selectedvalue of dropdown
	$G_DD_i(theid): 	returns selectedindex of dropdown
	$G_DD_t(theid): 	returns selectedtext of dropdown
	
	$S(theid,theval): 				sets value of element
	change_DD(the_object,the_value): 	changes selectedindex of dropdown by value
	$S_html(theid,thehtml): 			sets innerHTML of element
	
	
*/

//________________________________________________________________________________________ 
//________________________________________________________________________________________ Input validation

function filterInput (input,the_type) {
	var s = input.value;
	var RE;
	var RE_replace;
	var F_check = false;
	var F_replace = false;
	var returnString = "";
	var hasChanged = false;
	var dummy;
	
	switch (the_type) {
		case "REG_text":
			RE_replace = /[^a-z A-Z0-9.!?,;:'"()&-]/g;
			F_replace = true;
			break;
		case "REG_stricttext":
			RE_replace = /[^a-zA-Z0-9]/g;
			F_replace = true;
			break;
		case "REG_stricttextSpaces":
			RE_replace = /[^a-z A-Z0-9]/g;
			F_replace = true;
			break;
		case "REG_num":
			RE_replace = /[^0-9]/g;
			F_replace = true;
			break;
		case "Y-m-d":
			RE = /^[0-9]{4}\-[0-9]{1,2}\-[0-9]{1,2}$/g;
			F_check = true;
			break;
		case "REG_email":
			RE = /^([a-zA-Z0-9\_\.\-]+\@(([a-zA-Z0-9\-])+\.)+[a-zA-Z]{2,3})$/;
			F_check = true;
			break;
		case "REG_phone":
			RE = /^[0-9]{3}\s?[0-9]{3}\s?\-?\s?[0-9]{4}$/g;
			F_check = true;
			
			RE_replace = /[^0-9\s]/g;
			F_replace = true;
			break;
		default:
			RE = /^$/g;
			break;
	}
	
	if (F_check) {
		returnString = s;
		
		if ((!(RE.test(returnString))) && (returnString.length > 0))
			hasChanged = true;
	}
	
	if (F_replace) {
		if (s.search(RE_replace) != -1) {
			returnString = s.replace(RE_replace,'');
			hasChanged = true;
		}
		else {
			returnString = s;
			hasChanged = false;
		}
	}
	
	if ((the_type == "REG_num") && (returnString.length == 0)) {
		input.value = "0";
		hasChanged = true;
	}
	else {
		input.value = returnString;
	}
	
	input.style.backgroundColor = (hasChanged) ? "#FFFF99" : "#FFFFFF";
	input.style.border = "1px solid #CCC";
}

//________________________________________________________________________________________ 
//________________________________________________________________________________________ Limit characters in textarea

function limitInput(theobj,thesize) {
	var thestr = theobj.value;
	var thelen = theobj.value.length;
	
	if (thelen > thesize) {
		theobj.value = thestr.substring(0,thesize);
		theobj.style.backgroundColor = "#FFFF99";
	}
	else
		theobj.style.backgroundColor = "#FFFFFF";
}

//________________________________________________________________________________________ 
//________________________________________________________________________________________ Email validation

function test_email(the_str) {
	var emailFilter = /^([a-zA-Z0-9\_\.\-]+\@(([a-zA-Z0-9\-])+\.)+[a-zA-Z]{2,3})$/;
	
	return ((the_str == "") || (emailFilter.test(the_str)));
}

function test_email_strict(the_str) {
	var emailFilter = /^([a-zA-Z0-9\_\.\-]+\@(([a-zA-Z0-9\-])+\.)+[a-zA-Z]{2,3})$/;
	
	return (emailFilter.test(the_str));
}
//________________________________________________________________________________________ 
//________________________________________________________________________________________ Date validation

function test_date(the_str) {
	var dateFilter = /^([0-9]{4})\-([0-9]{1,2})\-([0-9]{1,2})$/;
	
	return ((the_str == "") || (dateFilter.test(the_str)));
}

//________________________________________________________________________________________ 
//________________________________________________________________________________________ Phone number validation

function test_phones(phone1,phone2) {
	var is_ok = true;
	
	if ((phone1.length != 0) && (phone1.length != 3))
		is_ok = false;
	else if ((phone2.length != 0) && (phone2.length != 4))
		is_ok = false;
	else if (((phone1.length == 0) && (phone2.length != 0))||((phone1.length != 0) && (phone2.length == 0)))
		is_ok = false;
	
	return is_ok;
}

function test_longPhone(thenum) {
	var is_ok = true;
	
	if ((thenum.length != 0) && (thenum.length != 7) && (thenum.length != 10) && (thenum.length != 11))
		is_ok = false;
	else if ((thenum.length == 11) && (thenum.charAt(0) != '1'))
		is_ok = false;
	
	return is_ok;
	
}

//________________________________________________________________________________________ 
//________________________________________________________________________________________ Change selected index of drop-down

function change_DD(the_object,the_value) {
	var not_found = true;
	var i_change_DD;
	
	for (i_change_DD = 0; ((i_change_DD < GEBI(the_object).length)&&(not_found)); i_change_DD++) {
		if (GEBI(the_object).options[i_change_DD].value == the_value) {
			GEBI(the_object).selectedIndex = i_change_DD;
			not_found = false;
		}
	}
}

//________________________________________________________________________________________ 
//________________________________________________________________________________________ Is In List 

function isinlist(T_element,T_id) {
	var ret = false;
	
	for (var i = 0;i < GEBI(T_id).length;i++) {
		if (GEBI(T_id).options[i].value == T_element) {
			ret = true;
			break;
		}
	}
	
	return ret;
}


//________________________________________________________________________________________ 
//________________________________________________________________________________________ Toggle .display 

function toggle_the_div(thediv) {
	GEBI(thediv).style.display = (GEBI(thediv).style.display != 'none')?'none':'block';
}


//________________________________________________________________________________________ 
//________________________________________________________________________________________ getElementById shorthand 

function GEBI(theid) {
	try {
		if (top.document.getElementById(theid)) {
			return top.document.getElementById(theid);
		}
		else if (top.document.all[theid])
			return top.document.all[theid];
	}
	catch (e) { return null; }
	
	return null;
}

//________________________________________________________________________________________ 
//________________________________________________________________________________________ Is Empty? 

function empty(theval) {
	return ((typeof(theval) === "undefined") || (theval == null) || (theval == '') || (theval.length == 0));
}

//________________________________________________________________________________________ 
//________________________________________________________________________________________ Manipulate/return value of element by ID 

//________________________________________ GETTERS

	//____________________ normal get value
	function $G(theid) {
		var T_dummy = "";
		
		try { T_dummy = GEBI(arguments[0]).value; } catch(e) {}
		return T_dummy;
	}

	//____________________ get value for drop down
	function $G_DD(theid) {
		var T_dummy = "";
		
		try { T_dummy = GEBI(theid)[GEBI(theid).selectedIndex].value; } catch(e) {}
		return T_dummy;
	}

	//____________________ get selected index of dropdown
	function $G_DD_i(theid) {
		var T_dummy = "";
		
		try { T_dummy = GEBI(theid).selectedIndex; } catch(e) {}
		return T_dummy;
	}

	//____________________ get selected text of dropdown
	function $G_DD_t(theid) {
		var T_dummy = "";
		
		try { T_dummy = GEBI(theid)[GEBI(theid).selectedIndex].text; } catch(e) {}
		return T_dummy;
	}

	//________________________________________ SETTERS
	//____________________ set value of element
	function $S(theid,theval) {
		try { GEBI(theid).value = theval; } catch(e) {}
		return true;
	}

	//____________________ set innerHTML of element
	function $S_html(theid,thehtml) {
		try { GEBI(theid).innerHTML = thehtml; } catch(e) {}
		return true;
	}


	//________________________________________ OTHER
	//____________________ set styles

	function $style(theid,the_attr,the_val) {
		eval("GEBI('" + theid + "').style." + the_attr + " = " + the_val + ";");
	}

//________________________________________________________________________________________ 
//________________________________________________________________________________________  Focus on 1st text element 

function focus_form() {
	var i = 0;
	var inputs = document.forms[0].getElementsByTagName("input");
	
	if (inputs.length > 0) {
		while (inputs[i].type != "text")
			i++;
		
		if (i < inputs.length)
			inputs[i].focus();
	}
}

//________________________________________________________________________________________ 
//________________________________________________________________________________________  makes the enter key submit the form 

function enter_submit_form(obj,the_event) {
	if ((window.event && window.event.keyCode == 13) || (the_event.which && the_event.which == 13))
		obj.submit();
	else
		return true;
}

//___ Specified function instead of form.submit()
function enter_submit_formFn(theFn,the_event) {
	if ((window.event && window.event.keyCode == 13) || (the_event.which && the_event.which == 13))
		eval("window." + theFn);
	else
		return true;
}

//________________________________________________________________________________________ 
//________________________________________________________________________________________  Check password for strength

function check_password(theid,strength) {
	var has_letters = false;
	var has_numbers = false;
	var thestrength = false;
	var has_specials = false;
	
	if ($G(theid).match(/([0-9])+/))
		has_numbers = true;
	if ($G(theid).match(/([a-z]|[A-Z])+/))
		has_letters = true;
	if ($G(theid).match(/[^a-zA-Z0-9]/))
		has_specials = true;
	
	var PWD_MINLENGTH = 6;
	
	switch (strength) {
		case "alphanumeric":
			thestrength = !has_specials && has_letters && ($G(theid).length >= PWD_MINLENGTH);
			break;
		case "alphanumeric_strict":
			thestrength = has_letters && has_numbers && !has_specials && ($G(theid).length >= PWD_MINLENGTH);
			break;
		case "strong":
			thestrength = has_letters && has_numbers && has_specials && ($G(theid).length >= PWD_MINLENGTH);
			break;
		default:
			break;
	}
	
	return thestrength;
}

//________________________________________________________________________________________ 
//________________________________________________________________________________________  Move background image
	function slide_bg(theobj,newval) {
		GEBI(theobj).style.backgroundPosition = newval;
	}

//________________________________________________________________________________________ 
//________________________________________________________________________________________  Offsets
	function get_winDimensions() {
		// Thanks to QuirksMode: http://www.quirksmode.org/viewport/compatibility.html
		var x,y;
		if (self.innerHeight) {
			// all except Explorer
			x = self.innerWidth;
			y = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight) {
			// Explorer 6 Strict Mode
			x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		}
		else if (document.body) {
			// other Explorers
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}
		
		return [x,y];
	}
	
	
	
	function get_pageOffset() {
		// Thanks to QuirksMode: http://www.quirksmode.org/viewport/compatibility.html
		var x,y;
		
		var test1 = document.body.scrollHeight;
		var test2 = document.body.offsetHeight;
		
		if (test1 > test2) {
			// all but Explorer Mac
			x = document.body.scrollWidth;
			y = document.body.scrollHeight;
		}
		else {
			// Explorer Mac;
			// would also work in Explorer 6 Strict, Mozilla and Safari
			x = document.body.offsetWidth;
			y = document.body.offsetHeight;
		}
		
		return [x,y];
	}
	
	
	function get_scrollOffset() {
		// Thanks to QuirksMode: http://www.quirksmode.org/viewport/compatibility.html
		var x,y;
		if (self.pageYOffset) {
			// all except Explorer
			x = self.pageXOffset;
			y = self.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop) {
			// Explorer 6 Strict
			x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
		}
		else if (document.body) {
			// all other Explorers
			x = document.body.scrollLeft;
			y = document.body.scrollTop;
		}
		
		return [x,y];
	}
	
	function get_centeredCoords(T_width,T_height) {
		var winDimensions = get_winDimensions();
		var scrollOffset = get_scrollOffset();
		
		return [((winDimensions[0] / 2) - (T_width / 2)),(scrollOffset[1] + ((winDimensions[1] / 2) - (T_height / 2)))];
	}

//________________________________________________________________________________________ 
//________________________________________________________________________________________  NV Fake WIN
	function show_NV_fakeWin_center(w,h) {
		var centeredCoords = get_centeredCoords(w,h);
		GEBI('NV_fakeWin').style.width = w + 'px';
		
		GEBI('NV_fakeWin').style.left = centeredCoords[0] + 'px';
		GEBI('NV_fakeWin').style.top = centeredCoords[1] + 'px';
		
		$style('NV_fakeWin','display',"'block'");
	}
	
		function show_NV_fakeWin_center_fixed(w,h) {
			var centeredCoords = get_centeredCoords(w,h);
			
			GEBI('NV_fakeWin').style.width = w + 'px';
			
			GEBI('NV_fakeWin').style.height = h + 20 + 'px';
			
			GEBI('NV_fakeWin').style.left = centeredCoords[0] + 'px';
			GEBI('NV_fakeWin').style.top = centeredCoords[1] + 'px';
			
			$style('NV_fakeWin','display',"'block'");
		}
	
	function close_NV_fakeWin() {
		GEBI('NVFW_content').innerHTML = '';
		
		$style('NV_fakeWin','display',"'none'");
	}

	
//________________________________________________________________________________________ 
//________________________________________________________________________________________  tooltip on hover
	
		function findPos(obj) {
			var curleft = curtop = 0;
			
			if (typeof(obj) == "string")
				obj = GEBI(obj);
			
			if (obj && obj.offsetParent) {
				curleft = obj.offsetLeft;
				curtop = obj.offsetTop;
				while (obj = obj.offsetParent) {
					curleft += obj.offsetLeft;
					curtop += obj.offsetTop;
				}
			}
			
			return [curleft,curtop];
		}
		
		var isIE = document.all && !window.opera;
		
		function show_tooltip(tooltipID,theobj,thetext,w,h) {
			var objOffset = findPos(theobj);
			var scrollOffset = get_scrollOffset();
			var winDimensions = get_winDimensions();
			
			var y_centeredWithObj = Math.floor((objOffset[1]) - (h / 2));
			var y_picBottom = Math.floor((objOffset[1]) + (h / 2));
			var y_winBottom = scrollOffset[1] + winDimensions[1];
			
			var true_offset = (y_picBottom > y_winBottom) ? (y_winBottom - h - 15) : ((y_centeredWithObj < scrollOffset[1]) ? (scrollOffset[1] + 15) : y_centeredWithObj);
			
			var T_left;
			
			if (((objOffset[0] + 20 + theobj.offsetWidth) + w) < winDimensions[0])
				T_left = objOffset[0] + 20 + theobj.offsetWidth;
			else
				T_left = objOffset[0] - 20 - w;
			
			GEBI(tooltipID).style.left = T_left + 'px';
			GEBI(tooltipID).style.top = true_offset + 'px';
			
			GEBI(tooltipID).innerHTML = thetext;
			$style(tooltipID,"display","'block'");
		}
		
		function hide_tooltip(tooltipID) {
			if (GEBI(tooltipID).innerHTML != '') {
				GEBI(tooltipID).innerHTML = '';
				$style(tooltipID,"display","'none'");
			}
		}
		
		function show_staticTooltip(tooltipID,theobj) {
			var w = parseInt(GEBI(tooltipID).style.width.replace("px",""));
			var h = parseInt(GEBI(tooltipID).style.height.replace("px",""));
			var objOffset = findPos(theobj);
			var scrollOffset = get_scrollOffset();
			var winDimensions = get_winDimensions();
			
			var y_centeredWithObj = Math.floor((objOffset[1]) - (h / 2));
			var y_picBottom = Math.floor((objOffset[1]) + (h / 2));
			var y_winBottom = scrollOffset[1] + winDimensions[1];
			
			var true_offset = (y_picBottom > y_winBottom) ? (y_winBottom - h - 25) : ((y_centeredWithObj < scrollOffset[1]) ? (scrollOffset[1] + 15) : y_centeredWithObj);
			
			var T_left;
			
			if (((objOffset[0] + 20 + theobj.offsetWidth) + w) < winDimensions[0])
				T_left = objOffset[0] + 20 + theobj.offsetWidth;
			else
				T_left = objOffset[0] - 20 - w;
			
			GEBI(tooltipID).style.left = T_left + 'px';
			GEBI(tooltipID).style.top = true_offset + 'px';
			
			$style(tooltipID,"display","'block'");
		}
		
		function hide_staticTooltip(tooltipID) {
			$style(tooltipID,"display","'none'");
		}