// set the variable that should allow enter
var allowEnter = 1;
var dragObj = new Object();
dragObj.zIndex = 0;

function rollOver(elem, action)
	{
	if (action=='on')
		{
		elem.style.color = '#ffffff';
		elem.style.backgroundImage = 'url(./images/header_mid_pressed.gif)';
		document.body.style.cursor='pointer';
		}
	else
		{
		elem.style.color = "#464646";
		elem.style.backgroundImage = 'url(./images/header_mid.gif)';
		document.body.style.cursor='auto';	
		}
	}

function checkContents(elem, action, defText)
	{
	if (action==1)
		{
		// got focus, see if the contents equal "search"
		if (elem.value==defText)
			{
			elem.value = "";
			}
		allowEnter = 0;
		}
	else
		{
		// lost focus, see if the contents are empty
		if (elem.value=="")
			{
			elem.value = defText;
			}
		allowEnter = 1;
		}
	}

function slideInOut(elem, action)
	{
	element = document.getElementById(elem);
	if (action=="in")
		{
		element.style.display = "block";
		objTimer = setTimeout("fader(\""+elem+"\",100);",0);
		objTimer = setTimeout("slider(\""+elem+"\",-80);",10);
		objTimer = setTimeout("slider(\""+elem+"\",-75);",20);
		objTimer = setTimeout("slider(\""+elem+"\",-70);",30);
		objTimer = setTimeout("slider(\""+elem+"\",-65);",40);
		objTimer = setTimeout("slider(\""+elem+"\",-60);",50);
		objTimer = setTimeout("slider(\""+elem+"\",-55);",60);
		objTimer = setTimeout("slider(\""+elem+"\",-50);",70);
		objTimer = setTimeout("slider(\""+elem+"\",-45);",80);
		objTimer = setTimeout("slider(\""+elem+"\",-40);",90);
		objTimer = setTimeout("slider(\""+elem+"\",-35);",100);
		objTimer = setTimeout("slider(\""+elem+"\",-30);",110);
		objTimer = setTimeout("slider(\""+elem+"\",-25);",120);
		objTimer = setTimeout("slider(\""+elem+"\",-20);",130);
		objTimer = setTimeout("slider(\""+elem+"\",-18);",150);
		objTimer = setTimeout("slider(\""+elem+"\",-14);",170);
		objTimer = setTimeout("slider(\""+elem+"\",-10);",190);
		objTimer = setTimeout("slider(\""+elem+"\",-8);",210);
		objTimer = setTimeout("slider(\""+elem+"\",-6);",225);
		objTimer = setTimeout("slider(\""+elem+"\",-5);",250);
		objTimer = setTimeout("slider(\""+elem+"\",-4);",280);
		objTimer = setTimeout("slider(\""+elem+"\",-3);",315);
		objTimer = setTimeout("slider(\""+elem+"\",-2);",355);
		objTimer = setTimeout("slider(\""+elem+"\",-1);",400);
		objTimer = setTimeout("slider(\""+elem+"\",0);",450);
		document.getElementById('username').focus();
		}
	else
		{
		objTimer = setTimeout("slider(\""+elem+"\",-80);",180);
		element.style.display = "none";
		}
	}

function fadeInOut(elem, action)
	{
	element = document.getElementById(elem);
	if (action=="in")
		{
		objTimer = setTimeout("fader(\""+elem+"\",0);",0);
		element.style.display = "block";
		objTimer = setTimeout("fader(\""+elem+"\",20);",100);
		objTimer = setTimeout("fader(\""+elem+"\",40);",200);
		objTimer = setTimeout("fader(\""+elem+"\",60);",300);
		objTimer = setTimeout("fader(\""+elem+"\",80);",400);
		objTimer = setTimeout("fader(\""+elem+"\",100);",500);
		}
	else
		{
		objTimer = setTimeout("fader(\""+elem+"\",0);",100);
		element.style.display = "none";
		}
	}

