
function AskLogout()
{
	if(confirm('Are you sure you want to log out of your account?'))
		window.location='/login.php?do=logout&noTemplate=1';
}

function AjaxRequest(url,params,h_success,h_error)
{
	var aj = new Ajax.Request(
	url, 
	{
		requestTimeout: 1,
		parameters:params, 
		onSuccess: function(t) {
			if(t.responseText.length > 1000000)
			{
				if(!confirm('This request is too big and may cause your computer to slow down or freeze!\n\nDo you want to continue?'))
					return 0;
			}
			h_success(t);
		},
		onFailure: h_error,
		onTimeout: function() {
			alert('Error, request timeout!');
		}
	});	
}

function FadeOutObject(id)
{
	var fd = document.getElementById(id);
	if(fd)
		fd.style.opacity = '.45';
}

function FadeInObject(id)
{
	var fd = document.getElementById(id);
	if(fd)
		fd.style.opacity = '1';
}

function BuildQueryStringFromForm(f)
{
	var inps = f.getElementsByTagName('input'); 
	var sels = f.getElementsByTagName('select'); 
	var txtarea = f.getElementsByTagName('textarea'); 
	var qs = '';
	
	// Do all of the inputs
	for(var i=0;i<inps.length;i++)
	{
		if(!inps[i].name)
			continue;
	
		var l_type = inps[i].type;
		if(l_type == 'checkbox' || l_type == 'radio')
		{
			if(inps[i].checked == true)
			{
				qs += '&' + inps[i].name + '=' + escape(inps[i].value);
			}
		}
		else
		{
			qs += '&' + inps[i].name + '=' + escape(inps[i].value);
		}
	}
	
	// Do all of the selects
	for(var i=0;i<sels.length;i++)
	{
		if(!sels[i].name)
			continue;

		if(sels[i].multiple == true)
		{
			for(var j=0;j<sels[i].length;j++)
			{
				if(sels[i][j].selected == true)
				{
					qs += '&' + sels[i].name + '=' + escape(sels[i][j].value);
				}				
			}
		}
		else
		{
			qs += '&' + sels[i].name + '=' + escape(sels[i][sels[i].selectedIndex].value);
		}
	}
	
	// Do all of the textareas
	for(var i=0;i<txtarea.length;i++)
	{
		qs += '&' + txtarea[i].name + '=' + escape(txtarea[i].value);
	}
	
	// Remove the first &
	qs = qs.replace(/^\&/,"");

	return qs;	
}

function GetAllSelectedCheckboxIds(frm,name)
{
 	var str = '';
	var objs = frm.getElementsByTagName('input');	
	for(var i=0;i<objs.length;i++)
	{
		if(objs[i].type == 'checkbox')
		{
			if(objs[i].name == name)
			{
				if(objs[i].checked == true)
				{
					str = str + '&' + name + '=' + objs[i].value;
				}
			}
		}
	}
	
	return str;
}

function SelectOptionByValue(select,value)
{
	for(var i=0;i<select.length;i++)
	{
		if(select[i].value == value)
		{
			select.selectedIndex = i;
			continue;
		}
	}
}

function GetSelectValue(select)
{
	if(select.selectedIndex >= 0)
		return select[select.selectedIndex].value;
	else
		return false;
}

function CheckAll(frm,name)
{
	var inputs = frm.getElementsByTagName('input');
	
	// Get the total CHK cnt
	var chk_cnt = 0;
	var chk_chk = 0;
	var chk_to	= false;
	
	for(var i=0;i<inputs.length;i++)
	{
		if(inputs[i].type == 'checkbox')
		{
		 	if(name && inputs[i].name != name)
		 		continue;
		 		
			chk_cnt++;
			if(inputs[i].checked)
				chk_chk++;
		}
	}
	
	if(chk_cnt == chk_chk)
		chk_to = false;
	else
		chk_to = true;
		
	for(var i=0;i<inputs.length;i++)
	{
		if(inputs[i].type == 'checkbox')
		{
		 	if(name && inputs[i].name != name)
		 		continue;
		 		
			inputs[i].checked = chk_to;
		}
	}	
	
}

