//Language Javascript

var expanders = new Array();
var expandercontent = new Array();
var expanderCount = 0;
var sitetheme = "default";
var countrycode = "";
var basketinitpos = 0;

function init(theme, ccode) 
{
	sitetheme = theme;
	countrycode = ccode;
	
	document.onmouseover = rollon;
	document.onmouseout = rolloff;
	
	initialiseExpanders();
	initialiseWizard();
	initialiseFaders();
}

function initialiseExpanders(e)
{
	var els = document.getElementsByTagName("TD");
	for( i=0; i<els.length; i++ )
	{
		if( els[i].id.substr(0,9) == "expander_" )
		{
			expanders[expanderCount] = els[i].id;
			content = "expandercontent_"+els[i].id.substr(9,els[i].id.length-9);
			expandercontent[expanderCount] = content;
			els[i].onclick = function(e) {
				if (window.event) e = window.event;  
				var targ = e.srcElement? e.srcElement : e.target; 
				
				content = "expandercontent_"+targ.id.substr(9,targ.id.length-9);
				elcontent = document.getElementById(content);
				if( elcontent )
				{
					if( elcontent.style.display == 'block' )
						elcontent.style.display = 'none';
					else
						elcontent.style.display = 'block';
				}
			}
			expanderCount++;
		}
	}
}

function doNothing()
{
}

function focusText(id)
{
	el = document.getElementById(id);
	if( el ) {
		el.value = '';
	}
}

function focusPassword(id)
{
	el = document.getElementById(id);
	if( el ) {
		if( el.type != 'password' ) {
			var el2= el.cloneNode(false); 
			el2.type='password'; 
			el2.value=''; 
			el.parentNode.replaceChild(el2,el);
			el2.focus();
		}
	}
}

var WizardIds = new Array();
var WizardPage = 1;
var WizardFieldChanged = false;
var WizardFinishPage = "";

function initialiseWizard()
{
	els = document.getElementsByTagName("DIV");
	for( i=0; i<els.length; i++ )
	{
		el = els[i];
		if( el.className == 'WizardMenu' )
		{
			WizardFinishPage = el.getAttribute('finishpage');
			for( j=0; j<el.childNodes.length; j++ )
			{
				tab = el.childNodes[j];
				if( tab.tagName == 'A' ) 
				{	
					WizardIds[WizardIds.length] = tab.id;
					if( WizardIds.length == 1 ) 
					{
						tab.className = "selected";
						if( tab.getAttribute('tabable') == 'true' ) el.removeAttribute("href");

						var request = "default.php?ajax=ModuleWizardTab&tab="+tab.id;
						AjaxRequest(request);
					} else {
						tab.className = "";
						if( tab.getAttribute('tabable') == 'true' ) tab.href = "javascript:showWizardTab('"+tab.id+"');";
					}
				}
			}
		}
	}
	setWizardButtonStates();
}

function setWizardButtonStates()
{
	if( el = document.getElementById('previous') ) 
	{
		if( WizardPage == 1 ) {
			el.className = "disabled";
			el.removeAttribute("href");
		} else {
			el.className = "";
			el.href="javascript:stepWizardPage(-1)";
		}
	}
	if( el = document.getElementById('next') ) 
	{
		if( WizardPage < WizardIds.length ) {
			el.className = "";
			el.href="javascript:stepWizardPage(1)";
		} else {
			el.className = "disabled";
			el.removeAttribute("href");
		}
	}
	if( el = document.getElementById('finish') ) 
	{
		if( WizardPage == WizardIds.length ) {
			el.className = "";
			el.href="javascript:finishWizard()";
		} else {
			el.className = "disabled";
			el.removeAttribute("href");
		}
	}
	if( el = document.getElementById('cancel') ) 
	{
		el.className = "";
		el.href="javascript:cancelWizard()";
	}
}

function stepWizardPage(step)
{
	WizardPage += step;
	if( WizardPage > WizardIds.length ) WizardPage = WizardIds.length;
	else if( WizardPage < 1 ) WizardPage = 1;
	
	showWizardTab(WizardIds[WizardPage-1]);
}

function initWizardFields()
{
	els = document.getElementsByTagName("INPUT");
	for( i=0; i<els.length; i++ )
	{
		els[i].onchange = function() {
			WizardFieldChanged = true;
		}
	}
	els = document.getElementsByTagName("TEXTAREA");
	for( i=0; i<els.length; i++ )
	{
		els[i].onchange = function() {
			WizardFieldChanged = true;
		}
	}
	els = document.getElementsByTagName("SELECT");
	for( i=0; i<els.length; i++ )
	{
		els[i].onchange = function() {
			WizardFieldChanged = true;
		}
	}
}

