// rolovers
var rollOvers = new Array();
    
function imgRollOver(src,width,height,id)
{

    var i = src.indexOf(".",src.length-5);
    var srcOver = src.substr(0,i) + "_over" + src.substr(i);

    var s = '<img src="' + src + '"'
        + ' align="absmiddle"' 
        + ' border="0"' 
        + ' onMouseOver="this.src=\'' + srcOver + '\'"'
        + ' onMouseOut="this.src=\'' + src + '\'"';

    if (width)
    {
        s += ' width="' + width + '"';
    }

    if (height)
    {
        s += ' height="' + height + '"';
    }

    if (id)
    {
        s+= ' id="' + id + '"';
    }

    s += '>';


    rollOvers[rollOvers.length] = new Image();
    rollOvers[rollOvers.length-1].src = srcOver;
    rollOvers[rollOvers.length-1].src2 = src;
    rollOvers[rollOvers.length-1].id = id;

    document.write(s);
}

function imgOverById(id,src)
{
    if (!src)
    {
        var i;
        for (i=0; i<rollOvers.length; i++)
        {
            if (rollOvers[i].id == id)
            {
                src = rollOvers[i].src;
            }
        }
    }

    var img = document.getElementById(id);
    if (img)
    {
        img.src = src;
    }
}

function imgOutById(id,src)
{
    if (!src)
    {
        var i;
        for (i=0; i<rollOvers.length; i++)
        {
            if (rollOvers[i].id == id)
            {
                src = rollOvers[i].src2;
            }
        }
    }

    var img = document.getElementById(id);
    if (img)
    {
        img.src = src;
    }
}

function showBiger(path, width, height, title, top, left)
{
	if (width=='') width=16;
	if (height=='') height=16;

	width = width*1;
	height = height*1;

    if (!title) title = path;
    if (!top) top = 150;
    if (!left) left = 250;
    width += 30;
    height += 60;

    // scroll if neaded 
    var margin = 100;
    var scroll = "no";
    if (((window.screen.width-margin<width) || (window.screen.height - margin<height)))
    {
        scroll = "yes";
        if (window.screen.width - margin < width) width = window.screen.width - margin;
        if (window.screen.height - margin < height) height = window.screen.height - margin;
    }

	if( path.charAt(0) != '/' )
	  path = '/' + path;

    var rand = Math.random();
    window.showModalDialog("/products/modalphoto.xml?rand=" + rand + "&image=" + path + "&title=" + title, "",
      "top: " + top + "px;left: " + left + "px;dialogWidth:" + width + "px;dialogHeight:" + height + "px;status:0;help:0;scroll:" + scroll);
}

function PopupWindow(url,width,height)
{
	_PopupWindow(url,width,height,150);
}

function _PopupWindow(url,width,height,top)
{
	var center_x = (screen.width - width)/2;
	var center_y = (screen.height - height)/2;	
    var num = Math.round(Math.random()*10000000);
    var str = new String(num);
    var win = window.open(url,str,"toolbar=no,location=no,directories=no,"+
      "status=no,menubar=no,scrollbars=no,resizable=no,"+
      "copyhistory=no,left="+center_x+",top="+center_y+",width="+width+",height="+height);
}

function _PopupWindow2(url,width,height,top)
{
	var center_x = (screen.width - width)/2;
	var center_y = (screen.height - height)/2;	
    var num = Math.round(Math.random()*10000000);
    var str = new String(num);
    var win = window.open(url,str,"toolbar=no,location=no,directories=no,"+
      "status=yes,menubar=yes,scrollbars=yes,resizable=no,"+
      "copyhistory=no,left="+center_x+",top="+center_y+",width="+width+",height="+height);
}

function _PopupWindow3(url,width,height,top)
{
	var center_x = (screen.width - width)/2;
	var center_y = (screen.height - height)/2;	
    var num = Math.round(Math.random()*10000000);
    var str = new String(num);
    var win = window.open(url,str,"toolbar=no,location=no,directories=no,"+
      "status=no,menubar=no,scrollbars=yes,resizable=no,"+
      "copyhistory=no,left="+center_x+",top="+center_y+",width="+width+",height="+height);
}

function selectInterests(form, select){
	for( i = 0; i < form.categories.length; i++ ){
		form.categories[ i ].selected = select;
	}
	return false;
}

var defaultSelected = new Array();

function clearOptions(form)
{
	selectInterests(form, false);
	var opts = form.categories.options;
	for (i = 0 ; i < opts.length; i++)
	{
		for (j = 0; j < defaultSelected.length; j++)
		{
			if (form.categories[i].value == defaultSelected[j])
			{
				form.categories[i].selected = true;
				break;
			}
		}
	}
}

// Rolovers Caching ------------------------------------------------------------
var images = Array();
images[0] = new Image(4,4);

function addToFavorites()
{
	if(navigator.appName.indexOf('Microsoft') >= 0 || navigator.appName.indexOf('MSIE') >= 0)
    {
    	window.external.AddFavorite( location.href, document.title );
	} 
	else 
	if(document.layers) 
	{
    	window.alert( 'Please click OK then press Ctrl+D to create a bookmark' );
	} 
	else 
	{
	    window.alert( 'Please use your browser\'s bookmarking facility to create a bookmark' );
	}
	return false;
}

// BMI Calculator
function formatNumber(expr, decplaces) 
{
	var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));

	while (str.length <= decplaces) 
	{
		str = "0" + str;
	}
	var decpoint = str.length - decplaces;
	return str.substring(0,decpoint) + 
		"." + 
		str.substring(decpoint, str.length);

} 

