

/** basic browser definitions. **/

var AgntUsr=navigator.userAgent.toLowerCase();
var DomYes=(document.getElementById)?1:0;				// DOM
var NavYes=(AgntUsr.indexOf('mozilla')!=-1&&AgntUsr.indexOf('compatible')==-1)?1:0;	// Netscape
var ExpYes=(AgntUsr.indexOf('msie')!=-1)?1:0;				// IExplorer
var Opr5=(AgntUsr.indexOf('opera 5')!=-1||AgntUsr.indexOf('opera/5')!=-1)?1:0;	// Opera 5
var DomNav=(DomYes&&NavYes)?1:0;					// Netscape 6 up
var DomExp=(DomYes&&ExpYes)?1:0;					// IExplorer 5 up
var Nav4=(NavYes&&!DomYes&&document.layers)?1:0;			// Netscape 4
var Exp4=(ExpYes&&!DomYes&&document.all)?1:0;				// IExplorer 4
var PosStrt=((NavYes||ExpYes)&&!Opr5)?1:0;				// Can start

/** End browser detection. **/

/** the following functions are designed to be used to control a set of list menus where the data is sourced from bar delimited strings.**/

var distinctElements,elements,formOb,indexArray;
indexArray = 0;
var indexRef = 0;
var saveSelected = 0;//set to false.

distinctElements = new Array();
elements = new Array();

function loadMenu(menuName,srcControl,distinct,formObject){
//alert("Load Menu Called");
clearElements();//clear the elements array.
indexArray = 0;//duhjhhhhhh, I forgot to reset this before and multiple calls to this function could result in invalid index's..
if(!formObject)formOb = document.forms[0];
	else formOb = formObject;
if(!formOb){
	alert("No form in page. cannot continue");
	return;
	}

//try and find out if the menu has an index array associated with it.

if(indexRef){
	//alert("Found the index ref for: " + menuName);
	if(indexRef.indexOf(menuName + "_index") != -1)indexArray = eval(menuName + "_index");
	}


if(distinct && srcControl)loadRelated(menuName,srcControl,distinct);
	else if(srcControl)loadRelated(menuName,srcControl);
		else if(distinct)loadDistinct(menuName);
			else loadNonDistinct(menuName);

elements.sort(descending);
loadControl(menuName);//load the sorted array into the listbox.
clearElements();//clear the elements array.
setSavedSelected(menuName);//lastly if a value for this list has been saved from a previous session select it.
}

function loadDistinct(menuName){

	//alert("Menu Name = " + menuName);
	/*var els = formOb.elements;
	for(var x=0; x < els.length; x++){
		if(els[x].name == menuName)control = els[x];
		alert("el Name = " + els[x].name);
		}*/

	control = formOb[menuName];
	srcArray = eval(menuName);
	distinctElements.length = 0;
	control.options.length = 0;
	control.options[0] = new Option("","");

	for(var x=0; x < srcArray.length; x++){
		addDistinctElement(srcArray[x],x);
		}
}

function loadNonDistinct(menuName){

	control = formOb[menuName];
	srcArray = eval(menuName);
	distinctElements.length = 0;
	control.options.length = 0;
	control.options[0] = new Option("","");

	for(var x=0; x < srcArray.length; x++){
		addElement(srcArray[x],x);
		}
}

function addDistinctElement(value,index){
for(var x=0; x < distinctElements.length; x++){
	if(distinctElements[x] == value)return;
	}
distinctElements[distinctElements.length] = value;
addElement(value,index);
}

function addElement(value,index){
	if(!value)return;
	if(value.length == 0)return;//ignore empty or null values.
	//check for a proper index.
	if(indexArray){
		index = indexArray[index];
		//alert("Found an index Array, index index = " + index);
		}
	//alert("name = " + value + " & index = " + index);
	elements[elements.length] = new formObject(value,index);
	}

function descending(fo1,fo2){
	var fo1Num,fo2Num;
	var fo1Char,fo2Char;

	fo1Char = fo1.name.substring(0,1).toLowerCase();
	fo2Char = fo2.name.substring(0,1).toLowerCase();
	fo1Num = fo1Char.charCodeAt(0);
	fo2Num = fo2Char.charCodeAt(0);

	return fo1Num - fo2Num;
	}

function loadRelated(menuName,srcControl,distinct){
var menuArray = eval(menuName);
var srcArray = eval(srcControl);

var srcControlIndex = -1;

var destControl = formOb[menuName];

srcControl = formOb[srcControl];

if(!srcControl.selectedIndex || srcControl.selectedIndex == -1)return;

//alert(srcControl.options[srcControl.selectedIndex].value);

srcControlIndex = new Number(srcControl.options[srcControl.selectedIndex].value);
//alert("srcControlIndex = " + srcControlIndex);
if(srcControlIndex == -1)return;

var likeThis = srcArray[srcControlIndex];

for(var x=0; x < menuArray.length; x++){
	if(srcArray[x] == likeThis){
		if(!distinct)addElement(menuArray[x],x);
			else addDistinctElement(menuArray[x],x);
		}
	}
}

