// All javascript functions to be shared should go in here or this folder


//Submit forms
function setForm(action,url) {
	if (action) {
		if (action == 'save') {
			loading(3);
			if (document.ContactForm) {
				combPhone();
				combFax();
			}
			document.forms[0].submit();
		}
		else if (action == 'nothanks') {
			javascript:jQuery.facebox({ajax:'core/nothanks.jsp'});
		}
		else {
			loading();
			window.location = url;
		}
	}
}

//Preload loader image
img1 = new Image();
img1.src = "img/loader.gif";

//Get window dimensions
var getWindowSize = function(){ 
	var size = {
		width: window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth),
		height: window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight),
		scrollheight: document.body.scrollHeight
	};
	return size;
};

// Append example offers
function setOffer(obj) {
	$("#offerText").val($("#" +obj).html());
	$("#offerMsg").html("Effective Offer Has Replaced Existing Offer");
	$.facebox.close();
}
// Call example offer categories
function setOfferCat(cat1,cat2,cat3) {
	var url = "baseAccount/offerExamples.jsp";
	if (cat1) url = url + "?cat1=" +cat1;
	if (cat2) url = url + "&cat2=" +cat2;
	if (cat3) url = url + "&cat3=" +cat3;
	
	jQuery.facebox({ajax:url})
}

// Loading message
function loading(m) {
	//Set message
	var msg = "Please Wait";
	$("#loading").css({left:"827px"});
	if (m == 0) {
		msg = "Loading";
		$("#loading").css({left:"0"});
	}
	if (m == 1) {
		msg = "Loading";
		$("#loading").css({left:"851px"});
	}
	if (m == 2) {
		msg = "Processing";
		$("#loading").css({left:"834px"});
	}
	
	//Reset window scroll position
	window.scrollTo(0,0);
	
	//Set background shade
	var winsize = getWindowSize();
	var wsize = winsize.scrollheight;
	if (m == 3) {
		wsize = "100%";
		$(document).ready(function() {
			$("#jqifade").hide();
			$("#loading").css({visibility:"hidden"});
			$(".catselects").removeAttr("disabled");
		});
	}
	//Show background shade
	$("#jqifade").css({position:"absolute", zIndex:998, height:wsize, display:"none", opacity:0.5, width:"100%", top:0, left:0, right:0, bottom:0}).fadeIn("slow");
	//Show loading message
	$("#loading").html(msg + ' <img src="img/loader.gif" />').css({visibility:"visible"});
}

function killLoad() {
	$("#jqifade").fadeOut("slow");
	$("#loading").css({visibility:"hidden"});
}

//Open link in parent window and close the child window
function updateParent(newURL,closeWin) {
	opener.document.location=newURL;
	opener.focus();
	if (closeWin == "true") {
		window.close();
	}
}

function toggleHelp(whichBox) {
	if (document.getElementById(whichBox).style.display=="none") {
		document.getElementById(whichBox).style.display="block";
	}
	else {
		document.getElementById(whichBox).style.display="none";
	}
}

// Function -- confirmation panel for delete button
function shouldDelete() {
	confirmBox=confirm("Are you sure you want to delete this item?");
	if (confirmBox==true) {
	return true;
	} else {
	return false;
	}
}

function confirmReplyDelete() {
	confirmBox=confirm("You have elected to delete this reply. Are you sure?");
	if (confirmBox==true) {
		return true;
	} else {
		return false;
	}
}

function showLayer(name) {
	var layerToShow = document.getElementById(name);
	layerToShow.style.display="block";
}

function hideLayer(name) {
	var layerToHide = document.getElementById(name);
	layerToHide.style.display="none";
}

function checkForUpdateOnly() {
	if (document.forms[0] != null && document.forms[0].elements['updateInfo']) {
		checkContact();
	}
}

var isNN = (navigator.appName.indexOf( "Netscape" ) != -1); 
 
function autoTab(input,len,e) {
	var keyCode	= (isNN) ? e.which : e.keyCode; 
	var filter	= (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46]; 
	if (input.value.length >= len && !containsElement(filter, keyCode)) { 
		input.value = input.value.slice(0,len);
		if (input.form[(getIndex(input) + 1) % input.form.length].value == '') {
			input.form[(getIndex(input) + 1) % input.form.length].focus();
		} else {
			input.form[(getIndex(input) + 1) % input.form.length].select();
		}
	}
	return true; 
} 
 