function showWizardTab(id)
{
	var changetab = true;
	
	if( WizardFieldChanged ) 
	{
		if( confirm("This page has changed, if you navigate away without saving your changes they will be lost. Do you want to save you changes first?") ) 
			changetab = false;
	}
	
	if( changetab )
	{
		WizardFieldChanged = false;
		for( i=0; i<WizardIds.length; i++ )
		{
			el = document.getElementById(WizardIds[i]);
			if( el )
			{
				if( el.id == id ) 
				{
					el.className = "selected";
					if( el.getAttribute('tabable') == 'true' ) el.removeAttribute("href");
				} else {
					el.className = "";
					if( el.getAttribute('tabable') == 'true' ) el.href = "javascript:showWizardTab('"+el.id+"');";
				}
			}
		}

		var request = "default.php?ajax=ModuleWizardTab&tab="+id;
		AjaxRequest(request);	
		
		setWizardButtonStates();
	}
}

function saveWizardPage()
{
	var postdata = "";
	els = document.getElementsByTagName('INPUT');
	for( i=0; i<els.length; i++ ) 
		postdata += "&"+els[i].name+"="+els[i].value;
		
	els = document.getElementsByTagName('TEXTAREA');
	for( i=0; i<els.length; i++ ) 
		postdata += "&"+els[i].name+"="+els[i].value;
		
	els = document.getElementsByTagName('SELECT');
	for( i=0; i<els.length; i++ ) 
		postdata += "&"+els[i].name+"="+els[i].value;
		
	postdata = postdata.substring(1);
	
	WizardFieldChanged = false;
	
	var request = "default.php?ajax=SaveWizardTab&tab="+WizardIds[WizardPage-1];
	AjaxPost(request,postdata);
}

function finishWizard()
{
	document.location.href = "default.php?page="+WizardFinishPage;
}

function cancelWizard()
{
}

function rollon() {
	var targ;
	var e = window.event;
	if( e ) {
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;
		if (targ.nodeType == 3) // defeat Safari bug
			targ = targ.parentNode;
	
		if(targ.parentElement) {
			if(targ.parentElement.className == "row_up") { 
				targ.parentElement.className = "row_over"; }}
		if(targ.className == "row_up") {
			targ.className = "row_over"; }
		if(targ.parentElement) {
			if(targ.parentElement.className == "cell_up") { 
				targ.parentElement.className = "cell_over"; }}
		if(targ.className == "cell_up") {
			targ.className = "cell_over"; }
	}
}
function rolloff() {
	var targ;
	var e = window.event;
	if( e ) {
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;
		if (targ.nodeType == 3) // defeat Safari bug
			targ = targ.parentNode;
	
		if(targ.parentElement) {
			if(targ.parentElement.className == "row_over") {
				targ.parentElement.className = "row_up"; }}
		if(targ.className == "row_over") 
			targ.className = "row_up";
		if(targ.parentElement) {
			if(targ.parentElement.className == "cell_over") {
				targ.parentElement.className = "cell_up"; }}
		if(targ.className == "cell_over") 
			targ.className = "cell_up";
	}
}
function showrows(obj) {
	if( obj ) {
		var type = obj.id+"_";
		var id = type+obj.value;
		var objArray = document.getElementsByTagName('tr')
		for(var i=0; i<objArray.length; i++) {
			if(objArray[i].id == id) objArray[i].style.display = "block";
			else if(objArray[i].id.substring(0,type.length) == type) objArray[i].style.display = "none";
		}	
	}
}

function callAjaxFunction( request, callback, element, post, data )
{
	var xmlHttp;
	
	try { xmlHttp=new XMLHttpRequest(); } // Firefox, Opera 8.0+, Safari  
	catch (e) {  
    	try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } // Internet Explorer 
  		catch (e) {    
			try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
    		catch (e) { 
				alert("Your browser does not support AJAX!");
				return false;      
			}   
		}  
	} 
	
	var randomnumber=Math.floor(Math.random()*99999999)
	if( post == true || data == null ) request = request.replace( "?", "?rid="+randomnumber+"&" );
	else request = request.replace( "?", "?rid="+randomnumber+"&data="+urlencode(data)+"&" );
	
	xmlHttp.onreadystatechange = function()
	{
		if( xmlHttp.readyState == 4 && xmlHttp.status == 200 ) 
		{
			if( xmlHttp.responseText.length > 0 ) 
				callback( xmlHttp.responseText, element );
			else 
				callback( xmlHttp.responseBody, element );
		}
		delete xmlHttp;
	}
	
	if( post == true ) {
		xmlHttp.open("POST",request,true);
		xmlHttp.send(data);
	} else {
		xmlHttp.open("GET",request,true);
		xmlHttp.send(null);
	}
}