var currenttime = 'March 13, 2010 07:29:30'
var date_obj = new Date(currenttime);

var add_secs = 0;
var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	
		
function updateTime(id)
{
	if(!id)
		id = 'real_time';

 	// Get our html obj
	var obj = document.getElementById(id);

	//date_obj.setTime(1.26848697E+12 + (add_secs * 1000));
	date_obj.setSeconds(date_obj.getSeconds()+1)

	var month	= date_obj.getMonth()
	var day		= date_obj.getDate();
	var year	= date_obj.getFullYear();
	var hour	= date_obj.getHours();
	var min		= date_obj.getMinutes();
	var sec		= date_obj.getSeconds();

	var ampm	= 'am';
	if(hour > 12)
	{
		hour -= 12;
		ampm = 'pm';
	}
	else if(hour == 12)
	{
		ampm = 'pm';	
	}
	
	if(hour == 0)
		hour = '12';

	// Fix the second and min
	if(min < 10)
		min = "0" + min;
	if(sec < 10)
		sec = "0" + sec;

	var dstr = day.toString();

	var suf = 'th';
	var last_num = dstr.substr(1,dstr.length - 1);
	
	if(last_num == 1)
		suf = 'st';
	else if(last_num == 2)
		suf = 'nd';
	else if(last_num == 3)
		suf = 'rd';

	obj.innerHTML = MONTH_NAMES[month + 12] + ' ' + day + suf + ', ' + year + ' ' + hour + ':' + min + ':' + sec + ' ' + ampm;

	// Add another second
	add_secs++;

	// Keep callin our function
	setTimeout("updateTime('" + id + "')",1000);
}

function LoadAjaxData(url,qs,dataDiv,fadeDiv,showIndicator)
{
	if(!showIndicator)
		showIndicator = false;
	else
		showIndicator = true;

	
	FadeOutObject(fadeDiv);
	
	if(showIndicator)
	{
		ind_html = '<div style="position: absolute;align: center;width: 300px;background-color: #EDF8FF;border: 1px solid black;padding: 5px;"><table cellspacing="0" cellpadding="0" border="0" width="100%"><tr><td width="20"><img src="/interface/images/indicator.gif" width="16" height="16"></td><td><span style="font-family:verdana;font-size: 10px;color: gray;">Loading... Please be Patient...</span></td></tr></table></div>';
		var d_ref = document.getElementById(dataDiv);			
			
		if(navigator.appName != "Microsoft Internet Explorer")
		{
			if(d_ref.innerHTML != ind_html)
				d_ref.innerHTML = ind_html + d_ref.innerHTML;
		}

	}
	
	
	// Load the data via Ajax
	AjaxRequest(url,qs,
	function (t) {	
		var r = t.responseText;
		
		// Rejex for JS extraction
		//var re = /<script.*?>(.*?)<\/script>/igm;
		var re = /<script.*?>([\s\S]*?)<\/script>/igm;
		var re_nog = /<script.*?>([\s\S]*?)<\/script>/im;

		// Display the HTML
		var html = r.replace(re,"");
		document.getElementById(dataDiv).innerHTML = html;
		
		// Run the scripts
		var match;
		while (match = re.exec(r)) {
			eval(match[1]);
		}
		
		var jsm = r.match(re);
		for(var i=0;i<jsm.length;i++)
		{		
			var ls = new String(jsm[i]);
			var lm = re_nog.exec(ls);
			
			eval(lm[1]);
		}

		FadeInObject(fadeDiv);
	},
	function ()
	{
		alert('Error updating, please try again!');			
	}
	);
}