function  clearElements(){
	//distinctElements.length = 0;
	//elements.length = 0;
	distinctElements = new Array();
	elements = new Array();
	//alert(elements.length);
	}

function formObject(name,value){
this.name = name;
this.value = value;
}

function loadControl(menuName){
var object = formOb[menuName];
if(object.type == "text")return;
object.options.length = 0;
object.options[0] = new Option("","-1");

//alert("Set length to 0 & elements.length = " + elements.length);

for(var x=0; x < elements.length; x++){
	object.options[object.options.length] = new Option(new String(elements[x].name),new String(elements[x].value));
	}
}

function clearList(menuName){
if(!formOb)formOb = document.forms[0];
if(!formOb){
	alert("Invalid request, as you don't have a form in the page, and / or form[0] is invalid");
	return;
	}
var object = formOb[menuName];
object.options.length = 0;
}

function extendArrays(arrayNames,newValues,loadFunction){

listNames = arrayNames.split(",");
newValues = newValues.split("|");
var tempList;

for(var x=0; x < listNames.length; x++){
	tempList = eval(listNames[x]);
	//if(!tempList)continue;//hmm, seem to get some wierd stuff here, like extra elements that aren't there...
	tempList[tempList.length] = newValues[x];//extend the list.

	}

	eval(loadFunction);//hmm why not work ??

}

function setSelected(name){//sets the selected option to the last element from the src Array.

var srcArray = eval(name);
var object = formOb[name];
var value = srcArray[srcArray.length-1];

for(var x=0; x < object.options.length; x++){
	if(object.options[x].text == value){
		object.selectedIndex = x;
		//alert("OK");
		}
	}

}

function setSavedSelected(name){

var object = formOb[name];
var value = 0;

//alert("Name = " + name);

if(saveSelected){
	if(saveSelected.indexOf(name) != -1)
		value = (eval(name + "_saveSelected"));
}
else return;//no saved values to reference.


if(useText){
	for(var x=0; x < object.options.length; x++){
		//alert("value = " + object.options[x].text + " & saved_value = " + value);
		if(object.options[x].text == value){
			object.selectedIndex = x;
			break;
			}
		}
	}//end f

else{
	for(var x=0; x < object.options.length; x++){
		//alert("value = " + object.options[x].value + " & saved_value = " + value);
		if(object.options[x].value == value){
			object.selectedIndex = x;
			break;
			}
		}
	}//end else

}

function set_selected(form_ob,value){//sets the selected option to the given element from the src Array.
//alert("form_ob = " + form_ob + " value = " + value);
for(var x=0; x < form_ob.options.length; x++){
	if(form_ob.options[x].value == value){
		form_ob.selectedIndex = x;
		//alert("OK");
		}
	}

}

/** End list box functions **/

/**Next a set of functions to simplify creating popup windows. Keeping track of all open windows, and cleaning up any that are left over
when the user exits the site.**/

var popWin = null;

var windows = [];
var windowNames = [];

function showPopup(pageLocation,reload,args){
//alert("Showpopup called. popWin = " + popWin);
if(popWin == null){
	//alert("popWin == null");
	popWin = window.open(pageLocation,"", args);
	return;
	}
else if(popWin.closed)popWin = window.open(pageLocation, "popWin", args);

	if(reload){
		popWin.document.location.reload(true);
      popWin.focus();
		}
		else popWin.focus();
}

function make_child(baseLocation,jspArgs,pageArgs,reload,callerWindow,winName,top,left){

var hasOpenWindow = false;
var foundWin = false;
if(callerWindow)curWin = callerWindow;
else curWin = window;

var posArgs = calculateLocation(curWin,pageArgs,top,left);

pageArgs = pageArgs + posArgs;

pageArgs = "toolbar=no,status=no,menubar=no,location=no,directories=no," + pageArgs;

//alert("page_args = " + pageArgs);

if(!winName)winName = baseLocation;

//alert("args = " + jspArgs);
//alert(callerWindow);
//alert("callerWindowName = " + callerWindow.name);

for(var x=0; x < windowNames.length; x++){
   if(windowNames[x] == winName)foundWin = true;
   }

if(!foundWin)windowNames[windowNames.length] = new String(winName);

if(windows[winName]){
   if(!windows[winName].closed){
      if(reload){
			//windows[winName].close();//if we don't do this we can end up in a right mess with the base application window being reloaded and all sorts.
			windows[winName].document.location = baseLocation + "?" + jspArgs;
			}
      windows[winName].focus();
      }
      else windows[winName] = curWin.open(baseLocation + "?" + jspArgs,"",pageArgs);
   }
   else windows[winName] = curWin.open(baseLocation + "?" + jspArgs,"",pageArgs);

	return windows[winName];

}

