function Sale( price, name, start, end )
{
	this.price = price;
	this.name = name;
	this.start = start;
	this.end = end;

}

function Product( id, title, summary, pic, price, qty, hash, adjustedPrice  )
{
	this.id = id;
	this.title = title;
	this.summary = summary;
	this.pic = pic;
	this.price = price;
	this.qty = qty;
	this.optionList = new Array();
	this.configList = new Array();
	this.hash = hash;
	this.adjustedPrice = adjustedPrice;
	this.sale = null;
}

Product.prototype.addOption = function(id, price, name, lbl, type)
{
	this.optionList[this.optionList.length] = new ProductOptions(id, price, name, lbl, type );
}

Product.prototype.addSale = function(price, name, start, end)
{
	this.sale = new Sale( price, name, start, end );
}



Product.prototype.addConfig = function(id, title, qty, price)
{
	this.configList[this.configList.length] = new ProductConfig(id, title, qty, price );
}


function ProductOptions( id, price, name, lbl, type )
{
	this.id = id;
	this.price = price;
	this.name = name;
	this.label = lbl;
	this.type = type;
}


function ProductConfig( id, title, qty, price )
{
	this.id = id;
	this.title = title;
	this.qty = qty;
	this.price = price;
}


function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,"");
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();

	if(intCents<10)
		strCents = "0" + strCents;

	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
			dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+","+
			dblValue.substring(dblValue.length-(4*i+3));

	return (((blnSign)?"":"-") + "$" + dblValue + "." + strCents);
}

function Tax( name, amount )
{
	this.name = name;
	this.amount = amount;
}

function validate(noClassChange)
{
	var err = false;

	// First name
	if( document.getElementById("first_name").value == "" )
	{
		if( !noClassChange ) showError("first_name");
		err = true;
	} else
		hideError("first_name");

	// Last name
	if( document.getElementById("last_name").value == "" )
	{
		if( !noClassChange ) showError("last_name");
		err = true;
	} else
		hideError("last_name");

	// Address
	if( document.getElementById("address").value == "" )
	{
		if( !noClassChange )  showError("address");
		err = true;
	} else
		hideError("address");

	// City
	if( document.getElementById("city").value == "" )
	{
		if( !noClassChange ) showError("city");
		err = true;
	} else
		hideError("city");

	// Postal Code
	if( document.getElementById("postal_code").value == "" )
	{
		if( !noClassChange ) showError("postal_code");
		err = true;
	} else
		hideError("postal_code");

	// Phone number
	if( document.getElementById("phone").value == "" )
	{
		if( !noClassChange ) showError("phone");
		err = true;
	} else
		hideError("phone");

	if( document.getElementById("province").selectedIndex == 0 )
	{
		err = true;
		if( !noClassChange )  showError( "province" );
	} else {
		hideError( "province" );
	}	
	
	if( document.getElementById("country").selectedIndex == 0 )
	{
		err = true;
		if( !noClassChange )  showError( "country" );
	} else {
		hideError( "country" );
	}	


	// Email
	if( document.getElementById("email") && document.getElementById("email").value == "" )
	{
		if( !noClassChange ) showError("email");
		err = true;
	} else
		hideError("email");


	return !err;
}

function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

function toggleDisplay($id, $display)
{
	if ($display==null) $display = 'block';

	document.getElementById($id).style.display =
	    (getStyle(document.getElementById($id), 'display') == 'none') ? $display : 'none';
}

function swapOptions($fromSelectID, $toSelectID, $swapButton, $func)
{
	opts = document.getElementById($fromSelectID);
	size = opts.options.length;
	selCats = document.getElementById($toSelectID);
	offset = 0;

	for( i = opts.selectedIndex; i < size; i++ )
	{
		if( opts.options[i - offset].selected )
		{
			sLength = selCats.options.length;


			newCat = new Option();
			newCat.text = opts.options[i - offset].text;
			newCat.value = opts.options[i - offset].value;

			selCats.options[sLength] = newCat;

			opts.options[i - offset] = null;
			offset++;
		}
	}

	if($swapButton != null)
		document.getElementById($swapButton).disabled = selCats.options.length == 0;
}

function removeOptions($selectID, $buttonID, $func)
{
	select = document.getElementById($selectID);
	size = select.options.length;
	offset = 0;

	if (select.selectedIndex != -1)
	{
		for( i = select.selectedIndex; i < size; i++ )
			if( select.options[i - offset].selected )
			{
				select.options[i - offset] = null;
				offset++;
			}
	}

	// sets the button to disabled if there are no options to remove
	if ($buttonID != null)
		document.getElementById($buttonID).disabled = select.options.length == 0;
}

function moveOption ($selectID, $buttonID, $func)
{
	select = document.getElementById($selectID);
	if (select.selectedIndex != -1)
	{
    	if ($func == 'down')
    	{
      		if (select.selectedIndex != select.options.length - 1)
        		var i = select.selectedIndex + 1;
      		else
        		return;
    	}
    	else
    	{
      		if (select.selectedIndex != 0)
        		var i = select.selectedIndex - 1;
      		else
        		return;
    	}
		var swapOption = new Object();
		swapOption.text = select.options[select.selectedIndex].text;
		swapOption.value = select.options[select.selectedIndex].value;
		swapOption.selected = select.options[select.selectedIndex].selected;
		swapOption.defaultSelected = select.options[select.selectedIndex].defaultSelected;
		var anIndex = select.selectedIndex;
		for (var property in swapOption)
			select.options[anIndex][property] = select.options[i][property];
		for (var property in swapOption)
			select.options[i][property] = swapOption[property];
	}

	// sets the button to disabled if there are no options to remove
	if ($buttonID != null)
		document.getElementById($buttonID).disabled = select.options.length == 0;
}

function checkValue($id,$regExp,$msg)
{
	$obj = document.getElementById($id);

	if($obj.value.match( $regExp ) != null)	return true;
	//else if($orelse != null) eval($orelse);

	alert($msg); $obj.select();	$obj.focus();
	return false;
}

function compareValue($compare, $compareTo, $comparator, $msg)
{
	$c = document.getElementById($compare);
	$c2 = document.getElementById($compareTo);

	compareFunc = new Function("return (" + $c.value + $comparator + $c2.value + ") ? true : false;");

	if(compareFunc()) return true

	//else
	alert($msg);
	return false;
}
