Array.prototype.max = function () {
	return Math.max.apply(null, this);
};

Array.prototype.min = function () {
	return Math.min.apply(null, this);
};

Array.prototype.sum = function () {
	for (var i = 0, sum = 0; i < this.length; sum += this[i++]);
	return sum;
}

var ns4 = (document.layers) ? true : false;
var ie4 = (document.all && !document.getElementById) ? true : false;
var ie5 = (document.all && document.getElementById) ? true : false;
var ns6 = (!document.all && document.getElementById) ? true : false;

 // Increments persistent cart values
function cart_add(count, subtotal) {
	var divCART_COUNT = document.getElementById('divcart_count');
	var divCART_SUBTOTAL = document.getElementById('divcart_total');
	var CART_COUNT = 0;
	var CART_SUBTOTAL = 0;
	
	if (divCART_COUNT && divCART_SUBTOTAL) {
		CART_COUNT=parseInt(divCART_COUNT.innerHTML);
		CART_SUBTOTAL=parseFloat(divCART_SUBTOTAL.innerHTML.replace('$',''));
		if (!isNaN(CART_COUNT) && !isNaN(CART_SUBTOTAL)) {
			CART_COUNT += parseInt(count);
			CART_SUBTOTAL += parseFloat(subtotal)*parseInt(count);
			divCART_COUNT.innerHTML = CART_COUNT;
			divCART_SUBTOTAL.innerHTML = "$" + CART_SUBTOTAL.toFixed(2);
		}
	}
}

function cart_update(count, subtotal) {
	var divCART_COUNT = document.getElementById('divcart_count');
	var divCART_SUBTOTAL = document.getElementById('divcart_total');
	var CART_COUNT = 0;
	var CART_SUBTOTAL = 0;
	
	if (divCART_COUNT) {
		divCART_COUNT.innerHTML = count;
	}

	if (divCART_SUBTOTAL) {
		divCART_SUBTOTAL.innerHTML = subtotal;
	}
}

function setcookie(name, value, expires)
{
	// expires is an integer representing the number of 
	// milliseconds into the future the cookie should expire
	// oneYear = 1000*60*60*24*365
	// very important to include the path otherwise the cookie scope is a sub-directory
	var today = new Date();
	var expiredate = new Date(today.getTime()+expires);
  if (expires > 0)
		{document.cookie = name + '=' + escape(value) + '; path=/; expires=' + expiredate.toUTCString();}
	else
		{document.cookie = name + '=' + escape(value) + '; path=/';}
}

function isEmail(btn) {
	var re = new RegExp('^(.*)btn.*$'); // Extract "Prefix" from Prefix_btnWhatever
	if (re.test(btn.id)) {
		var Prefix=RegExp.$1;
		
		var email=document.getElementById(Prefix+"txtEmail");
		if (email) {
			if ( !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value))) {
				alert("Invalid E-mail Address! Please re-enter.");
				return (false);
			}
		}
	}
	return (true);
}

function isDate(source, arguments) {

	var dateStr = arguments.Value;
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2,4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	var baseyear;
	var d;
	
	d = new Date();
	baseyear = d.getFullYear();
	arguments.IsValid=true;

	if (matchArray == null) {
		arguments.IsValid=false;
		return arguments.IsValid;
	}

	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[5];

	if ((year > 99 && year < 1000) || year < 0) { // check year range
		arguments.IsValid=false;
	}
	
	if (year < 50) { //check for short year
		year = parseInt(year)+Math.floor(baseyear / 100.0)*100;
	} else {
		if (year < 100) {
			year = parseInt(year)+Math.floor(baseyear / 100.0)*100-100;
		}
	}

	if (month < 1 || month > 12) { // check month range
		arguments.IsValid=false;
	}

	if (day < 1 || day > 31) { //check day range
		arguments.IsValid=false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) { //check short months
		arguments.IsValid=false;
	}

	if (month == 2) { // check feb allowing for leap year
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			arguments.IsValid=false;
		}
	}
	return arguments.IsValid;
}

function qs() {
	var a = new Array();
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			a[key.toUpperCase()] = val;
		}
	}
	return a;
}

function fixwidth(selector, hspace) {
	var max = 0;
	jQuery(selector).each(function () {
		if (jQuery(this).width() > max) {
			max = jQuery(this).width();
		}
	});

	if (typeof hspace == "undefined")
		hspace = 0;

	jQuery(selector).width(max + hspace);
}

function redirect(url) {
	location.href = url;
	return false;
}

function popcontent(key) {
	$.ajax({
		type: "POST",
		url: "/aspx/webmethods.aspx/PopContent",
		data: "{Key:'"+key+"'}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: popcontent_ok,
		fail: popFail
	});
}

function popcontent_ok(msg) {
	$('#PAGE_POPCONTENT').html(msg.d.Content);
	$('#PAGE_POPCONTENT').dialog({
		modal:true,
		position: 'center',
		resizable: false,
		title: msg.d.Title,
		buttons: {
			"Ok": function () {
				$(this).dialog("close");
			}
		}

	});
}

function ajaxButton(btn, command, msg, options) {

	value = $(btn).attr('value');

	var settings = {
		btn: btn,
		value: value,
		library: 'aspx',
		mode: 'simple',
		type: 'POST',
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: ajaxButton_OK
	};
	
	if (options) {
		$.extend(settings, options);
	}

	settings.url = '/' + settings.library + '/webmethods.aspx/' + command;

	$(btn).attr('value', msg)
	$.ajax(settings);
}

function ajaxButton_OK(msg) {
	var btn = this.btn;
	var value = this.value;

	switch (this.mode) {
		case 'toggle':
			value = msg.d ? this.valueTrue : this.valueFalse;
			setTimeout(function () {
				$(btn).attr('value', value);
			}, 1000);
			break;
		default:
			setTimeout(function () {
				$(btn).attr('value', value);
			}, 1000);
	}

}