function loginFade(elem1, elem2)
	{
	element1 = document.getElementById(elem1);
	element2 = document.getElementById(elem2);
	objTimer = setTimeout("fader(\""+elem1+"\",80);",20);
	objTimer = setTimeout("fader(\""+elem1+"\",60);",40);
	objTimer = setTimeout("fader(\""+elem1+"\",40);",60);
	objTimer = setTimeout("fader(\""+elem1+"\",20);",80);
	objTimer = setTimeout("fader(\""+elem1+"\",0);",100);
	element1.style.display = "none";

	element2.style.display = "block";
	objTimer = setTimeout("fader(\""+elem2+"\",20);",100);
	objTimer = setTimeout("fader(\""+elem2+"\",40);",120);
	objTimer = setTimeout("fader(\""+elem2+"\",60);",140);
	objTimer = setTimeout("fader(\""+elem2+"\",80);",160);
	objTimer = setTimeout("fader(\""+elem2+"\",100);",180);

	loginUser();
	}

function fader(elem, valOpacity)
	{
	elem = document.getElementById(elem);
	//if (document.all)
	//	{
		//elem.style.filter = "alpha(opacity="+valOpacity+")";
	//	}
	//else
	//	{
		elem.style.opacity = (valOpacity/100);
	//	}
	}

function slider(elem, valPosition)
	{
	elem = document.getElementById(elem);
	//if (document.all)
	//	{
	//	elem.style.top = valPosition;
	//	}
	//else
	//	{
		elem.style.top = valPosition+"px";
	//	}
	}

function Browser()
	{
	var ua, s, i;
	
	this.isIE    = false;
	this.isNS    = false;
	this.version = null;
	
	ua = navigator.userAgent;
	
	s = "MSIE";
	if ((i = ua.indexOf(s)) >= 0)
		{
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
		}
	
	s = "Netscape6/";
	if ((i = ua.indexOf(s)) >= 0)
		{
		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
		}
	
	// Treat any other "Gecko" browser as NS 6.1.
	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0)
		{
		this.isNS = true;
		this.version = 6.1;
		return;
		}
	}
	
var browser = new Browser();

function dragStart(event, id)
	{
	// set the mouse cursor
	if (browser.isIE)
	window.event.srcElement.style.cursor = "move";
	if (browser.isNS)
	event.target.style.cursor = "move";

	var el;
	var x, y;

	// If an element id was given, find it. Otherwise use the element being clicked on.
	if (id)
		{
		dragObj.elNode = document.getElementById(id);
		}
	else
		{
		if (browser.isIE)
		dragObj.elNode = window.event.srcElement;
		if (browser.isNS)
		dragObj.elNode = event.target;
		}

	// If this is a text node, use its parent element.
	if (dragObj.elNode.nodeType == 3)
		{
		dragObj.elNode = dragObj.elNode.parentNode;
		}

	// Get cursor position with respect to the page.
	if (browser.isIE)
		{
		x = window.event.clientX + document.documentElement.scrollLeft
		+ document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop
		+ document.body.scrollTop;
		}
	if (browser.isNS)
		{
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
		}
	
	// Save starting positions of cursor and element.
	dragObj.cursorStartX = x;
	dragObj.cursorStartY = y;
	dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
	dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);
	if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
	if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;
	
	// Update element's z-index.
	dragObj.elNode.style.zIndex = ++dragObj.zIndex;

	// Capture mousemove and mouseup events on the page.
	if (browser.isIE)
		{
		document.attachEvent("onmousemove", dragGo);
		document.attachEvent("onmouseup",   dragStop);
		window.event.cancelBubble = true;
		window.event.returnValue = false;
		}
	if (browser.isNS)
		{
		document.addEventListener("mousemove", dragGo,   true);
		document.addEventListener("mouseup",   dragStop, true);
		event.preventDefault();
		}
	}

function dragGo(event)
	{
	// hide the cart contents
	document.getElementById("cartContentsNormal").style.display = "none";
	document.getElementById("wishlistContentsNormal").style.display = "none";

	var x, y;
	// Get cursor position with respect to the page.
	if (browser.isIE)
		{
		x = window.event.clientX + document.documentElement.scrollLeft
		+ document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop
		+ document.body.scrollTop;
		}
	if (browser.isNS)
		{
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
		}

	// Move drag element by the same amount the cursor has moved.
	dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
	dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";
	if (browser.isIE)
		{
		window.event.cancelBubble = true;
		window.event.returnValue = false;
		}
	if (browser.isNS)
		{
		event.preventDefault();
		}
	}

