//-----------------------------------------------------
// ©2007 Ecliptic Code, Inc. All rights reserved.
//-----------------------------------------------------

//-----------------------------------------------------
// Client Script Functionality
//-----------------------------------------------------
var DEBUG = true;
var UseTestCase = false;
var WOW = new Array();
var WOWOn = false;

if(!DEBUG) 
	window.onerror = null;
	
	
var useIE = false;
if(navigator.appVersion.indexOf('MSIE') > 0) useIE = true;
	
//Handle the OnKeyPress Event
document.onkeypress = HandleKeyPress;

/*
   Key Event Handler
*/
function HandleKeyPress(e)
{
   var keyCode;
   var keyTarget;
   
   if(!useIE) 
   {
      keyCode = e.keyCode;
      keyTarget = e.target;      
   }
   else
   {
      keyCode = window.event.keyCode;
      keyTarget = document.activeElement;
   }
   
   //alert(keyCode);
   
   /* Wow Movie clip Easter Egg
   try
   {
      if(keyCode == 27)
      {
         WOWOn = false;
         WOW = new Array();
         return;
      }
      
      if(keyCode == 47)
      {
         WOWOn = true;
         return;
      }
      
      WOW.push(keyCode);
      
      if(WOW.length == 3 && WOWOn)
      {
         if(WOW[0] == 119 && WOW[2] == 119 && WOW[1] == 111)
         {
           
            if(typeof(player) != "undefined")
            {            
               LayDownTheLaw();               
            }
            
            WOWOn = false;
            WOW = new Array();
            return;
         }
         
      }
      
      if(WOW.length > 10)
      {
         WOW = new Array();
         WOWOn = false;
      }
   }
   catch(ex)
   {
      alert(ex.message);
   }
   */
   
      
   try
   {  
   
      switch(keyCode)
      {
         case 13: //Enter
         
            //alert(keyTarget.id); 
            
            if(keyTarget.id.toLowerCase() == 'txtcomments' || keyTarget.id.toLowerCase() == 'txtc2comments') return true;
            if(keyTarget.id == 'ctl00_ContentPlaceHolder1_txtComments' || keyTarget.id == 'ctl00_ContentPlaceHolder1_txtComments') return true;
            if(keyTarget.id == 'ctl00_ContentPlaceHolder1_txtComment') return true;
            
            if(keyTarget.id.toLowerCase() == 'txtsitesearch') 
            {
               HandleSearch()
               return false;
            }
            
            if(keyTarget.id.toLowerCase() == 'txtzipcode')
            {
               HandleLocClick();
               return false;
            }
            
            if(useIE)
               keyTarget.click();
            else
            {
               var clickevent=document.createEvent("MouseEvents")
               clickevent.initEvent("click", true, true)
               keyTarget.dispatchEvent(clickevent);
            }
               
            return false; 
         break;
         
         case 113: //Q
            //StartTest();
         break;
         
         case 97: //A
         break;
         
         case 98: //B
         break;
         
         case 99: //C
         break;
         
         default:
            //alert('No Code');
            
         break;
      }   
     
   }
   catch(ex)
   {
      DebugAlert(ex.message);
   }
}




/*---------------------------------------
   Utility Functions
---------------------------------------*/

/*
   Launches content 
   
*/
function OpenWindow(url) 
{
	try 
	{						
		WindowSpecifications = "channelmode=no,directories=no,fullscreen=no,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=no,titlebar=yes,toolbar=yes";					
		window.open(url, "_blank", WindowSpecifications);		
	} 
	catch(ex) 
	{
	   DebugAlert("OpenContentWindow(): " + exception.message);
	} 		
}

/*
   Helper function for parsing URL parameters.
   Takes a parameter to search for and return the result, or null is parameter not found 
*/
function SearchForUrlParam(paramName) 
{
   try
   {
	   sArgs = unescape(location.search.slice(1).split('&'));
	   sArgs = sArgs.split(',');
	   	   
      for(cnt = 0; cnt < sArgs.length; cnt++)
      {
         sArgs2 = sArgs[cnt].split('=');
         value = sArgs2[1];
         name = sArgs2[0];
         
         if(name == paramName) return value;
      }
      
      return null;
    }
    catch(ex)
    {
      DebugAlert(ex.message);
      return '';
    }
}



/*
   debug alerts (traces)
*/
function DebugAlert(msg)
{
   if(DEBUG)
      alert(msg)
}


/*
   Takes in a string with '\n' and converts the '\n' to <br/>
*/
function stripNewLines(txt)
{
   var regex = /\\n/g;
   
   try
   {
      txt = txt.replace(regex, '<br/>');      
      return txt;
   }
   catch(ex)
   {
      DebugAlert(ex.message);
   }
}

/*
   Takes in a string with '\b' and converts the '\b' to <li>
*/
function stripBullets(txt)
{
   var regex = /\\b/g;
   
   try
   {
      txt = txt.replace(regex, '<li>');      
      return txt;
   }
   catch(ex)
   {
      DebugAlert(ex.message);
   }
}

/*
   Function Facade for the FormatString Function.
   If the param1 parameter is an array then it forwards to the array handling version of the FormatString functino
   If the param1 is not an array then processing goes to the four param value function handler.
*/
function FormatString(txt, param1, param2, param3, param4)
{  
   if(param1.constructor.toString().indexOf("Array") == -1)
      return FormatStringInternal1(txt, param1, param2, param3, param4);
   else   
      return FormatStringInternal2(txt, param1);
}

/*
   Helper function for replacing string values
   txt = string to modify (required)
   param1 = first param to replace (%1 will be replaced)
   param2 = second param to replace (%2 will be replaced) (optional)
   param3 = third param to replace (%3 will be replaced)(optional)
   
   example:
      var tst = "onclick='%1'";
      var method = FormatString(tst, "alert('hi')");
      
*/
function FormatStringInternal1(txt, param1, param2, param3, param4)
{
   var results, regex;
   
   try
   {
      //Replace the first param
      regex = /%1/g;      
      results = txt.replace(regex, param1);
      
      if(param2 != null && param2 != "")
      {
         regex = /%2/g;
         results = results.replace(regex, param2);
      }
         
      if(param3 != null && param3 != "")
      {
         regex = /%3/g;
         results = results.replace(regex, param3);
      }
      
      if(param4 != null && param4 != "")
      {
         regex = /%4/g;
         results = results.replace(regex, param4);
      }
      
      return results;
   }
   catch(ex)
   {
      DebugAlert(ex.message);
   }
}

/*
   Helper function for replacing string values
   txt = string to modify (required)
   params = an array of parameters to replace
   
   example:
      var tst = "onclick='%1' onmouseover='%2' onmouseoff='%3'";
      var params = {"alert('hi')","alert('on')","alert('off')"};
      var method = FormatString(tst, params);
      
*/
function FormatStringInternal2(txt, QueryParams)
{
   var results, regex;
   var cnt = QueryParams.length + 1;
   //regex = /%i/g;
   
   try
   {      
      for(i = 1; i < cnt; i++)
      {
         regex = eval("/%" + i + "/g;");                 
         txt = txt.replace(regex, QueryParams[i-1]);
      }    
      
      return txt;
   }
   catch(ex)
   {
      DebugAlert(ex.message);
   }
}


