function ModuleMenuSelect(subpage)
{
	var request = "default.php?ajax=ModuleMenuSelect&subpage="+subpage;
	AjaxRequest(request);
}

function ModuleListCommand(command,id)
{
	var request = "default.php?ajax=ModuleListCommand&command="+command+"&id="+id;
	AjaxRequest(request);
}

function AjaxRequest(request)
{
	var elContent = document.getElementById('ModuleContent');
	
	if( elContent ) {
		elContent.innerHTML = '<img src="themes/'+sitetheme+'/images/wait.gif" alt="Please Wait..." />';
		callAjaxFunction( request, OnAjaxResponse, elContent, false, null );
	}
}

function AjaxPost(request,data)
{
	var elContent = document.getElementById('ModuleContent');
	
	if( elContent ) {
		elContent.innerHTML = '<img src="themes/'+sitetheme+'/images/wait.gif" alt="Please Wait..." />';
		callAjaxFunction( request, OnAjaxResponse, elContent, true, data );
	}
}

function OnAjaxResponse(response, obj)
{
	if( obj ) 
	{
		obj.innerHTML = response;
		initialiseListBuilders();
		initialiseRTEs();
	}
}

function ShowSubMenu(parentid)
{
	var request = "?ajax=ShowSubMenu&parentid="+parentid;
	callAjaxFunction( request, onSubMenu, parentid, false, null );
}

function onSubMenu(response, id)
{
	els = document.getElementsByTagName('DIV');
	for( i=0; i<els.length; i++ )
	{
		if( els[i].id.substr(0,10) == "SubMenuOf_" ) 
			document.body.removeChild(els[i]);
	}
	
	if( response != 'EMPTY' )
	{
		menuabove = false;
		elPage = document.getElementById('Page');
		el = document.getElementById("HeaderMenuParent_"+id);
		if( !el ) {
			menuabove = true;
			el = document.getElementById("FooterMenuParent_"+id);
		}
		
		if( el && elPage )
		{
			submenu = document.createElement('DIV');
			submenu.id = "SubMenuOf_"+id;
			submenu.className = "hovermenu";
			submenu.innerHTML = response;
			
			ScrollTop = document.body.scrollTop;
			if( ScrollTop == 0 ) {
				if (window.pageYOffset) ScrollTop = window.pageYOffset;
				else ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
			}
		
			submenu.style.left = (elPage.offsetLeft+el.offsetLeft)+'px';
			
			submenu.onmouseout = function() {
				if (!e) var e = window.event; 
				
				var current_mouse_target = null;
				if( e.toElement ) { current_mouse_target = e.toElement; } 
				else if( e.relatedTarget ) { current_mouse_target = e.relatedTarget; }
				if( !is_child_of(this, current_mouse_target) && this != current_mouse_target ) 
				{
					el = document.getElementById("SubMenuOf_"+id);
					if( el ) document.body.removeChild(el);
				}
			}
			
			document.body.appendChild(submenu);
		}
	}
}

function is_child_of(parent, child) {
	if( child != null ) {			
		while( child.parentNode ) {
			if( (child = child.parentNode) == parent ) {
				return true;
			}
		}
	}
	return false;
}

var upload_triggercomplete = false;

function onuploadclicked(id)
{
	elFrame = document.getElementById('uploadframe_'+id);
	elImage = document.getElementById('uploadwait_'+id);
	
	if( elFrame && elImage )
	{
/*		elFrame.style.visibility = 'hidden'; */
		elImage.style.visibility = 'visible';
		upload_triggercomplete = true;
	}
}

function onuploadcomplete(onfileuploaded,id)
{
	elFrame = document.getElementById('uploadframe_'+id);
	elImage = document.getElementById('uploadwait_'+id);
	
	if( elFrame && elImage && upload_triggercomplete )
	{
		elFrame.style.visibility = 'visible';
		elImage.style.visibility = 'hidden';
		upload_triggercomplete = false;
		
		setTimeout(onfileuploaded,500);
	}
}

function editImage(filename,id)
{
}

function removeFile(filename,onfileremoved)
{
	if( confirm("Are you sure you want to remove this file? This operation is irreversable.") )
	{
		el = null;
		els = document.getElementsByTagName('div');
		for( i=0; i<els.length; i++ ) {
			if( els[i].className == 'FileInfo' ) {
				el = els[i];
				break;
			}
		}
		
		if( el )
		{
			var request = "?ajax=RemoveFile&filename="+filename;
			callAjaxFunction( request, onfileremoved, el.parentNode.id, false, null );
		}
	}
}

