// pageView.js
//uses Prototype.js
// This script contains function that control element behavior and layout.

function urlValuePairResult(theURL,varName){
	var tmpStr = "";
	var pos1 = theURL.indexOf(varName+'=');
	if(pos1>=0){
		tmpStr = theURL.substring(pos1+varName.length+1,theURL.length);
		var pos2 =  tmpStr.indexOf('&');
		if(pos2>0){
			tmpStr = tmpStr.substring(0,pos2);
		}
		else{
			tmpStr = tmpStr.substring(0,tmpStr.length);
		}
	}
	return tmpStr;
}

function refreshMyItems(){
	if(document.getElementById("myItemsWrapper")){
		new Ajax.PeriodicalUpdater("myItemsWrapper","myItems.php?dispMode=small&sort=status",{
								   frequency:20,decay:1});	
	}
	if(document.getElementById("my_items_body")){
		var theURL = location.href;
		var sortType = urlValuePairResult(theURL,"sort");
		new Ajax.PeriodicalUpdater("my_items_body","myItems.php?dispMode=normal&sort="+sortType,{
								   frequency:20,decay:1});	
	}
}

function resizeItemView(){
	/*var obj;
	if(obj=document.getElementById("item_box")){ 	
		var lp = document.getElementById("left");
		var rp = document.getElementById("right");
		var h = lp.offsetHeight>rp.offsetHeight?lp.offsetHeight+4:rp.offsetHeight-25;
		var bh = browserWindowSize();
		if(h>bh[1]){
			obj.style.height = (h-10)+"px";
		}else{
			obj.style.height = (bh[1]-223)+"px";
		}
	}*/
}

function browserWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth,myHeight];
}

//This function modifies the bid button text based on the value of the bid amount list value.
function BidButtonText(unitValue){
	if(document.getElementById("bid_amount")){
		var theBtn = document.getElementById("btn_bid");
		var theBid = document.getElementById("bid_amount");
		var index = theBid.selectedIndex;
		theBtn.value = "Bid $"+theBid.options[index].value;
	}
	else if(document.getElementById("unit_count")){
		var theValue = document.getElementById("theValue");
		var theBtn = document.getElementById("btn_bid");
		var theUnits = document.getElementById("unit_count");
		var index = theUnits.selectedIndex;
		var unitCt = theUnits.options[index].value;
		if(unitValue>0){
			theBtn.value = "Buy - "+unitCt+" X $"+unitValue+" = $"+(unitCt*unitValue);
		}
		else{
			theBtn.value = "Buy - 1 X "+theValue.innerHTML+" = "+theValue.innerHTML;
		}
	}
}

//This function enables or disables the Bid button based on the status of the BidTerms checkbox.
function BidTermsCB(){
	if(document.getElementById("bid_cb")){
		var termsCB = document.getElementById("bid_cb");
		var theBtn = document.getElementById("btn_bid");
		if (!termsCB.checked){
			theBtn.disabled = true;
		}
		else{
			theBtn.disabled = false;
		}
	}
}

//This function sorts the item list
function SortItems(){
	if(document.getElementById("item_sort")){
		var sortList = document.getElementById("item_sort");
		var index = sortList.selectedIndex;
		var theURL = location.href;
		var pos = theURL.indexOf('&sort=');
		if(pos>=0){
			window.location = theURL.substring(0,pos)+'&sort='+sortList.options[index].value;	
		}
		else{
			window.location = location.href+'&sort='+sortList.options[index].value;
		}
	}
}

function goToPage(){
	var obj = this;
	if(obj!=null){
		var idx = obj.selectedIndex;
		var page = obj.options[idx].value;
		var theURL = location.href;
		var pos = theURL.indexOf('&page=');
		if(pos>=0){
			window.location = theURL.substring(0,pos)+'&page='+obj.options[idx].value;	
		}
		else{
			window.location = location.href+'&page='+obj.options[idx].value;
		}
	}
}

function UpdateBidFormDisplay(){
	resizeItemView();
	BidButtonText(-1);
	BidTermsCB();
	refreshMyItems();
	if(obj=document.getElementById('page_select1')){
		obj.onchange = goToPage;	
	}
	if(obj=document.getElementById('page_select2')){
		obj.onchange = goToPage;	
	}
}

function onLoadFunctions(){
	setEvents();
	UpdateBidFormDisplay();
	setInterval('blinkIt()',500);
}

function setEvents(){
	var obj;
	if(obj=document.getElementById("forgot_link")){
		obj.onclick=onForgetPWClick;
	}
	if(obj=document.getElementById("logout_link")){
		obj.onclick=onLogoutLinkClick;
	}
}

function onForgetPWClick(){
	var obj;
	if(obj=document.getElementById("logaction")){
		obj.name="forgotpw";
	}
	document.LoginForm.submit();
}

function onLogoutLinkClick(){
	document.LogoutForm.submit();
}

window.onload=onLoadFunctions;
window.onresize = resizeItemView;