function dragStop(event)
	{
	// Stop capturing mousemove and mouseup events.
	if (browser.isIE)
		{
		document.detachEvent("onmousemove", dragGo);
		document.detachEvent("onmouseup",   dragStop);
		}
	if (browser.isNS)
		{
		document.removeEventListener("mousemove", dragGo,   true);
		document.removeEventListener("mouseup",   dragStop, true);
		}

	// show the cart contents
	document.getElementById("cartContentsNormal").style.display = "block";
	document.getElementById("wishlistContentsNormal").style.display = "block";
	// set the mouse cursor
	if (browser.isIE)
	window.event.srcElement.style.cursor = "auto";
	if (browser.isNS)
	event.target.style.cursor = "auto";
	}

function loginUser()
	{
	var index = ajax.length;
	ajax[index] = new sack();
	ajax[index].requestFile = 'http://www.philshopping.com/login.php?usr=' + document.loginForm.username.value + '&pwd=' + document.loginForm.password.value;	// Specifying which file to get
	ajax[index].onCompletion = function(){ userLoggedIn(index) };	// Specify function that will be executed after file has been found
	ajax[index].runAJAX();		// Execute AJAX function
	}

function userLoggedIn(index)
	{
	eval(ajax[index].response);

	// check if the user is logged in
	if (ec==1)
		{
		// Username not found
		fadeInOut('loginDiv3', 'out');
		document.getElementById('buffText').style.height = "63px";
		document.getElementById('loginText').innerHTML = "&nbsp; <FONT COLOR=\"#FF0000\"><B>Error: your username does not exist</B></FONT><BR>&nbsp; Welcome! <A HREF=\"javascript:fadeInOut('loginDiv', 'out'); slideInOut('loginDiv2', 'in');\">Sign in</A> or <A HREF=\"register.php\">register now</A>";
		fadeInOut('loginDiv', 'in');
		}
	else if (ec==2)
		{
		// Incorrect Password
		fadeInOut('loginDiv3', 'out');
		document.getElementById('buffText').style.height = "63px";
		document.getElementById('loginText').innerHTML = "&nbsp; <FONT COLOR=\"#FF0000\"><B>Error: your password was incorrect</B></FONT><BR>&nbsp; Welcome! <A HREF=\"javascript:fadeInOut('loginDiv', 'out'); slideInOut('loginDiv2', 'in');\">Sign in</A> or <A HREF=\"register.php\">register now</A>";
		fadeInOut('loginDiv', 'in');
		}
	else
		{
		// user logged in, format the details in the right DIV
		fadeInOut('loginDiv3', 'out');
		document.getElementById('buffText').style.height = "83px";
		document.getElementById('loginText').innerHTML = "&nbsp; Welcome " + fullname + "! &nbsp; &nbsp; <A HREF=\"javascript:logoutUser();\">Logout</A>";
		fadeInOut('loginDiv', 'in');

		// document.getElementById('divAccount').innerHTML = "<A ID=\"idAccount\" HREF=\"account.php\"></A>";
		// redirect the user to the logged in page (with toolbar)
		top.location.href=gotoReferer;
		}
	}

function logoutUser(member_id)
	{
	var index = ajax.length;
	ajax[index] = new sack();
	ajax[index].requestFile = 'logout.php?id=' + member_id;	// Specifying which file to get
	ajax[index].onCompletion = function(){ userLoggedOut(index) };	// Specify function that will be executed after file has been found
	ajax[index].runAJAX();		// Execute AJAX function
	}

