var allChecked=false;

var LightboxOptions = {
    fileLoadingImage:        fileLoadingImage,
    fileBottomNavCloseImage: fileBottomNavCloseImage
};

function addImage(fileUrl) {
	findObj('previewImage').src = fileUrl;
	findObj('imageName').value = fileUrl;
}

function addRows(parent, quantity) {
	var qty			= document.getElementById(quantity).value;
	var rowCount	= document.getElementById('rowCount').value;
	var parentNode	= document.getElementById(parent);

	qty = (qty >= 1)  ? qty: 1;
	qty = (qty <= 20) ? qty: 20;

	for (i=0; i<qty; i++) {
		var newRow		= document.createElement('tr');
		newRow.setAttribute('id', 'orderRow_'+rowCount);
		parentNode.appendChild(newRow);
		newRow.innerHTML = '<td valign="top"><a href="" onclick="return delRow(\'orderRow_'+rowCount+'\')"><img src="admin/images/del.gif" alt="" /></a></td><td valign="top"><input type="hidden" name="id['+rowCount+']" value="" /><input type="text" name="prodName['+rowCount+']" class="textbox" value="" /></td><td valign="top"><input name="productCode['+rowCount+']" type="text" class="textbox" value="" size="15" /></td><td valign="top"><textarea name="product_options['+rowCount+']" cols="30" rows="1" class="textbox"></textarea></td><td align="center" valign="top"><input name="quantity['+rowCount+']" type="text" class="textbox" style="text-align:center;" value="" size="3" /></td><td align="center" valign="top"><input name="price['+rowCount+']" type="text" class="textbox" style="text-align:right;" value="" size="7" /></td>';
		rowCount++;
	}
	document.getElementById('rowCount').value = rowCount;
	return false;
}

function checkAll(fieldName,val) {
	var checks = findObj(fieldName)
	for (i=0; i<checks.length; i++) {
		if(val == 'true') {
			checks[i].checked = true;
		} else {
			checks[i].checked = false;
		}
	}
}

function checkUncheck(parent, className) {
	var elements = $(parent).getElementsByClassName(className);
	for (i=0;i<elements.length;i++) {
		var ele = elements[i];
		(allChecked==false) ? ele.checked='checked' : ele.checked='';
	}
	(allChecked!=true) ? allChecked=true : allChecked=false;
	return false;
}

function compareInputbox(idNo) {
	if (findObj('custom_'+idNo) != findObj('default_'+idNo)) {
		// show revert buttons
		findObj('revertLink_'+idNo).style.display = '';
		findObj('revAllLink').style.display='';
		var tds = findObj('tr_'+idNo).getElementsByTagName('td');
		for (var i = 0; i <= 2; i++) {
			tds[i].className = 'tdModified';
		}
	}
}

function decision(message, url) {
	if (confirm(message)) {
		location.href = url;
	}
}

function delRow(element) {
	var node	= document.getElementById(element);
	var parent	= node.parentNode;
	parent.removeChild(node);
	return false;
}

function disableSubmit(obj,msg) {
	obj.value=msg;
	obj.disabled=true;
	obj.disabled=true;
	obj.className='submitDisabled';
}

function displayStatusMsg(msgStr) { //v1.0
	status=msgStr;
	document.returnValue = true;
}

function editVal(id,val) {
	findObj(id).value = val;
}

function findObj(n, d) {
	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=findObj(n,d.layers[i].document);
	if (!x && d.getElementById) x=d.getElementById(n);
	return x;
}

function flashBasket(flashes) {
	setTimeout("flash("+flashes+")", 500);
}

function flash(flashes) {
	var targetBtn = findObj('flashBasket');
	if (flashes<=0) return;
	if ((flashes%2) == 0) {
		targetBtn.className="flashBasket";
	} else {
 		targetBtn.className="txtviewCart";
	}
	setTimeout("flash("+(flashes-1)+")", 300);
	return;
}

function getImage(imageName) {
	var img	= findObj('img');
	img.src	= img.src.replace(/language\/[a-z\-_]{2,5}\/flag.gif/gi, imageName);
}

function setMainImage(imageName) {
	findObj('img').src = imageName;
}

