﻿// JScript File

var mapVisible = true;
    
// pbw - will show a Call to Action element
function callToAction()
{
    document.getElementById("CallToAction").style.display = "block";
    var els = document.getElementsByName("CallToAction");
      
    for (var i = 0; i < els.length; i++)
    {
	  els[i].style.display = "block";   
	}
		
	document.location = "#CallToAction";	    
}
   
// open printer dialog
function myprint() {
        window.frameElement.frame;
        window.print();
}


 function textboxMultilineMaxNumber(txt,maxLen){  
 try{  
     if(txt.value.length > (maxLen-1))
     return false;  
    }
   catch(e){}  
                                               }  
 
// Adds query string parameters to an array
var qsParm = new Array();
function qs()
{
    var query = window.location.search.substring(1);
    var parms = query.split('&');
   
    for (var i=0; i<parms.length; i++) {
        var pos = parms[i].indexOf('=');
       
        if (pos > 0)   {
             var key = parms[i].substring(0,pos);
            var val = parms[i].substring(pos+1);
            qsParm[key] = val;
        }
    }
} 

function sprintf() {
	if (!arguments || arguments.length < 1 || !RegExp)	{
	  return;
	}
	
	var str = arguments[0];
	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str))
	{
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];

		//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

		numMatches++;
		if (pType == '%')
		{
			subst = '%';
		}
		else
		{
			numSubstitutions++;
			if (numSubstitutions >= arguments.length)
			{
				alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
			}
			var param = arguments[numSubstitutions];
			var pad = '';
					if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
				else if (pPad) pad = pPad;
			var justifyRight = true;
					if (pJustify && pJustify === "-") justifyRight = false;
			var minLength = -1;
					if (pMinLength) minLength = parseInt(pMinLength);
			var precision = -1;
					if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
			var subst = param;
					if (pType == 'b') subst = parseInt(param).toString(2);
				else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
				else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
				else if (pType == 'u') subst = Math.abs(param);
				else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
				else if (pType == 'o') subst = parseInt(param).toString(8);
				else if (pType == 's') subst = param;
				else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
				else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
		}
		str = leftpart + subst + rightPart;
	}
	return str;
}


//show waiting dialog
function wait_show(msg, targetDiv) {
	document.getElementById(targetDiv).innerHTML = '<b>' + msg + '</b><img width=20 height=20 src="../../images/waiting.gif">';
	document.getElementById(targetDiv).style.display="block";
}

// hide the waiting dialog
function wait_hide() {
	document.getElementById("waitingdialog").style.display="none";
}

//pbw - This implementation works for both IE and FF
// see: http://www.devguru.com/technologies/xmldom/QuickRef/node_replaceChild.html
function showHideMap()
{        
	    // Show/Hide the map	    	    
        //showHide("MapContainer");
                
	    var map = document.getElementById("MapContainer");
	    map.style.display = !mapVisible ? "block" : "none";
	    mapVisible = !mapVisible;
	    
	    //lnkLabel = mapVisible ? "Hide Map" : "Show Map";
	    
	    // Get the link reference to update the innerText
	    //var link = document.getElementById("LnkDisplayMap");
	    
	    // Replace the link text
	    //var newNode=document.createTextNode(lnkLabel);
	    
        //link.replaceChild(newNode, link.lastChild);   
        
         // Change the show/hide button
        var hideLink = document.getElementById("ImgDisplayMap");
        
        if (hideLink)
            hideLink.src = !mapVisible ? "../../images/buttons/showmap_btn.gif" : "../../images/buttons/hidemap_btn.gif" ;               
        
        showHide("mapHiddenMessage");     
 }

function showHideStyle(divName, display)
{
        // Get the map reference
	    var theDiv = document.getElementById(divName);
	    
	    // Show/hide the div
	    if (theDiv.style.display == "none")
	    {
	        theDiv.style.display = display;
	    }
	    else
	    {
	        theDiv.style.display = "none";
	    }
 }
function showHide(divName)
{
        // Get the map reference
	    var theDiv = document.getElementById(divName);
	    
	    // Show/hide the div
	    if (theDiv.style.display == "inline")
	    {
	        theDiv.style.display = "none";
	    }
	    else
	    {
	        theDiv.style.display = "inline";
	    }
 }

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