function checktohide(e, id)
{
	if (window.event) e = window.event;  
	elsrc = e.srcElement ? e.srcElement : e.target; 
	el = document.getElementById(id);
	if( el && elsrc ) {
		if(elsrc.checked) el.style.display = 'none';
		else el.style.display = 'inline-block';
	}
}

function checktoshow(e, id)
{
	var e = window.event || e;
	var targ = e.target || e.srcElement;

	el = document.getElementById(id);
	
	if( el && targ ) {
		if(targ.checked) el.style.display = 'inline-block';
		else el.style.display = 'none';
	}
}

function submitajaxform( ajaxfunction, formname, callback )
{
	request = "?ajax="+ajaxfunction;
	validated = true;

	elForm = document.getElementById(formname);
	if( elForm )
	{
		data = "form="+formname;
		
		els = elForm.getElementsByTagName('INPUT');
		for( i=0; i<els.length; i++ ) 
		{
			if( els[i].type == 'radio' ) {
				if( els[i].checked == true ) data += "&"+els[i].name+"="+urlencode(els[i].value); 
			} else if( els[i].type == 'checkbox' ) {
				if( els[i].checked == true ) data += "&"+els[i].name+"=1"; 
				else data += "&"+els[i].name+"=0"; 
			} else data += "&"+els[i].name+"="+urlencode(els[i].value); 
			if( !validate(els[i]) ) validated = false; 
		}
		
		els = elForm.getElementsByTagName('TEXTAREA');
		for( i=0; i<els.length; i++ ) { 
			data += "&"+els[i].name+"="+urlencode(els[i].value); 			
			if( !validate(els[i]) ) validated = false; 
		}
		
		els = elForm.getElementsByTagName('SELECT');
		for( i=0; i<els.length; i++ ) { 
			data += "&"+els[i].name+"="+urlencode(els[i].value); 
			if( !validate(els[i]) ) validated = false; 
		}

		if( validated ) 
			callAjaxFunction( request, callback, formname, true, data );
	} 
	else
	{
		callAjaxFunction( request, callback, formname, false, null );
	}			
}

function validateform()
{
	els = document.getElementsByTagName('INPUT');
	for( i=0; i<els.length; i++ ) 
		if( !validate(els[i]) ) return false;
	
	els = document.getElementsByTagName('TEXTAREA');
	for( i=0; i<els.length; i++ )
		if( !validate(els[i]) ) return false;
	
	els = document.getElementsByTagName('SELECT');
	for( i=0; i<els.length; i++ )  
		if( !validate(els[i]) ) return false;
		
	return true;
}

function validate(el)
{
	validated = true;
	func = els[i].getAttribute('validate');
	if( func ) {
		if( func.length > 0 ) {
			func = func.replace("(","( els[i],");
			result = eval(func);
			if( result != 'ok' ) {
				validated = false;
				alert(result);
			}
		}
	}
	return validated;
}

function validate_min( el, field, minval )
{
	ret = "ok";
	if( parseFloat(el.value) < minval ) ret = "'"+field+"' must be greater than "+minval;
	return ret;
}

function validate_minmax( el, field, minval, maxval )
{
	ret = "ok";
	if( parseFloat(el.value) < minval ) ret = "'"+field+"' must be greater than "+minval;
	else if( parseFloat(el.value) > maxval ) ret = "'"+field+"' must be less than "+maxval;
	return ret;
}

function validate_max( el, field, maxval )
{
	ret = "ok";
	if( parseFloat(el.value) > maxval ) ret = "'"+field+"' must be less than "+maxval;
	return ret;
}

function urlencode( str ) 
{                            
	if( str.length < 1 ) return str;

	var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}

function submitajaxchoice( ajaxfunction, formname, callback, value )
{
	elForm = document.getElementById(formname);
	if( elForm )
	{
		request = "?ajax="+ajaxfunction;
		data = "form="+formname+"&value="+value;	
		
		els = elForm.getElementsByTagName('INPUT');
		for( i=0; i<els.length; i++ ) { data += "&"+els[i].name+"="+urlencode(els[i].value); }
	
		callAjaxFunction( request, callback, formname, true, data );
	}
}

function SearchKeyUp(e)
{
	var characterCode;
	
	if(e && e.which) { //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	} else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13) { //if generated character code is equal to ascii 13 (if enter key)
		DoSearch() //submit the form
		return false 
	} else {
		return true 
	}
}