function goToURL() { //v3.0
  var i, args=goToURL.arguments; document.returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function jumpMenu(target, object, restore) {
	eval(target+".location='"+object.options[object.selectedIndex].value+"'");
	if (restore) object.selectedIndex=0;
}

function menuBlinds() {

}

function openPopUp(url, windowName, w, h, scrollbar) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scrollbar+',resizable=1';
	win = window.open(url, windowName, winprops);
	if (parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}

function resizeOuterTo(w,h) {
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			top.outerWidth=w;
			top.outerHeight=h;
		} else {
			top.resizeTo(w,h);
		}
	}
}

function revert(idNo,className) {
	var tds = findObj('tr_'+idNo).getElementsByTagName('td');
	for (var i = 0; i <= 2; i++) {
		tds[i].className = className;
	}
	findObj('custom_'+idNo).value = findObj('default_'+idNo).value;
	findObj('revertLink_'+idNo).style.display = 'none';
}

function revertAll(maxId) {
	for (var idNo = 1; idNo < maxId; idNo++) {
		// find array of TD's
		var tds = findObj('tr_'+idNo).getElementsByTagName('td');
		var binary = idNo.toString(2);
		for (var j = 0; j <= 2; j++) {
			// restore class
			if (binary.charAt(binary.length - 1) == "1") {
				tds[j].className = 'tdOdd';
			} else {
				tds[j].className = 'tdEven';
			}
		}
		// set default value back
		findObj('custom_'+idNo).value = findObj('default_'+idNo).value;
		// hide revert button
		findObj('revertLink_'+idNo).style.display = 'none';
	}
	// hide revert all button
	findObj('revAllLink').style.display='none';
}

function setTextOfLayer(objName,x,newText) {
	if ((obj=findObj(objName))!=null) with (obj)
		if (document.layers) {document.write(unescape(newText)); document.close();}
		else innerHTML = unescape(newText);
}

function submitDoc(formName) {
	var obj=findObj(formName);
	if (obj!=null) {
		obj.submit();
	} else {
		alert('The form you are attempting to submit called \'' + formName + '\' couldn\'t be found. Please make sure the submitDoc function has the correct id and name.');
	}
}

function reloadPage(init) {  //reloads the window if Nav4 resized
	if (init==true) with (navigator) {
		if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    		document.pgW=innerWidth; document.pgH=innerHeight;
			onresize=reloadPage;
		}
	} else if (innerWidth!=document.pgW || innerHeight!=document.pgH) location.reload();
}
reloadPage(true);

