/**
*	window.onload event handler
*/
function init()
{				
	fancyRules();	// replace hr
	
	var upsellMsg = (document.getElementById) ? document.getElementById('upsellMsg') : null;
	var welcomeMsg = (document.getElementById) ? document.getElementById('welcomeMsg') : null;
	
	if (null != upsellMsg)
	{
		if ('' == upsellMsg.innerHTML.toLowerCase().trim())
			upsellMsg.style.display='none';
	}
	if (null != welcomeMsg)
	{
		if ('welcome' == welcomeMsg.innerHTML.toLowerCase().trim())
			welcomeMsg.style.display='none';
		else
			welcomeMsg.style.display='inline';
	}
}

/**
*	replaces hr with div tags for fancy css styling
*/
function fancyRules() 
{ 
	if (!document.getElementsByTagName) return;  
	var hr = document.getElementsByTagName("hr");   
	for (var i=0; i<hr.length; i++) 
	{           
		var newhr = hr[i]; 
		var wrapdiv = document.createElement('div');    	
		wrapdiv.className = 'line';                     
		newhr.parentNode.replaceChild(wrapdiv, newhr);  
		wrapdiv.appendChild(newhr);                     
	}
} 

/**
*   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");
        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 (-1 != navigator.userAgent.indexOf('MSIE')) alert(e.message);
        else alert(e);
    }
}

/**
*	Calls loadMovie with a slight delay to account for IE issues
*/
function loadMovieDelayed(sMoviePath, sId, sWidth, sHeight, sMinFlash, sBackgroundColor, sIdToLoadInto, bTransparentMode, sVariables)
{
	var del = "loadMovie('" + sMoviePath + "','" + sId + "','" + sWidth + "','" + sHeight + "','" + sMinFlash + "','" + sBackgroundColor + "','" + sIdToLoadInto + "'," + bTransparentMode + ",'" + sVariables + "')";
	setTimeout(del, 999);
}

/**
*	window.open which defeats/notifies-of pop-up blockers
*/
function popWin(path, name, options)
{
	try 
	{
		var winx = window.open(path, name, options);
		if (null == winx)
			alert('Please disable your pop-up blocker!');
		else
			winx.focus();
	} catch(e) {}
	return 0;
}

/**
*	Sets a flash variable
*/
function setFlashVar(varName, varValue)
{
	if (navigator.appName.indexOf ("Microsoft") !=-1) 
	{
		movie = window["products"]
	} 
	else 
	{
		movie = document["products"]
	}
	movie.SetVariable(varName, varValue);
	movie.Play();
}


/** 
*	determines whether a product is completely sold out or not
*/
function isProductSoldOut()
{
	if (!document.getElementById) return false;
	// else
	
	var hdn = document.getElementById('hdnQuanMsg');
	if (('object' != typeof hdn) || (null == hdn)) return false;
	// else
	
	try {
		var vals = hdn.value.split('*');
		var iqty = 0;
		var len = vals.length;
		for(var i = 0; i < len; i++)
		{
			var keys = vals[i].split('|');
			var qty = parseInt(keys[keys.length - 1]);
			iqty += ((isNaN(qty)) ? 0 : qty);
		}
		return (iqty == 0);
	} catch(e) {
		//if (-1 != navigator.userAgent.indexOf('MSIE')) alert(e.message);
		//else alert(e);
		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 whether a given string contains an email address
if ('undefined' == typeof String.prototype.isEmail)
{
	function isEmail() 
	{
		var emailAddress = new String(this);
		var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		return re.test(emailAddress);
	}
	String.prototype.isEmail = isEmail;
}