function DoSearch()
{
	el = document.getElementById('searchphrase');
	if( el ) {
		document.location.href="?page=onlineshop&search="+el.value;
	}
}

function ViewBasket()
{
	alert("No payment methods are enabled on this site.");
}

function AddToBasket(productid, qtyid)
{
	if( countrycode.length < 1 || countrycode == '0' )
	{
		BasketPickCountry(productid, qtyid);
	}
	else
	{
		elAlternatives = document.getElementById('alternatives');
		el = document.getElementById(qtyid);
		if( el ) {
				
			if( elAlternatives ) productid = elAlternatives.value;
				
			var request = "?ajax=AddToBasket&cc="+countrycode+"&productid="+productid+"&qty="+el.value;
			
			els = document.getElementsByTagName("SELECT");
			for( i=0; i<els.length; i++ ) {
				if( els[i].name.substring(0,12) == 'Form_option_' )
					request += "&"+els[i].name+"="+urlencode(els[i].value);
			}
			
			eloption = document.getElementById('Form_optional');
			if( eloption ) request += "&optional="+urlencode(eloption.value);
			
			el = document.getElementById('SummaryBasket');
			if( el ) {
				el.style.border = 'solid 1px #CC0000';
				setTimeout( el.style.border = 'none', 2000 );
			}
			callAjaxFunction( request, OnBasketChanged, 'Basket', false, null );
		}
	}
}

function RemoveFromBasket(basketref,full)
{
	var request = "?ajax=RemoveFromBasket&basketref="+basketref+"&full="+full;
	callAjaxFunction( request, OnBasketChanged, 'Basket', false, null );
}

function OnBasketChanged(response, id)
{
	el = document.getElementById(id);
	if( el ) {
		el.innerHTML = response;
	}
}

function BasketPickCountry(productid, qtyid)
{
	WebDialog( "CountryPicker", OnSelectCountry, productid+"|"+qtyid );
}

function OnSelectCountry( result, context )
{
	countrycode = result;
	
	values = context.split('|');
	if( countrycode.length > 0 ) {
		AddToBasket(values[0], values[1]);
	}
}

function WebDialog( ajax, callback, context )
{
	dlg = document.createElement('div');
	dlg.className = 'webDialog';
	dlg.id = 'webDialog';
	
	content = document.createElement('div');
	content.id = 'webDialogContent';
	content.innerHTML = '<img src="themes/'+sitetheme+'/images/wait.gif" alt="Please Wait" />';

	btnCancel = document.createElement('a');
	btnCancel.onclick = function() {
		el = document.getElementById('webDialog');
		if( el ) document.body.removeChild(el);
	}
	btnCancel.href = "#";
	btnCancel.innerHTML = "Cancel";
	
	btnSave = document.createElement('a');
	btnSave.onclick = function() {
		output = document.getElementById('dlgOutput');
		if( output ) {
			result = "";
			if( output.tagName == 'select' ) result = output[output.selectedIndex].value;
			else result = output.value;
			
			el = document.getElementById('webDialog');
			if( el ) document.body.removeChild(el);
			callback( result, context );
		}
	}
	btnSave.href = "#";
	btnSave.innerHTML = "OK";
	
	dlg.appendChild(content);
	dlg.appendChild(btnSave);
	dlg.appendChild(btnCancel);
	
	document.body.appendChild(dlg);
	
	var h = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
	dlg.style.top = h;
		
	if( ajax != undefined ) 
		callAjaxFunction( "?ajax="+ajax, webOnDialogContent, callback, false, null );
}

function webOnDialogContent( response, id )
{
	el = document.getElementById('webDialogContent');
	if( el ) el.innerHTML = response;
}

function initialiseListBuilders()
{
	divs = document.getElementsByTagName('DIV');
	for( i=0; i<divs.length; i++ )
	{
		if( divs[i].id.substring(0,17) == "ListBuilder_List_" )
		{
			listid = divs[i].id.substring(17);
			ListBuilder_Refresh(listid);
		}
	}
}

function ListBuilder_Refresh(id)
{
	elForm = document.getElementById('Form_'+id);
	if( elForm )
	{
		var request = "?ajax=ListCallback_"+id+"&data="+elForm.value;
		callAjaxFunction( request, OnListBuilderChanged, id, false, null );
	}
}

