//--------------------------------------------------
// ObjectCode Framework by The Found Corporation
//--------------------------------------------------
    /* @date: 01-16-2007
     * @author: cho
     * @notes: common jscript inclde file
     ***********************************************/

var host_url = 'http://www2.stuntgrowth.com/';

//--------------------------------------------------
// Detect Browsers
//--------------------------------------------------
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
/***************************************************
 * BrowserDetect.browser
 * BrowserDetect.version
 * BrowserDetect.OS
 ***************************************************/

//--------------------------------------------------
// Get XML HTTP Request Object
//--------------------------------------------------
function getXmlHttpRequestObject() {
    /* Create a new XMLHttpRequest object to talk to the Web server */
    var xmlHttp = false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        xmlHttp = false;
      }
    }
    @end @*/
    
    if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
      xmlHttp = new XMLHttpRequest();
    }
    return xmlHttp;
}

//--------------------------------------------------
// Do XML Request
//--------------------------------------------------
function doRequest(url,ajax_tagid) 
{
    if (receiveReq.readyState == 4 || receiveReq.readyState == 0) 
    {       
        try {
            receiveReq.open("GET", url, true);
            
           receiveReq.onreadystatechange=function() {
               if (receiveReq.readyState==4) {
               
            	if (ajax_tagid == 'content')
            	ajax_tagid = 'trigger';
            	
                Element.update(ajax_tagid,receiveReq.responseText);
               }
            }
            receiveReq.send(null); 
        }
        catch (e) {
            alert("xml:http request failure to url "+url);
        }
    }
}

//--------------------------------------------------
// Do LONG XML Request
//--------------------------------------------------
function doLongRequest(ajax_url,ajax_params,ajax_tagid) 
{
    if (receiveReq.readyState == 4 || receiveReq.readyState == 0) 
    {       
        try {
            receiveReq.open("POST", ajax_url);
            receiveReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            receiveReq.send(ajax_params); 
            
            receiveReq.onreadystatechange=function() {
               if (receiveReq.readyState==4) {
               
            	if (ajax_tagid == 'content')
            	ajax_tagid = 'trigger';
            	
                Element.update(ajax_tagid,receiveReq.responseText);
               }
            }
        }
        catch (e) {
            alert("xml:http request failure to url "+url);
        }
    }
}


//--------------------------------------------------
// Handle Name and Value XML Pairs
//--------------------------------------------------
function complainIE(name)
{
    switch(name)
    {
        case 'head':
            var msg = "<h1 style='color:#00FF00;text-decoration:blink;'> <a href='http://www.mozilla.com/en-US/firefox/' style='color:#00FF00;text-decoration:blink;' target='_blank'><span style='color:#00FF00;text-decoration:blink;'> &nbsp;&nbsp;Upgrade to FireFox (HIGHLY RECOMMENDED)</span></a></h1>";
            return msg;
        break; 
        case 'alert':
            if (BrowserDetect.browser == 'Explorer')
            {
            var msg = 'ATTENTION: You are using Internet Explorer '+BrowserDetect.version+
                      "\n\n"+'It is HIGHLY recommended that you upgrade to FireFox and use it'+
                      ' as your default web-browser.'+"\n\n"+'Found Corp. suggests this to all clients'+
                      ' because Internet Explorer currently has many open security vulnerabilities which'+
                      ' can be exploited by mailicious script writers.  Since this admin utility gives an authenticated user full administrative control over this website, it would be a very bad thing if the login to this website got into the wrong hands.  '+
                      ' Internet viruses are currently circulating for Internet Explorer that may make it possible for someone to compromise its data; and by doing so, also compromise the security of this website.'+
                      "\n\n"+'The features of this admin tool will work normally until you upgrade, but we would really appreciated it if you did!';
            alert (msg);
            }
            return;
        break;
    }
}

//--------------------------------------------------
// Handle Name and Value XML Pairs
//--------------------------------------------------
function handleData(name,value)
{
   var update = true;
   switch(name)
   {
       case 'content':
           return value;
       break; 
   }
   if(update) update_element(name,value);
   return true;
}

