﻿//this allows you to preload images like this:
//preload('image1.gif','image2.gif');
var imageCache = [];
function preloadCommonImages() {
	for (x = 0; x < preloadCommonImages.arguments.length; x++)
	{
	    var cacheImage = document.createElement('img');
	    cacheImage.src = imgCommonpath + arguments[x];
	    imageCache.push(cacheImage);
	}
}
var themeImageCache = [];
function preloadThemeImages() {
	for (x = 0; x < preloadThemeImages.arguments.length; x++) {
	    var cacheImage = document.createElement('img');
	    cacheImage.src = imgpath + arguments[x];
	    themeImageCache.push(cacheImage);
	}
}

//this prevents the window from scrolling to the top on validation
window.scrollTo = function(){}

//for masking input
$(function($){
   applyMasks();
});

function applyMasks() {
  
	$(".phone:not(.masked)").addClass('masked').mask("(999) 999-9999",{placeholder:" "});
	$(".zipcode:not(.masked)").mask("99999",{placeholder:" "}).addClass('masked');
	$(".ssn:not(.masked)").mask("999-99-9999",{placeholder:" "}).addClass('masked');
	$(".ein:not(.masked)").mask("99-9999999",{placeholder:" "}).addClass('masked');
	$(".date:not(.masked)").mask("99/99/9999", { placeholder: " " }).addClass('masked');
}

function fadeMessageBox() {
    $('#divErrorPanel').fadeOut(3000);
}


function popupWin(url, name, width, height, scrollbars)
{
	var win = window.open(url, name, "width=" + width + ",height=" + height + ",scrollbars=" + scrollbars + ",toolbar=no,addressbar=no,resizable=yes,menubar=no");
	if(win){ win.focus(); }
}


//***********************************************************************************************
//***********************************************************************************************
// methods for collapsing and expanding sections
//***********************************************************************************************
//***********************************************************************************************
function SectionVisibility_SetControls(PersistStateTextBox, ToggleButton, DivName){
	// this method is called by SectionVisibility_Toggle and sets the link text, button src, and visibility of the DIV	
	if(document.getElementById(DivName)){ //make sure the div is visible
		var tbox = document.getElementById(PersistStateTextBox);
		var state = tbox.value;	    
		
		var btn = $("#"+ToggleButton);
		
		//alert(DivName + ': ' + state);
		//$('#'+DivName).slideToggle(400);
		
		if(state=='show'){
			//document.getElementById(DivName).style.display = 'block';
			//animatedcollapse.toggle(DivName);
			//btn.src = btn.src.replace('down','up');
			btn.attr("title", 'Hide this section');
			btn.removeClass('buttonExpand')
			btn.addClass('buttonCollapse');
			$('#' + DivName).addClass('showMe');
			$('#' + DivName).removeClass('hideMe');
		}else{
			//document.getElementById(DivName).style.display = 'none';
			//animatedcollapse.toggle(DivName);
			//btn.src = btn.src.replace('up','down');
			//btn.setAttribute('title', 'Expand this section');
			btn.attr("title", 'Expand this section');
			btn.removeClass('buttonCollapse')
			btn.addClass('buttonExpand');
			$('#' + DivName).addClass('hideMe');
			$('#' + DivName).removeClass('showMe');
		}
		
		//set the cursor back to default in case we had set it to the wait-cursor
		//document.body.style.cursor = 'default';
		//btn.style.cursor = 'pointer';
	}
		
}

function SectionVisibility_Toggle(PersistStateTextBox, ToggleButton, DivName){
	//Instead of just toggling the controls in response to the click,
	//we'll set a flag in a hidden field. Then we'll call a method which
	//will toggle the controls based on the flag value.
	//This way, the visibility state can be persisted after a post-back.
	var tbox = document.getElementById(PersistStateTextBox);
	
	if(tbox.value=='show'){
		tbox.value='hide';
		setCookie(PersistStateTextBox, 'hide', null, null, null, false);				
	}else{
		tbox.value='show';
		setCookie(PersistStateTextBox, 'show', null, null, null, false);			
	}	
	SectionVisibility_SetControls(PersistStateTextBox, ToggleButton, DivName);
}