function ListBuilder_AddItem(id)
{
	elItem = document.getElementById('ListBuilder_Item_'+id);
	elQty = document.getElementById('ListBuilder_Qty_'+id);
	elList = document.getElementById('ListBuilder_List_'+id);
	elForm = document.getElementById('Form_'+id);
	
	if( elItem && elQty && elList && elForm )
	{
		found = false;
		items = elForm.value.split('|');
		for( i=0; i<items.length; i++ )
		{
			itemparts = items[i].split(',');
			if( itemparts[0] == elItem.value )
			{
				itemparts[1] = parseInt(itemparts[1])+parseInt(elQty.value);
				items[i] = itemparts.join(',');
				found = true;
			}
		}
		if( !found ) items.push(elItem.value+","+elQty.value);
		elForm.value = items.join('|');
		
		var request = "?ajax=ListCallback_"+id+"&data="+elForm.value;
		callAjaxFunction( request, OnListBuilderChanged, id, false, null );
	}
}

function ListBuilder_RemoveItem(id,itemid)
{
	elList = document.getElementById('ListBuilder_List_'+id);
	elForm = document.getElementById('Form_'+id);
	
	if( elList && elForm )
	{
		found = false;
		items = elForm.value;
		if( elForm.value.charAt(0) == '|' ) items = items.substring(1);
		items = items.split('|');
		
		newitems = new Array();
		for( i=0; i<items.length; i++ )
		{
			itemparts = items[i].split(',');
			if( parseInt(itemparts[0]) != itemid ) 
				newitems.push(items[i]);
		}
		elForm.value = newitems.join('|');
		
		var request = "?ajax=ListCallback_"+id+"&data="+elForm.value;
		callAjaxFunction( request, OnListBuilderChanged, id, false, null );
	}
}

function OnListBuilderChanged(response, id)
{
	el = document.getElementById('ListBuilder_List_'+id);
	if( el )
	{
		if( response!=undefined && response!='NONE') 
		{
			html = '<table width="100%">';
			items = response.split('|');
			for( i=0; i<items.length; i++ )
			{
				row = items[i].split(',');
				html += '<tr>';
				for( j=1; j<row.length; j++ ) html += '<td>'+row[j]+'</td>';
				html += '<td align="right"><a href="javascript:ListBuilder_RemoveItem(\''+id;
				html += '\','+row[0]+');">Remove</a></td></tr>';
			}
			html += '</table>';
			
			el.innerHTML = html;
		} else {
			el.innerHTML = "&nbsp;";
		}
	}
}

function SelectPage(id)
{
	els = document.getElementsByTagName('TR');
	for( i=0; i<els.length; i++ ) {
		if( els[i].id.substring(0,12) == 'ProductPage_' ) {
			if( els[i].id == 'ProductPage_'+id || id == 0 ) els[i].style.display = 'block';
			else els[i].style.display = 'none';
		}
	}
	
	els = document.getElementsByTagName('A');
	for( i=0; i<els.length; i++ ) {
		if( els[i].id.substring(0,8) == 'pagesel_' ) {
			if( els[i].id == 'pagesel_'+id ) els[i].className = 'highlight';
			else els[i].className = '';
		}
	}
}

function clfxCalculate()
{
	totals = new Array();
	
	elCells = document.getElementsByTagName('TD');
	for( index=0; index<elCells.length; index++ )
	{
		attrib = elCells[index].getAttribute('function');
		currency = elCells[index].getAttribute('currency');
		colname = elCells[index].getAttribute('colname');
		if( attrib ) 
		{
			if( attrib.charAt(attrib.length-1) == '|' ) 
				attrib = attrib.substring(0,attrib.length-1);

			param = "";
			nextoperation = "=";
			total = 0;
			formatting = false;
			
			parts = elCells[index].id.split('_');
			if( parts.length > 2 ) {
				rownum = parseInt(parts[1]);
				paramname = parts[2];
			} else {
				rownum = '=';
				paramname = parts[1];
			}
			
			for(i=0; i<attrib.length; i++)
			{
				if( attrib.charAt(i) == '+' || attrib.charAt(i) == '-' || attrib.charAt(i) == '*' || attrib.charAt(i) == '/' )
				{
					total = clfxSubCalculate( total, param, nextoperation, rownum );
					nextoperation = attrib.charAt(i);
					param = "";
				} else if( attrib.charAt(i) == ':' ) {
					total = clfxSubCalculate( total, param, nextoperation, rownum );
					nextoperation = attrib.charAt(i);
					param = "";
					formatting = true;
				} else {
					param += attrib.charAt(i);
				}
			}
			
			if( !formatting )
				total = clfxSubCalculate( total, param, nextoperation, rownum );
			
			if( totals[colname]==undefined ) totals[colname] = total;
			else {
				value = totals[colname].split(':');
				totals[colname] = parseFloat(value[0])+total;
			}
			
			isnegative = false;
			if( total < 0 ) isnegative = true;
			
			text = "";
			if( formatting ) {
				totals[colname] = ''+totals[colname]+':'+param;
				text = clfxFormatValue(total,param,currency);
			} else {
				totals[colname] = ''+totals[colname]+':S';
				text = clfxFormatValue(total,'S',currency);
			}
			
			if( isnegative ) elCells[index].style.color = '#FF0000';
			else elCells[index].style.color = '';
			elCells[index].innerHTML = text;
			
			clfxUpdateTotals(totals);
		}
	}
	
	elCells = document.getElementsByTagName('INPUT');
	for( index=0; index<elCells.length; index++ )
	{
		colname = elCells[index].getAttribute('colname');
		if( elCells[index].id.substr(0,6) == 'clvar_' ) {
			if( totals[colname]==undefined ) totals[colname] = ''+parseFloat(elCells[index].value)+':S';
			else {
				value = totals[colname].split(':');
				totals[colname] = ''+(parseFloat(value[0])+parseFloat(elCells[index].value))+':S';
			}
		}
	}
	
	clfxUpdateTotals(totals);
}

