// .dwt template
function behave_popUp(URL) {
	window.open(URL, 'termspop', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=430,height=460');
}

// editor
function behave_checkOptionForm() {
	if(!behave_removeSymbol()) {
		testname = document.proDep['name'].value;
		if (testname.length > 55)
			alert('Warning: option name longer than recommended 55 character limit.');
		// don't hold up the action either way
		return(true);
	} else {
		return(false);
	}
}	

function behave_removeSymbol() {
	var tmpStr = "";
	var charFound = false;
	var itemList = ['name','mfGroup'];
	jQuery.each(itemList, function(){
		tmpStr = $('#'+this).val();
		tmpId = $('#'+this).parent().prev().html();
		if(tmpStr.search(/[,"'`:]/) != -1) {
			alert('Illegal character found in "'+tmpId+'" --> '+tmpStr.charAt(tmpStr.search(/[,"'`]/))+'\nChanges have not been saved');
			charFound = true;
		}
		if(tmpStr.search(/\u00A3/g)){
			$('#'+this).val(tmpStr.replace(/\u00A3/g,"&pound;")); 
		}
	});
	return(charFound);
}

// fuelben
function behave_testValue( field, val, errorMess) {
	if (field != null)
	{
		if (field.value == val)
		{
			alert(errorMess);
			return(false);
		} else {
			return(true);
		}
	} else {
		return(true);
	}
}

// x1select
function behave_safeDisable( formname, fieldname) {
	var form = document.forms[formname];
	if (form != null)
	{
		var target = form[fieldname];
		if (target != null)
		{
			target.disabled=true;
		}
	}
}

function behave_processSelect(form) {
	behave_safeDisable('proDepP','Pgo');
	behave_safeDisable('proDepA','Ago');
	behave_safeDisable('proDepB','Bgo');
	behave_safeDisable('proDepC','Cgo');
	form.submit();
	return(true);
}

// x2model
function behave_limitField(form,field,low,high) {
	if (form == null)
		return(true);
		
	if (form.elements[field] == null)
		return(true);
		
	value = form.elements[field].value;
	if (value < 0)
		form.elements[field].value = 0;
	if (value > high)
		form.elements[field].value = high;
		
	return(true);
}

function behave_postTo(where) {
	document.proDep.action=where;
	document.forms['proDep'].submit();
	return(true);
}

function behave_postToExplicit( where, formname) {
	document.forms[formname].action=where;
	document.forms[formname].submit();
	return(true);
}

function behave_testPostTo(sel,dest) {
	if(!sel)
	{
		alert('Please make a selection first.\n\n(Sorry if you have already selected - the page wasn\'t fully loaded and you will need to select again.)');
		return(false);
	}
	return(behave_postTo(dest));
}
	
// xlabel 
function behave_processLabelReset(selector, label) {
	// must be safe because called in ilabelCommon
	if (selector != null)
		selector.value="150";
	if (label != null)
		label.src="/newcar/assets/images/colourlabels/" + "b" + ".gif";
}

function behave_processLabelSelect(selector, label) {
	icon="blank";
	if(selector.value != 'ANY') {
		$.ajax({
			url:'/newcar/assets/code/ajax.cfm',
			type:'GET',
			cache:'false',
			data:{
				co2high:selector.value,
				action:'ved'
			},
			success:function(data){
				data = jQuery.trim(data);
				items = data.split(',');
				tmp = items.length;
				for (x=0;x < tmp; x++) {
					tmpObj = items[x];
					tmpObj = tmpObj.split('@');
					tmpVar = tmpObj[0];
					tmpVal = tmpObj[1];
					switch(tmpVar) {
						case 'BAND':
							icon = tmpVal.toLowerCase();
							break;
						case 'FYALTPRICE':
							bhv_set('vedcost_alt_first','\u00A3'+tmpVal);
							break;							
						case 'FPRICE':
							bhv_set('vedcost_pd_first','\u00A3'+tmpVal);
							break;							
						case 'APRICE':
							bhv_set('vedcost_alt','\u00A3'+tmpVal);
							break;							
						case 'SPRICE':
							bhv_set('vedcost_pd','\u00A3'+tmpVal);
							break;
						default: break;
					}
				}
				label.src="/newcar/assets/images/colourlabels/" + icon + ".gif";				
			},
			error:function(xhr, ajaxOptions, thrownError){
				// do nothing if an error is thrown
			}
		});
	} else {
		// ANY case
		label.src="/newcar/assets/images/colourlabels/blank.gif";
		bhv_set('vedcost_alt_first','');
		bhv_set('vedcost_pd_first','');
		bhv_set('vedcost_alt','');
		bhv_set('vedcost_pd','');
	}
}

	// field functions
	function behave_checkFilled(field)
	{
		if (document.proDep[field].value=="")
		{
			alert("Please enter a value in the "+field+" field.");
			return(false);
		} else {
			return(true);
		}
	}
	
	function behave_checkSelBlank(field)
	{
		if (document.proDep[field].value=="")
		{
			alert("Please select a value from the "+field+" drop down.");
			return(false);
		} else {
			return(true);
		}
	}	

	function behave_checkFilledMess(field,mess)
	{
		if (document.proDep[field].value=="")
		{
			alert(mess);
			return(false);
		} else {
			return(true);
		}
	}

	function behave_updateVATS(elem, other, netToGross)
	{
		var k = 1.2;
		if (netToGross == false)
		{
			k = 1/1.2;
		}
		// alert(elem.value*k);
		document.proDep[other].value=(elem.value*k).toFixed(2);
		return(true);
	}
	
	function behave_compareRanges()
	{
		if (document.proDep["frm_range"].value==document.proDep["outputrange"].value)
		{
			document.getElementById('newsn').disabled=true;
		} else {
			document.getElementById('newsn').disabled=false;
		}
		return(true);
	}

	function behave_toggleCosts(elem,elemId,net,gross)
	{
		if (elem.checked)
		{
			document.proDep["frm_txtCosNet"+elemId].disabled = false;
			document.proDep["frm_txtCosNet"+elemId].value = 0-net;
			document.proDep["frm_txtCGross"+elemId].disabled = false;
			document.proDep["frm_txtCGross"+elemId].value = 0-gross;
		} else {
			document.proDep["frm_txtCosNet"+elemId].disabled = true;
			document.proDep["frm_txtCosNet"+elemId].value = net;
			document.proDep["frm_txtCGross"+elemId].disabled = true;
			document.proDep["frm_txtCGross"+elemId].value = gross;
		}
		return true;
	}

// shorthands

	function bhv_clear( name)
	{
		if (document.proDep[name] != null)
			document.proDep[name].value = "";
	}
	
	function bhv_set( name, val)
	{
		if (document.proDep[name] != null)
			document.proDep[name].value = val;
	}
	
	function bhv_dis( name)
	{
		if (document.proDep[name] != null)
			document.proDep[name].disabled = true;
	}

	function bhv_en( name)
	{
		if (document.proDep[name] != null)
			document.proDep[name].disabled = false;
	}

	function bhv_tk( name)
	{
		if (document.proDep[name] != null)
			document.proDep[name].checked = true;
	}

	function bhv_utk( name)
	{
		if (document.proDep[name] != null)
			document.proDep[name].checked = false;
	}
	
	function bhv_istk( name)
	{
		return(document.proDep[name].checked);
	}
	
	function bhv_sel( name, i)
	{
		if (document.proDep[name] != null)
			document.proDep[name].selectedIndex = i;
	}

	function bhv_unsel( name)
	{
		if (document.proDep[name] != null)
			document.proDep[name].selectedIndex = 0;
	}

	function bhv_foc( name)
	{
		if (document.proDep[name] != null)
			document.proDep[name].focus();
	}

// Multiple selector (submit to directory)

	function behave_arrowsAddSelected(formname, field_name, listname_to, listname_from)
	{ 
		var formObj = document.forms[formname];
		var i=-1,list_toRef=-1, list_fromRef=-1;
		for (i=0 ; i<formObj.length ; ++i)
		{
		  var comp = formObj.elements[i].name;
		  if (comp == listname_to)
			list_toRef = i;
		  if (comp == listname_from)
			list_fromRef = i;
		  if (comp == field_name)
			field_ref = i;
		}
		
		var optionArray = formObj.elements[list_fromRef];
		var targetArray = formObj.elements[list_toRef];
		var targetLen = targetArray.length;
		var hidden_list = formObj.elements[field_ref].value;
		for (i=0 ; i<optionArray.length ; ++i)
		{
		   var opt = optionArray[i];
		   if (opt.selected)
		   {
			 targetArray[targetLen++] = new Option(opt.text, opt.value);
			 if (hidden_list != "")
			 {
			   hidden_list+="/";
			 }
			 hidden_list += opt.value;
			 opt.selected = false;
		   }
		}
		// alert("Debug: Hidden list: "+hidden_list);    
		formObj.elements[field_ref].value = hidden_list;
	} 
	
	function behave_arrowsRemoveSelected(formname, field_name, listname_to)
	{
		var formObj = document.forms[formname];
		var i=-1, list_toRef=-1;
		for (i=0 ; i<formObj.length ; ++i)
		{
		  var comp = formObj.elements[i].name;
		  if (comp == listname_to)
			list_toRef = i;
		  if (comp == field_name)
			field_ref = i;
		}
		var targetArray = formObj.elements[list_toRef];
		var hidden_list = "";
		for (i=0 ; i<targetArray.length ; ++i)
		{
		   var opt = targetArray[i];
		   if (!opt.selected)
		   {
			 if (hidden_list != "")
			 {
			   hidden_list += "/";
			 }
			 hidden_list += opt.value;
		   }
		}
		// two passes because nullifying entries upsets list
		for (i=0 ; i<targetArray.length ; ++i)
		{
		   var opt = targetArray[i];
		   if (opt.selected)
		   {
			 targetArray[i] = null;
		   }
		}
		// alert("Debug: Hidden list: "+hidden_list);    
		formObj.elements[field_ref].value = hidden_list;
	}

	function behave_charsleft( keyobj, limit, targetid)
	{
		var target = document.getElementById(targetid);
		var len = keyobj.value.length;
		target.value = limit-len;
	}

// Dreamweaver JS
function MM_preloadImages() { //v3.0
	var d=document; 
	if(d.images){ 
		if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
		for(i=0; i<a.length; i++) 
			if (a[i].indexOf("#")!=0){ 
				d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];
			}
	}
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
	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=MM_findObj(n,d.layers[i].document);
	if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	 if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}

function MM_validateForm() { //v4.0
	var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
	for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
		if (val) { nm=val.name; if ((val=val.value)!="") {
			if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
				if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
			} else if (test!='R') {
				if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
				if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
					min=test.substring(8,p); max=test.substring(p+1);
					if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
		} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
	} if (errors) alert('The following error(s) occurred:\n'+errors);
	document.MM_returnValue = (errors == '');
}