//--------------------------------------------------
// Handle Receiving Response Text for IE
//--------------------------------------------------
function handleReceiveRequest(url,ajax_tagid)
{
    //  0      Uninitated
    //  1      Loading
    //  2      Loaded
    //  3      Interactive
    //  4      Complete
    
    if (receiveReq.readyState == 4)
    {
            if (receiveReq.responseText)
                textResponse = receiveReq.responseText;
            else
                alert("No XML Response was received");
            
            Element.update(ajax_tagid,textResponse);
            return textResponse;
    } 
return false;
mTimer = setTimeout('Element.update(ajax_tagid,textResponse)',10000);
}

//--------------------------------------------------
// Handle Receiving XML Request for IE
//--------------------------------------------------
function handleXMLReceiveRequest(url)
{
    //  0      Uninitated
    //  1      Loading
    //  2      Loaded
    //  3      Interactive
    //  4      Complete
    
    if (receiveReq.readyState == 4)
    {
        var xmldoc = receiveReq.responseXML.documentElement;
        var root_node = xmldoc.getElementsByTagName('root').item(0);
        var nodes     = root_node.childNodes;
        var n         = nodes.length;
       
        for (i = 0; i < n; i++)
        {
            var node  = nodes[i];
            var name  = node.nodeName;
            var value = '';
            if(node.firstChild) value = node.firstChild.nodeValue;
            //alert(name + '=' + value);
            handleData(name,value);        
        }        
    return;
    } 
//mTimer = setTimeout('handleReceiveRequest()',10000);
}

//--------------------------------------------------
// Get Element by ID
//--------------------------------------------------
function getElement(id)
{
      var element = 0;
      if(document.getElementById)
      {
          element = document.getElementById(id);
      }
      else if (document.all)
      {
          element = document.all[id];
      }
      return element;
}

//--------------------------------------------------
// Ajax Update Content - Edit Mode
//--------------------------------------------------

var receiveReq = getXmlHttpRequestObject();

function updateContent(tagid,fname)
{
	var ajax_tagid = tagid;
	var ajax_fname = fname;
	
	if (ajax_tagid == 'content')
	{
	var inst = tinyMCE.getInstanceById('content');
	var ajax_content = inst.getHTML();
	}
	else
	{
	var fieldname = "content-" + ajax_tagid;
	var ajax_content = document.getElementById(fieldname).value;
	}
	
    /********************************************************************
    * Note: ie6+ ie7 do not support Ajax.Updater as of prototype 1.4.0
    * ... the xmlhttprequest is sent, but no response code is pulled.
    * so alas, until there is support for this class, we have to
    * fool ie into refreshing the updated tag by.
    *
    * what sucks even more about this whole situation is that the
    * innerHTML property doesn't work properly in ie when sending it
    * fresh html to insert.
    *********************************************************************/
    
	var ajax_url = '/cms_update_ajax';
	var ajax_params = 'file=' + ajax_fname + '&tag=' + ajax_tagid + '&content=' + escape(ajax_content);
   
    doLongRequest(ajax_url,ajax_params,ajax_tagid);
    
	if (ajax_tagid == 'content')
	ajax_tagid = 'trigger';
    
    handleReceiveRequest(ajax_url+'?'+ajax_params,ajax_tagid);
    

}

//--------------------------------------------------
// Ajax Update Microcart
//--------------------------------------------------

function updateCart(tagid,fpage)
{
	var ajax_tagid = tagid;
	var ajax_fpage = fpage;
	
    /********************************************************************
    * Note: ie6+ ie7 do not support Ajax.Updater as of prototype 1.4.0
    * ... the xmlhttprequest is sent, but no response code is pulled.
    * so alas, until there is support for this class, we have to
    * fool ie into refreshing the updated tag by.
    *
    * what sucks even more about this whole situation is that the
    * innerHTML property doesn't work properly in ie when sending it
    * fresh html to insert.
    *********************************************************************/
    if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version >=6)
    {
    	var url = host_url + 'microcart_ajax/?div=' + ajax_tagid + '&page=' + ajax_fpage;
       
            doRequest(url,'edit-light-c01');
            handleReceiveRequest(url,'edit-light-c01');
    } 
    else 
    {
	    var url = '/microcart_ajax/?div=' + ajax_tagid + '&page=' + ajax_fpage;
    
    	var myAjax = new Ajax.Updater(
    	    'edit-light-c01', 
    		url, 
    		{
    		    asynchronous:true,
    		    evalScripts:true,
    			method: 'post', 
    			onComplete: showResponse,
    			onFailure: reportError
    		});
    }
}