//***********************************************************************************************
//***********************************************************************************************
// methods for working with cookies via client-side code
//***********************************************************************************************
//***********************************************************************************************
/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {
	//if no expiry was passed, set it to a year from now
	if(expires == null){
		expires = new Date();
		expires.setHours(expires.getHours() + (365 * 24));
	}
	document.cookie= name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function toggleCompsView(view)
{
	var summ_display = '';
	var detail_display = '';
	
	if(view == 'detail')
	{
		summ_display = 'none';
		detail_display = '';
		
		$get('divCompsViewLink1').style.display = 'none';
		$get('divCompsViewLink2').style.display = '';
	}
	else
	{
		summ_display = '';
		detail_display = 'none';
		
		$get('divCompsViewLink1').style.display = '';
		$get('divCompsViewLink2').style.display = 'none';
	}
	
	var tbl = $get('tblComps');
	var cells = tbl.getElementsByTagName("td");
	
	for (var i=0;i<cells.length;i++) 
	{
		if(cells[i].className == "summary")
		{
			cells[i].style.display = summ_display;
		}
		else if(cells[i].className == "detail")
		{
			cells[i].style.display = detail_display;
		}
	}
}

function toggleNewTransSection(div, toggleLink, togSpeed)
{
	var speed = getToggleSpeed(togSpeed, div);

	$('#' + div).slideToggle(speed, function ()
	{
		if($('#'+toggleLink).html() == '-')
		{
			$('#'+toggleLink).html('+');
		}
		else
		{
			$('#'+toggleLink).html('-');
			runOptionalCodeOnNewTransSectionOpen(div);
		}
	});
}
function closeNewTransSection(div, toggleLink, togSpeed)
{
	var speed = getToggleSpeed(togSpeed, div);

	$('#'+div).slideUp(speed,function(){
		$('#'+toggleLink).html('+');		
	});
}
function openNewTransSection(div, table, toggleLink, togSpeed)
{
	var speed = getToggleSpeed(togSpeed, div);

	$('#'+table).removeClass('newTransHeader_disabled');
	$('#'+table).addClass('newTransHeader');

	$('#' + div).slideDown(speed, function ()
	{
		$('#'+toggleLink).html('-');
		$('#'+toggleLink).removeAttr('disabled');
		$('#' + toggleLink).attr("href", "javascript:toggleNewTransSection('" + div + "','" + toggleLink + "'," + speed + ");");
		runOptionalCodeOnNewTransSectionOpen(div);			
	});
}
function getToggleSpeed(passedInSpeed, div)
{
	var speed = passedInSpeed;
	if ($("input[id$='" + div + "_toggleSpeedOverride']").length > 0)
	{
		var val = $("input[id$='" + div + "_toggleSpeedOverride']").val();
		if (val && val != '')
		{
			speed = parseInt(val);
		}
	}

	return speed;
}
function runOptionalCodeOnNewTransSectionOpen(divName)
{
	var funcExists = eval('typeof ' + divName + '_optionalCode == \'function\'');
	if(funcExists == true)
	{
		eval(divName + '_optionalCode();');
	}
}

function JQscroll(obj, offset) {
	if (location.pathname.replace(/^\//, '') == obj.pathname.replace(/^\//, '') && location.hostname == obj.hostname) {
		var jQuerytarget = jQuery(obj.hash);
		jQuerytarget = jQuerytarget.length && jQuerytarget || jQuery('[name=' + obj.hash.slice(1) + ']');
		if (jQuerytarget.length) {
			var targetOffset = jQuerytarget.offset().top + offset;
			jQuery('html,body').stop().animate({ scrollTop: targetOffset }, 500, 'swing');
			return false;
		}
	}
}

function addMarginPublicLogin() {
	if ($("#pageIDWrapper")) {
		$("#pageIDWrapper").attr('style', 'margin-top:15px;')
	}
}

function removeMarginPublicLogin() {
	if ($("#pageIDWrapper")) {
		$("#pageIDWrapper").attr('style', 'margin-top:0px;')
	}
}

String.repeat = function(chr,count){        
	var str = "";     
	for(var x=0;x<count;x++) {str += chr};     
	return str;
}

String.prototype.padL = function(width,pad){    
	if (!width ||width<1)        
		return this;        
	if (!pad) 
		pad=" ";            
		var length = width - this.length    
	if (length < 1) 
	return this.substr(0,width);     
	return (String.repeat(pad,length) + this).substr(0,width);    
}    

String.prototype.padR = function(width,pad){    
	if (!width || width<1)        
	return this;             
	if (!pad) pad=" ";
	var length = width - this.length
	if (length < 1) 
	this.substr(0, width);
	return (this + String.repeat(pad, length)).substr(0, width);
}

Date.prototype.formatDate = function (format)
{
	var date = this;

	if (!format)
		format = "MM/dd/yyyy";

	var month = date.getMonth() + 1;
	var year = date.getFullYear();

	if (format.indexOf("MM") > -1)
	{
		format = format.replace("MM", month.toString().padL(2, "0"));
	}
	else
	{
		format = format.replace("M", month.toString());
	}

	if (format.indexOf("yyyy") > -1)
		format = format.replace("yyyy", year.toString());
	else if (format.indexOf("yy") > -1)
		format = format.replace("yy", year.toString().substr(2, 2));

	if (format.indexOf("dd") > -1)
	{
		format = format.replace("dd", date.getDate().toString().padL(2, "0"));
	}
	else
	{
		format = format.replace("d", date.getDate().toString());
	}

	var hours = date.getHours();

	if (format.indexOf("t") > -1)
	{
		if (hours > 11)
			format = format.replace("t", "PM")
		else
			format = format.replace("t", "AM")
	}

	if (format.indexOf("HH") > -1)
		format = format.replace("HH", hours.toString().padL(2, "0"));

	if (format.indexOf("h") > -1)
	{
		if (hours > 12)
			hours = hours - 12;

		if (hours == 0)
			hours = 12;

		if (format.indexOf("hh") > -1)
		{
			format = format.replace("hh", hours.toString().padL(2, "0"));
		}
		else
		{
			format = format.replace("h", hours.toString());
		}
	}

	if (format.indexOf("mm") > -1)
		format = format.replace("mm", date.getMinutes().toString().padL(2, "0"));

	if (format.indexOf("ss") > -1)
		format = format.replace("ss", date.getSeconds().toString().padL(2, "0"));

	return format;
}

function hoverTags(toggle, id) {
    if (toggle) {
        $("#tagThis_" + id).hide();
        $("#hoverThis_" + id).show()//append("<div class=\"myTag floatLeft myTag1\"><div class=\"floatLeft removeItem\"><a href=\"#\"><img src=\"../img/contacts/remove_item.gif\"  /></a></div><div class=\"tagLeft floatLeft\"></div><div class=\"tagMiddle floatLeft txtContactGray1\" style=\"width:auto;\"><div class=\"floatLeft\" >" + mainTag.innerHTML + "</div></div><div class=\"tagRight floatLeft\"></div></div>")
    } else {
        $("#hoverThis_" + id).hide();
        $("#tagThis_" + id).show();
    }
}