function clfxUpdateTotals(totals)
{
	els = document.getElementsByTagName('TH');
	for( i=0; i<els.length; i++ )
	{
		parts = els[i].id.split('_');
		if( parts[0] == "cltot" )
		{
			if( totals[parts[1]] != undefined )
			{
				currency = els[i].getAttribute('currency');
				value = totals[parts[1]].split(':');	
				text = clfxFormatValue(value[0],value[1],currency);
				
				if( parseFloat(value[0]) < 0 ) els[i].style.color = '#FF0000';
				else els[i].style.color = '';
				els[i].innerHTML = text;
			}
		}
	}
}

function clfxSubCalculate( total, param, operator, rownum )
{
	value = clfxGetParamValue(param, rownum);
	if( operator == '=' ) total = value;
	else if( operator == '+' ) total += value;
	else if( operator == '-' ) total -= value;
	else if( operator == '*' ) total *= value;
	else if( operator == '/' ) total /= value;

	return total;
}

function clfxGetParamValue( param, rownum )
{
	el = null;
	valname = "";
	valname2 = "";
	value = 0.0;
	isinputcontrol = true;
	
	if( !isNaN(param) ) {
		value = parseFloat(param);
	} else if( param.charAt(0) == '!' ) {
		valname2 = 'cltot_'+param.substring(1);
		isinputcontrol = false;
	} else if( param.charAt(0) == '#' && isNaN(rownum) ) {
		valname = 'clvar_'+param.substring(1);
		valname2 = 'clfx_'+param.substring(1);
	} else if( isNaN(rownum) ) {
		valname = 'clvar_'+param;
		valname2 = 'clfx_'+param;
	} else if( param.charAt(0) == '#' ) {
		valname = 'clvar_'+rownum+'_'+param.substring(1);
		valname2 = 'clfx_'+rownum+'_'+param.substring(1);
	} else {
		valname = 'clvar_'+rownum+'_'+param;
		valname2 = 'clfx_'+rownum+'_'+param;
	}

	if( valname.length > 0 || valname2.length > 0 )
	{
		el = document.getElementById(valname);
		el2 = document.getElementById(valname2);
		if( el ) value = parseFloat(el.value);
		else if( el2 ) value = clfxMakeNumeric(el2.innerHTML);
		else alert("Could not find value: "+valname+" or "+valname2);
	}
	
	return value;
}

function clfxFormatValue(value,format, currency)
{
	if( isNaN(value) ) value = 0;
	
	txt = ''+value;
	if( format == 'C' ) {
		if( currency == 'GBP' ) { txt = '&pound;'+parseFloat(value).toFixed(2); }
		else if( currency == 'EUR' ) { txt = '&euro;'+parseFloat(value).toFixed(2); }
		else { txt = currency+' '+parseFloat(value).toFixed(2); }
	}
	else if( format == '%' ) txt = parseFloat(value).toFixed(2)+'%';
	
	return txt;
}

function clfxMakeNumeric(sText)
{
	ValidChars = "0123456789.";
	NumericText = "";
	value = 0.0;
	
	for (charno = 0; charno < sText.length; charno++) 
		if (ValidChars.indexOf(sText.charAt(charno)) != -1) 
			NumericText = NumericText+sText.charAt(charno);
	
	value = parseFloat(NumericText);
	return value;
}

var ziCurrentImage = 0;
var ziAltText = "";

