// similar to PHP function, removes slashes from strings i.e. I can\'t find a string
function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}


/*
 * Popup window script 
 *  
 * Example markup 
 * <a href="mypage.html" onclick="popUpWin(this.href,'console',660,520);return false;" title="Open popup window">Link text</a>
 */

var newWindow = null;
function closeWin(){
	if (newWindow != null){
		if(!newWindow.closed)
			newWindow.close();
	}
}

function popUpWin(url, type, strWidth, strHeight){
	closeWin();
	if (type == "fullScreen"){
		strWidth = screen.availWidth - 10;
		strHeight = screen.availHeight - 160;
	}
	var tools="";
	if (type == "standard" || type == "fullScreen") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";
	if (type == "console") tools = "resizable,toolbar=no,location=no,scrollbars=yes,width="+strWidth+",height="+strHeight+",left=0,top=0";
	newWindow = window.open(url, 'newWin', tools);
	newWindow.focus();
}


/*
 * Unobtrusive external links 
 *  
 * Example markup 
 * <a href="mypage.html" rel="external" title="Take me to my link">Link text</a>
 */

$(document).ready( function() {
	// find all containers with the imageAdjust class and center images horizonally and vertically
	$(".imageAdjust").each(function(i) {
		var heightDiff = $(this).find("img").height() - $(this).height();
		var widthDiff = $(this).find("img").width() - $(this).width();
		
		if(heightDiff > 0) {
			$(this).find("img").css("marginTop",-heightDiff/2);
		}

		if(widthDiff > 0) {
			$(this).find("img").css("marginLeft",-widthDiff/2);
		}
    });
	
	$("a[rel=newWin]").click(function() {
		window.open( $(this).attr('href') );
		return false;
	}); 

	$("#searchtext").bind("keydown", function() {
		var searchstring = $(this).val().toLowerCase();
		$("div#faqs div").hide();
		$("div#faqs div").filter(":contains("+searchstring+")").show();
	});

	$("#searchtext").bind("blur", function() {
		var searchstring = $(this).val().toLowerCase();
		$("div#faqs div").hide();
		$("div#faqs div").filter(":contains("+searchstring+")").show();
	});	
});

/*
 * Form validation functions
 * 
 */
 
 
// Form Validation
//browser detection
var strUserAgent = navigator.userAgent.toLowerCase(); 
var isIE = strUserAgent.indexOf("msie") > -1; 
var isNS6 = strUserAgent.indexOf("netscape6") > -1; 
var isNS4 = !isIE && !isNS6  && parseFloat(navigator.appVersion) < 5; 

function GetMyObjectByID(itemID)
{
// In the future will need to build in the ability to handle v4 netscape browsers.
	if (document.getElementById) // Good browsers
		return document.getElementById(itemID)
	else if (document.all) // medium browsers
		return document.all[itemID];
		
	// Need to handle v4 browsers in the future.
}
function CheckField(itemID)
{
	var o = GetMyObjectByID(itemID);
	if(o.value.length<1)
		return false;
	else
		return true;
}

function CheckFieldLength(itemID, pType, pLen)
{
	var o = GetMyObjectByID(itemID);

	if(pType == ">")
	{
		return (o.value.length >= pLen)
	}
	else
	{
		return (o.value.length <= pLen)
	}	
}

function CheckDD(itemID)
{
	var o = GetMyObjectByID(itemID);
	if(o.selectedIndex < 0)
		return false;
		
	var select = o.options[o.selectedIndex].value;
	
	if(select.length<1 || select == "0")
		return false;
	else
		return true;
}
function CheckCheckBox(itemID)
{
	var o = GetMyObjectByID(itemID);
	if(!o.checked)
		return false;
	else
		return true;
}
function CheckRadio(itemID)
{	
	//var o = GetMyObjectByID(itemID);
	var o = document.forms[0][itemID];
	
	var cnt = -1;
	for (var i=0; i < o.length; i++) {
		if (o[i].checked) {
	   	cnt = i; 
		i = o.length;
	   }
	}
	if (cnt > -1) 
		return true;
	else 
		return false;
}

