// JavaScript Document
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function fixedSize(url,name,features) {
	mywindow = window.open(url,name,features);
	mywindow.focus();
}
//function to show/hide windows in browser
function show(e){
	document.getElementById(e).style.display = '';
}
function hide(e){
	document.getElementById(e).style.display = 'none';
}
function redirect(url){
	document.location.href = url;
}
function emptyCartWarning(num){
	if(num){
		if(confirm("Are you sure you want to empty your shopping cart?")){
			document.location.href = 'emptycart.php';
		}
	} else {
		alert("There are no items in your shopping cart.");
	}
}
function checkLogin(){
	if(document.getElementById('username').value&&document.getElementById('password').value){
		document.forms['login'].submit();
	} else if(document.getElementById('username').value) {
		alert('Please enter a password.');
		document.login.password.focus();
	} else if(document.getElementById('password').value) {
		alert('Please enter a username.');
		document.login.username.focus();
	} else {
		alert('Please enter a username and password.');
		document.login.username.focus();
	}
}
function searchProducts(e){
	if(document.getElementById(e).value){
		document.location = "search.php?q="+document.getElementById(e).value;
	} else {
		alert('Please enter a search query.');
		document.getElementById(e).focus();
	}
}
function checkSearch(e){
	if(!document.getElementById(e).value){
		alert('Please enter a search query.');
		document.getElementById(e).focus();
		return false;
	} else {
		return true;	
	}
}
//cookie functions
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	if(readCookie(name)!=''&&value!=''){
		if(readCookie(name).search(value.split(',')[1])==-1){
			document.cookie = name+"="+value+'|'+readCookie(name)+expires+"; path=/";
		}
	} else {
		document.cookie = name+"="+value+expires+"; path=/";
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return '';
}

function eraseCookie(name) {
	createCookie(name,"",-1);
	//document.getElementById("recent_container").style.display = "none";
	var r = readCookie('recent');
	if(r != ""){
		document.getElementById("recent_container").innerHTML = "<div class=\"body_text_black\" style=\"padding:5px; padding-top:0px;\"><i>Empty</i></div>";
	}
}

function recentlyViewed(){
	var r = readCookie('recent').split('|');
	var max_shown = 10;
	if(r != ""){
		document.write('<div id=\"recent_container\"><ul>');
		for(i=0;i<r.length;i++){
			rv = r[i].split(',');
			if(i<max_shown){
				document.write('<li class=\"list_elements\"><a href=\"'+rv[1]+'\">'+rv[0]+'</a></li>');
				//if(i<max_shown-1&&r.length>1&&i<r.length-1){
					//document.write(' | ');
				//}
			}
		}
		document.write('</ul></div>');
	} else {
		document.write('<div id=\"recent_container\" class=\"body_text_black\" style=\"padding:5px; padding-top:0px;\"><i>Empty</i></div>');
	}
}