// Format a phone number (###) ###-####
function formatPhone(phone){
    
        try 
        {        
            var re= /\D/;
            // test for this format: (xxx)xxx-xxxx
            var re2 = /^\({1}\d{3}\)\d{3}-\d{4}/; 
            // test for this format: xxx-xxx-xxxx
            //var re2 = /^\d{3}-\d{3}-\d{4}/;



            var newNum;
             if (phone != "" && re2.test(phone)!=true)
             {
                 if (phone != "")
                 {
                   while (re.test(phone)){
                       phone = phone.replace(re,"");
                   }
                 }
             
                 // for format (xxx)xxx-xxxx
                 newNum = '(' + phone.substring(0,3) + ') ' + phone.substring(3,6) + '-' + phone.substring(6,10);
                     // for format xxx-xxx-xxxx
                     // newNum = num.substring(0,3) + '-' + num.substring(3,6) + '-' + num.substring(6,10);
                 return newNum;
            }
       }
       catch (e) 
       { // Just return what was passed in
            return phone;
       }
     }
    
     function showPopup(id)
     {
     
      var popup = document.getElementById(id);
      
      if (popup.style.visibility == "visible")          
        popup.style.visibility = "hidden";
 
      else      
        popup.style.visibility = "visible";      
     }
     
    // LStrip text from the beginning of the string
    function LStrip(text, removeText)
    {
        var pos = text.toUpperCase().indexOf(removeText.toUpperCase());
        
        if (pos != -1)  // Found it
            return text.substring(pos + removeText.length);
        else 
            return text; // as is
    }
    
    function Open_Window(page) 
    {
        var options="toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=480, height=490, top=170, left=430";
        window.open(page,"",options);
    }
      
    function popUp(filename, width, height) {
		var page = filename;
		var windowprops = "height=" + height + ",width=" + width + ",location=no,scrollbars=yes,menubar=no,toolbar=no,resizable=yes";
		var popup = window.open(page, "PopUp", windowprops);
	}

	function popUpNoScroll(filename, width, height) {
		var page = filename;
		var windowprops = "height=" + height + ",width=" + width + ",location=no,scrollbars=no,menubar=no,toolbar=no,resizable=yes";
		var popup = window.open(page, "PopUp", windowprops);
	}
	
	function isZip(s) 
    { 
   
         if (s == "")
            return true;
            
        // Check for correct zip code format       
         //see: http://geekswithblogs.net/MainaD/archive/2007/12/03/117321.aspx
         var reZip = new RegExp("[0-9]{5}(-[0-9]{4})?|[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d?");         
         if (!reZip.match(s)) {              
              return false;
         }
 
        return true;
    }
    
    function printPage() {
        window.print();  
    }
    
    //01/02/08 Josh added for cybersam drop down.
    function gotoLink(form) {
        var OptionIndex=form.LstArchives.selectedIndex;
        parent.location=form.LstArchives.options[OptionIndex].value;
    }

//01/04/08 Wade added for Help Control.
    var closeDiv ="";
    function showAnswer(openDiv)
    {
        if (closeDiv.length > 0)
            document.getElementById(closeDiv).style.display = 'none';
        document.getElementById(openDiv).style.display = 'block';
        closeDiv = openDiv;
    }

//HELP CONTROL FUNCTIONS:
function findPos(obj)
{
    var curleft = curtop = 0;
    if (obj.offsetParent)
    {
        do { curleft += obj.offsetLeft;
             curtop += obj.offsetTop;
        }while (obj = obj.offsetParent); 
    }
    return [curleft,curtop]; 
}

function setPos()
{
    var helpText = document.getElementById('helpText');
    var helpDiv = document.getElementById('helpDiv');

    helpDiv.style.left = (findPos(helpText)[0] - 400) + 'px';
    helpDiv.style.top = (findPos(helpText)[1] + 22) + 'px';
  
}

//function init(){}

function openAd(url)
{
    var page = url;
	var winProps = "height=400,width=440,location=no,top=0,scrollbars=yes,menubar=yes,toolbar=no,resizable=yes";
	var theWin = window.open(page, "xWindow", winProps);

}