function calculateBMI(form)
{
	var w = form.weight.value;
	var h = form.height.value;

	var american = false;
	
	if (form.system.value == 'am')
		american = true;
	
	if (!american)
		h /= 100;
	
	var gender = 'Male';//form.gender[form.gender.selectedIndex].value;
	
	if (w == null || h == null || h == 0) 
		return false;
	
	var bmi = (w / h / h);
	
	if (american == true)
		bmi *= 703;
				
	form.bmi.value = formatNumber(bmi,2);
	form.desc.value = getDescription(bmi, gender);
}

function getDescription(bmi,gender)
{
	if (bmi < 20)
		return 'Underweight';
		
	if (bmi >= 20 && bmi <= 25)
		return 'In Normal Range';

	if (bmi > 25 && bmi < 29.9)
		return 'Overweight';

	if (bmi >= 30 && bmi < 35)
		return 'Obese';

	if (bmi >= 35 && bmi < 40)
		return 'Severly Obese';

	if (bmi >= 40)
		return 'Morbidly Obese';
}

function runBMICalc(form)
{
	if (checkInput(form.weight, 'Please enter your weight') && checkInput(form.height, 'Please enter your height'))
		calculateBMI(document.bmiform); 
	return false;
}

FAILED_INPUT_BACKGROUND_COLOR = "#FEDDBC";

function checkInput(input,message)
{
    if (input.value.length==0) 
    {
        focusFailedInput(input,message);
        return false;
    }
    return true;
}

function checkTextInput(input,message)
{
	var i = 0;
	for (i = 0; i < input.value.length; i++)
	{
		if (isNumber(input.value.charAt(i)))
		{
	        focusFailedInput(input,message);
	        return false;
	    }
	}
    return true;
}

function isNumber(x)
{
	return '0123456789'.indexOf(x) != -1;
}

function checkSelect(input,message)
{
    if (input.selectedIndex==0) 
    {
        focusFailedInput(input,message);
        return false;
    }
    return true;
}

function focusFailedInput(input, message)
{
    input.oldOnBlur = input.onblur;
    input.oldBackgroundColor = input.style.backgroundColor;
    input.style.backgroundColor = FAILED_INPUT_BACKGROUND_COLOR;
    if (message) alert(message);
    input.onblur = onAfterBlurFailedInput; 
    input.focus();
}

function markFailedInput(input)
{
    input.style.border = "1px solid red";
}

function checkEmail(input,message)
{
    if (!_checkEmail(input.value)) 
    {
        focusFailedInput(input,message);
        return false;
    }
    return true;
}

function _checkEmail(email)
{
	if (email == "") return false;
  template=/^[0-9a-z]+[0-9a-z.-_]*\@[0-9a-z]+[0-9a-z.-_]*\.[0-9a-z]{2,}$/i;
  if (template.test(email) == false) return false;
	return true;
}

function checkCheckbox(input, message)
{
    if (!input.checked)
    {
		alert(message);
		input.focus();
        return false;
    }
    return true;
}

function checkCheckboxes(input,message)
{
    if (input.length)
    {
        var i;
        for (i=0; i<input.length; i++)
        {
            if (input[i].checked)
            {
                return true;
            }
        }
        alert(message);
        input[0].focus();
        return false;
    }
    return true;
}

function onAfterBlurFailedInput()
{
    if (this.oldOnBlur)
    {
        this.onblur = this.oldOnBlur; 
    }
    if (this.oldBackgroundColor!=null)
    {
        this.style.backgroundColor = this.oldBackgroundColor;
    }
}

function setCountry(countryinput, stateinput){
  if (stateinput.value != "Outside US") countryinput.value="United States";
}
function setState(countryinput, stateinput){
  if (countryinput.value != "United States") stateinput.value = "Outside US";
}


function checkPaymentForm(form)
{
	return (checkInput(form.description,'Please select your order description') &&
		checkInput(form.total,'Please enter your order amount') &&
		checkInput(form.company,'Please enter your company name') &&
		checkInput(form.first_name,'Please enter your first name') &&
		checkInput(form.last_name,'Please enter your last name') &&
		checkInput(form.email,'Please enter your email address') &&
		checkEmail(form.email,'Please enter a correct email address') &&		
		checkInput(form.address_1,'Please enter your billing address') &&
		checkInput(form.city,'Please enter your city') &&
		checkInput(form.state,'Please choose your state') &&
		checkInput(form.zip,'Please enter your zip code'));
}

function checkReviewForm(form)
{
	return (
		checkInput(form.ccnumber,'Please enter your credit card number') &&
		checkInput(form.cvv2,'Please enter your card\'s Verfication ID'));
}

function showBanner(baseUrl, what)
{
    _showBanner(baseUrl, what);
}

function _showBanner(baseUrl, what)
{
   if (!document.phpAds_used) document.phpAds_used = ',';
   phpAds_random = '';new String (Math.random()); phpAds_random = phpAds_random.substring(2,11);
   
   var s = "<" + "script language='JavaScript' type='text/javascript' src='";
   s += baseUrl + "/ads/adjs.php?n=" + phpAds_random;
   s += "&amp;what=" + what + "&amp;block=1";
   s += "&amp;exclude=" + document.phpAds_used;
   if (document.referer)
      s += "&amp;referer=" + escape(document.referer);
   s+= "'><" + "/script>";
   document.write(s);
   return false;
}