function getWin(winName){

var foundWin = false;

for(var x=0; x < windowNames.length; x++){
	//alert("winName " + x + " = " + windowNames[x]);
   if(windowNames[x] == winName){
		foundWin = true;
		//alert("winName = " + winName);
			if(!windows[winName].closed)return windows[winName];
				else return false;
		}
   }
}

function closeAllChildren(topLocation){
return;//comment out when want to re-enable.
//window.open("/generic/logOff.jsp?ID=" + sessionUserID,"","width=10,height=10");
//alert("Close All Children called " + windowNames.length);
   for(var x=0; x < windowNames.length; x++){
      if(!windows[windowNames[x]].closed)windows[windowNames[x]].close();
      }
}


function getTop(ignoreErrors){

//alert("Get top called");

var tempWin;

tempWin = window;
if(!tempWin.top.nav){//we are not in the top level frameset yet.

while(tempWin.opener){
   tempWin = tempWin.opener;
	if(tempWin.top.nav)break;//the nav bar is not neccesarily in the top level window !!!!!
   if(!tempWin.opener){
      if(tempWin.top.opener)tempWin = tempWin.top.opener;
      }
   }
}
//if the window is in a frameset then we go straight to here.

if(!tempWin.top.nav){
   tempWin = top;
   while(tempWin.opener){
      tempWin = tempWin.opener;
		if(tempWin.top.nav)break;//the nav bar is not neccesarily in the top level window !!!!!
      /**check to see if our opener was in a frameset, which case find the frameset and check it's opener.**/
      if(!tempWin.opener){
         if(tempWin.top.opener)tempWin = tempWin.top.opener;
         }
      }
   }
   if(tempWin.top.nav);//alert("Got nav bar");
      else {
			if(!ignoreErrors){
         	alert("Can't find base window. please close all browser windows and connect again.");
         	//window.close();
				}
				else return window;
         }

//alert("tempWin = " + tempWin);

return tempWin.top.nav;
}

function calculateLocation(winObject,winArgs,top,left){

var screenX,screenY;
var winX,winY,width,height;

if(!winArgs){
	alert("No window arguments sent. Make sure you are using 'make_child()' properly");
	return ("0,0");
	}

if(winArgs.indexOf("width=") == -1){
	alert("Invalid child def.\n you must include width and height parameters or the window will not be centered");
	return("0,0");
	}

if(!top){
screenX = winObject.screen.width;
screenY = winObject.screen.height;

width = winArgs.substring(winArgs.indexOf("width=")+6);
height = winArgs.substring(winArgs.indexOf("height=")+7);

if(width.indexOf(",") > 0)
	width = new Number(width.substring(0,width.indexOf(",")));
	else width = new Number(width);
if(height.indexOf(",") > 0)
	height = new Number(height.substring(0,height.indexOf(",")));
	else height = new Number(height);

winX = (screenX/2) - (width/2);
winY = (screenY/2) - (height/2);
	}
	else {
		winX = left;
		winY = top;
		}
//alert("winX = " + winX + " , winY = " + winY);

//alert("width = " + width + ", height = " + height);

//alert("parent X = " + screenX);

var positionArgs;

if(NavYes)positionArgs = ",screenx=" + winX + ",screeny=" + winY;
else positionArgs = ",left=" + winX + ",top=" + winY;

return(positionArgs);
}

function arrayDesc(element1,element2){
	var strEl1,strEl2,val1,val2;

	strEl1 = new String(element1);//do this in case the array is numeric.
	strEl2 = new String(element2);

	val1 = strEl1.toLowerCase().charCodeAt(0);
	val2 = strEl2.toLowerCase().charCodeAt(0);

	return val1-val2;
	}


function changeImage(tableName,imageField,tableIndex){
modImage("CHANGE",tableName,imageField,tableIndex);
}

function deleteImage(tableName,imageField,tableIndex){
modImage("DELETE",tableName,imageField,tableIndex);
}

function viewImage(tableName,imageField,tableIndex){
modImage("VIEW",tableName,imageField,tableIndex);
}