function containsElement(arr,ele) {
	var found = false, index = 0;
	while (!found && index < arr.length)
	if (arr[index] == ele) {
		found = true; 
	} else {
		index++; 
	} 
	return found; 
} 
 
function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
	if (input.form[i] == input) {
		index = i; 
	} else {
		i++; 
	} 
	return index; 
} 

// Character Limit Validation
function textCounter(field,countfield,maxlimit) {
	if (field.value.length > maxlimit)
	field.value = field.value.substring(0,maxlimit);
	else 
	countfield.value = maxlimit - field.value.length;
}

function selectThis(whereami) {alert(whereami);}

function policyPopup(theURL) {
	var origWidth=screen.availWidth;
	var	origHeight=screen.availHeight;
	var newWidth=600;
	var newHeight=600;
	var leftPos = (origWidth - newWidth)/2;
	var topPos = (origHeight - newHeight)/2;
	window.open(theURL,'policies','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=' + newWidth + ',height=' + newHeight + ',top=20, left=' + leftPos).focus();
}

function centerPopup2(theURL) {
	var origWidth=screen.availWidth;
	var	origHeight=screen.availHeight;
	var newWidth=origWidth - 100;
	var newHeight=origHeight - 100;
	var leftPos = (origWidth - newWidth)/2;
	var topPos = (origHeight - newHeight)/2;
	window.open(theURL,'','toolbar=yes,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=' + newWidth + ',height=' + newHeight + ',top=20, left=' + leftPos).focus();
}

function centerPopup(theURL,width,height) {
	if (width == null) {width = 650} if (height == null) {height = 500}
	var origWidth=screen.availWidth;
	var	origHeight=screen.availHeight;
	var newWidth=width;
	var newHeight=height;
	var leftPos = (origWidth - newWidth)/2;
	var topPos = (origHeight - newHeight)/2;
	window.open(theURL,'','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=' + newWidth + ',height=' + newHeight + ',top=20, left=' + leftPos).focus();
}

function sessionTimeout(theURL,time) {
	var urlCheck = window.location;
	if (location.href.match(theURL) == null) {
		setTimeout("window.location='"+theURL+"?sessionTimeout=true'", time);
	}
}

/* toggleField - Description:
		Show and hide form field.
 */
function toggleField(objectID) {
	var obj = document.getElementById(objectID).style;
	if (obj.visibility == 'visible') {obj.visibility = 'hidden'}
	else {obj.visibility = 'visible'}
}

/* Show Hint script 
	© Dynamic Drive (www.dynamicdrive.com)
*/
var horizontal_offset="7px" //horizontal offset of hint box from anchor link
var vertical_offset="0" //vertical offset of hint box from anchor link
var ie=document.all
var ns6=document.getElementById&&!document.all
function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}
function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}
function clearbrowseredge(obj, whichedge){
var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_offset)*-1 : parseInt(vertical_offset)*-1
if (whichedge=="rightedge"){
var windowedge=ie && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-30 : window.pageXOffset+window.innerWidth-40
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure+obj.offsetWidth+parseInt(horizontal_offset)
}
else{
var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure-obj.offsetHeight
}
return edgeoffset
}
function showhint(menucontents, obj, e, tipwidth, nohide){
if ((ie||ns6) && document.getElementById("hintbox")){
dropmenuobj=document.getElementById("hintbox")
dropmenuobj.innerHTML=menucontents
dropmenuobj.style.left=dropmenuobj.style.top=-500
if (tipwidth!=""){
dropmenuobj.widthobj=dropmenuobj.style
dropmenuobj.widthobj.width=tipwidth
}
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+obj.offsetWidth+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+"px"
dropmenuobj.style.visibility="visible"
if (nohide != 'true') {obj.onmouseout=hidetip}
}}
function hidetip(e){
dropmenuobj.style.visibility="hidden"
dropmenuobj.style.left="-500px"
}
function createhintbox(){
var divblock=document.createElement("div")
divblock.setAttribute("id", "hintbox")
document.body.appendChild(divblock)
}
if (window.addEventListener)
window.addEventListener("load", createhintbox, false)
else if (window.attachEvent)
window.attachEvent("onload", createhintbox)
else if (document.getElementById)
window.onload=createhintbox