function ziChangePicture(id,imgno)
{
	elMain = document.getElementById('ziMain');
	elZoom = document.getElementById('ziZoom');
	if( elMain && elZoom ) {
		elMain.src = '?ajax=ProductImage&id='+id+'&img='+imgno;
		elZoom.style.backgroundImage = 'url(?ajax=ProductImageFull&id='+id+'&img='+imgno+')';
		ziCurrentImage = imgno;
	}
}

function ziMouseOver(id)
{
	elMain = document.getElementById('ziImageChanger');
	elImage = document.getElementById('ziMain');
	elZoom = document.getElementById('ziZoom');
//	elZoom = document.createElement('DIV');
	
	if( elZoom && elMain && elImage )
	{
		e = window.event;
		
		ziAltText = elImage.alt;
		elImage.alt = '';
		
//		elZoom.id = 'ziZoom';
		elZoom.style.left = e.x+'px';
		elZoom.style.top = e.y+'px';
		
		elZoom.style.display = 'block';
//		elMain.appendChild(elZoom);
	}
}

function ziMouseMove()
{
	elPage = document.getElementById('Page');
	elMain = document.getElementById('ziImageChanger');
	elZoom = document.getElementById('ziZoom');
	
	if( elZoom && elMain )
	{
		e = window.event;
		
		ScrollTop = document.body.scrollTop;
		if( ScrollTop == 0 ) {
			if (window.pageYOffset) ScrollTop = window.pageYOffset;
			else ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}

		elZoom.style.left = (e.x-elPage.offsetLeft+10)+'px';
		elZoom.style.top = (e.y-elPage.offsetTop+10+ScrollTop)+'px';
		
		elZoom.style.backgroundPosition = (1000-((e.x-elPage.offsetLeft)*3))+'px '+(950-((e.y-elPage.offsetTop+ScrollTop)*3))+'px';
	}
}

function ziMouseOut()
{
	elMain = document.getElementById('ziImageChanger');
	elZoom = document.getElementById('ziZoom');
	
	if( elZoom && elMain )
	{
		elZoom.style.display = 'none';
		elImage.alt = ziAltText;
		
//		elMain.removeChild(elZoom);
	}
}

function ziNewWindow(id)
{
	window.open('?ajax=ProductImageFull&id='+id+'&img='+ziCurrentImage,'enlarged','width:1000px;height=1000px;');
}

function ShowContentListSection(section)
{
	elRows = document.getElementsByTagName('tr');
	for( i=0; i<elRows.length; i++ )
	{
		if( elRows[i].id == 'ContentListSection_'+section ) elRows[i].className = 'sectioncontract';
		else if( elRows[i].id.substr(0,19) == 'ContentListSection_' ) elRows[i].className = 'sectionexpand';
		else if( elRows[i].id == 'ContentListRow_'+section ) {
			if( elRows[i].className = 'odd_hidden' ) elRows[i].className = 'odd';
			else if( elRows[i].className = 'even_hidden' ) elRows[i].className = 'even';
		} else if( elRows[i].id.substr(0,15) == 'ContentListRow_' ) {
			if( elRows[i].className = 'odd' ) elRows[i].className = 'odd_hidden';
			else if( elRows[i].className = 'even' ) elRows[i].className = 'even_hidden';
		}
	}
}

function DoLogin()
{
	elForm = document.getElementById('tradelogin');
	if( elForm ) elForm.submit();
}

function GetTop(oElement)
{
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function PositionBasket()
{
	el = document.getElementById('SummaryBasket');
	if( el ) 
	{
	   	var h = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
		if( h > 300 )
		{
			if( basketinitpos == 0 ) basketinitpos = GetTop(el);
			if( h < basketinitpos ) h = basketinitpos;
		
			el.style.position = "absolute";
			el.style.top = h;
		} else {
			el.style.position = "";
		}
	}
}

function DeliveryFields(e)
{
	if (window.event) e = window.event;  
	var targ = e.srcElement? e.srcElement : e.target; 
	{
		styledisplay = targ.value > 0 ? 'none' : '';
		elDelivery = document.getElementById('delivery');
		if( elDelivery ) elDelivery.style.display = styledisplay;
	}
}

function FormSeperatorClicked( id )
{
	els = document.getElementsByTagName('DIV');
	for( i=0; i<els.length; i++ )
	{
		if( els[i].id == "FormSeperatorContent_"+id )
			els[i].style.display= 'block';
		else if( els[i].id.substr(0,21) == "FormSeperatorContent_" )
			els[i].style.display= 'none';
	}
}