function PopupViewMember_DEP(id)
{
	var popW = 690;
	var popH = 480;

	var leftPos = (window.screen.width / 2) - ((popW + 10) / 2);
	var topPos = (window.screen.height / 2) - ((popH + 50) / 2);
	
	var w = window.open('/interface/admin_members.php?do=edit&noTemplate=1&id=' + id,'admin_members_' + id,'width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos + ',resizable=no,scrollbars=yes');
	w.focus();	
}

function PopupViewMember(id)
{
	var popW = 690;
	var popH = 480;

	var leftPos = (window.screen.width / 2) - ((popW + 10) / 2);
	var topPos = (window.screen.height / 2) - ((popH + 50) / 2);
	
	var w = window.open('/interface/admin_members_edit.php?isPopup=1&memberId=' + id,'admin_members_' + id,'width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos + ',resizable=no,scrollbars=yes');
	w.focus();	
}

function PopupMemberProfile(id)
{
	//alert(id);
}

// ================= Help Popup Mouseovers ======================
OffsetX=15; /* -110 */
OffsetY=15;
var old,skn,iex=(document.all),yyy=-1000;
var ns4=document.layers
var ns6=document.getElementById&&!document.all
var ie4=document.all

function init_helppopup()
{
	var pdq = document.createElement('div');
	pdq.setAttribute('id','pdq');
	document.body.appendChild(pdq);

	if (ns4) skn=document.pdq
	else if (ns6) skn=document.getElementById("pdq").style
	else if (ie4) skn=document.all.pdq.style
	if (ns4) document.captureEvents(Event.MOUSEMOVE);
	else {
		if(typeof(skn) != 'undefined')
		{	
			skn.visibility="visible"
			skn.display="none"
		}
	}
	document.onmousemove=get_mouse;
}


function popup(msg)
{
	var content="<div class=boxpopup>"+msg+"</div>";
	yyy=OffsetY;
	
	if(typeof(skn) == 'undefined')
		return 0;
	
	if(ns4)
	{
		skn.document.write(content);
		skn.document.close();
		skn.visibility="visible";	
	}
	
	if(ns6){
		document.getElementById("pdq").innerHTML=content;
		skn.display='';
	}
	if(ie4){
		document.all("pdq").innerHTML=content;
		skn.display='';
	}
}

function get_mouse(e)
{
	var winW = 630, winH = 460;
	if (parseInt(navigator.appVersion)>3)
	{
		if (navigator.appName=="Netscape")
		{
			winW = window.innerWidth;
			winH = window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) 
		{
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
		}
	}
	
	// Make sure we don't go over the bounds of the window
	var x=(ns4||ns6)?e.pageX:event.x+document.body.scrollLeft;
	var new_left = x+OffsetX;
	if(new_left + 290 >= winW)
		new_left = winW - 290;
	else if(new_left <= 10)
		new_left = 10;
		

	if(typeof(skn) == 'undefined')
		return 0;

	skn.left=new_left;

	var y=(ns4||ns6)?e.pageY:event.y+document.body.scrollTop;
	skn.top=y+yyy;
}

function remove_popup()
{
	if(typeof(skn) == 'undefined')
		return 0;

	yyy=-1000;
	if(ns4){skn.visibility="hidden";}
	else if (ns6||ie4)
	skn.display="none"
}

function ViewCart()
{
	var popW = 680;
	var popH = 380;

	var leftPos = (window.screen.width / 2) - ((popW + 10) / 2);
	var topPos = (window.screen.height / 2) - ((popH + 50) / 2);
	
	var w = window.open('/interface/products.php?do=display_cart&noTemplate=1','view_cart','width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos + ',resizable=no,scrollbars=yes');
	w.focus();	
}

Event.observe(document,'dom:loaded',function(e) {
	init_helppopup();
});

function uiPrevPage(obj)
{
	if(!obj)
		return 0;

	var cur_id = new Number(obj.value);
	if(isNaN(cur_id) || cur_id <= 0)
		cur_id = new Number(1);

	if(cur_id > 1)
	{
		obj.value = new Number(cur_id - 1).toFixed();
	}
}

function uiNextPage(obj,max)
{
	if(!obj)
		return 0;

	var cur_id = new Number(obj.value);
	if(isNaN(cur_id) || cur_id <= 0)
		cur_id = new Number(1);

	if(cur_id < max)
	{
		obj.value = new Number(cur_id + 1).toFixed();
	}
}




var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}