function userLoggedOut(index)
	{
	eval(ajax[index].response);

	fadeInOut('loginDiv', 'out');
	document.getElementById('loginText').innerHTML = "&nbsp; Welcome! <A HREF=\"javascript:fadeInOut('loginDiv', 'out'); slideInOut('loginDiv2', 'in');\">Sign in</A> or <A HREF=\"register.php\">register now</A>";
	fadeInOut('loginDiv', 'in');

	document.getElementById('divAccount').innerHTML = "<A ID=\"idAccount\" HREF=\"javascript:fadeInOut('loginDiv', 'out'); slideInOut('loginDiv2', 'in');\"></A>";

	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	// if (sPage=="account.php") {location.href="index.php";}
	location.href="index.php";
	}

function openFAQ(elemID)
	{
	elem1 = "question" + elemID;
	elem2 = "answer" + elemID;
	elem3 = "icon" + elemID;
	element01 = document.getElementById(elem1);
	element02 = document.getElementById(elem2);
	element03 = document.getElementById(elem3);
	if (element02.style.display=="none")
		{
		element02.style.display = "block";
		element03.src = "./images/icon_collapse.png";
		}
	else
		{
		element02.style.display = "none";
		element03.src = "./images/icon_expand.png";
		}
	}

function editField(elem, action)
	{
	element = document.getElementById(elem);
	if (action=="on")
		{
		//element.disabled = false;
		element.style.border = "1px solid #DDDDDD";
		element.focus();
		}
	else
		{
		//element.disabled = true;
		element.style.border = "0px solid #DDDDDD";
		}
	}

function tabOver(elem, action)
	{
	element = elem;
	if (action=='on')
		{
		element.style.background = "url(./images/tab_over.png)";
		document.body.style.cursor='pointer';
		}
	else
		{
		element.style.background = "";
		document.body.style.cursor='auto';
		}
	}

function switchLayer(layerID)
	{
	document.getElementById('tabContents1').style.display = "none";
	document.getElementById('tabContents3').style.display = "none";
	document.getElementById('tabContents4').style.display = "none";
	document.getElementById('tabContents5').style.display = "none";
	document.getElementById('Bars01').style.display = "none";
	document.getElementById('Bars03').style.display = "none";
	document.getElementById('Bars04').style.display = "none";
	document.getElementById('Bars05').style.display = "none";

	layerElemOn = document.getElementById('tabContents'+layerID);
	barsElemOn = document.getElementById('Bars0'+layerID);
	layerElemOn.style.display = "block";
	barsElemOn.style.display = "block";
	}

function showLog()
	{
	window.open('login_log.php','showLog','toolbars=no,menubar=no,directories=no,location=no,status=no,scrollbars=yes,resizable=no,width=300,height=400');
	}

function addToCart(productID)
	{
	cartAction('normal');
	document.getElementById("loadingNormal").style.display = "block";
	var index = ajax.length;
	ajax[index] = new sack();
	ajax[index].requestFile = 'cart_add.php?id=' + productID + "&frc=0";	// Specifying which file to get
	ajax[index].onCompletion = function(){ itemAdded(index) };	// Specify function that will be executed after file has been found
	ajax[index].runAJAX();		// Execute AJAX function
	}

function addToCartForce(productID)
	{
	cartAction('normal');
	document.getElementById("loadingNormal").style.display = "block";
	var index = ajax.length;
	ajax[index] = new sack();
	ajax[index].requestFile = 'cart_add.php?id=' + productID + "&frc=1";	// Specifying which file to get
	ajax[index].onCompletion = function(){ itemAdded(index) };	// Specify function that will be executed after file has been found
	ajax[index].runAJAX();		// Execute AJAX function
	}

function addToCartRL(productID, regListID)
	{
	wishlistCartAction('normal');
	document.getElementById("wishlistLoadingNormal").style.display = "block";
	var index = ajax.length;
	ajax[index] = new sack();
	ajax[index].requestFile = 'cart_add.php?id=' + productID + "&rid=" + regListID + "&frc=0";	// Specifying which file to get
	ajax[index].onCompletion = function(){ itemAdded(index) };	// Specify function that will be executed after file has been found
	ajax[index].runAJAX();		// Execute AJAX function
	}