//--------------------------------------------------
// Ajax Update Microcart Summary Screen
//--------------------------------------------------

function updateCartSummary(tagid,fpage)
{
	var ajax_tagid = tagid;
	var ajax_fpage = fpage;
	
    /********************************************************************
    * Note: ie6+ ie7 do not support Ajax.Updater as of prototype 1.4.0
    * ... the xmlhttprequest is sent, but no response code is pulled.
    * so alas, until there is support for this class, we have to
    * fool ie into refreshing the updated tag by.
    *
    * what sucks even more about this whole situation is that the
    * innerHTML property doesn't work properly in ie when sending it
    * fresh html to insert.
    *********************************************************************/
    if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version >=6)
    {
    	var url = host_url + 'microcart_ajax/?div=' + ajax_tagid + '&page=' + ajax_fpage;
       
            doRequest(url,'shoppingcart');
            handleReceiveRequest(url,'shoppingcart');
    } 
    else 
    {
	    var url = '/microcart_ajax/?div=' + ajax_tagid + '&page=' + ajax_fpage;
    
    	var myAjax = new Ajax.Updater(
    	    'shoppingcart', 
    		url, 
    		{
    		    asynchronous:true,
    		    evalScripts:true,
    			method: 'post', 
    			onComplete: showResponse,
    			onFailure: reportError
    		});
    }
}

//--------------------------------------------------
// Ajax Update Image Verification
//--------------------------------------------------

function updateImgVerify()
{
    if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version >=6)
    {
    	var url = '/geo';
       
            doRequest(url,'imgverify');
            handleReceiveRequest(url,'imgverify');
    } 
    else 
    {
	    var url = host_url + 'geo';
    
    	var myAjax = new Ajax.Updater(
    	    'imgverify', 
    		url, 
    		{
    		    asynchronous:true,
    		    evalScripts:true
    		});
    }
}


//--------------------------------------------------
// Ajax Show Response
//--------------------------------------------------
function showResponse(req)
{
    //alert('RESPONSE:'+req.responseText);
}

//--------------------------------------------------
// Ajax Report Error
//--------------------------------------------------
function reportError(request)
{
	//alert('Sorry. There was an AJAX System Response error.');
}

//--------------------------------------------------
// Make Layer Vis/InVisible
//--------------------------------------------------
function toggleLayer(layer,visi)
{
    if(document.getElementById) document.getElementById(layer).style.visibility = visi;
    else if (document.all) document.all[layer].style.visibility = visi;
    else if (document.layers) document.layers[layer].visibility = visi;
}

//--------------------------------------------------
// Swap Image Restore
//--------------------------------------------------
function MM_swapImgRestore() 
{ 
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

//--------------------------------------------------
// Preloader
//--------------------------------------------------
function MM_preloadImages() 
{
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

//--------------------------------------------------
// Find Objects
//--------------------------------------------------
function MM_findObj(n, d) 
{
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

//--------------------------------------------------
// Swap Image
//--------------------------------------------------
function MM_swapImage() 
{
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//--------------------------------------------------
// Auto Jump between form fields
//--------------------------------------------------

var downStrokeField;
function autojump(fieldName,nextFieldName,fakeMaxLength)
{
var myForm=document.forms[document.forms.length - 1];
var myField=myForm.elements[fieldName];
myField.nextField=myForm.elements[nextFieldName];

if (myField.maxLength == null)
   myField.maxLength=fakeMaxLength;

myField.onkeydown=autojump_keyDown;
myField.onkeyup=autojump_keyUp;
}

function autojump_keyDown()
{
this.beforeLength=this.value.length;
downStrokeField=this;
}

function autojump_keyUp()
{
if (
   (this == downStrokeField) && 
   (this.value.length > this.beforeLength) && 
   (this.value.length >= this.maxLength)
   )
   this.nextField.focus();
downStrokeField=null;
}