/**
* GLOBALS
*/ 
var isIE = (-1 != navigator.userAgent.indexOf('MSIE')) ? true : false;

/**
* window.onload event handler
*/
function init()
{
    focusFirstElement();
    addTrackingToExternalLinks();
}

/**
*   Hooks up external links to be tracked
*/
function addTrackingToExternalLinks()
{
    if ('undefined' == typeof document.getElementsByTagName) return;
    // else
    
    var ahrefs = document.getElementsByTagName("a");
    var ilen = ahrefs.length;
    var host = location.hostname;
    try 
    {
        for (var i = 0; i < ilen; i++)
        {
            var a = ahrefs[i];
            var hrf = new String(a.href).toLowerCase();
            if (('http' == hrf.substr(0, 4)) && (-1 == hrf.indexOf(host)))
            {
                // only track external and non-javascript links
                a.onclick = trackExternalLink;
            }
        }
    } catch(e) { }
}

/**
*   Onclick handler for external links so they can be tracked
*/
function trackExternalLink()
{
    try
    {
        if ('undefined' == typeof this.href) return;
        
        var rand = Math.random();   // appended to the querystring prevent caching
        var oImg = new Image();
        oImg.src = '/js/ExternalLinkTracking.ashx?link=' + esc(this.href) + '&rand=' + esc(rand);
    } catch(e) { alert(e.message); }
}

/**
*   Onclick handler for external links in flash so they can be tracked
*/
function trackFlashExternalLink(href)
{
    try
    {
        var rand = Math.random();   // appended to the querystring prevent caching
        var oImg = new Image();
        oImg.src = '/js/ExternalLinkTracking.ashx?link=' + esc(href) + '&rand=' + esc(rand);
    } catch(e) { }
}

/**
*   Browser dependant url escape function
*/
function esc(url)
{
    if ('function' != typeof encodeURIComponent)
        return escape(url);
    else
        return encodeURIComponent(url);
}

/**
* Sets the focus to the first active form element
*/
function focusFirstElement()
{
    if(null == document.forms[0]) return;
    // else
    var ilen = document.forms[0].length;
    for(var i = 0; i < ilen; i++)
    {
        try {
            var el = document.forms[0][i];
            if(el.disabled) continue;
            if('hidden' == el.type) continue;
            // else
            
            el.focus();
            break;
        } catch(e) {
            //if(isIE) alert(e.message);
            //else alert(e);
        }
    }
}

/**
*   Scrolls the given element into view
*/
var g_times = 0;
function scrollToElement(id)
{
    var el = document.getElementById(id);
    if ((null != el)&&('object' == typeof el))
    {
        if ('undefined' != typeof el.scrollIntoView)
        {
            el.scrollIntoView();
        }
    }
    else
    {
        if(++g_times < 10)
            setTimeout('scrollToElement("' + id + '")', (222 * g_times));
    }
}

/**
*    Clears a textbox if its value matches the value of defaultText
*/
function clearIf(txtBox, defaultText)
{
    if ('object' != typeof txtBox) return;
    // else
    if (txtBox.value == defaultText)
    {
        txtBox.value = '';
    }
}

/**
*   Validates the CuteEditor without requiring a postback to occur
*/
function validateEditor(source, args)
{
    if ('object' != typeof source) return;
    // else
    var sId = source.id.replace('custom','txt').replace(/_/ig,'$');
    var el = document.forms[0][sId];
    if ((null != el)&&('object' == typeof el))
    {
        if (el.value.isEmpty())
        {
            args.IsValid = false;
        }
    }
}


/**
*   Loads the specified movie into the specified html element as a flash movie
*/
function loadMovie(sMoviePath, sId, sWidth, sHeight, sMinFlash, sBackgroundColor, sIdToLoadInto, bTransparentMode, sVariables)
{
    try 
    {
        if (null == document.getElementById(sIdToLoadInto)) return;
        // else
        var so = new SWFObject(sMoviePath, sId, sWidth, sHeight, sMinFlash, sBackgroundColor);
        so.addParam("quality", "high");
        so.addParam("menu", "false");
        so.useExpressInstall('/swf/expressinstall.swf');

        if (bTransparentMode)
            so.addParam("wmode", "transparent");
        
        // check for variables and add them if they exist
        if ((null != sVariables) && (sVariables.trim() != ''))
        {
	        if (-1 != sVariables.indexOf('&'))
	        {
	            var values = sVariables.split('&');
	            var len = values.length;
	            for(var i = 0; i < len; i++)
	            {
	               var keypair = values[i].split('=');
	               so.addVariable(keypair[0],keypair[1]);
	            }
            }
            else
            {
	        	var keypair = sVariables.split('=');
	        	so.addVariable(keypair[0],keypair[1]);    
	        }
        }
        so.write(sIdToLoadInto);
    } 
    catch(e) 
    { 
//        if (isIE) alert(e.message);
//        else alert(e);
    }
}

function showObj(o)
{
    if ('object' != typeof o) return;
    var str = '';
    for(x in o) str += x + ' is ' + o[x] + '\n';
    alert(str);
}

/**
*   Used by testimonials to display images
*/
function showImg(src)
{
    var oImg = new Image();
    oImg.src = src;
    var wd = (parseInt(oImg.width) + 50);
    var ht = (parseInt(oImg.height) + 75);
    var opts = 'resizable=1,top=0,left=0,width=250,height=250,scrollbars=0,status=0,' + 
        'location=0,menubar=0,toolbar=0,address=0';
            
    var winx = null;
    try 
    {
        winx = window.open('','imgWin',opts);
        if (null != winx)
        {
            var sHTML = '<html><head><title>Crocs, Inc.</title></head><body style="margin:0px">' + 
                '<p style="text-align:center">' +
		        '<img onload="window.resizeTo(this.width + 30, this.height + 90)" src="' + src + 
		        '" border="0" /></p></body></html>';
            winx.document.writeln(sHTML);
            winx.document.close();
            winx.focus();
        }
    } catch(e) { }
}

/** 
*   when assigned to the onkeypress event for a textarea it
*   limits the amount of characters that a textarea can hold
*/
function limit(oTextArea, iMaxChars)
{
	if("object" != typeof(oTextArea)) return false;
	if(isNaN(parseInt(iMaxChars))) return;
	// else
	if(oTextArea.value.trim().length >= parseInt(iMaxChars))
	{
		oTextArea.value = oTextArea.value.trim().substring(0,iMaxChars);
		alert("Only " + iMaxChars + " characters may be entered for this field!");
		if(isIE) { window.event.returnValue = false; window.event.cancelBubble = true; }
		return false;
	}
}

// Strip whitespace from the beginning and end of a string 
if ("undefined" == typeof String.prototype.trim) 
{    
    String.prototype.trim = function() 
    {        
	    return this.replace(/(^\s*)|(\s*$)/g, "");
    }
}
// Determines if string is null or empty
if ("undefined" == typeof String.prototype.isEmpty)
{
    String.prototype.isEmpty = function()
    {
        var s = new String(this);
        if('' == s.trim()) return true;
        else return false;        
    }
}
// Enumerates the error object
if ("undefined" != typeof Error)
{
    if ("undefined" == typeof Error.prototype.report)
    {
        Error.prototype.report = function()
        {
            var str = '';
            for(var x in this)
            {
                if (-1 != x.indexOf('report')) continue;
                str += x + ' is ' + this[x] + '\n';
            }
            alert(str);
        }
    }
}