function addToCartRLForce(productID, regListID)
	{
	wishlistCartAction('normal');
	document.getElementById("wishlistLoadingNormal").style.display = "block";
	var index = ajax.length;
	ajax[index] = new sack();
	ajax[index].requestFile = 'cart_add.php?id=' + productID + "&rid=" + regListID + "&frc=1";	// Specifying which file to get
	ajax[index].onCompletion = function(){ itemAdded(index) };	// Specify function that will be executed after file has been found
	ajax[index].runAJAX();		// Execute AJAX function
	}

function itemAdded(index)
	{
	eval(ajax[index].response);

	// check if the item is already in the cart
	if (ec==1)
		{
		// item already in the shopping cart, ask if another should be added
		if (window.confirm("That item is already in your shopping cart.\nDo you want to add another?"))
			{
			addToCartForce(productID);
			}
		}
	else if (ec==2)
		{
		// item not added to the cart, out of stock
		alert("Sorry, that item is currently not in stock.");
		}
	else if (ec==3)
		{
		// item not added to the cart, request exceeds stock
		alert("Sorry, your requested quantity exceeds that item's current stock.");
		}
	else if (ec==4)
		{
		// item already in the wishlist cart, ask if another should be added
		if (window.confirm("That item is already in your wishlist cart.\nDo you want to add another?"))
			{
			// alert("Forcing the add to the wishlist!\nURL: cart_add.php?id="+productID+"&rid="+regListID+"&frc=1");
			addToCartRLForce(productID, regListID);
			}
		}
	else if (ec==99)
		{
		// user is not logged in
		alert("Before shopping at PhilShopping.com, you should be logged in.\nPlease login at the top of the page, or sign up for a free new account!");
		}
	else if (ec==0)
		{
		// item successfully added to the cart
		// update the cart by refreshing frames
		CartFrameMaxi.location.href = "cart_maxi.php";
		WishlistFrameMaxi.location.href = "wishlist_maxi.php";
		}

	objTimer = setTimeout("document.getElementById('loadingNormal').style.display = 'none';",1000);
	objTimer = setTimeout("document.getElementById('wishlistLoadingNormal').style.display = 'none';",1000);
	}

function addToWishList(listID, productID)
	{
	hideWishList();
	var index = ajax.length;
	ajax[index] = new sack();
	ajax[index].requestFile = 'wishlist_add.php?id=' + listID + "&pid=" + productID;	// Specifying which file to get
	ajax[index].onCompletion = function(){ wlItemAdded(index) };	// Specify function that will be executed after file has been found
	ajax[index].runAJAX();		// Execute AJAX function
	}

function wlItemAdded(index)
	{
	eval(ajax[index].response);

	// check if the item is already in the wishlist
	if (ec==1)
		{
		// item already in the cart
		alert("That item is already in that wishlist.\nYou may change the quantity of this item in \"My Shopping Lists\".");
		}
	else if (ec==0)
		{
		// item successfully added to the cart
		// update the cart by refreshing frames
		alert("The item was added to your wishlist!");
		}
	}

function checkUserName()
	{
	changeHasChecked = 0;
	var index = ajax.length;
	ajax[index] = new sack();
	ajax[index].requestFile = 'check_user.php?usr=' + document.frmReg.username.value;	// Specifying which file to get
	ajax[index].onCompletion = function(){ userChecked(index) };	// Specify function that will be executed after file has been found
	ajax[index].runAJAX();		// Execute AJAX function
	}