function showHideLayers() { //v6.0
  var i,p,v,obj,args=showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function stars(rating, pathImg) {
	/* Positive Stars */
	for (var i = 0; i <= rating; i++) {
		if (i>0) findObj("star"+i).src = pathImg+'1.gif';
	}
	/* Negative Stars */
	for (var i = rating+1; i <= 5; i++) {
		findObj("star"+i).src = pathImg+'0.gif';
	}
	findObj('rating_val').value=rating;
}

function toggleReg() {
	var password = findObj('password');
	var passwordConf = findObj('passwordConf');
	var password_required = findObj('password_required');
	var passwordConf_required = findObj('passwordConf_required');
	if (password.disabled == false && passwordConf.disabled==false) {
		password.disabled=true;
		passwordConf.disabled=true;
		password.className="textboxDisabled";
		passwordConf.className="textboxDisabled";
		passwordConf_required.style.visibility="hidden";
		password_required.style.visibility="hidden";
	} else {
		password.disabled=false;
		passwordConf.disabled=false;
		password.className="textbox";
		passwordConf.className="textbox";
		passwordConf_required.style.visibility="visible";
		password_required.style.visibility="visible";
	}
	password.value="";
	passwordConf.value="";
}

function toggleProdStatus(i,messageRemove, messageNotRemoved,removeImgSrc,noRemoveImgSrc){
	if(findObj('delId['+i+']').value==1) {
		var result = false;
		var className = 'textbox';
		findObj('del['+i+']').src = removeImgSrc;
		findObj('delId['+i+']').value = 0;
		// change class
		findObj('prodName['+i+']').className = "textbox";
		findObj('productCode['+i+']').className = "textbox";
		findObj('product_options['+i+']').className = "textbox";
		findObj('quantity['+i+']').className = "textbox";
		findObj('price['+i+']').className = "textbox";
		alert(messageNotRemoved);
	} else {
		var result = true;
		var className = 'textboxDisabled';
		findObj('del['+i+']').src = noRemoveImgSrc;
		findObj('delId['+i+']').value = 1;
		// change class
		findObj('prodName['+i+']').className = "textboxDisabled";
		findObj('productCode['+i+']').className = "textboxDisabled";
		findObj('product_options['+i+']').className = "textboxDisabled";
		findObj('quantity['+i+']').className = "textboxDisabled";
		findObj('price['+i+']').className = "textboxDisabled";
		alert(messageRemove);
	}

	findObj('prodName['+i+']').disabled = result;
    findObj('prodName['+i+']').className = className;

	findObj('productCode['+i+']').disabled = result;
	findObj('productCode['+i+']').className = className;

	findObj('product_options['+i+']').disabled = result;
	findObj('product_options['+i+']').className = className;

	findObj('quantity['+i+']').disabled = result;
	findObj('quantity['+i+']').className = className;

	findObj('price['+i+']').disabled = result;
	findObj('price['+i+']').className = className;
}


function toggleProductStatus() {
	return false;
}

function goUrl(element) {
	var url = document.getElementById(element).options[document.getElementById(element).selectedIndex].value;
	window.location = url;
}



/* Start Cross-Browser DHTML Menu */
function showSubMenu() {
	var objThis = this;
	for (var i = 0; i  < objThis.childNodes.length; i++) {
		if (objThis.childNodes.item(i).nodeName == "UL")	{
			objThis.childNodes.item(i).style.display = "block";
		}
	}
}

function hideSubMenu() {
	var objThis = this;
	for (var i = 0; i  < objThis.childNodes.length; i++) {
		if (objThis.childNodes.item(i).nodeName == "UL") {
			objThis.childNodes.item(i).style.display = "none";
			return;
		}
	}
}

function initialiseMenu() {
	var objLICollection = document.body.getElementsByTagName("LI");
	for (var i = 0; i < objLICollection.length; i++) {
		var objLI = objLICollection[i];
		for (var j = 0; j  < objLI.childNodes.length; j++) {
			if(objLI.childNodes.item(j).nodeName == "UL") {
				objLI.onmouseover=showSubMenu;
				objLI.onmouseout=hideSubMenu;
				for (var j = 0; j  < objLI.childNodes.length; j++) {
					if(objLI.childNodes.item(j).nodeName == "A") {
						objLI.childNodes.item(j).className = "hassubmenu";
					}
				}
			}
		}
	}
}
/* End Cross-Browser DHTML Menu */

/* New Options code */
function optionEdit(assign_id, optionData) {
	var data		= optionData.split('|');
	var elements	= $('opt_mid').getElementsByTagName('option');
	for (i=0; i<elements.length; i++) {
		elements[i].removeAttribute('selected');

		if (data[1] == '0') {
			if (elements[i].value == data[0] && elements[i].getAttribute('class') == 'top') {
				elements[i].setAttribute('selected', 'selected');
				elements[i].selected = true;
			}
		} else {
			if (elements[i].value == data[1] && elements[i].getAttribute('class') == 'sub') {
				elements[i].setAttribute('selected', 'selected');
				elements[i].selected = true;
			}
		}
	}
	$('opt_price').value = data[2];
	$('opt_assign_id').value = assign_id;

	optionRemove(assign_id, true);
}

function optionRemoveTemp(element) {
	var object = $(element).parentNode
	$('options_added').removeChild(object);
}

function optionRemove(id, preserve) {
	$('option_'+id).remove();
	if (!preserve) {
		new Insertion.Bottom($('options_added'), '<input type="hidden" name="option_remove[]" value="'+id+'"/>');
	}
}

function optionAdd() {

	var assign_id = $('opt_assign_id').value;

	var opt_top_title = $('opt_mid').options[$('opt_mid').selectedIndex].parentNode.getAttribute('label');
	var opt_mid_title = $('opt_mid').options[$('opt_mid').selectedIndex].innerHTML;

	var opt_mid_value	= $('opt_mid').options[$('opt_mid').selectedIndex].value;
	var opt_top_value	= $('opt_mid').options[$('opt_mid').selectedIndex].parentNode.id;

	var opt_price	= $('opt_price').value;
	opt_price		= (!opt_price) ? 0.00 :  opt_price;

	if (opt_top_value != '' && opt_mid_value != '') {
		if (assign_id != '0') {
			var method	= 'option_edit['+assign_id+']';
			$('opt_assign_id').value = '0';
		} else {
			var method	= 'option_add[]';
		}
		if (opt_top_title == null) opt_top_title = 'Custom';
		var hidden = '<input type="hidden" name="'+method+'" value="'+opt_top_value+'|'+opt_mid_value+'|'+opt_price+'"/>';
		new Insertion.Bottom('options_added', '<div style="clear: right;">'+hidden+'<span style="float: right;"><a href="#" onclick="optionRemoveTemp(this.parentNode); return false;"><img src="images/icons/delete.png" alt="delete" /></a></span><strong>'+opt_top_title+'</strong>: '+opt_mid_title+' ('+opt_price+')</div>');
		$('opt_price').value = '0.00';
	}
}

function showloader() {
	var eltDims = $('loading').getDimensions();
	var browserDims = document.viewport.getDimensions();
	var y = (browserDims.height - eltDims.height) / 2;
	var x = (browserDims.width - eltDims.width) / 2;
	$('bg_fade').setStyle({
		display: 'block'
	});
	$('loading').setStyle({
		display: 'block',
		position:'absolute',
		'z-index' : 99999,
		top : y + 'px',
		left : x + 'px'
	});
}
function hideloader() {
	$('bg_fade').setStyle({
		display: 'none'
	});
	$('loading').setStyle({
		display: 'none'
	});
}

/* Tabbed Content by leeofaccy */
function switch_tabs(tab) {
	var tabs = document.getElementById('tabs').getElementsByTagName('li').length;
	//switch off all tabs and content
	for (i=0; i < tabs; i++) {
		document.getElementById('tab'+(i+1)).className = '';
		document.getElementById('tab'+(i+1)+'_content').style.display = 'none';
	}
	//switch on the selected tab and content
	document.getElementById(tab).className = 'selected';
	document.getElementById(tab+'_content').style.display = 'block';
}

/* Virtual pagination by leeofaccy */

document.write('<style type="text/css">' //write out CSS for class ".hidepeice" that hides pieces of contents within pages
	+'.hidepiece{display:none}\n'
	+'@media print{.hidepiece{display:block !important;}}\n'
	+'</style>')
	
function virtualpaginate(config){ //config: {piececlass:, piececontainer:, pieces_per_page:, defaultpage:, wraparound:, persist}
	this.piececlass=config.piececlass
	var elementType=(typeof config.piececontainer=="undefined")? "div" : config.piececontainer //The type of element used to divide up content into pieces. Defaults to "div"
	this.pieces=virtualpaginate.collectElementbyClass(config.piececlass, elementType) //get total number of divs matching class name
	//Set this.chunksize: 1 if "chunksize" param is undefined, "chunksize" if it's less than total pieces available, or simply total pieces avail (show all)
	this.chunksize=(typeof config.pieces_per_page=="undefined")? 1 : (config.pieces_per_page>0 && config.pieces_per_page<this.pieces.length)? config.pieces_per_page : this.pieces.length
	this.pagecount=Math.ceil(this.pieces.length/this.chunksize) //calculate number of "pages" needed to show the divs
	this.wraparound=config.wraparound || false
	this.paginatediv=[], this.flatviewlinks=[], this.cpspan=[], this.selectmenu=[], this.prevlinks=[], this.nextlinks=[]
	this.persist=config.persist
	var persistedpage=virtualpaginate.getCookie("dd_"+this.piececlass) || 0
	var urlselectedpage=virtualpaginate.urlparamselect(this.piececlass) //returns null or index from: mypage.htm?piececlass=index
	this.currentpage=(typeof urlselectedpage=="number")? urlselectedpage : ((this.persist)? persistedpage : config.defaultpage)
	this.currentpage=(this.currentpage<this.pagecount)? parseInt(this.currentpage) : 0 //ensure currentpage is within range of available pages
	this.showpage(this.currentpage) //Show selected page
}

// -------------------------------------------------------------------
// PUBLIC: navigate(keyword)- Calls this.showpage() based on parameter passed (0=page1, 1=page2 etc, "next", "first", or "last")
// -------------------------------------------------------------------

virtualpaginate.prototype.navigate=function(keyword){
	if ((!this.wraparound && keyword=="previous" && this.currentpage==0) || (!this.wraparound && keyword=="next" && this.currentpage==this.pagecount-1))
		return //exit immediately if wraparound is disabled and prev link is clicked when on 1st content or last link is clicked when on final content
	var prevlinkindex=this.currentpage //Get index of last clicked on page
	if (keyword=="previous")
		this.currentpage=(this.currentpage>0)? this.currentpage-1 : (this.currentpage==0)? this.pagecount-1 : 0
	else if (keyword=="next")
		this.currentpage=(this.currentpage<this.pagecount-1)? this.currentpage+1 : 0
	else if (keyword=="first")
		this.currentpage=0
	else if (keyword=="last")
		this.currentpage=this.pagecount-1 //last page number
	else
		this.currentpage=parseInt(keyword)
	this.currentpage=(this.currentpage<this.pagecount)? this.currentpage : 0 //ensure pagenumber is within range of available pages
	this.showpage(this.currentpage)
	for (var p=0; p<this.paginatediv.length; p++){ //loop through all pagination DIVs
		if (this.flatviewpresent)
			this.flatviewlinks[p][prevlinkindex].className="" //"Unhighlight" previous page (before this.currentpage increments)
		if (this.selectmenupresent)
			this.selectmenu[p].selectedIndex=this.currentpage
		if (this.flatviewpresent)
			this.flatviewlinks[p][this.currentpage].className="selected" //"Highlight" current page
	}
	if (!this.wraparound){
		for (var i=0; i<this.prevlinks.length; i++) //add or remove "disable" class from prev links depending on current page number
			virtualpaginate.setcssclass(this.prevlinks[i], "disabled", (this.currentpage==0)? "add" : "remove")
		for (var i=0; i<this.nextlinks.length; i++) //add or remove "disable" class from next links depending on current page number
			virtualpaginate.setcssclass(this.nextlinks[i], "disabled", (this.currentpage==(this.pagecount-1))? "add" : "remove")
	}
}


// -------------------------------------------------------------------
// PUBLIC: buildpagination()- Create pagination interface by calling one or more of the paginate_build_() functions
// -------------------------------------------------------------------

virtualpaginate.prototype.buildpagination=function(divids, optnavtext){
	var divids=(typeof divids=="string")? [divids] : divids //force divids to be an array of ids
	var primarypaginatediv=divids.shift() //get first id within divids[]
	var paginaterawHTML=document.getElementById(primarypaginatediv).innerHTML
	this.paginate_build(primarypaginatediv, 0, optnavtext)
	for (var i=0; i<divids.length; i++){
		document.getElementById(divids[i]).innerHTML=paginaterawHTML
		this.paginate_build(divids[i], i+1, optnavtext)
	}
}

// -------------------------------------------------------------------
// PRIVATE utility functions
// -------------------------------------------------------------------

virtualpaginate.collectElementbyClass=function(classname, element){ //Returns an array containing DIVs with specified classname. Requires setcssclass()
	if (document.querySelectorAll){
		var pieces=document.querySelectorAll(element+"."+classname) //return pieces as HTMLCollection
	}
	else{
		var pieces=[]
		var alltags=document.getElementsByTagName(element)
		for (var i=0; i<alltags.length; i++){
			if (virtualpaginate.setcssclass(alltags[i], classname, "check")) //if element carries class name in question
				pieces[pieces.length]=alltags[i] //return pieces as array
		}
	}
	return pieces
}

// -------------------------------------------------------------------
// PRIVATE: setcssclass() method- Checks, Add, or Removes a class from an element
// -------------------------------------------------------------------

virtualpaginate.setcssclass=function(el, targetclass, action){
	var needle=new RegExp("(^|\\s+)"+targetclass+"($|\\s+)", "ig")
	if (action=="check")
		return needle.test(el.className)
	else if (action=="remove")
		el.className=el.className.replace(needle, "")
	else if (action=="add" && !needle.test(el.className))
		el.className+=" "+targetclass
}

virtualpaginate.urlparamselect=function(vpclass){
	var result=window.location.search.match(new RegExp(vpclass+"=(\\d+)", "i")) //check for "?piececlass=2" in URL
	return (result==null)? null : parseInt(RegExp.$1) //returns null or index, where index (int) is the selected virtual page's index
}

virtualpaginate.getCookie=function(Name){ 
	var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
	if (document.cookie.match(re)) //if cookie found
		return document.cookie.match(re)[0].split("=")[1] //return its value
	return null
}

virtualpaginate.setCookie=function(name, value){
	document.cookie = name+"="+value
}

// -------------------------------------------------------------------
// PRIVATE: showpage(pagenumber)- Shows a page based on parameter passed (0=page1, 1=page2 etc)
// -------------------------------------------------------------------

virtualpaginate.prototype.showpage=function(pagenumber){
	var totalitems=this.pieces.length //total number of broken up divs
	var showstartindex=pagenumber*this.chunksize //array index of div to start showing per pagenumber setting
	var showendindex=showstartindex+this.chunksize-1 //array index of div to stop showing after per pagenumber setting
	for (var i=0; i<totalitems; i++){
		if (i>=showstartindex && i<=showendindex)
			this.pieces[i].style.display="block"
		else
			this.pieces[i].style.display="none"
	}
	if (this.persist){ //if persistence enabled
		virtualpaginate.setCookie("dd_"+this.piececlass, this.currentpage)
	}
	if (this.cpspan.length>0){ //if <span class="paginateinfo> element is present, update it with the most current info (ie: Page 3/4)
		for (var p=0; p<this.cpspan.length; p++)
			this.cpspan[p].innerHTML='Page '+(this.currentpage+1)+'/'+this.pagecount
	}
}

// -------------------------------------------------------------------
// PRIVATE: build() methods- Various methods to create pagination interfaces
// paginate_paginate_build()- Main build() paginate function
// paginate_output_flatview()- Accepts <span class="flatview"> element and populates it with sequential pagination links
// paginate_paginate_build_flatview()- Parses the modified <span class="flatview"> element and assigns click behavior to the pagination links
// paginate_build_selectmenu(paginatedropdown)- Accepts an empty SELECT element and turns it into pagination menu
// paginate_build_regularlinks(paginatelinks)- Accepts a collection of links and screens out/ creates pagination out of ones with specific "rel" attr
// paginate_build_cpinfo(cpspan)- Accepts <span class="paginateinfo"> element and displays current page info (ie: Page 1/4)
// -------------------------------------------------------------------

virtualpaginate.prototype.paginate_build=function(divid, divpos, optnavtext){
	var instanceOfBox=this
	var paginatediv=document.getElementById(divid)
	if (this.chunksize==this.pieces.length){ //if user has set to display all pieces at once, no point in creating pagination div
		paginatediv.style.display="none"
		return
	}
	var paginationcode=paginatediv.innerHTML //Get user defined, "unprocessed" HTML within paginate div
	if (paginatediv.getElementsByTagName("select").length>0) //if there's a select menu in div
		this.paginate_build_selectmenu(paginatediv.getElementsByTagName("select")[0], divpos, optnavtext)
	if (paginatediv.getElementsByTagName("a").length>0) //if there are links defined in div
		this.paginate_build_regularlinks(paginatediv.getElementsByTagName("a"))
	var allspans=paginatediv.getElementsByTagName("span") //Look for span tags within passed div
	for (var i=0; i<allspans.length; i++){
		if (allspans[i].className=="flatview")
			this.paginate_output_flatview(allspans[i], divpos, optnavtext)
		else if (allspans[i].className=="paginateinfo")
			this.paginate_build_cpinfo(allspans[i], divpos)
	}
	this.paginatediv[divpos]=paginatediv
}

virtualpaginate.prototype.paginate_output_flatview=function(flatviewcontainer, divpos, anchortext){
	var flatviewhtml=""
	var anchortext=anchortext || new Array()
	for (var i=0; i<this.pagecount; i++){
		if (typeof anchortext[i]!="undefined") //if custom anchor text for this link exists
			flatviewhtml+='<a href="#flatview" rel="'+i+'">'+anchortext[i]+'</a> ' //build pagination link using custom anchor text
		else
			flatviewhtml+='<a href="#flatview" rel="'+i+'">'+(i+1)+'</a> ' //build  pagination link using auto incremented sequential number instead
	}
	flatviewcontainer.innerHTML=flatviewhtml
	this.paginate_build_flatview(flatviewcontainer, divpos, anchortext)
}

virtualpaginate.prototype.paginate_build_flatview=function(flatviewcontainer, divpos, anchortext){
	var instanceOfBox=this
	var flatviewhtml=""
	this.flatviewlinks[divpos]=flatviewcontainer.getElementsByTagName("a")
	for (var i=0; i<this.flatviewlinks[divpos].length; i++){
		this.flatviewlinks[divpos][i].onclick=function(){
			var prevlinkindex=instanceOfBox.currentpage //Get index of last clicked on flatview link
			var curlinkindex=parseInt(this.getAttribute("rel"))
			instanceOfBox.navigate(curlinkindex)
			return false
		}
	}
	this.flatviewlinks[divpos][this.currentpage].className="selected" //"Highlight" current flatview link
	this.flatviewpresent=true //indicate flat view links are present
}

virtualpaginate.prototype.paginate_build_selectmenu=function(paginatedropdown, divpos, anchortext){
	var instanceOfBox=this
	var anchortext=anchortext || new Array()
	this.selectmenupresent=1
	for (var i=0; i<this.pagecount; i++){
		if (typeof anchortext[i]!="undefined") //if custom anchor text for this link exists, use anchor text as each OPTION's text
			paginatedropdown.options[i]=new Option(anchortext[i], i)
		else //else, use auto incremented, sequential numbers
			paginatedropdown.options[i]=new Option("Page "+(i+1)+" of "+this.pagecount, i)
	}
	paginatedropdown.selectedIndex=this.currentpage
	setTimeout(function(){paginatedropdown.selectedIndex=instanceOfBox.currentpage}, 500) //refresh currently selected option (for IE's sake)
	paginatedropdown.onchange=function(){
	instanceOfBox.navigate(this.selectedIndex)
	}
	this.selectmenu[divpos]=paginatedropdown
	this.selectmenu[divpos].selectedIndex=this.currentpage //"Select" current page's corresponding option
}

virtualpaginate.prototype.paginate_build_regularlinks=function(paginatelinks){
	var instanceOfBox=this
	for (var i=0; i<paginatelinks.length; i++){
		var currentpagerel=paginatelinks[i].getAttribute("rel")
		if (/^(previous)|(next)|(first)|(last)$/.test(currentpagerel)){ //screen for these "rel" values
			paginatelinks[i].onclick=function(){
				instanceOfBox.navigate(this.getAttribute("rel"))
				return false
			}
		}
		if (currentpagerel=="previous" || paginatelinks[i].href.indexOf("previous")!=-1){ //check if this is a "previous" link
			if (!this.wraparound && this.currentpage==0) //if current page is first page, disable "prev" link
				virtualpaginate.setcssclass(paginatelinks[i], "disabled", "add")
			this.prevlinks.push(paginatelinks[i])
		}
		else if (currentpagerel=="next" || paginatelinks[i].href.indexOf("next")!=-1){ //check if this is a "next" link
			if (!this.wraparound && this.currentpage==this.pagecount-1) //if current page is last page, disable "next" link
				virtualpaginate.setcssclass(paginatelinks[i], "disabled", "add")
			this.nextlinks.push(paginatelinks[i])
		}
		
	}
}

virtualpaginate.prototype.paginate_build_cpinfo=function(cpspan, divpos){
	this.cpspan[divpos]=cpspan
	cpspan.innerHTML='Page '+(this.currentpage+1)+'/'+this.pagecount
}