function modImage(action,tableName,imageField,tableIndex){
	if(action != "VIEW")getTop().make_child("modImage.jsp","tableName=" + tableName + "&imageField=" + imageField + "&tableIndex=" + tableIndex + "&action=" + action,"width=300,height=300,scrollbars=yes,resizable=yes",true,window);
		else getTop().make_child("/pages/members/viewImage.jsp","tableName=" + tableName + "&imageField=" + imageField + "&tableIndex=" + tableIndex,"width=300,height=300,scrollbars=yes,resizable=yes",true,window);
}

function containsValue(selectObject,value){//use to scan a select box for an arbitary value.
	for(var x=0; x < selectObject.options.length; x++){
		if(selectObject.options[x].value == value)return x+1;//value found so return true. making sure it is not false !!!
		}
return false;//value not found.

}

function showItem(itemID,template,shopID){
//alert("ShopID = " + shopID);
if(template)
	document.location = "/pages/members/itemtemplates/" + template + "?itemID=" + itemID + "&shopID=" + shopID;
	else document.location = "/pages/members/itemtemplates/showItem.jsp?itemID=" + itemID + "&shopID=" + shopID

//getTop().make_child("/pages/members/itemtemplates/" + template,"itemID=" + itemID + "&shopID=" + shopID,"width=500,height=550,scrollbars=yes,resizable=yes",true,window);
//else getTop().make_child("/pages/members/itemtemplates/showItem.jsp","itemID=" + itemID + "&shopID=" + shopID,"width=500,height=550,scrollbars=yes,resizable=yes",true,window);

}

function showAllItems(template,shopID,specialism){
if(specialism == -1){
	alert("No specialist subject has been stated by this seller.");
	return;
}
if(specialism)document.location = "/pages/members/allitemtemplates/" + template + "?shopID=" + shopID + "&specialism=" + specialism;
	else document.location = "/pages/members/allitemtemplates/" + template + "?shopID=" + shopID;
}

function showSoldItems(template,shopID){
document.location = "/pages/members/allitemtemplates/" + template + "?shopID=" + shopID + "&sold=true";
}

function showNewItems(template,shopID){
document.location = "/pages/members/allitemtemplates/" + template + "?shopID=" + shopID + "&new=true";
}

function showLocation(postcode){
getTop().make_child("http://www.streetmap.co.uk/streetmap.dll","P2M?P=" + escape(postcode) + "&Z=1","width=700,height=600,scrollbars=yes,resizable=yes",false,window);
}

function showShop(template,shopID,businessName,postcode){

if(!template){
	alert("You must select a template for your shop first");
	return;
}
	else if(template.indexOf(".jsp") == -1){
		alert("You must select a template for your shop first");
		return;
	}

getTop().make_child("/pages/members/templates/" + template,"shopID=" + shopID + "&title=" + escape(businessName) + "&postcode=" + escape(postcode),"width=800,height=600,scrollbars=yes,resizable=yes",true,window);
}

function goHome(){
getTop(true).closeAllChildren();
getTop(true).document.location = "/index.jsp";
}

function contactSeller(itemID){
getTop(true).make_child("../contact.jsp","itemID=" + itemID,"width=600,height=400,scrollbars=yes,resizable=yes",true,window);
}

function swap_image(image_name,new_src){

	if(!document["" + image_name + ""])alert("Can't find Image name in document: '" + image_name + "'");
		else document["" + image_name + ""].src = new_src;
	}

function highlight(object){

object.style.color = object.style.color2;
}

function lowlight(object){
object.style.color = object.style.color1;
}

function add_book_mark(title){
	if(ExpYes)window.external.AddFavorite(document.location,title);
		else alert("Sorry this only works for Internet Explorer");
}

var toggle = false;
function select_all_checks(form_ob){
toggle = !toggle;
	with(form_ob){
		var els = elements;
		var em_len = els.length;
		for(var x=0; x < em_len; x++){
			if(els[x].type == "checkbox")els[x].checked = toggle;
			}
		}
	}
	
//probably never needed
function deselect_all_checks(form_ob){
	with(form_ob){
		var els = elements;
		var em_len = els.length;
		for(var x=0; x < em_len; x++){
			if(els[x].type == "checkbox")els[x].checked = false;
			}
		}
	}
	
function new_window(location_url,page_args,window_args){
	var position_args;
    if(!window_args)window_args = "width=100,height=100";
if(NavYes)position_args = ",screenx=0,screeny=0";
	else position_args = ",left=0,top=0";
    window.open(location_url + "?" + page_args,"",window_args + position_args);
	}

var is_pressed = false;
function depress_button(button_ob){
	//alert("button pressed");
	//if(!is_pressed)
		button_ob.style.border = "2px inset #eeeeee";
		//else button_ob.style.border = "2px outset #eeeeee";
	//is_pressed = !is_pressed;
	}
	
function release_button(button_ob,command){
	button_ob.style.border = "2px outset #eeeeee";
	if(command)eval(command);
}