function userChecked(index)
	{
	eval(ajax[index].response);
	// check if the username is available
	if (ec==1)
		{
		document.getElementById("checkIcons").innerHTML = "<DIV ID=\"buffer\" STYLE=\"width: 60px; height: 10px;\"><!-- --></DIV><A HREF=\"javascript:checkUserName();\"><IMG SRC=\"./images/icon_deny.png\" WIDTH=60 HEIGHT=60 ALT=\"Username taken!\" TITLE=\"Username taken!\" BORDER=0></A><BR><A HREF=\"javascript:checkUserName();\">Username already taken</A>";
		formHasError = 1;
		}
	else if (ec==2)
		{
		document.getElementById("checkIcons").innerHTML = "<DIV ID=\"buffer\" STYLE=\"width: 60px; height: 10px;\"><!-- --></DIV><A HREF=\"javascript:checkUserName();\"><IMG SRC=\"./images/icon_deny.png\" WIDTH=60 HEIGHT=60 ALT=\"No username entered!\" TITLE=\"No username entered!\" BORDER=0></A><BR><A HREF=\"javascript:checkUserName();\">No username entered!</A>";
		formHasError = 3;
		}
	else
		{
		document.getElementById("checkIcons").innerHTML = "<DIV ID=\"buffer\" STYLE=\"width: 60px; height: 10px;\"><!-- --></DIV><IMG SRC=\"./images/icon_ok.png\" WIDTH=60 HEIGHT=60 ALT=\"Username available!\" TITLE=\"Username available!\" BORDER=0><BR>Username available!";
		formHasError = 0;
		}
	}

function verifyRegistration(actValue, varValue)
	{
	var index = ajax.length;
	ajax[index] = new sack();
	ajax[index].requestFile = 'verify_registration.php?act=' + actValue + '&var=' + varValue + "";	// Specifying which file to get
	ajax[index].onCompletion = function(){ regVerified(index) };	// Specify function that will be executed after file has been found
	ajax[index].runAJAX();		// Execute AJAX function
	}

function regVerified(index)
	{
	eval(ajax[index].response);

	if (ec==1)
		{
		// email address verified, now check the security code
		document.getElementById("step3Title").style.background = "#E5FFE5";
		document.getElementById("errorMsgDiv").innerHTML = "<DIV ID=\"buffer\" STYLE=\"width: 500px; height: 7px;\"><!-- --></DIV> &nbsp; <IMG SRC=\"./images/verify_anim.gif\" WIDTH=16 HEIGHT=16 ALT=\"Verifying security code...\" TITLE=\"Verifying security code...\" BORDER=0 ALIGN=ABSMIDDLE> &nbsp; <FONT COLOR=\"#666666\">Verifying security code...</FONT>";
		verifyRegistration('captcha', document.frmReg.security_code.value);
		}
	else if (ec==2)
		{
		// invalid email address entered
		// enable the buttons
		document.getElementById("frmButtons").style.display = "block";
		errorMessage = "Email verification failed, please enter a new one.";
		document.getElementById("step3Title").style.background = "#FFE5E5";
		document.getElementById("errorMsgDiv").innerHTML = "<DIV ID=\"buffer\" STYLE=\"width: 500px; height: 4px;\"><!-- --></DIV> &nbsp; <IMG SRC=\"./images/icon_warning.png\" WIDTH=23 HEIGHT=22 ALT=\"Error!\" TITLE=\"Error!\" BORDER=0 ALIGN=ABSMIDDLE> &nbsp; <FONT COLOR=\"#BB0000\">An error occurred: " + errorMessage + "</FONT>";
		}
	else if (ec==3)
		{
		// correct security code entered
		document.getElementById("step3Title").style.background = "#E5FFE5";
		document.getElementById("errorMsgDiv").innerHTML = "<DIV ID=\"buffer\" STYLE=\"width: 500px; height: 4px;\"><!-- --></DIV>";
		// disable the buttons
		document.getElementById("frmButtons").style.display = "none";
		document.frmReg.submit();
		}
	else if (ec==4)
		{
		// incorrect security code entered
		// enable the buttons
		document.getElementById("frmButtons").style.display = "block";
		errorMessage = "Incorrect security code entered, please try again.";
		document.getElementById("step3Title").style.background = "#FFE5E5";
		document.getElementById("errorMsgDiv").innerHTML = "<DIV ID=\"buffer\" STYLE=\"width: 500px; height: 4px;\"><!-- --></DIV> &nbsp; <IMG SRC=\"./images/icon_warning.png\" WIDTH=23 HEIGHT=22 ALT=\"Error!\" TITLE=\"Error!\" BORDER=0 ALIGN=ABSMIDDLE> &nbsp; <FONT COLOR=\"#BB0000\">An error occurred: " + errorMessage + "</FONT>";
		}
	}