function CheckEmail(itemID) 
{
	var o = GetMyObjectByID(itemID);	
	if (o != null)
	{	
		// Make sure we let an email go through that is empty... The required field catcher will get that
		if (o.value.length < 1)
		{
			return false;
		}
		var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;		
		if (!regex.test(o.value))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}
function isSpecialKey(strValue){
// Returns true if one of these special key codes is pressed
//backspace Ctrl + C,enter, Ctrl + X, Ctrl + V, Ctrl + Z
var reKeyboardChars = /[\x00\x03\x08\x0D\x16\x18\x1A]/;
	return reKeyboardChars.test(strValue);
}

/*
To filter the max length here is some example code 
to catch both key down and to catch paste events
onkeypress="return FilterMaxLength(event,this,250)" onpaste="return CheckPaste(this,250)"
*/
function FilterMaxLength(e,o,max){
// we must allow spaces , backspace etc to go through 
	var iKeyCode, strKey;  
	
	if (isIE) {
		iKeyCode = e.keyCode;
	} else {
		iKeyCode = e.which;
	}	
	strKey = String.fromCharCode(iKeyCode);	
	// Make sure we let special keys go through.
		if (isSpecialKey(strKey))
			return true;	
		var maxLength = max;
     	if(o.value.length > maxLength-1){
	 		alert('You are only able to make a maximum payment of $99,999. Please reduce the amount and try again');
        	return false;
     	}
    	return true
	}
function CheckPaste(o,max){
	var maxLength = max;
	// Get from clipboard
	var data = window.clipboardData.getData("Text");

	var CurrentLength = o.value.length + data.length;
    if (CurrentLength > maxLength)
    {
		alert('You are only able to make a maximum payment of $99,999. Please reduce the amount and try again');
		return false;
	}
	else
		return true;
}
//mask input to allow integer only
function CheckNumeric(objEvent) {
	var iKeyCode, strKey;  
	var reValidChars = /\d/;	
	var reKeyboardChars =       /[\x00\x03\x08\x0D\x16\x18\x1A]/;
	if (isIE) {
		iKeyCode = objEvent.keyCode;
	} else {
		iKeyCode = objEvent.which;
	}
	
	strKey = String.fromCharCode(iKeyCode);
	
	if (!reValidChars.test(strKey) && !reKeyboardChars.test(strKey)) {
		return false;
	}
}

function CheckComparison(item1ID, item2ID)
{

	var o = GetMyObjectByID(item1ID);
	var o2 = GetMyObjectByID(item2ID);

	if(o.value.length==0)
		return false;
		
	if (o.value != o2.value)
	{
		return false;
	}
	else
	{
		return true;
	}
}



function bookmarkCurrentPage() {
title = document.title; 
url = location.href;

if (window.sidebar) { // Mozilla Firefox Bookmark
	window.sidebar.addPanel(title, url,"");
} else if( window.external ) { // IE Favorite
	window.external.AddFavorite( url, title); }
else if(window.opera && window.print) { // Opera Hotlist
	return true; }
} 

/****************** EMAIL DECRYPTION *********************/
function decrypt_string(crypted_string) {

	var numbers = crypted_string.split(' ');			
	n = numbers[0];	decryption_key = numbers[1];			
	numbers[0] = ""; numbers[1] = "";				
	crypted_string = numbers.join(" ").substr(2);


	var decrypted_string = '';
	var crypted_characters = crypted_string.split(' ');
	var i;
	
	for(i = 0; i< crypted_characters.length; i++) {
		var current_character = crypted_characters[i];
		var decrypted_character = exponentialModulo(current_character,n,decryption_key);
		decrypted_string += String.fromCharCode(decrypted_character);
	}
	


	return decrypted_string;
}


// Finds base^exponent % y for large values of (base^exponent)
function exponentialModulo(base,exponent,y) {
	if (y % 2 == 0) {
		answer = 1;
		for(var i = 1; i <= y/2; i++) {
			temp = (base*base) % exponent;
			answer = (temp*answer) % exponent;
		}
	} else {
		answer = base;
		for(var i = 1; i <= y/2; i++) {
			temp = (base*base) % exponent;
			answer = (temp*answer) % exponent;
		}
	}
	return answer;
}


function sendEmail(dat)
{
    document.location.href='mailto:' + do_decrypt ( dat );
}


function do_decrypt(dat)
{ 
	return decrypt_string ( dat, 0, 0 ,'');
}
