var defaults = new Array();
var screenName;
var sweep = new Array();
//////////////////////////////////////////////////////////////////////////////////////////////////

   var IE_http_request = false;

   function makePOSTRequest(url, parameters, feedback) {
      var http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
                // set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
      if (window.navigator.userAgent.indexOf("MSIE") > -1) IE_http_request = http_request;
   }

   function alertContents() {
      if (IE_http_request){
        http = IE_http_request;
      }else{
        http = this;
      }
      if (http.readyState == 4) {
         if (http.status == 200) {
            //alert(http.responseText);
            var result = http.responseText;
            eval(result);
         } else {
            alert('There was a problem with the request.');
         }
         IE_http_request = false;
      }
   }

///////////////////////////////////////////////////////////////////////////////////////////////////


var xCoord = null;
var yCoord = null;
var intransit = null;
var wdays = new Array('Sun','Mon','Tues','Wed','Thur','Fri','Sat');
var Rmonths = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var default_padding = "1px 1px 3px 1px";
if (window.navigator.userAgent.indexOf("MSIE") > -1) default_padding = "2px 2px 4px 2px";

if(document.captureEvents) {document.captureEvents(Event.MOUSEMOVE);}

function Character(evt){
         var EventObject;
         if (evt){
             EventObject = evt;
         }else{
             EventObject = event;
         } 
         var charCode =
              document.layers ? evt.which :
              document.all ? event.keyCode :
              document.getElementById ? evt.keyCode : 0;
         return charCode;

}

function pleaseWait(message){

	var span = document.getElementById('plw');
	
	if (span == null){
	span = document.createElement("div");
	span.id = "plw";
	span.style.position = "absolute";
	span.style.top = parseInt(document.body.scrollTop) + "px";
	span.style.left = parseInt(document.body.scrollLeft) + "px";
	
	if ( window.navigator.userAgent.indexOf("MSIE") > -1 ){
                span.style.paddingTop = Math.round( parseFloat(document.documentElement.clientHeight) * 0.25 ) + "px";
	        span.style.height =( document.documentElement.clientHeight - 10)+ "px";
		span.style.width = (document.documentElement.clientWidth - 10) + "px";
	}else{
		span.style.paddingTop = Math.round( parseFloat(window.innerHeight) * 0.25 ) + "px";
		span.style.height =( window.innerHeight - 10)+ "px";
		span.style.width = (window.innerWidth - 10) + "px";
	}
	span.style.zIndex = 1000;
	span.style.opacity = 0.45;
	span.style.filter = 'alpha(opacity=45)';
	span.style.textAlign = "center";
	span.style.background = "#FFF";
	span.style.color= "#000";
	span.style.fontWeight = "bold";

	document.body.appendChild(span);
	}
	span.innerHTML = "&nbsp;";

	var span2 = document.getElementById('plw_note');
	if (span2 == null){
		span2 = document.createElement("div");
		span2.id = "plw_note";
		document.body.appendChild(span2);
		span2.style.zIndex = 1001;
		span2.style.background = "#FFF";
		span.style.filter = 'alpha(opacity=45)';
		span.style.opacity = 0.45;
		span2.style.width = "300px";
		span2.style.position = "absolute";
		span2.style.top = (parseInt(document.body.scrollTop) + 100) + "px";
		span2.style.left = ((document.documentElement.clientWidth * 0.5) + parseInt(document.body.scrollLeft) - 155) + "px";
		span2.style.padding = "10px";
		span2.style.border = "solid 1px #333";
		span2.style.color = "#333";

	}
	span2.innerHTML = "Please wait...";
	if (message) span2.innerHTML += " " + message;
	window.setTimeout("finishWait()",10000);
}

function finishWait(){
	var span = document.getElementById('plw');
	if (span){
		document.body.removeChild( span );
	}
	var span2 = document.getElementById('plw_note');
	if (span2){
	        document.body.removeChild( span2 );
	}
}

function ajax_feedback(){
        for(var i=0; i<arguments.length; i++){
            eval(arguments[i])
        }
}



function js_showjob (jobid){

        var Box = new js_box("View Summary");
        Box.AddtoBody();
	Box.width = 730
        var span = document.createElement("span");
        span.style.zIndex = 710;
        span.style.width = 710;
        span.style.height = 420;
        span.innerHTML = '<iframe src="/summarylite?jobid=' + jobid + '&popup=1&rand=' + Math.random() + '" width=710 height=400></iframe>';
	Box.append(span);
	Box.centre();
}

function js_suggest_service (description, agentid, zone){

	var Box = new js_balloon( "agent_area" );
	Box.AddtoBody();

	var span = document.createElement("span");
	span.innerHTML = description;
	Box.append(span);

}

function js_suggest_agent (description, agentid){

        var Box = new js_balloon( "agent_area" );
        Box.AddtoBody();

        var span = document.createElement("span");
        span.innerHTML = description;
        Box.append(span);

}


function js_agent_options(list){

	var d = document.getElementById('agent_option');
	if (d){
		d.parentNode.removeChild(d);
	}

	if (list == null) return;
	if (list.length == 0) return;

	var Box = new js_box("Agent Options",'agent_option');

	Box.append(document.createTextNode( "Select the required agent options" ));

	var form = document.createElement("form");
	
	for (var i=0;i<list.length;i++) {

			var div = document.createElement('div');
			div.align = "right";
			div.appendChild(document.createTextNode( list[i] ));

			var checkbox = document.createElement('input');
			    checkbox.title = list[i++];
			    checkbox.type = 'checkbox';
			    checkbox.id = "agent-" + list[i++];
			    checkbox.name = checkbox.id;
			    checkbox.value = list[i++];
			
				checkbox.onclick = function(){
				
		//			if (save) return save(this);

					if (update)
						return update(this);


				}

			if (list[i++] == true){
				checkbox.defaultChecked = true;
			}
			
			div.appendChild( checkbox );
			Box.append( div );
	}
	
	Box.append(form);
	Box.AddtoBody();

}

function js_repeat_work(){

	var repeatField = document.getElementById('repeat');
	if (!repeatField) return js_alert('No Repeat field found');
	var repeats = parseInt(repeatField.value);
	if (isNaN(repeats)){
		repeatField.value = "0";
		repeats = 0;
	}

        var periodField = document.getElementById('period');
	if (!periodField) return js_alert('No Repeat Period found');
        var period  = parseInt(periodField.value);
        if (isNaN(period)){
                periodField.value = "0";
                period = 0;
        }	

        var periodField = document.getElementById('weekdays');
        if (!periodField) return js_alert('No repeat weekday field found');
        var weekday = true;

        if (periodField.value == "N"){
                weekday = false;
        }else{
		periodField.value ="Y";
	}

        var Box = new js_box('Book on multiple dates', 'multi_panel');
        Box.AddtoBody();

        var div = document.createElement('div');
        Box.append( div );
	var ttl = document.createElement('span');
	ttl.style.width = "250px";
	ttl.style.display = "inline-block";
	ttl.appendChild(document.createTextNode( "Number of times this job is to be repeated:" ))
        div.appendChild(ttl);
	var input = document.createElement('input');
        input.type = "text";
	input.style.width= "30px";
	input.value = repeats;
	input.title = "Enter the number of times this job is to be repeated." 
	input.onchange = function(){
		var r = document.getElementById('repeat');
		if (r){
			r.value = this.value;
			save(r);
			calc_dates();
		}
	}        
	div.appendChild( input );
	div.appendChild(document.createElement("br"));
        var ttl2 = document.createElement('span');
        ttl2.style.width = "250px";
	ttl2.style.display = "inline-block";
        ttl2.appendChild(document.createTextNode( "Days separating bookings:" ))
        div.appendChild(ttl2);

        var input2 = document.createElement('input');
        input2.type = "text";
	input2.value = period;
	input2.style.width= "30px";
        input2.title = "Enter the number of days apart between the bookings."
        input2.onchange = function(){
                var r = document.getElementById('period');
                if (r){
                        r.value = this.value;
                        save(r);
                        calc_dates();
                }
        }
        div.appendChild( input2 );
	div.appendChild(document.createElement("br"));
        var ttl3 = document.createElement('span');
        ttl3.style.width = "250px";
        ttl3.style.display = "inline-block";
	ttl3.appendChild(document.createTextNode( "Week days (Mon-Fri) only:" ))
        div.appendChild(ttl3);
        var checkbox = document.createElement('input');
        checkbox.type = 'checkbox';
        checkbox.title = 'Check to only repeat the bookings on weekdays';
        checkbox.value = "Y";
        if (weekday == true){
                checkbox.defaultChecked = true;
        }	
        checkbox.onchange = function(){
                var r = document.getElementById('weekdays');
                if (r){
			if (this.checked){
				r.value = "Y";
			}else{		
				r.value = "N";
			}	
                        save(r);
                        calc_dates();
                }
        }
	div.appendChild( checkbox );
	div.appendChild(document.createElement("br"));
	var tdiv = document.createElement('div');
            tdiv.style.height = "180px";
            tdiv.style.width = "255px";
            tdiv.style.overflow = "auto";
	Box.append(tdiv);
	tdiv.id = "repeat_date_table";
	var dw = document.createElement('input');
            dw.type = "button";
	    dw.value = "Close";
	    dw.onclick = function (){
	    	CloseBox(this.parentNode.id);
	    } 
            Box.appendButton(dw);


//	var table = document.createElement('table');
//	table.style.width = "230px";
//	tdiv.appendChild(table);
//	var tbody = document.createElement('tbody');
//	tbody.id = "repeat_date_table";
//	table.appendChild(tbody);
	calc_dates();

}


function calc_dates(){
	var period = parseInt(document.getElementById('period').value);
	if (isNaN(period)) period = 0;
	var repeats = parseInt(document.getElementById('repeat').value);
	if (isNaN(repeats)) repeats = 0;

	var weekdays = document.getElementById('weekdays');

	var tbody = document.getElementById('repeat_date_table');
tbody.innerHTML = "";
//        while ( tbody.rows.length > 0){
//                tbody.deleteRow(0);
//                }
	tbody.style.marginTop = "3px";
	tbody.style.paddingTop = "5px";
	tbody.style.paddingLeft = "5px";
	tbody.style.border = "";
	tbody.style.background = "";

	if (repeats < 1) return;
	tbody.style.border = "solid 1px #333";
	tbody.style.background = "#777";
                var Maincell = document.createElement("div");
                    Maincell.appendChild(document.createTextNode( "Dates:" ));
                    Maincell.style.fontWeight = "bold";
                    Maincell.style.fontSize = "9px";
		    Maincell.width = "230px";
                    tbody.appendChild(Maincell);


	var dates = new Array();

        var pickup = new Date();
        var source_obj = document.getElementById('pickupdate');

        if (source_obj){
           if (source_obj.value.match(/\d+\-\d+/)){
           var param = source_obj.value.split(/\D/);
           var m = parseFloat(param[1]);
               m--;
           pickup = new Date( parseFloat(param[0]), m,parseFloat(param[2]));
           }
        }
         pickup.setHours(12);
	 var newDate = new Date();
         var msecs = pickup.getTime();// + 43200000;
         newDate.setTime(msecs);
         for(var i=(repeats-1); i>-1; i--){

                var msecs = newDate.getTime();
                msecs += 86400000 * period;
                newDate.setTime(msecs);
                if (weekdays.value == "Y"){

                        if (newDate.getDay() == 6){
                        var msecs = newDate.getTime();
                        msecs += 86400000 * 2;
                        newDate.setTime(msecs);
                        }
                        if (newDate.getDay() == 0){
                        var msecs = newDate.getTime();
                        msecs += 86400000;
                        newDate.setTime(msecs);
                        }


                }
		var year = newDate.getYear();
                if(parseInt(year) < 1990){
			year += 1900;
		}
		
		var vis =  wdays[newDate.getDay()] + " " + newDate.getDate() + ", " + Rmonths[newDate.getMonth()] + " " + year;
               // var row = document.createElement("TR");
                var cell = document.createElement("div");
                    cell.appendChild(document.createTextNode( vis ));
                    cell.width = "230px";
		    tbody.appendChild(cell);
                if (repeats == i + 1){
			Maincell.appendChild(document.createTextNode( " " + vis ));
		}    
		if (i == 0){
			Maincell.appendChild(document.createTextNode( " > " + vis ));
		}
		    //tbody.insertRow(0).appendChild(row);

        }


}

function js_clear_field_rules(){

	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].onlinehelp){
			x[i].onlinehelp = function(){};
			x[i].title = "";
			x[i].style.borderColor = "";
		}
	}
}

function js_field_rule(id, title){
	
	var field = document.getElementById(id);
	if (field){
		field.onfocus = function(){ this.onlinehelp(); this.select(); }
		field.onlinehelp = function(){ 
			
			var Box = new js_balloon( this.name );
			Box.AddtoBody();
			var span = document.createElement("span");
			span.innerHTML = title;
			Box.append(span);
		};
		field.style.borderColor = "yellow";
		field.title = title;
	}
	
}


function nop(){
//nop function to block default funcions
}

function getValueByName(form, name){
	var field = "";
	for (var i = 0; i < form.elements.length; i++){
		if (form.elements[i].name == name) field = form.elements[i].value;
	}
	return field;
}
function setValueByName(form, name, value){
        var field = "";
        for (var i = 0; i < form.elements.length; i++){
                if (form.elements[i].name == name) form.elements[i].value = value;
        }
}

function getFormElement(form,name){
	if (!form) return;
	for (var i = 0; i < form.elements.length; i++){
		if (form.elements[i].name == name) return form.elements[i];
	}
	return;
}

function js_checkwhere(e) {
	
var old = xCoord;
var yold = yCoord;

if (document.layers){
        xCoord = e.x;
        yCoord = e.y;
}
else if (document.all){
        xCoord = event.clientX;
        yCoord = event.clientY;
}
else if (document.getElementById){
        xCoord = e.clientX;
        yCoord = e.clientY;
}
if (old !=null){
	if (intransit && yold != 0) {
        	var obj = document.getElementById(intransit);
        	if (obj){
			var btop = parseFloat(obj.style.top);
			if (isNaN(btop) ) btop = 0;
			var movement = parseInt(yCoord) - parseInt(yold);
	                btop += movement;// - parseInt(obj.offsetTop);
			obj.style.top = parseInt(yCoord);
        	}

        	self.status = intransit+ " " + (obj.style.top || 0) +  "X= "+ xCoord + "  Y= " + yCoord;
	}
}
}

function roam(e){

	if (intransit == null) return; 

	var X;
	var Y;
	if (document.layers){
	     X = e.x;
	     Y = e.y;
	}
	else if (document.all){
	     X = event.clientX;
	     Y = event.clientY;
	}
	else if (document.getElementById){
	     X = e.clientX;
	     Y = e.clientY;
	}
	
	var obj = document.getElementById(intransit);
	if (obj){
		if (obj.xcoords != null){

			var vertical = parseFloat(obj.ycoords) - parseFloat(Y);
			var horizontal = parseFloat(obj.xcoords) - parseFloat(X);


			obj.style.top  = (parseFloat(obj.style.top) - vertical) + "px";
			obj.style.left = (parseFloat(obj.style.left) - horizontal) + "px";

		}
		obj.xcoords = X;
		obj.ycoords = Y;
	}
}

function findPos(obj){
	var curleft = curtop = 0;
	if (obj.offsetParent){
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent){
			curleft += parseInt(obj.offsetLeft) - parseInt(obj.scrollLeft);
			curtop += parseInt(obj.offsetTop) - parseInt(obj.scrollTop);
		}
	}
	curleft += parseInt(document.body.scrollLeft);
	curtop +=parseInt(document.body.scrollTop);

	return [curleft,curtop];
}


function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}


function js_popup_menu(fieldid, value, title, options, labels){

	if (!options) return;
	if (!labels){
		labels = new Array();
	}

	var element = document.getElementById( fieldid )
	if (element){
	alert(element.type);
		if (element.type == "text"){
			element.value = value;
		}else{
			element.innerHTML = value;
		}
	}else{

		var box = document.getElementById( fieldid + "_box");
		if (box){
			
			var table = document.createElement('table');
			box.appendChild(table);
				
			var Row = table.insertRow(0);
			var b = Row.insertCell(0);
			b.style.background = "red";
			b.width = "120px";
			b.style.textAlign = "left";
			b.style.width = "200px";
			b.style.margin = "4px";
			b.innerHTML = title || fieldid;


			var cell = Row.insertCell(1);

			var select = document.createElement('select');
			//select.value = value;
			cell.appendChild(select);

			select.name = fieldid;
			select.id = fieldid;
			select.onchange = function() { save(this);JumpToNext(this);};

			var selectedIndex = 0;
			for (var i = 0; i < options.length;i++){

				var option = document.createElement('option');
				option.value = options[i];
				option.text = labels[i] || options[i];

			        if (window.navigator.userAgent.indexOf("MSIE") > -1){
					select.add(option);
				}else{
					select.appendChild(option);
				}
				if (options[i] == value) select.selectedIndex = i;


			}


			Row.appendChild(cell);			
			box.style.display = "block";
			box.style.padding = "4px";


		}


	}	


}

function save(field){

//	js_alert(field.name + " = " + field.value);

}

function check_field_saved( id , saveStamp){

        var field = document.getElementById( id );

        if (field){
                if (field.saveStamp == saveStamp){
                field.style.border = "1px solid yellow";
                save(field, 1);   //re-send request to server.
                }
        }
}

function confirm_field_save( objectid, stamp){

        var field = document.getElementById( objectid );

        if (field){
                if (field.saveStamp == stamp){
                        field.saveStamp = null;
                        field.style.border = "";
                }
        }
        return;
}










function js_hide(id){
			var obj = document.getElementById(id);
			if (obj) obj.parentNode.removeChild(obj);
}


function js_dyn_force(fieldid, value, title, options, extras, command, feedback){

	var obj = js_field(fieldid,value,title, false,false);
	js_dyn_textCommand_functions (obj, options, extras, command, feedback);

}

function js_dyn_force_functions (obj, options, extras, command, feedback){
	if (obj){

		obj.listing = options;
		obj.extras = extras;
		obj.feedback = feedback;	//fired on enter or select from dropdown	
		obj.command = command;		//fired on keydown prior to checking list

		obj.selected = 0;
		obj.onfocus = function (){
			js_dropdown(this);
			this.style.borderColor = "#AAA";
			this.style.background = "#FFF"; 
			if (this.highlight){ this.highlight() }
		}

		obj.onmousedown = function(){
			js_dropdown(this);
		}

		obj.onblur = function(){
			if (this.selected == null && ( this.nullOk == null || (this.nullOk == true && this.value !="")))
			{
                                if (this.OKnull && this.value ==""){
                                        //skip bitching
                                }else{
                                this.style.borderColor = "red";
                                this.style.background = "#F99";
                                js_warnselect(this, 'Please select a valid value');
                                }
			}else{
				this.style.borderColor = "#AAA";
				this.style.background = "#FFF"; 
				if (this.saved != this.value || this.selected != this.saved_selected){
					this.saved = this.value;
					this.saved_selected = this.selected;
					if (this.feedback){
						eval( this.feedback + "(this)" );
					}else{
						save(this);
					}
				}
			}
			if (this.tidy){ this.tidy() }
			if (this.fade){this.fade();}
			var obj = document.getElementById(this.id + '_drop_down');
			if (obj) obj.parentNode.removeChild(obj);

		}

		obj.onchanged = function(){
			if (this.selected == null)
			{
				if (this.OKnull && this.value ==""){
					//skip bitching
				}else{
				this.style.borderColor = "red";
				this.style.background = "#F99"; 
				js_warnselect(this, 'Please select a valid value');
				}
				if (this.tidy){ this.tidy() }

			}else{
				this.style.borderColor = "#AAA";
				this.style.background = "#FFF"; 
				if (this.saved != this.value || this.selected != this.saved_selected){
					this.saved = this.value;
					this.saved_selected = this.selected;
					if (this.feedback){
						eval( this.feedback + "(this)" );
					}else{
						save(this);
					}
				}
			}			

		}

		obj.onkeydown = function (evt){

			if (evt){
				if (js_enTer(evt,this) != 1){
			  	if (this.command){
					eval( this.command + "(this)" );
				}
				js_checklist(this);
				}	
				return;	
			}	

			if (js_enTer(window.event,this) != 1){
		 	  if (this.command){
				eval( this.command + "(this)" );
			  }
			  js_checklist(this);
			}
		}
	}

}
function js_dyn_flexible(fieldid, value, title, options, extras, command, feedback){

	var obj = js_field(fieldid,value,title, false,false);
	js_dyn_flexible_functions (obj, options, extras, command, feedback);

}

function js_dyn_flexible_functions (obj, options, extras, command, feedback){
	if (obj){
		obj.extras;
		obj.listing = options;
		obj.selected = 0;
		obj.feedback = feedback;	//fired on enter or select from dropdown	
		obj.command = command;		//fired on keydown prior to checking list

		obj.onfocus = function (){
			js_dropdown(this);
			if (this.highlight){ this.highlight() }
		}
		obj.onmousedown = function(){
			js_dropdown(this);
		}

		obj.onblur = function(){
			this.style.borderColor = "#AAA";
			this.style.background = "#FFF"; 
			if (this.saved != this.value || this.selected != this.saved_selected){
				this.saved = this.value;
				this.saved_selected = this.selected;
				if (this.feedback){
					eval( this.feedback + "(this)" );
				}else{
					save(this);
				}
			}
			var obj = document.getElementById(this.id + '_drop_down');
			if (obj) obj.parentNode.removeChild(obj);
			if (this.fade){this.fade();}

		}

		obj.onchange = function(){

				this.style.borderColor = "#AAA";
				this.style.background = "#FFF"; 
				if (this.saved != this.value|| this.selected != this.saved_selected){
					this.saved_selected = this.selected;
					this.saved = this.value;
					if (this.feedback){
						eval( this.feedback + "(this)" );
					}else{
						save(this);
					}
				}		
		}


		obj.onkeydown = function (evt){

			if (evt){
				if (js_enTer(evt,this) != 1){
			  	if (this.command){
					eval( this.command + "(this)" );
				}
				js_checklist(this);
				}	
				return;	
			}	

			if (js_enTer(window.event,this) != 1){
			  if (this.command){
				eval( this.command + "(this)" );
			  }
			  js_checklist(this);
			}
		}
	}

}


function js_dyn_textCommand(fieldid, value, title, options, extras, command, feedback){


	var obj = js_field(fieldid,value,title, false,true);
	js_dyn_textCommand_functions (obj, options, extras, command, feedback);

}

function js_dyn_textCommand_functions (obj, options, extras, command, feedback){
	if (obj){
		obj.listing = options;
		obj.extras = extras;
		obj.selected = 0;
		obj.feedback = feedback;	//fired on enter or select from dropdown	
		obj.command = command;		//fired on keydown prior to checking list
		obj.onfocus = function (){
			js_dropdown(this);
			if (this.highlight){ this.highlight() }
		}
		obj.onmousedown = function(){
			js_dropdown(this);
		}

		obj.onblur = function(){
			if (this.selected == null)
			{
				 this.style.borderColor = "red"; 
				 this.style.background = "#F99";
				 js_warnselect(this, 'Please select a valid value');
			}else{
				this.style.borderColor = "#AAA";
				this.style.background = "#FFF"; 
				if (this.saved != this.value || this.selected != this.saved_selected){
					this.saved_selected = this.selected;
					this.saved = this.value;
					if (this.feedback)
						eval( this.feedback + "(this)" );
				}
			}
			if (this.fade){this.fade();}
			var obj = document.getElementById(this.id + '_drop_down');
			if (obj) obj.parentNode.removeChild(obj);

		}

		obj.onchange = function(){
			if (this.selected == null)
			{
				this.style.borderColor = "red";
				this.style.background = "#F99"; 
				js_warnselect(this, 'Please select a valid value');
			}else{
				this.style.borderColor = "#AAA";
				this.style.background = "#FFF"; 
				if (this.saved != this.value || this.selected != this.saved_selected){
					this.saved_selected = this.selected;
					this.saved = this.value;
					if (this.feedback)
					eval( this.feedback + "(this)" );
				}
			}			

		}


		obj.onkeydown = function (evt){

			if (evt){
				if (js_enTer(evt,this) != 1){
					if (this.command){
						eval( this.command + "(this)" );
					}
					js_checklist(this);
				}	
				return;	
			}	

			if (js_enTer(window.event,this) != 1){
			  	if (this.command){
					eval( this.command + "(this)" );
				}
				js_checklist(this);
			}
		}
	}

}


function js_dyn_fire(fieldid, value, title, options, extras, fire, command){

	var obj = js_field(fieldid,value,title, false,true);
	js_dyn_fire_functions (obj, options, extras, fire, command);

}

function js_dyn_fire_functions (obj, options, extras, fire, command, onchange, goto){

	if (obj){
		obj.listing = options;
		obj.fire = fire;		//item in array fired on enter or select from dropdown	
		obj.command = command;		//fired on keydown prior to checking list
		obj.fire_onchange = onchange;   //fired if no fire command or no option is selected.
		obj.extras = extras;
		obj.selected = 0;
		obj.onfocus = function (){
			js_dropdown(this);
			this.selected = 0;
			if (this.id == "pc") this.select(); // allow for overtype
			if (this.highlight){ this.highlight() }
		}
		obj.onmousedown = function(){
			js_dropdown(this);
		}
		obj.onblur = function(){
			var error = js_check(this);
			if (error)
			{
				 this.style.borderColor = "red"; 
				 this.style.background = "#F99";
				 js_warnselect(this, error);
			}else{
				this.style.borderColor = "#AAA";
				this.style.background = "#FFF";
				if (!this.selected && this.value.length == 0){
					this.listing = new Array();
					this.fire = new Array();
				}
				
				if (this.saved != this.value || this.selected != this.saved_selected){
					this.saved_selected = this.selected;
					if (this.fire){
						if (this.fire[this.selected]){
							if (this.fire[this.selected].match(/\);$/)){
								eval( this.fire[this.selected]);
							}else
								eval( this.fire[this.selected] + "(this)" );
						}else if (this.fire_onchange != null && (this.saved != this.value || this.saved != null)){
							if (this.fire_onchange.match(/\);$/)){
								eval( this.fire_onchange);
							}else{
								eval( this.fire_onchange + "(this)" );
							}
						}else if (this.tidy != null){
							this.tidy();
						}
					}else if(this.tidy != null) this.tidy();

					this.saved = this.value;
					if (this.onsave){ this.onsave() };	
				} 
			}
			if (this.fade){this.fade();}
			var obj = document.getElementById(this.id + '_drop_down');
			if (obj) obj.parentNode.removeChild(obj);
			return false;
		}
		obj.onchange = this.onblur;
		obj.onchange1 = function(){
			var error = js_check(this);
			if (error)
			{
				this.style.borderColor = "red";
				this.style.background = "#F99"; 
				js_warnselect(this, error);
			}else{
				this.style.borderColor = "#AAA";
				this.style.background = "#FFF"; 
				if (this.saved != this.value || this.selected != this.saved_selected){
					this.saved_selected = this.selected;
					if (this.fire){
					   if (this.fire[this.selected]){
						if (this.fire[this.selected].match(/\);$/)){
							eval( this.fire[this.selected]);
						}else
							eval( this.fire[this.selected] + "(this)" );
					    }else if (this.fire_onchange != null && (this.saved != this.value || this.saved != null)){
						if (this.fire_onchange.match(/\);$/)){
                                                        eval( this.fire_onchange);
                                                }else{
                                                        eval( this.fire_onchange + "(this)" );
                                                }
                                            }else if (this.tidy != null){
                                                this.tidy();
	
					    }
					}else if (this.tidy != null){
						this.tidy();
					}
					this.saved = this.value;
					if (this.onsave){ this.onsave() };
				}
			}		
			var obj = document.getElementById(this.id + '_drop_down');
			if (obj) obj.parentNode.removeChild(obj);
			return false;
		}


		obj.onkeydown = function (evt){
		if (evt){
			if (js_enTer(evt,this) != 1){
			  	if (this.command){
				  eval(this.command + "(this)");	
				}
				js_dropdown(this);
				js_checklist(this);
				
				return true;
			}	
			return false;	
		}	

		if (js_enTer(window.event,this) != 1){
			if (this.command){
				eval( this.command + "(this)" );
			}
			js_dropdown(this);
			js_checklist(this);
			return true;
		}
		return false;

	    }
	}

}


function js_text_force(fieldid, value, title, rule){

	var obj = js_field(fieldid,value,title, false,false);
	if (obj){
		obj.rule = rule;
		obj.onfocus = function (){
			this.style.borderColor = "#AAA";
			this.style.background = "#FFF"; 
			if (this.highlight){ this.highlight() }
		}
		obj.onblur = function(){
			var error = js_check(this);
			if (error)
			{
				 this.style.borderColor = "red"; 
				 this.style.background = "#F99";
				 js_warnselect(this, error);
			}else{
				this.style.borderColor = "#AAA";
				this.style.background = "#FFF"; 
			}
			if (this.fade){this.fade();}

		}

		obj.onchange = function(){
			var error = js_check(this);
			if (error)
			{
				this.style.borderColor = "red";
				this.style.background = "#F99"; 
				js_warnselect(this, error);
			}else{
				this.style.borderColor = "#AAA";
				this.style.background = "#FFF"; 
				save(this);
			}			

		}
		obj.onkeydown = function (evt){ js_enTer(evt,this); }	

	}

}

function js_check(field){

        switch( field.rule ) {
            
		case "number": 
			if (isNaN(field.value) == true) return "Please Supply a Valid Number";
		case "integer":
			if (isNaN(field.value) == true) return "Please Supply a Valid Whole Number";
	}


	return null;


}
function l(fieldid, value){
	js_field(fieldid,value,null,true);
}

function u(fieldid, value, title, no_save){
	js_field(fieldid, value, title, false, no_save);
}

function q(value, locked){

	var qb = js_field('quote',value);

        qb.ondblclick = function(){
	     js_pricecheck();
	     
	}
	qb.title = "Double Click to check previous prices";

	if (document.getElementById('locked')){
		if (locked)
		document.getElementById('locked').checked = locked;
		return;
	}
	
	var checkbox = document.createElement('input');
	checkbox.type = 'checkbox';
	checkbox.name = 'locked';
	checkbox.id = 'locked';
	checkbox.title = 'Lock quote to current value';
	checkbox.value = "1";
	if (locked == true){
		checkbox.defaultChecked = true;
	}
	qb.style.width = (qb.offsetWidth - 21) + "px";
	qb.parentNode.appendChild(checkbox);
}


function js_label(fieldid, value, title){
	js_field(fieldid,value,title,true);
}

function js_update(fieldid, value, title, no_edit, no_save){
	return js_field(fieldid, value, title, no_edit, no_save)
}

function js_field(fieldid, value, title, no_edit, no_save){	
	var element = document.getElementById( fieldid );
	if (element){
	//alert(element.type);
		if (element.type == "text"){
                                element.type = "text";
                                if (value != null) element.value = value;
                                element.name = fieldid;
                                element.id = fieldid;
                                element.className = "TEXTBOX";
				element.reset = function(){ 
						this.value = "";
		                                this.selected = null;
						this.saved = null
                		                this.saved_selected = null;		
						}

                                element.onkeydown = function (evt){ js_enTer(evt,this); }
                                if (no_save != true){

                                        element.onchange = function() {
                                                save(this);
                                                var obj = document.getElementById(this.id + '_drop_down');
                                                if (obj) obj.parentNode.removeChild(obj);
                                                JumpToNext(this);

                                         };

                                }else{

                                        element.onchange = function() {
                                                var obj = document.getElementById(this.id + '_drop_down');
                                                if (obj) obj.parentNode.removeChild(obj);
                                                JumpToNext(this);
                                        }

                                }
		}else if (element.type == "hidden"){
		
			element.value = value;
		
		}else{
			if(document.all){
				element.reset = function (){
					element.innerText = "";
				}
				element.innerText = value;
			}else{
				element.reset = function (){
					element.innerHTML = "";
				}
				element.innerHTML = value;
			}
		}
	}else{

		var box = document.getElementById( fieldid + "_box");
		if (box){
			
			var table = document.createElement('table');
			box.appendChild(table);
				
			var Row = table.insertRow(0);
			var b = Row.insertCell(0);
			//b.style.background = "red";
			b.width = "93px";
			b.style.textAlign = "left";
			b.style.width = "93px";
			b.style.margin = "4px";
			b.style.marginRight = "0px";
			b.style.marginLeft = "0px";
			b.style.padding = "0px";
			if (!title){
				title = fieldid.substr(0, 1).toUpperCase() + fieldid.substr(1);
				title = title.replace('_',' ');
			}
			b.innerHTML = title;
			var re = new RegExp('href');
			if (re.exec(title) == null){ b.title = title; }
			if (b.innerHTML.length > 14){ b.style.fontSize = "8pt" }
			if (b.innerHTML.length > 17){ b.style.fontSize = "7pt" }
			
			var cell = Row.insertCell(1);
			if (no_edit != true){
				element = document.createElement('input');
				element.type = "text";
				element.value = value;
				cell.appendChild(element);
				element.name = fieldid;
				element.id = fieldid;
				element.className = "TEXTBOX";
				element.onkeydown = function (evt){ js_enTer(evt,this); }
				if (no_save != true){ 
					
					element.onchange = function() { 
						save(this);
						var obj = document.getElementById(this.id + '_drop_down');
						if (obj) obj.parentNode.removeChild(obj);	
						JumpToNext(this);		
						
					 };

				}else{

					element.onchange = function() {
						var obj = document.getElementById(this.id + '_drop_down');
						if (obj) obj.parentNode.removeChild(obj);
						JumpToNext(this);
					}
					
				}
			element.reset = function(){ 
				this.value = ""; 
                                this.saved = null;
				this.selected = null;
                                this.saved_selected = null;
			}
			element.onfocus = function(){ this.highlight() }
			element.onblur = function(){ this.fade() }
			element.setAttribute("autocomplete", "off");
                        element.highlight = function (){
                                this.style.marginLeft = "2px";
                                this.parentNode.style.borderLeft = "3px red solid";
                        }
                        element.fade = function (){
                                this.style.marginLeft = "5px";
                                this.parentNode.style.borderLeft = "0px";
                        }

			element.style.marginLeft = "5px";

			}else{
				cell.id = fieldid;
				if(document.all){
					cell.innerText = value;
					cell.reset = function (){
						cell.innerText = "";
					}
				}else{
					cell.reset = function (){
						cell.innerHTML = "";
					}
					cell.innerHTML = value;
					
				}
			}

			Row.appendChild(cell);			
			box.style.display = "block";
			box.style.padding = "2px";


		}else{

			element = document.createElement('input');
			element.type = "text";
			element.value = value;
			element.name = fieldid;
			element.id = fieldid;
			element.className = "TEXTBOX";
			element.onkeydown = function (evt){ js_enTer(evt,this); }
			if (no_save != true){
				element.onchange = function() {
					save(this);
					var obj = document.getElementById(this.id + '_drop_down');
					if (obj) obj.parentNode.removeChild(obj);
					if (obj) obj.parentNode.removeChild(obj);
					JumpToNext(this);
				};
			}else{
				element.onchange = function() {
					var obj = document.getElementById(this.id + '_drop_down');
					if (obj) obj.parentNode.removeChild(obj);
				}
			}
                        element.onfocus = function(){ this.highlight() }
                        element.onblur = function(){ this.fade() }
			element.reset = function(){ 
				this.value = ""; 
                                this.saved = null;
                                this.selected = null;
				this.saved_selected = null;
			}
                        element.highlight = function (){
                                this.style.marginLeft = "2px";
                                this.parentNode.style.borderLeft = "3px red solid";
                        }
                        element.fade = function (){
                                this.style.marginLeft = "5px";
                                this.parentNode.style.borderLeft = "0px";
                        }
			element.setAttribute("autocomplete", "off");
                        element.style.marginLeft = "5px";	


		}


	}
	return element;
}

function js_balloon( anchor, id ){

	this.div = document.createElement("div");
	this.AddtoBody = AddtoBody;

	if (id == null){
	        this.div.id = Math.random();
	}else
		this.div.id = id;

	this.div.style.position = "absolute";

	if (anchor != null){
	var field = document.getElementById(anchor);
	  if (field != null){

		this.div.style.left=zb(field) + "px";
		this.div.style.top=(Yb(field) - 24) +"px";

	   }else{
		var field = eval("document.main." + anchor);
		if (field){
			this.div.style.left=zb(field)+"px";
                	this.div.style.top=(Yb(field) - 24 )+"px";
		}
	  }
	}
	this.div.close = function (){
	document.body.removeChild( this );
	}
	this.div.style.height = "18";
	this.div.style.backgroundColor = "#FFD";
        this.div.style.color = "black";
	this.div.style.fontSize = "8pt";
	this.div.style.paddingLeft = "3px";
	this.div.style.paddingRight = "3px";
	this.div.style.margin = "4px";
	this.div.style.border = "solid 1px #000";
	this.div.style.opacity = 0.85;
	this.div.style.filter = 'alpha(opacity=85)';
	this.append = Append;
	this.appendButton = AppendButton;
	window.setTimeout("document.getElementById(" + this.div.id + ").close()", 7000);

}


function js_box(title, id){


		this.div = document.createElement("div");
		this.AddtoBody = AddtoBody;
		
		if (id == null){
			this.div.id = Math.random();
		}else
			this.div.id = id;
	
	this.div.style.background = "#999";
	this.div.style.position = "absolute";
	this.div.style.left = "300px";

	if (navigator.appName == 'Netscape'){
	var h = window.pageYOffset ||
	        document.body.scrollTop ||
		document.documentElement.scrollTop;
	this.div.style.top = 100 + parseInt(h) + "px";
	}else{
	var h = document.body.scrollTop ||
	        document.documentElement.scrollTop;
	this.div.style.top = 100 + parseInt(h) + "px";
	}

	this.div.align = "center";
	
	this.div.popup = true;
	this.div.style.border = "1px solid black";

        var img_container = document.createElement('span');
        this.div.appendChild(img_container);

        img_container.style.margin = "0px";
        img_container.style.padding= "0px";
        img_container.style.position = "absolute";
        img_container.style.top = "2px";
        img_container.style.left = "2px";
        img_container.style.width = "21px";
        img_container.style.height = "21px";
        img_container.onmouseover = function (){
                this.style.background = "#FFCC00";
        }
        img_container.onmouseout = function (){
                this.style.background = "#FFF";
        }
        img_container.onmousedown = function(){
        }
        img_container.onmouseup = function(){
                document.body.removeChild( this.parentNode );
        }
        img_container.style.background = "#FFF";


        var image = new Image(1,1);//document.createElement('img');
        img_container.appendChild(image);
        image.src = "/close.gif";

        image.width = 21;
        image.height = 21;
        image.style.margin = "0px";
        image.style.padding= "0px";


	
	var bar = document.createElement('div');
	bar.style.display = "block";
	bar.style.background = "#66F";
	bar.style.fontWeight = "bold";
	bar.style.border = "1px solid #33F";
	bar.style.margin = "1px";
	bar.style.marginLeft = "25px";
	bar.style.padding = "2px";
	bar.style.paddingLeft = "5px";
	bar.innerHTML = title;
	bar.style.color = "#CCF";
	bar.style.cursor = "pointer";
	bar.align = "left";
	this.div.appendChild(bar);
	bar.onselectstart = function() {return false;}
	bar.onmousedown = function(){
                ycoord = null;
                xcoord = null;
                intransit = this.parentNode.id;
                document.onmousemove = roam;
                document.onmouseup = function(){
                        document.onmousemove = null;
                        document.onmouseup = null;
                        var obj = document.getElementById(intransit);
                        if (obj){
                                obj.xcoords = null;
                                obj.ycoords = null;
                        }
                        intransit = null;
                }
                if (document.layers){
                        evt.preventDefault;
                }else if(document.all)
                        event.returnValue = false;

                return false;

        }

	var space = document.createElement('div');
	space.align="left";
	space.style.padding="5px";
	this.div.appendChild(space);
	space.style.background = "#999";
	this.append = Append;	
	this.appendButton = AppendButton;
	this.centre = CentreBox;
	this.alignRight = alignRight;
	this.alignLeft = alignLeft;
	this.Width = boxWidth;
	this.notStacked = notstacked;
}


function notstacked(){

	var h_org = parseFloat(this.div.style.left);
	var v_org = parseFloat(this.div.style.top);
	var shift = "vertical";
	
	var overlap = true;
	var stop = false;

        var maplet = document.getElementById('maparea');
	var base = 0;
	var elements = document.body.childNodes.length;
	if (maplet){
		if (maplet.parentNode != document.body){
			base--;
		}
		maplet.popup = true;
	}

	while (overlap == true && stop == false){	
	//
		var i = base;
		while ( i < elements ){

		   var pop;
		   if (i > -1){
			pop = document.body.childNodes[i];
		   }else pop = maplet;
		   
		   if (pop){
			if (pop.popup == true){
			overlap = false;
			
			var left = pop.offsetLeft; 
			if (window.navigator.userAgent.indexOf("MSIE") > -1) left = zb(pop);
			var top = Yb(pop);
			
			if (pop.id != this.div.id){
				if (
				        (
					top <= parseFloat(this.div.offsetTop) &&
					(top + parseFloat(pop.offsetHeight)) > parseFloat(this.div.offsetTop) &&
					left <= parseFloat(this.div.offsetLeft) &&
					(left + parseFloat(pop.offsetWidth)) > parseFloat(this.div.offsetLeft)
					) || (
					 parseFloat(this.div.offsetTop) <= (top + parseFloat(pop.offsetHeight)) &&
					 (parseFloat(this.div.offsetTop) + parseFloat(this.div.offsetHeight)) > top &&
					 (parseFloat(this.div.offsetLeft) + parseFloat(this.div.offsetWidth)) > left &&
					 (parseFloat(this.div.offsetLeft) + parseFloat(this.div.offsetWidth)) <= (left + parseFloat(pop.offsetWidth))
					)
				   
				){
	
					overlap = true;
					
					
					
					
					
					if (( top + parseFloat(pop.offsetHeight) + 2 + parseFloat(this.div.offsetHeight) ) > parseFloat(document.body.clientHeight)){
						//shift = "horiz";
						this.div.style.top = v_org;
						if ( this.div.AlignRight == null && (left + parseFloat(pop.offsetWidth) + 2) > parseFloat(this.div.style.left)){
							this.div.style.left = (left + parseFloat(pop.offsetWidth) + 2) + "px"
						}
						if ( this.div.AlignRight){
							this.div.style.left = (parseFloat(left) - parseFloat(this.div.offsetWidth) - 2) + "px";
						}
						if (
							(this.div.AlignRight == null && parseFloat(this.div.offsetLeft) + parseFloat(this.div.offsetWidth) > parseFloat(document.body.clientWidth)) || this.div.AlignRight && parseFloat(this.div.style.left) < 60 ){
							stop = true;
							this.div.style.left = (h_org + 5) + "px";
							this.div.style.top = (v_org + 5) + "px";
							i = base;
							i--;
							elements = 0;
						}else{
						        i = base;
							i--;
						}
						
					}else{
						this.div.style.top = (top + parseFloat(pop.offsetHeight) + 2) + "px"
					}
					

				}
					
			}

			}
		   }
		   i = i + 1;
		}

	}

}

function boxWidth(width){
	this.div.style.width = width;
}


function CentreBox(){

	this.div.offsetWidth;
	var screenwidth = window.innerWidth;
	if (!screenwidth) screenwidth = document.body.clientWidth;
	var maplet = document.getElementById('maparea');
	if (maplet){

		if (parseFloat(maplet.offsetLeft) > (parseFloat(screenwidth) * 0.5)){

			this.div.style.left = ( (parseFloat(maplet.offsetLeft) * 0.5) - (parseFloat(this.div.offsetWidth) * 0.5)) + "px";
			

		}

	}else{
		this.div.style.left = ((parseFloat(screenwidth) * 0.5) - (parseFloat(this.div.offsetWidth) * 0.5)) + "px";
	}
	return;

}


function alignRight(){

	this.div.AlignRight = 1;
	this.div.style.left = (parseFloat(document.body.offsetWidth) - parseFloat(this.div.offsetWidth)) + "px";

}



function alignLeft(){

	this.div.style.left = parseFloat(document.body.offsetLeft) + "px";
}

function AddtoBody(){
	document.body.appendChild(this.div);	
}

function Append(field){
	if (this.div.lastChild){
		return this.div.lastChild.appendChild(field);
	}
	return this.div.appendChild(field);
}
function AppendButton(button){
	this.div.appendChild(button);
}

function js_pricecheck(){

        var accfield = document.getElementById('accountid');

        if (accfield == null){
                js_alert('Please select a customer account');
                return;
        }
        if (accfield.value == "" || accfield.value == null){
                js_alert('Please select a customer account');
                return;
        }

        var Box = new js_box('Check Previous Prices', 'price_panel');
        Box.AddtoBody();

        var iframe = document.createElement('iframe');
        Box.append( iframe );
        iframe.src = "/ajax/comparison?accountid=" + accfield.value+ "&"+ ref.type +"=" + ref.reference;
        iframe.name = "comp";
        iframe.id = "comp";
	iframe.style.margin = "0px";
	iframe.style.padding = "0px";
        iframe.style.width = "500px";
        iframe.style.height = "500px";

}

function js_accountQuotes(){

        var accfield = document.getElementById('accountid');

        if (accfield == null){
                js_alert('Please select a customer account');
                return;
        }
        if (accfield.value == "" || accfield.value == null){
                js_alert('Please select a customer account');
                return;
        }

        var Box = new js_box('Open Quotations', 'quote_panel');
        Box.AddtoBody();

	var div = document.createElement('div');
	Box.append( div );
	
	var input = document.createElement('input');
	input.type = "text";
	input.id = "open_quotes";
	div.appendChild( input );

        var button = document.createElement('input');
        button.type = "button";
        button.value = "Find Quote";
	button.onclick = function(){

		var iframe = document.getElementById('comp');
		var accfield = document.getElementById('accountid');
		var search = document.getElementById('open_quotes');
		iframe.src = "/ajax/quotecheck?accountid=" + accfield.value+ "&class=" + migrate_class + "&search=" + escape(search.value);
	}
        div.appendChild( button );

        var iframe = document.createElement('iframe');
        Box.append( iframe );
        iframe.src = "/ajax/quotecheck?accountid=" + accfield.value+ "&class=" + migrate_class;
        iframe.name = "comp";
        iframe.id = "comp";
        iframe.style.width = "500px";
        iframe.style.height = "500px";

}

function hidedrivers(){
	var box = document.getElementById('driveraddition');
	document.body.removeChild(box);
}

function js_addDriver(){

        var Box = new js_box('Add/Edit Driver','driveraddition');
        Box.AddtoBody();

        var iframe = document.createElement('iframe');
        Box.append( iframe );
        iframe.src = "/add/drivers?" + ref.getParam();
        iframe.name = "comp";
        iframe.id = "comp";
        iframe.style.width = "500px";
        iframe.style.height = "200px";

}

function getquote(code){

                        var params = ['NO_CACHE','wait__1'];
                        if (document.getElementById('locked')){
                                if (document.getElementById('locked').checked == true) params.push('locked');
                        }
                        params.push('cp_pending__' + code);
                        load(params,[ajax_feedback]);
}

function hideQuotes(newquote){
                        var lock = document.getElementById('locked');
                        if (lock){
                                if (confirm('Lock price at the new amount of ' + newquote)){
                                lock.checked = true;
                                }
                        }
                        var panel = document.getElementById('quote_panel');
                        if (panel){
                               panel.parentNode.removeChild(panel);
                        }
}



function js_accountHistory(){

	var accfield = document.getElementById('accountid');

	if (accfield == null){
		js_alert('Please select a customer account');
		return;
	}
	if (accfield.value == "" || accfield.value == null){
		js_alert('Please select a customer account');
		return;
	}

	var Box = new js_box('Account History', 'history_panel');
	Box.AddtoBody();


	var form = document.createElement('form');
	    form.action = "#";
	Box.append(form);
	
	var text = document.createElement('input');
	    text.type = 'text';
	    text.name = "address";
	    text.id = "address";
	    text.style.height = "17px";
	form.appendChild(text);

	var ac = document.createElement('input');
	    ac.value = accfield.value;
	    ac.name = "accountid";
	    ac.type = "hidden";
	    
	form.appendChild(ac);
	var button = document.createElement('input');
	    button.type = 'submit';
	 form.onsubmit = function (){
		var ife = document.getElementById('comp');
	    	ife.src = "/ajax/history?accountid=" + getValueByName(this, 'accountid') + "&address=" + escape( getValueByName(this, 'address')) + "&class=" + migrate_class;
		return false;
	    }
	    button.value = "Search by Address";
	form.appendChild(button);

	var iframe = document.createElement('iframe');
	Box.append( iframe );
	iframe.src = "/ajax/history?accountid=" + accfield.value+ "&class=" + migrate_class;
	iframe.name = "comp";
	iframe.id = "comp";
	iframe.style.width = "500px";
	iframe.style.height = "500px";

}


function migrate(jobid){

                        var params = ['NO_CACHE','wait__1'];
                        if (ref) params.push(ref.ajax());
                        if (document.getElementById('locked')){
                                if (document.getElementById('locked').checked == true) params.push('locked');
                        }
                        if (document.getElementById('cost_box')){
                        	params.push('show_cost__1');
	                }

			params.push('migrate_jobid__' + jobid);
			migrate_job(params,[ajax_feedback]);
}

function hideHistory(newquote){
        		var lock = document.getElementById('locked');
        		if (lock){
                		if (confirm('Lock price at the new amount of ' + newquote)){
                        	lock.checked = true;
                		}

        		}
			
                        var panel = document.getElementById('history_panel');
                        if (panel){
                               panel.parentNode.removeChild(panel);
                        }
}

function js_notes(id, title, content, width, height){

	var textbox = document.getElementById(id);

	if (textbox == null){
		var Box = new js_box(title);
		Box.AddtoBody();
		
		if (content == null){
			var params = ['NO_CACHE','wait__1','field__' + id];
			if (ref) params.push(ref.ajax());
			getNotes(params,[ajax_feedback]);
		}
		
		if (height != null){
			if (parseFloat(height) <= 30){
				textbox = document.createElement("input");
				textbox.type = "text";
			}else{
				textbox = document.createElement("textarea");
			}
		}else{
		textbox = document.createElement("textarea");
		}
		
		
		textbox.id = id;
		textbox.name = id;
		textbox.onchange = function (){
			save(this);	
		}
		
		if (width != null){
			textbox.style.width = parseFloat(width) + "px";
		}
		if (height != null){
		        textbox.style.height = parseFloat(height) + "px";
		}
		
		Box.append(textbox);
	}
	
	if (content != null) textbox.value = content;
	
}


function js_notice(message, timeout){

	var Box = new js_box("Notice");
	Box.AddtoBody();
	Box.Width("400px");
	var form = document.createElement("form");
	form.action = "#";
	form.style.padding = "0px";
        form.style.margins = "0px";
        form.style.display = "inline";
	form.onsubmit = function(evt){
		document.body.removeChild(this.parentNode.parentNode);
	}
        Box.append(form);
        var warn = document.createElement("div");
        var bang = document.createElement("div");
        bang.innerHTML = "( ! )";
        bang.style.color = "red";
        bang.style.fontSize = "16pt";
        bang.style.display = "inline";
        bang.style.padding = "5px";
        warn.appendChild(bang);

        var mess = document.createElement("div");
        mess.innerHTML = message;
        mess.style.color = "yellow";
        mess.style.fontSize = "10pt";
        mess.style.display = "inline";
        warn.appendChild(mess);
        form.appendChild(warn);


        var button = document.createElement("input");
        button.type = "submit";
        button.value = "OK";
        button.style.width = "80px";
        button.parentid = Box.id;

        form.appendChild(button);

        Box.div.message = message;

	window.setTimeout( 'CloseBox('+ Box.div.id+')',timeout);

}

function CloseBox(id){
	if (id){
		var box = document.getElementById( id );
		if (box){
			document.body.removeChild( box );
		}
	}
}	


function js_alert(message, ok_func, ignore_func){
	
	var Box = new js_box("Alert");

	if (window){
	    if (window.event){
		var elem = window.event.srcElement;
		if (elem){
			if (elem.type == "input") Box.div.callBack = elem;
			if (elem.type == "textarea") Box.div.callBack = elem;
			if (elem.type == "select") Box.div.callBack = elem;
		}
	     }
	}
	Box.AddtoBody();
	Box.Width("400px");
	var form = document.createElement("form");
	form.action = "#";
	form.style.padding = "0px";
	form.style.margins = "0px";
	form.style.display = "inline";
	form.onsubmit = function(evt){
		eval( this.parentNode.ok_func );
		if (this.parentNode.parentNode.callBack) this.parentNode.parentNode.callBack.focus();
		document.body.removeChild(this.parentNode.parentNode);
	

                	if (evt){
                		evt.returnValue = false;
                	        if (evt.stopPropagation) {
              	                	evt.stopPropagation();
                                	evt.preventDefault();
               	         	}
                	}else{
                	        if (window.event){
                	                window.event.keyCode = 0;
                	                window.event.returnValue = false;
                	        }
                	}
	}
	Box.append(form);
	var warn = document.createElement("div");
	var bang = document.createElement("div");
	bang.innerHTML = "( ! )";
	bang.style.color = "red";
	bang.style.fontSize = "16pt";
	bang.style.display = "inline";
	bang.style.padding = "5px";
	warn.appendChild(bang);

	var mess = document.createElement("div");
	mess.innerHTML = message;
	mess.style.color = "yellow";
	mess.style.fontSize = "10pt";	
	mess.style.display = "inline";
	warn.appendChild(mess);
	form.appendChild(warn);
	

	var button = document.createElement("input");
	button.type = "submit";
	button.value = "OK";
	button.style.width = "80px";
	button.parentid = Box.id;
	
	form.appendChild(button);

	Box.div.message = message;
	Box.div.ok_func = ok_func;

	
	if (ignore_func){

		Box.div.ignore_func = ignore_func;
		var button1 = document.createElement("input");
		button1.type = "button";
		button1.value = "Ignore";
		button1.style.width = "80px";
		Box.appendButton(button);
		button1.onclick = function(){

			eval( this.parentNode.ignore_func );		
			if (this.parentNode.callBack) this.parentNode.callBack.focus();
			document.body.removeChild(this);
		
		};


	}
	button.id = Math.random();
	Box.notStacked();
	window.setTimeout( 'focus_but('+button.id+')',100);

}

function focus_but(id){
	var obj = document.getElementById(id);
	if (obj) obj.focus();
}


function js_pickupdate(value){
        js_pickuptime(value);
}
function js_deliverydate(value){
        js_deliverytime(value);
}
	

function js_pickuptime(value){
	js_createdatefield('pickup',value, 'Pickup Date');
}
function js_deliverytime(value){
        js_createdatefield('delivery',value, 'Delivery Date');
}

function js_createdatefield( id, value, title ){

    	var field = document.getElementById(id + "date");
	if (field == null){
		field = js_field(id + "date",'',title);
	}
	//value.replace("T"," ")
	var values = value.split(/[ T]/);
	field.onkeydown = null;	
	js_prepDateField(field, values[0]);
	field.onsave = function(){
		var source = getFormElement(this.form,this.source)
		updatefield(source);
	}
	field.ondblclick = function(){
		js_popupDate( this );
	}
	
	field.onblur = function(){ if (this.changed==true){ this.onsave(); this.changed = false;} if (this.fade){this.fade()}}
	
	var time = document.getElementById(id + "time");
	if (time == null){
		time = document.createElement('input');
		time.type = "text";
		time.id = id + "time";
		if (field.parentNode) field.parentNode.appendChild( time );
	}
	time.className = "TEXTBOX";
	time.style.width = "50px";
	time.style.textAlign = "right";
	time.style.marginLeft = "5px";
	time.style.paddingRight = "5px";
	time.name = id + "time";
	if (values.length > 1){
		var c = values[1].split(':');
		time.value = c[0] + ":" + c[1];
	}
        time.onkeydown = function (evt){ js_enTer(evt,this) }
        time.onchange = function() {
                             var Allnumbers = new RegExp("^\\d\\d\\d\\d$");
			     var mymatch = Allnumbers.exec( this.value );
			     if (mymatch){
				this.value = mymatch[0].substring(0,2) + ":" + mymatch[0].substring(2,4)
				save(this);
				JumpToNext(this);
				return;
			     }
			     var Threenumbers = new RegExp("^\\d\\d\\d$");
			     var mymatch = Threenumbers.exec( this.value );
                             if (mymatch){
                                this.value = mymatch[0].substring(0,1) + ":" + mymatch[0].substring(1,3)
                                save(this);
                                JumpToNext(this);
				return;
                             } 
			     var Splitnumbers = new RegExp("^\\d+\\D+\\d\\d$");
			     var mymatch = Splitnumbers.exec( this.value );
			     if (mymatch){
				this.value = mymatch[0].replace(/\D+/,":");
			        save(this);
				JumpToNext(this);
				return;
			     }
			     js_alert('Please Supply a valid time format (hh:mm)');
			};	
	time.reset = function(){
		if (defaults[screenName]){
			if (defaults[screenName][this.id]){
				this.value = defaults[screenName][this.id];
				this.saved = null;
				return;
			}
		}
		var newD = new Date();
		this.value = newD.getHours() + ":" + MinuteFormat( newD.getMinutes());
	        this.saved = null;
	}

	return;
}

function MinuteFormat( min ){
	if (parseFloat(min) < 10) return "0" + min;
	return min;
}

function js_prepDateField( field, value){

	if (!field.parentNode ) return;
	var source;
	if (field.source)
	source = getFormElement(field.form,field.source)
	field.setAttribute('autocomplete', 'off');
	if (source == null){

		source = getFormElement(field.form,field.name + "_value");
		if (source) field.source = field.name + "_value"
	}

	if (source == null){
		source = document.createElement('input');
		source.type = "hidden";
		if (field.source == null){
			field.source = field.name + "_value";
			source.name = field.name + "_value";
			source.id = source.name;
		}
		else{
			source.name = field.source;
		}
		field.parentNode.appendChild(source);
	}
	
	source.reset = function(){
		var newD = new Date();
		this.value = milisecondsMysql(newD.getTime())
		this.saved = null;
	}
	field.reset = function(){

		var source = getFormElement(this.form,this.source);
		source.reset();
		var date = mysqlDate( source.value );
		this.value =  milisecondsReadable( date.getTime() );
		this.saved = null;
	}
	
	
	if (value != null) source.value = value;
	if (source.value){
		var date = mysqlDate( source.value );
                    field.value =  milisecondsReadable( date.getTime() );	
	}
	field.changed = false;
	if (field.onkeydown == null){

        	if (field.getAttribute('feedback')){
                	field.onsave = function(){
                	        var source = getFormElement(this.form,this.source)
                	        eval( field.getAttribute('feedback') + "('" + source.name + "','" + source.value + "');" );
                	}
        	}


		field.onkeydown = function (evt){

  			var charCode =
      			document.layers ? evt.which :
          		document.all ? window.event.keyCode :
	      		document.getElementById ? evt.keyCode : 0;
	      
        		if (charCode == 38 ||charCode ==45  || charCode == 109 || charCode == 8){
                		//back a day
                                var source = getFormElement(this.form,this.source)
                                var date = mysqlDate( source.value );
                                date = subDay(date);
                                source.value = milisecondsMysql( date.getTime() );
                                this.value =  milisecondsReadable( date.getTime() );
			//	js_popupDate( this );
        			this.changed = true;
				return false;
			} else   if (charCode ==40 || charCode ==43 ||charCode ==107 ){
                		//forward a day
				var source = getFormElement(this.form,this.source)
				var date = mysqlDate( source.value );
				date = addDay(date);
				source.value = milisecondsMysql( date.getTime() );
				this.value =  milisecondsReadable( date.getTime() );
			//	js_popupDate( this );
				this.changed = true;
				return false;
        		}else if (charCode > 112 && charCode < 119){
        			return; //nop
        		}else if ( charCode ==9 ||charCode ==13 ){
        			if (field.edit){
            				if (document.getElementById(this.edit)){
			                document.body.removeChild( document.getElementById(this.edit) );
            				}
			        }
				if (this.onsave){ this.onsave(); }

			}else{
				if (this.ondblclick) this.ondblclick();
			}
			this.changed = false;
			return false;

		}
        field.ondblclick = function(){
                js_popupDate( this );
        }


	}
	
	return source;

}

function js_popupDate( field ){

	var Box;

	var source = js_prepDateField( field );

	if (field.edit){
	    if (document.getElementById(field.edit)){
		document.body.removeChild( document.getElementById(field.edit) );
	    }
	}

	var Box = new js_box("Select Date", field.edit);
	Box.AddtoBody();

	var left = field.offsetLeft;
        //if (window.navigator.userAgent.indexOf("MSIE") > -1)
	left = zb(field);
	var top = Yb(field);

	Box.div.style.left = left + "px";
	Box.div.style.top = top + "px";
	field.edit = Box.div.id;

	var table = document.createElement("table");
	var tbody = document.createElement("tbody");

	//Box.append(table);
	table.appendChild(tbody);
	var date = mysqlDate( source.value );
	Box.div.date = date;
	var back = document.createElement('input');
	back.type = "button";
	back.value = "<<";
	back.onclick = function(){
		subMonth(this.parentNode.parentNode.date);
		while (tbody.rows.length > 0){
                        tbody.deleteRow(-1);
                }
		drawdatematrix(tbody, this.parentNode.parentNode.date, source, field);
	}
	Box.append(back);
	var monthfield = document.createElement('select');
	monthfield.style.fontSize = "9pt";
	monthfield.style.width = "49px";
	Box.append(monthfield);
	var yearfield = document.createElement('select');
	yearfield.style.fontSize = "9pt";
	yearfield.style.width = "44px";
	Box.append(yearfield);
        
	var forward = document.createElement('input');
        forward.type = "button";
        forward.value = ">>";
        forward.onclick = function(){
                addMonth(this.parentNode.parentNode.date);
                while (tbody.rows.length > 0){
                        tbody.deleteRow(-1);
                }
                drawdatematrix(tbody, this.parentNode.parentNode.date, source, field);
        }	
	Box.append(forward);
	Box.append(table);
	for (var i = 0; i < Rmonths.length; i++){
		var opt = document.createElement('option');
		opt.value = i;
		opt.text = Rmonths[i];
		if (parseFloat(date.getMonth()) == i) opt.selected = true;
		monthfield.options[ monthfield.options.length ] = opt;
		//monthfield.appendChild( opt );
	}
	monthfield.onchange = function(){
		date.setMonth( this.selectedIndex );
		date.setYear( yearfield.options[ yearfield.selectedIndex ].value  );
		while (tbody.rows.length > 0){
			tbody.deleteRow(-1);
		}
		drawdatematrix(tbody, date, source, field);
	}
        monthfield.value = parseFloat(date.getMonth()); 
	monthfield.id = "month";
	var year = parseFloat(date.getYear());
	
	if (year < 1900) year = year + 1900;
	
	for (var i = year - 5; i < year + 2; i++){
                var opt = document.createElement('option');
                opt.value = i;
                opt.text = opt.value.substring(2,4);
                if (i == year) opt.selected = true;
		yearfield.options[ yearfield.options.length ] = opt;
                //yearfield.appendChild( opt );
        }
        yearfield.value = year; 
	yearfield.id = "year";
	yearfield.onchange = function(){
		date.setMonth( monthfield.selectedIndex );
		date.setYear( this.options[ this.selectedIndex ].value  );
                while (tbody.rows.length > 0){
                        tbody.deleteRow(-1);
                }
                drawdatematrix(tbody, date, source, field);
        }


	drawdatematrix(tbody, date, source, field);

}

function drawdatematrix( tbody, start, source, field){

	var date = new Date();
	date.setTime(start.getTime());

	var thisday = date.getDate();
	date.setDate(1);
	
	var mymonth = date.getMonth();
        var year = parseFloat(date.getYear());
	if (year < 1900) year = year + 1900;

	for (var i = 0; i < tbody.parentNode.parentNode.childNodes.length; i++){
		if (tbody.parentNode.parentNode.childNodes[i].tagName == "SELECT"){
			if (tbody.parentNode.parentNode.childNodes[i].id == "year"){
				var year = parseFloat(date.getYear());
        			if (year < 1900) year = year + 1900;
				tbody.parentNode.parentNode.childNodes[i].value = year;
			}
			if (tbody.parentNode.parentNode.childNodes[i].id == "month"){
				tbody.parentNode.parentNode.childNodes[i].value = parseFloat(date.getMonth());
			}
		}
	}

	var row = tbody.insertRow(-1);
	for (var i = 0; i < date.getDay(); i++){
		var cell = row.insertCell(-1);
		cell.innerHTML = "-";
	}

	while (date.getMonth() == mymonth){

		for (var i = date.getDay(); i < 7; i++){	
			
			var cell = row.insertCell(-1);

			if (date.getMonth() == mymonth) {
				cell.innerHTML = date.getDate();
				if (date.getDate() == thisday){
					cell.style.background = "red";
				}else{
					cell.style.background = "white";
					cell.style.color = "#333";
				}
				cell.style.cursor = "pointer";
				cell.miliseconds = date.getTime();
				cell.title = milisecondsReadable(cell.miliseconds);
				cell.onclick = function(){
					source.value = milisecondsMysql(this.miliseconds);
					field.value = milisecondsReadable(this.miliseconds);
					if (source.onsave != null){ source.onsave(); }
					if (field.onsave != null){ field.onsave(); }				
					this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.removeChild( this.parentNode.parentNode.parentNode.parentNode.parentNode );
					
				}
			
			}else{
				cell.innerHTML = "-";
			}
			date = addDay(date);
		}

		if (date.getMonth() == mymonth) row = tbody.insertRow(-1)

	}
	date.setDate(thisday);

}
function addMonth( date ){
        var secs = date.getTime();
        var mon = date.getMonth();
	while( mon == date.getMonth()){
		secs += 86400000;
        	date.setTime(secs);
	}
        return date;
}
function subMonth( date ){
        var secs = date.getTime();
        var mon = date.getMonth();
        while( mon == date.getMonth()){
                secs = secs -86400000;
                date.setTime(secs);
        }
        return date;
}


function addDay( date ){
	var secs = date.getTime();
       	    secs += 86400000; //add a day
            date.setTime(secs);
	return date;
}

function subDay( date ){
        var secs = date.getTime();
            secs -= 86400000; //add a day
            date.setTime(secs);
        return date;
}


function milisecondsReadable( ms ){
        var date = new Date();
        date.setTime(ms);
        var year = date.getYear();
        if (parseFloat(year) < 1900) year = parseFloat(year) + 1900;

	return wdays[date.getDay()] + " " + date.getDate() + ", " + Rmonths[date.getMonth()] + " " + year;

}
function milisecondsMysql( ms ){

	var date = new Date();
	date.setTime(ms);
	var year = date.getYear();
	if (parseFloat(year) < 1900) year = parseFloat(year) + 1900;

	var mon = (date.getMonth() + 1);	

	var printdate = year + "-";
	if (mon < 10) printdate += "0";
	printdate += mon + "-";
	if (date.getDate() < 10) printdate += "0";
	
	return printdate + date.getDate();

}

function mysqlDate( value ){

        var date = new Date();

        if (value){
           if (value.match(/\d+\-\d+/)){
           var param = value.split(/\D/);
           if (param[2].match(/^0/)) param[2] = param[2].substr(1,1);
           if (param[1].match(/^0/)) param[1] = param[1].substr(1,1);
           var m = parseInt(param[1]) - 1;
           date = new Date( parseFloat(param[0]), m,parseFloat(param[2]));
           }
        }

	return date;

}


function js_edit_costing(costingid, reference, sell, cost, cata, driverid, agentid, oninvoice, jobId, agentOnly){

        var title = "Edit Costing";
        if (!costingid) title = "Add Costing";

        var Box = new js_box(title);

	Box.AddtoBody();

	var form = document.createElement("form");
                form.style.padding = "0";
                form.style.display = "inline";
		form.id = Math.random(); 
                Box.append(form);
		form.name = form.id;
                Box.Width("275px");
                form.action = "#";

                form.onsubmit = function(){
			var params = ['NO_CACHE','wait__1'];
			if (ref) params.push(ref.ajax());
			if (document.getElementById('locked')){
				if (document.getElementById('locked').checked == true) params.push('locked');
			}
			for (var i = 0; i < this.elements.length; i++){
				if (this.elements[i].type == "checkbox"){
					if (this.elements[i].checked) params.push(this.elements[i].id + "__" + this.elements[i].value);
				}else{
                                	if (this.elements[i].value != "") params.push(this.elements[i].id + "__" + this.elements[i].value);
				}
			}
			save_costing(params,[ajax_feedback]);
			document.body.removeChild(this.parentNode.parentNode);
			return false;
		}
		
		if (parseFloat(costingid) > 0){
			var costing = document.createElement("input");
			costing.type = "hidden";
			costing.value = costingid;
			costing.id = "costingid";
			form.appendChild(costing);
		}
                if (jobId){
                         var costing = document.createElement("input");
                         costing.type = "hidden";
                         costing.value = jobId;
                         costing.id = "jobid";
                         form.appendChild(costing);
                }
		js_costing_field(form,'Catagory','catagory', cata, '', 'ref');
                js_costing_field(form,'Reference','ref', reference, '', 'cost');
 		js_costing_field(form,'Cost','cost', cost, '', 'sell');
                if (!agentOnly){
                        js_costing_field(form,'Sell','sell', sell, '', 'driver');
                        js_costing_field(form,'Driver','driver', driverid, '', 'agent');
                        js_costing_field(form,'Agent','agent', agentid, '', 'oninvoice');
                        js_costing_checkbox(form,'Show On Invoice','oninvoice', oninvoice, '', 'create_addr');
                }else{
                        var costing = document.createElement("input");
                        costing.type = "hidden";
                        costing.value = agentid;
                        costing.id = "agentid";
                        form.appendChild(costing);
                }
	
	
	
	var button = document.createElement("input");
	    button.type = "button";
        
	form.appendChild(button);
               button.onclick = function(){
                       this.parentNode.onsubmit();
               }
		button.id = "create_addr";
                button.value = "Save Costing";
                button.style.marginTop = "5px";	
		button.style.width = "130px";

	if (parseFloat(costingid) < 10) button.disabled = true;

	var cancel = document.createElement("input");
	    cancel.type = "button";
	
	form.appendChild(cancel);

		cancel.onclick = function (){
		document.body.removeChild(this.parentNode.parentNode.parentNode);
		}
                cancel.type = "button";
                cancel.value = "Cancel";
                cancel.style.display = "inline";
                cancel.style.width = "130px";
                cancel.style.marginTop = "5px";
                cancel.onclick = function (){
                        document.body.removeChild(this.parentNode.parentNode.parentNode);
           	}

	if (parseFloat(costingid) >= 10){

		var Delete = document.createElement("input");
		Delete.type = "button";
		form.appendChild(Delete);
	
		Delete.value = "Remove Costing";
                Delete.style.display = "inline";
                Delete.style.width = "261px";
                Delete.style.marginTop = "5px";
                Delete.onclick = function (){
			params = ['costingid__' + this.parentNode.costingid.value ,'NO_CACHE','wait__1'];
			if (ref) params.push(ref.ajax());
			if (document.getElementById('locked')){
				if (document.getElementById('locked').checked == true) params.push('locked');	
                        }
			document.body.removeChild(this.parentNode.parentNode.parentNode);
			delete_costing(params,[ajax_feedback]);

                }
	}
	

}

function js_costing_checkbox(Box, label_text, name, value, format, goto){

	var entry = document.createElement("div");
	entry.style.height="20px";
	entry.style.width="260px";
	Box.appendChild(entry);

	var label = document.createElement("span");
	label.innerHTML = label_text;
	label.id = "Label_" + name + "_" + Box.id;
        entry.appendChild(label);

	var field = document.createElement("input");
	field.type = "checkbox";
	field.name = name;
	
	field.value = "y";
	field.id = name;
	if (value == "y"){ 
	field.defaultChecked = true;
	}
        field.onkeydown = function (evt){

                     if (evt){
                                js_enTer(evt,this);
                                return;
                        }

                        js_enTer(window.event,this)
         }
        field.style.position= "absolute";
        //field.style.margin = "0px";
        field.style.height="16px";
	//field.style.padding = "0px";
	field.style.left = "120px";
        field.gotoId = goto;
        field.setAttribute("autocomplete", "off");
        entry.appendChild(field);
}
function js_costing_field(Box, label_text, name, value, format, goto){

	var entry = document.createElement("div");
        entry.style.height="20px";
        entry.style.width="260px";
        
	Box.appendChild(entry);

	var label = document.createElement("span");
        label.innerHTML = label_text;
	label.id = "Label_" + name + "_" + Box.id;
        entry.appendChild(label);

        var field = document.createElement("input");
        field.type = "text";
        field.name = name;
	field.id = name;
	if (value != null) field.value = value;

        field.onkeydown = function (evt){

                     if (evt){
                                js_enTer(evt,this);
                                return;
                        }

                        js_enTer(window.event,this)
         }


	
	if (name == "driver"){
		if (CNav){
		field = CNav.driverDropDown(Box, 'driver','driverid', value);
		}
	}else if (name == "catagory"){
		if (CNav){
		field = CNav.CostDropDown(Box, 'catagory','cata', value);
		}
	}else if (name == "agent"){
		if (CNav){
		field = CNav.agentDropDown(Box, 'agent','agentid', value);
		}
	}else if (name == "cost" || name =="sell"){
		field.onchange = function (){
                	if (isNaN(this.value)){
				this.style.borderColor = "red";
                                this.style.background = "#F99";
                                js_warnselect(this, 'Please select a valid value');
                	}else{
				this.style.borderColor = "#AAA";
                                this.style.background = "#FFF";
				this.value = Currency(this.value);
			}

		}
	}
	
	field.style.position= "absolute";
        field.style.fontSize="8pt";
        field.style.height = "16px";
        field.style.margin = "0px";
        field.style.left = "120px";
        field.style.width = "140px";
        field.gotoId = goto;
        field.setAttribute("autocomplete", "off");
	entry.appendChild(field);
	
}

function js_show_costing (){

                        var params = ['NO_CACHE'];
                        if (ref) params.push(ref.ajax());
                        show_costing(params,[ajax_feedback]);

}

function flush_costing (){

        var cost_box = document.getElementById("cost_box");
        if (!cost_box){
                var Box = new js_box("Costings");
                Box.AddtoBody();
                var table = document.createElement("table");
                Box.append(table);
                cost_box = document.createElement("tbody");
                cost_box.id = "cost_box";
                table.appendChild(cost_box)
        }
	while (cost_box.rows.length > 0){
		cost_box.deleteRow(0);
	}
	var row = cost_box.insertRow(0);
	row.filler = 1;

        var Reference = document.createElement("td");
        Reference.style.width = "390px";
        Reference.innerHTML = "No Extra Costing Included";
        row.appendChild(Reference);
}

function print_costing (costingid, reference, sell, cost, cata, driverid, agentid, oninvoice){

	//(costingid, ref, sell, cost, cata, driverid, agentid, oninvoice)

	var cost_box = document.getElementById("cost_box");
	if (!cost_box){
		var Box = new js_box("Costings");
		Box.AddtoBody();
		var table = document.createElement("table");
		Box.append(table);
		cost_box = document.createElement("tbody");
		cost_box.id = "cost_box";
		table.appendChild(cost_box)
	}

	
	
	for (var i = 0; i < cost_box.rows.length; i++){

		if (cost_box.rows[i].costingid == costingid || cost_box.rows[i].filler == 1){
			cost_box.deleteRow(i);
		}
	}
	
	var row = cost_box.insertRow(0);
	row.costingid = costingid;
	row.cost = cost;
	row.sell = sell;
	row.cata = cata;
	row.driverid = driverid;
	row.agentid = agentid;
	row.oninvoice = oninvoice;
	row.reference = reference;
	row.style.background = "#666";
	row.onclick = function (){
		js_edit_costing(this.costingid, this.reference, this.sell, this.cost, this.cata, this.driverid, this.agentid, this.oninvoice);
	}
	var Catagory = document.createElement("td");
	Catagory.innerHTML = CNav.GetCostCataName( cata );
	Catagory.style.width = "140px";
	row.appendChild(Catagory);

        var Reference = document.createElement("td");
	Reference.style.width = "140px";
	Reference.innerHTML = reference;
        row.appendChild(Reference);


        var Cost = document.createElement("td");
        Cost.style.width = "40px";
	Cost.innerHTML = Currency(cost);
        row.appendChild(Cost);

        var Sell = document.createElement("td");
	Sell.style.width = "40px";
	Sell.innerHTML = Currency(sell);
        row.appendChild(Sell);


}

function js_cancelEDI( agentname, agentid, agentref, cjobid ){

	var Box = new js_box("Cancel EDI Job Booking");
        Box.AddtoBody();


        var warn = document.createElement("div");
        warn.style.verticalAlign = "top";
	var bang = document.createElement("div");
        bang.style.verticalAlign = "top";
	bang.innerHTML = "( ? )";
        
	bang.style.color = "yellow";
        bang.style.fontSize = "16pt";
        bang.style.display = "inline";
        bang.style.padding = "5px";
        warn.appendChild(bang);
	
	var mess = document.createElement("div");
        mess.innerHTML = "Before changing the Agent on this job you need to cancel the booking (" + agentref + ") made with " + agentname + ". When you are finished editing use <b>create job</b> button to book the job again with the agent. This does <b>not</b> cancel the job on this system.";
        mess.style.color = "white";
        mess.style.fontSize = "10pt";
	mess.style.width = "400px"
        mess.style.display = "inline";
        warn.appendChild(mess);

	Box.append(warn);
	var form = document.createElement("form");
	Box.appendButton(form);

	var field = document.createElement("input");
	field.type = "button";
	field.value = "Ignore and Close";
	form.appendChild(field);
	field.onclick = function (){
		this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
		return false;
	}
	var okbut = document.createElement("input");
	okbut.type = "button";
	okbut.value = "Continue and Cancel";
	form.appendChild(okbut);

	okbut.onclick = function (){
		this.disabled = true;
	        getXML('/edi/cancel?xml=1&jobid=' + cjobid + '&agentid=' +agentid);
	        var doc=xmlDoc.documentElement;
		scan(doc,'update');

		this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
		return false;
	}


}

function js_question( question, fieldname, func,value ){

	var Box = new js_box("Question");
	Box.AddtoBody();

	
	var warn = document.createElement("div");
	var bang = document.createElement("div");
	bang.innerHTML = "( ? )";
	bang.style.color = "yellow";
	bang.style.fontSize = "16pt";
	bang.style.display = "inline";
	bang.style.padding = "5px";
	warn.appendChild(bang);

	var mess = document.createElement("div");
	mess.innerHTML = question;
	mess.style.color = "white";
	mess.style.fontSize = "12pt";	
	mess.style.display = "inline";
	warn.appendChild(mess);
	Box.append(warn);
		
	var form = document.createElement("form");
	Box.appendButton(form);
	
	var field = document.createElement("input");
	field.type = "text";
	field.name = fieldname;
	if (value) field.value = value;
	form.appendChild(field);
        form.onsubmit = function(){
                js_question_feedback(this);
                this.parentNode.parentNode.removeChild(this.parentNode);
                return false;
        }

	var button = document.createElement("input");
	button.type = "button";
	button.value = "OK";
	button.style.width = "80px";
	form.appendChild(button);
	if (func == null){
		func = "js_question_feedback(this)";
	}
	
	form.func = func;
	button.onclick = function(){

		js_question_feedback(this.form);		
		this.form.parentNode.parentNode.removeChild(this.form.parentNode);

	};


	button.focus();	


}



function js_question_feedback ( form ){

	if (form.func == "save"){
		save( form.childNodes[0]);
	}else if( form.func != null){
		eval( form.func + "('" + form.childNodes[0].name + "','" + form.childNodes[0].value + "')");
	}
}

function js_option_question( question, fieldname, options, func ){

        var Box = new js_box("Question");
        Box.AddtoBody();
	Box.Width("450px");
        
        var warn = document.createElement("div");
        var bang = document.createElement("div");
        bang.innerHTML = "( ? )";
        bang.style.color = "yellow";
        bang.style.fontSize = "16pt";
        bang.style.display = "inline";
	bang.style.width = "460px";
        bang.style.padding = "5px";
        warn.appendChild(bang);

        var mess = document.createElement("div");
        mess.innerHTML = question;
        mess.style.color = "white";
        mess.style.fontSize = "10pt";   
        mess.style.width = "450px";
	mess.style.display = "inline";
        warn.appendChild(mess);
        Box.append(warn);
                
	
        var form = document.createElement("form");
        Box.appendButton(form);
        
	for (var i = 0; i < options.length; i++){

		 var button = document.createElement("input");
		 button.type = "button";
		 button.style.width = "80px";
		 button.value = options[i];
		 button.onclick = function(){
			js_question_options_feedback(this);
		 };
		 form.appendChild(button);
		 
	}
        form.func = func;
	form.onsubmit = function(){
                alert('Please select an option');
		return false;
        }

        form.func = func;

        button.focus(); 


}

function js_question_options_feedback(options){

	if (options.form.func){
		 eval( options.form.func + "('" + options.value + "')");
	}
	options.form.parentNode.parentNode.removeChild(options.form.parentNode);

}


function update(field){
	//var field = this.parentNode.childNodes[1].childNodes[1];
	alert("saving: " + field.name + "=" + field.value );
}



function js_enTer(evt,field) {

  var charCode =
    document.layers ? evt.which :
    document.all ? window.event.keyCode :
    document.getElementById ? evt.keyCode : 0;
   
    field.nextchar = null;

    if ((charCode == 13 || charCode == 9)){
               
                if (evt){
                        evt.returnValue = false;
                        if (evt.stopPropagation) {
                                evt.stopPropagation();
                                evt.preventDefault();
                        }
                }else{
                        if (event){
                                event.keyCode = 0;
                                event.returnValue = false;
                        }
                }


 
		if (field.selected != null && field.listing.length > 0){
		   
		   	if (field.listing[field.selected]){
                        	field.value = field.listing[field.selected];
				if (field) field.blur();
                   	}

		}else if (field.listing != null){
			if (field.onchange) field.onchange();
		}

		var obj = document.getElementById(field.id + '_drop_down');
		if (obj){
			obj.parentNode.removeChild(obj);
		}
		window.status = "next jump " + field.gotoId;

		if (field.gotoinform){
		
			if (field.form){
				var jump = getFormElement(field.form, field.gotoinform);
				if (jump){
					jump.focus();
					return 1;
				}
			}

		}

		if (field.gotoId){
			 if (field.form){
                                var jump = getFormElement(field.form, field.gotoId);
                                if (jump){
					jump.focus();
                                        return 1;
                                }
                        }
			var jump = document.getElementById( field.gotoId )
			if (jump){	
				jump.focus();
				return 1;
			}
		}
		window.status = "next jump " + field.gotoId;
		if (JumpToNext(field)) return 1;

		if (charCode == 13){
			field.blur();
		}
		
                return 1;

        }


	field.style.borderColor = "#AAA";
	field.style.background = "#FFF"; 

        var tbody = document.getElementById(field.id + '_drop_list');
	if (!tbody){
	 if (charCode ==40 || charCode ==43 ||charCode ==107 || charCode == 8 || charCode == 46){
		js_dropdown(field);
	 }
	 return 1;
	}
	
        if (field.listing.length > 0){
	   if (charCode ==40 || charCode ==43 ||charCode ==107 ){

                    if (field.selected != null){
                            var cell =  document.getElementById(field.id + '_option_' + field.selected);
                            if (cell) cell.style.backgroundColor = "#EEEEEE";
                    }
		if (field.selected == null) field.selected = 0;		
                field.selected = parseInt(field.selected) - 1;

                if (field.selected < 0) field.selected = tbody.rows.length - 1;
                if (document.getElementById(field.id + '_option_' + field.selected)){
                        field.value = field.listing[field.selected];
                        document.getElementById(field.id + '_option_' + field.selected).style.backgroundColor = "#7BB6FF";
                }
                return 1;

           }else if (charCode == 38 ||charCode ==45  || charCode == 109){

                    if (field.selected != null){
                            var cell =  document.getElementById(field.id + '_option_' + field.selected);
                            if (cell) cell.style.backgroundColor = "#EEEEEE";
                    }
		if (field.selected == null) field.selected = 0;	

                field.selected = parseInt(field.selected) + 1;
		
		
                if (field.selected > tbody.rows.length - 1) field.selected = 0;
                
		if (document.getElementById(field.id + '_option_' + field.selected)){
                        field.value = field.listing[field.selected];
                        document.getElementById(field.id + '_option_' + field.selected).style.backgroundColor = "#7BB6FF";
                }
                return 1;
	   }
        }
        if (charCode > 47 && charCode < 58){
                field.nextchar = parseInt(charCode) - 48;
        }else if (charCode > 95 && charCode < 106){
                field.nextchar = parseInt(charCode) - 96;
        }else if (charCode == 32){
                field.nextchar = ' ';
        }else if (charCode > 57 && charCode < 96)
                field.nextchar =String.fromCharCode(charCode);

	field.chop = false;
	field.snap = false;
	if (charCode == 8 || charCode == 46){


		var sel, rng, r2, i=-1, characters = 1;

		if(typeof field.selectionStart=="number") {

			i=field.selectionStart;
		
		} else if(document.selection && field.createTextRange) {

			sel=document.selection;
		
			if(sel){
				r2=sel.createRange();
				rng=field.createTextRange();
				rng.setEndPoint("EndToStart", r2);
				i=rng.text.length;
				characters = r2.text.length;
			}
		}

		window.status = "cursor at pos: " + i + " chars: " + characters;
		if (characters > 1){		

			field.snap = true;
			field.startsection = i;
			field.endsection  = i + characters;

		}else if (charCode ==8){
			if ( i == field.value.length){
				field.chop = true;
			}else if (i > 0){
				field.snap = true;
				field.endsection = i;
				field.startsection = (i - 1);
			}
		}else if (charCode == 46){
			if ( (i + 1) == field.value.length){
				field.chop = true;
			}else if ( (i - 1) < field.value.length){
				field.snap = true;
				field.endsection = (i + 1);
				field.startsection = i;				
			}

		}
		field.nextchar = null;
	}
	
       // if (field.last){
       //         if (field.value.length <= field.last) field.last = null;
       // }

        if (charCode > 111 && charCode < 124){
                //dont fire anything on the drop down if F key is pressed
                return 1;
        }

        return;
}

function js_warnselect(field, warning){

	var Box = document.createElement("div");
	Box.id = Math.random();
	Box.innerHTML = warning;
        Box.style.color = "black";
        Box.style.textAlign = "left";
        Box.style.position = "absolute";
        Box.style.border = "1px solid black";
        Box.style.opacity = ".85";
        Box.style.filter = "alpha(opacity=85)";
        Box.style.width = "150px";
        Box.style.padding = "3px";
        Box.style.backgroundColor = "#FFD";
	document.body.appendChild(Box);
	pt(field, Box);
	if (parseFloat(Box.offsetTop) < parseFloat(Box.offsetHeight)){
		Box.style.top = "50px";
	}	
	window.setTimeout("js_hide('" + Box.id +"');",3000);
}

function js_dropdown(field){

	var listing = field.listing;
	if (listing == null) return;	
	var obj = document.getElementById(field.id + '_drop_down');

	if (field.listing == null && obj){
		alert('no list');
		obj.parentNode.removeChild(obj);
		return;
	}	

	var tbody;
	var table;
	if (!obj){
		obj = document.createElement("div");
		obj.id = field.id + '_drop_down';
		obj.style.position = "absolute";
		obj.style.background = "#CCC";
		obj.style.color = "#222";
		obj.style.margin = "0px";
		obj.style.padding = "0px";
		document.body.appendChild(obj);

		table = document.createElement('table');
		table.id = field.id + '_drop_down_table';
		table.width = field.offsetWidth;
		table.style.width = field.offsetWidth + "px";
		table.style.margin = "0px";
		table.style.padding = "0px";
		
		obj.appendChild(table);

		tbody = document.createElement('tbody');
		tbody.id = field.id + '_drop_list';
		table.appendChild(tbody);

                obj.style.width = field.style.width;
                da(field,obj);		

	}else{

		tbody = document.getElementById( field.id + '_drop_list');
	
          	while (tbody.rows.length > 0){
                	tbody.deleteRow(0);
                }
	
	}
	
	var extras = new Array();
	if (field.extras){ extras = field.extras; }
	var pre = false;

        if (field.name == "company" && field.form){
        ///special rule for company search field///
        // DO NOT AUTOSELECT IF POSTCODE DOES NOT MATCH AN EXISTING ENTRY

                var postcode = getFormElement(field.form, 'postcode');
                if (postcode){
                        if (postcode.value.length > 0){
                                pre = true;
                        }
                }
        ///////////////////////////////////////////
        }


	var i = 0;
    	while ( i < listing.length){

        	var row = tbody.insertRow(0);
        	var td1 = document.createElement("TD");
        	    td1.innerHTML = listing[i];
		    td1.parentId = field.id;
		    td1.style.color = "#222";
		    td1.style.margin = "1px";
		    td1.style.padding = "1px";
		if (listing[i] == field.value && pre == false){			
			field.selected = i;
			td1.style.backgroundColor = "#7BB6FF";
			flag = true;		
			pre = true;
		}else{
			td1.style.backgroundColor = "#EEEEEE";
		}
		
		if (extras[i] != null){
        	  td1.innerHTML = listing[i] + " (" + extras[i] + ")" ;
        	}else
          	  td1.innerHTML = listing[i];

        	td1.index_number = i;
        	
		td1.style.listing = listing[i];
        
		td1.onmousedown = function(evt){
				if (this.parentId){
					var cell= document.getElementById(this.parentId);
					if (cell){
						 
					  	cell.selected = this.index_number;
						cell.value = cell.listing[this.index_number]; 				
						cell.style.borderColor = "#AAA"; 
						cell.style.background = "#FFF";
						if (cell.onchange) cell.onchange();
						//var obj = this.parentNode.parentNode.parentNode.parentNode;
						//obj.parentNode.removeChild(obj);
						return;
					}
				}
				alert('UNABLE TO LOCATE PARENT FIELD');	
				var obj = this.parentNode.parentNode.parentNode.parentNode;
				obj.parentNode.removeChild(obj);
				return;

        	};

        	td1.onmouseover = function(){
			if (this.parentId){
				var cell= document.getElementById(this.parentId);
				if (cell){
					if (cell.selected == this.index_number){
						this.style.backgroundColor = "#66AADD";
						return;
					}
				}
			}
			this.style.backgroundColor = "#99CCFF"; flag = false;
		};
        	td1.onmouseout = function(){ 
			if (this.parentId){
				var cell= document.getElementById(this.parentId);
				if (cell){
					if (cell.selected == this.index_number){
						this.style.backgroundColor = "#7BB6FF";
					return;
					}
				}
			}
			this.style.backgroundColor = "#EEEEEE"; flag = false;
		};
        	td1.style.width = field.style.width;
        	td1.id = field.id + '_option_' + i;
		row.appendChild(td1);
        	row.width = field.offsetWidth + "px";
        	
        	i++;

     	}

	if (table) table.style.width = parseInt(field.offsetWidth) + "px";

}


function js_checklist(field) {
	
	//var listing = field.listing;

	var tbody = document.getElementById(field.id + '_drop_list');

	var iset = false;
	var partset = false;
	//var extras = new Array();
	//var fire = new Array();
	//if (field.fire) fire = field.fire;
	//if (field.extras) extras = field.extras;

    	var value = field.value;

    	if (field.nextchar != null)
                value = value + field.nextchar;

	if (field.chop == true)
		value = value.substring(0,(value.length -1));


	if (field.snap == true){
		value = field.value.substring(0, field.startsection) + field.value.substring(field.endsection);

	}


	if (field.listing == 0){ field.selected = null; js_dropdown(field);return;}


	window.status = window.status + " checking.. " + value;
	
	if (field.name == "company" && field.form){
	///special rule for company search field///
	// DO NOT AUTOSELECT IF POSTCODE DOES NOT MATCH AN EXISTING ENTRY

		var postcode = getFormElement(field.form, 'postcode');
		if (postcode){
			if (postcode.value.length > 0){
				iset = true;
			}
		}
	///////////////////////////////////////////
	}
	
	var i = 0;
    	if (tbody){
	while ( i < tbody.rows.length ){

        var obj = document.getElementById(field.id + '_option_' + i);
	var ex = "";
	if (field.extras){ ex = field.extras[i] || "" }
        if (obj){
                if (	value.length > 0 && 
			(
				obj.innerHTML.substring(0,value.length).toLowerCase() == value.toLowerCase() || 
				field.listing[i].substring(0,value.length).toLowerCase() == value.toLowerCase() ||
				ex.substring(0,value.length).toLowerCase() == value.toLowerCase()
			)
			){
                		if (iset == false){
					if (value.toLowerCase() == obj.innerHTML.toLowerCase() || 
					field.listing[i].toLowerCase() == value.toLowerCase() ||
					ex.toLowerCase() == value.toLowerCase()){
						iset = true;
					}
					field.selected = i;
					partset = true;
				}

			}
		obj.style.backgroundColor = "#EEEEEE";
        	}
        i++;
    	}
	}
    	if ( partset == false ) field.selected = null;

	if (field.selected != null){
	      var cell =  document.getElementById(field.id + '_option_' + field.selected);
	      if (cell) cell.style.backgroundColor = "#7BB6FF";
	}


}

function pt(a,b){if(b){b.style.left=zb(a)+"px";b.style.top=Yb(a)-b.offsetHeight-5+"px";}}

function da(a,b){if(b){b.style.left=zb(a)+"px";b.style.top=Yb(a)+ (parseFloat(a.offsetHeight) + 2)+"px";b.style.width=Ta(a)+"px"}}
//function zb(s){return findPosX(s);}//kb(s,"offsetLeft")}
function Yb(s){return kb(s,"offsetTop")}
function kb(s,na){
var wb=0;
var sc = "scrollLeft";
  if (na == "offsetTop")  sc = "scrollTop";
while(s){
        wb+=parseInt(s[na])-parseInt(s[sc]);s=s.offsetParent
        } return wb + parseInt(document.body[sc])
}
function Ta(a){if(navigator&&navigator.userAgent.toLowerCase().indexOf("msie")==-1){return a.offsetWidth;-ea*2}else{return a.offsetWidth}}
	
function zb(obj)
  {
    var curleft = 0;//parseFloat(obj.offsetLeft);
    if(obj.offsetParent)
        while(1)
        {
          if (obj.parentNode){
                if (obj.parentNode.tagName == "BODY"){
    //                    curleft -= parseFloat(obj.offsetLeft);
                }
          }
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            {
            break;
            }
          obj = obj.offsetParent;
        }
    else if( obj.x )
        curleft += parseFloat(obj.x);
    else  
        curleft += parseFloat(obj.offsetLeft);
    
    return curleft;
  }

function getAttrPixValue(e,a){
   var px=0;
    if(window.getComputedStyle){
        var css,sty=window.getComputedStyle(e,'');
      if(sty&&sty.getPropertyCSSValue){
         css=sty.getPropertyCSSValue(a);
         if((css)&&css.primitiveType<=18){try{px=css.getFloatValue(5)|0;}catch(e){};}
      }
    }
    return px;
}

var sequence = new Array('account','booked_by','pc','vehicle','purchase_order','total_pieces','c_weight','pricedif','miles','quote','job');

function JumpToNext (field){

	var next;
	var start = false;
	var i = 0;
	var loop = 0;
	if (field.id == "pc" && document.getElementById('address_form')) return;

	while (next == null && loop < (sequence.length * 2)){

	    if (start == false){
		if (field.id == sequence[i]){
			start = true;
		}
	    }else if (next == null){
		var next = document.getElementById( sequence[i] );
	    	if (next !=null){
			if (next.tagName == "CHECKBOX" || next.tagName == "INPUT" || next.tagName == "TEXTAREA"){
				//ok
			}else{
				next = null;
	    		}
		}
	    }
	    i++;
	    if (i > sequence.length) i = 0;
	    loop++;
	
	}
	if (next){
		next.focus();
		return true;
		//window.setTimeout( 'document.getElementById( \'' + next.id + '\').focus()' , 100);
	}
	return false;

}



function js_emailquote(){

	var Box = new js_box("Email Quotation","emailquote_box");
	Box.AddtoBody();


	var form = document.createElement("form");
	Box.append(form);	
	var table = document.createElement("table");
	form.style.margin = "0px";
	form.appendChild(table);
	
	var tbody = document.createElement("tbody");
	table.appendChild(tbody);

	// line 1 To:

	var row = tbody.insertRow(-1);
	var td1 = document.createElement('td');
	td1.style.verticalAlign = "top";
	td1.innerHTML = "Email / Fax No.:";
	row.appendChild(td1);

	var td2 = document.createElement('td');
	row.appendChild(td2);
	

	var field = CNav.getAccount().emailDropDown('email');
	//document.createElement("input");
	//field.type = "text";
//	field.name = "email";
//	field.id = "email";
	field.style.width = "300px";
	td2.appendChild(field);


	var div = document.createElement("div");
	div.id = "to_box";
	td2.appendChild(div);


	// Line 2 Reference

	var row = tbody.insertRow(-1);
	var td1 = document.createElement('td');
	td1.innerHTML = "Reference *:";
	row.appendChild(td1);

	var td2 = document.createElement('td');
	row.appendChild(td2);
	
	var field = document.createElement("input");
	field.type = "text";
	field.name = "ref";
		var cn = document.getElementById('consign_note');
		if (cn){
			if (cn.tagName == 'input'){
			field.value = cn.value;
			}else{
			field.value = cn.innerText || cn.innerHTML;
			}
		}
	
	td2.appendChild(field);


	// Line 3 Message for email

	var row = tbody.insertRow(-1);
	var td1 = document.createElement('td');
	td1.style.verticalAlign = "top";
	td1.innerHTML = "Message";
	row.appendChild(td1);

	var td2 = document.createElement('td');
	row.appendChild(td2);
	
	var field = document.createElement("textarea");
	field.name = "message";
	field.style.width = "300px";
	field.style.height = "100px";
	td2.appendChild(field);	

	var div = document.createElement('div');
	div.innerHTML = "* Reference required so the Quote can be found in future<br>If a message is supplied it will precede the quotation in the email.";
	div.style.fontstyle = "Italics";
	form.appendChild(div);


                var plw = document.createElement("div");
                plw.style.visibility = "hidden";
                form.appendChild(plw);
                plw.align="center";
                plw.style.verticalAlign = "middle";
                plw.id = "pleasewait_create";


                var bl = document.createElement("div");
                bl.innerHTML = "Please Wait..";
                bl.style.verticalAlign = "middle";
                bl.style.padding = "15px";
                bl.style.display = "inline";
                var pl = document.createElement("div");
                pl.style.display = "inline";
                var image = document.createElement("img");
                image.src = "/pleasewait1.gif";
                image.height = "40";
                image.width = "40";
                image.style.verticalAlign = "middle";
                image.style.display = "inline";
                pl.style.height = "40px";
                pl.appendChild(image);
                plw.appendChild(pl);
		plw.appendChild(bl);



	var button = document.createElement("input");
	button.type = "button";
	button.value = "Cancel";
	button.style.width = "80px";
	form.appendChild(button);

	
	button.onclick = function(){

		//insert ajax request	
		this.parentNode.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode.parentNode);

	};


	var button = document.createElement("input");
	button.type = "button";
	button.value = "Send";
	button.style.width = "80px";
	form.appendChild(button);
	form.id = "email_quote_form";
	button.id = "sendemailquote";
	button.onclick = function(){

		js_option_question('Do you wish to take a exact copy of this quote as it now? This will create a second quote any further changes to this booking screen will not effect the copy.',this.id,['YES','NO'],'sendquery');

		//form.sendEmail()

	}

	form.sendEmail = function (option){

		if (document.getElementById('pleasewait_create')){
                    document.getElementById('pleasewait_create').style.visibility = "visible";
                }

		

		//insert ajax request
		var test = "";
		var params = ['NO_CACHE','wait__1'];
		
		if (option == "YES"){
			params.push('clone__1');
		}

		for (var i = 0; i < this.elements.length; i++){
		      params.push( this.elements[i].name + "__" + this.elements[i].value );
		}
	
		if (ref) params.push(ref.ajax());
		
		this.disabled = true;
		
		emailQuote(params,[ajax_feedback],'POST');
		
	//	this.parentNode.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode.parentNode);

		
	};



}

function sendquery(option){
	var form = document.getElementById('email_quote_form');
	if (option == "YES"){
		var con;
                var cn = document.getElementById('consign_note');
                if (cn){
                        if (cn.tagName == 'input'){
                        con = cn.value;
                        }else{
                        con = cn.innerText || cn.innerHTML;
                        }
                }
		if (con == getFormElement(form,'ref').value){
			js_question('In Order send a copy of this quote please provide a new Reference','new_ref','sendquery2', con);
			return;
		}
	}
	form.sendEmail(option);
}

function sendquery2(field,value){
	var form = document.getElementById('email_quote_form');
	getFormElement(form,'ref').value = value;
	sendquery('YES');
}

function resendemail(){
	var box = document.getElementById('sendemailquote');
	if (box){ box.disabled = false;}
        if (document.getElementById('pleasewait_create')){
           document.getElementById('pleasewait_create').style.visibility = "hidden";
	}
}

function close_emailquote(){

        var box = document.getElementById('emailquote_box');
        if (box){
                document.body.removeChild(box);
        }
}


function close_create(){

 	var box = document.getElementById('create_box');
	if (box){
		document.body.removeChild(box);
	}
}

function pausecomp(millis) 
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); } 
while(curDate-date < millis);
} 

var cancel_check = false;
var auto_retry = true;
function pre_check(retry){
	if (cancel_check == true) return;
	if (document.getElementById('pre_check_box')){
		document.body.removeChild( document.getElementById('pre_check_box'));
	}
	var Box = new js_box("Checking before creating job", 'pre_check_box');
	Box.AddtoBody();
	cancel_check = true;
	auto_retry = false;
	var ok = true;
	var max = 0;
	for ( var id in sweep ){

		var cell = document.createElement("div");
		cell.innerHTML = "checking: " + id
		Box.append(cell);
		var obj = document.getElementById(id);
		if (obj){
			if (obj.saveStamp != null){
				ok = false;
				if (retry != null &&  max == 0){
					if (updatefield){
						updatefield(obj,"noretry"); // send again but dont launch anymore retries automatically
						max++;
					}
				}
				cell.innerHTML = "checking: " + id + " Failed (not yet saved)";
			}else{
				cell.innerHTML = "checking: " + id + " OK";
			}
		}
	}

	if (ok == true){
		auto_retry = true;
		cancel_check = false;
		sweep = new Array();
		document.body.removeChild( Box.div );
		return create_job();
	}
	var cell = document.createElement("div");
	cell.innerHTML = "Please wait whilst we re-connect to the server";
	Box.append(cell);
	cancel_check = false;
	pre_check_retry = window.setTimeout('pre_check(1)',5000);
}


function create_job(){

		cancel_check = false;
		var i = 0;
		for ( var id in sweep ){
			i++;
		}
		if (i > 0) return pre_check();


//need to consult CNav object on the required information (vehicleid etc)
		if (ref.type == 'jobid'){
			return alert('Job has already been booked');
		}

	        var Box = new js_box("Create Job", 'create_box');
                Box.AddtoBody();

		var cell = document.createElement("div");
		var repeats = document.getElementById('repeat');	
		var quote = document.getElementById('quote');	
		var qt = "0.00";
		if (quote){
			qt = quote.value;
		}
		var count = 1;
		if (repeats){
			if (parseFloat(repeats.value) > 0)
				count = parseFloat(repeats.value) + 1;
		}
                        if (count > 1){
                                var text = document.createElement("div");
                                    text.align="center";
                                    text.marginBottom = "10px";
                                    text.style.fontWeight ="bold";
                                    text.style.color = "yellow";
                                    text.style.fontSize = "18px";
                                    text.innerHTML = (count) + " Bookings at " + qt;
                                cell.appendChild(text);
                        }else{
                                var text = document.createElement("div");
                                    text.align="center";
                                    text.marginBottom = "10px";
                                    text.style.fontWeight ="bold";
                                    text.style.color = "#BFEFFF";
                                    text.style.fontSize = "18px";
                                    text.innerHTML = "Single Booking at " + qt;
                                cell.appendChild(text);
                        }
	



		var container = document.createElement("div");
		Box.append(cell);
		
		var plw = document.createElement("div");
		plw.style.visibility = "hidden";
		cell.appendChild(plw);
		plw.align="center";
		plw.style.verticalAlign = "middle";
		plw.id = "pleasewait_create";

		
		var bl = document.createElement("div");
		bl.innerHTML = "Please Wait..";
		bl.style.verticalAlign = "middle";
		bl.style.padding = "15px";
		bl.style.display = "inline";
		var pl = document.createElement("div");
		pl.style.display = "inline";
		var image = document.createElement("img");
		image.src = "/pleasewait1.gif";
		image.height = "40";
		image.width = "40";
		image.style.verticalAlign = "middle";
		image.style.display = "inline";
		pl.style.height = "40px";	
		pl.appendChild(image);
		plw.appendChild(pl);
		plw.appendChild(bl);
		
		Box.append(container);
		create_job_option(container,'tocustomer','Has the customer been quoted the price?');
		create_job_option(container,'bookagain','Book another job for this Customer?');
		create_job_option(container,'continue_editing','Continue Editing this Booking after Creation?');
		create_job_option(container,'print_docket','Print Docket Booking after Creation?');
		create_job_option(container,'insure','Customer Requested Insurance?');

        	var cancel = document.createElement("input");
        	cancel.type = "button";
        	cancel.value = "Cancel";
		cancel.id= "cancel_create";
        	cancel.style.width = "100px";
        	Box.append(cancel);


        	cancel.onclick = function(){

        	        //insert ajax request
        	        document.body.removeChild(this.parentNode.parentNode);

	        };


		if (ref.type == "pending"){

        	        var but = document.createElement("input");
        	        but.type = "button";
        	        but.display = "inline";
        	        but.id = "continue";
        	        but.style.width = "100px";
        	        but.style.gotoId = "Add Driver";
        	        but.jobid = this.jobid;
        	        but.value = "Add Driver";
			but.onclick = function(){
				js_addDriver();
			}
	                Box.append(but);
	
		}

                var but = document.createElement("input");
                but.type = "button";
                but.display = "inline";
                but.id = "continue";
                but.style.width = "100px";
                but.style.gotoId = "tocustomer";
                but.jobid = this.jobid;
                but.value = "Continue";
                Box.append(but);

		but.onclick = function(){
				if (document.getElementById('pleasewait_create')){
					document.getElementById('pleasewait_create').style.visibility = "visible";
				}

				var cancelbt = document.getElementById('cancel_create')
				if (cancelbt){
					cancelbt.onclick = function(){
						document.body.removeChild(this.parentNode.parentNode);
						js_alert('Pressing Cancel after Continue will not prevent the Job being booked on the server, if you have been waiting a long time it is advised to check whether this Created using Job Search before trying again.');
					}
					
				}
				this.disabled = true;
				var params = ['NO_CACHE','wait__1'];
				
				if (ref){
                			params.push(ref.ajax());
        			}
				if (document.getElementById('locked')){
					if (document.getElementById('locked').checked == true) params.push('locked');
				}
				params.push('deliverytime','repeat','period','pickuptime','pickupdate_value','deliverydate_value','continue_editing','bookagain','tocustomer','print_docket');
				if (document.getElementById('insure')){
					if (document.getElementById('insure').checked == true)
					params.push('insure_value','insurance__yes');
				}
				job_creation( params , [ajax_feedback],'POST' );
		};
		but.focus();

}

function create_job_option(container,id,description,gotoid){

                        var label = document.createElement("div");
                        label.style.width = "295px";
                        label.style.padding = "5px";
                        label.style.margin = "5px";
                        label.align="right";
                        label.innerHTML = description;

                        var but = document.createElement("input");
                        but.type = "checkbox";
                        but.id = id 
                        but.style.gotoId = gotoid;
                        but.defaultChecked = false;
                        but.onfocus = function(){ this.parentNode.style.color = "yellow"; }
                        but.onblur = function(){ this.parentNode.style.color = ""; }
                        but.onkeypress = function(evt){
			  var charCode =
    				document.layers ? evt.which :
    				document.all ? window.event.keyCode :
    				document.getElementById ? evt.keyCode : 0;

    			   if ((charCode == 13 || charCode == 9)){

				for (var i = 0; i < this.parentNode.parentNode.childNodes.length; i++){
					if (this.parentNode == this.parentNode.parentNode.childNodes[i]){
						if (this.parentNode.parentNode.childNodes.length -1 == i){
						this.parentNode.parentNode.childNodes[0].lastChild.focus();
						}else
						this.parentNode.parentNode.childNodes[i + 1].lastChild.focus();
					}
				}
				return false;
			   }

			}
			if (id == "insure"){
                                but.onclick = function(){
                                      if (!document.getElementById('insure_value')){
                                      this.parentNode.appendChild(document.createElement("br"))
                                      this.parentNode.appendChild(document.createTextNode("Insurance Value:"));
                                      var drop = document.createElement("input");
                                        drop.type = "text";
                                        //drop.value = 1;
                                        drop.size = "2";
                                        drop.id = "insure_value";
                                        drop.style.gotoId = this.style.gotoId;
                                        this.parentNode.appendChild(drop);
                                        drop.onkeypress = this.onkeypress;
					drop.select();
                                        drop.focus;
                                        this.style.gotoId = "insure_value";
                                        }
                                }
			}
			but.value = "yes";

			label.appendChild(but);
                        container.appendChild(label);
}

function savePriority(level){

	var bar = document.getElementById('priority_box');
	if (bar){
		if (parseFloat(level) == parseFloat(bar.level))
		updatevalue('priority', bar.level);
	}

}

function setPriority(level) {

var bar = document.getElementById('priority_bar');
var box = document.getElementById('priority_box');
if (bar == null){

	var dw = document.createElement('input');
	    dw.type = "button";
	    box.appendChild(dw);
	    dw.value = "-";
	    dw.onclick = function (){
	    		if (this.parentNode.level > 0){
				setPriority((this.parentNode.level - 1));
				window.setTimeout('savePriority('+ this.parentNode.level+')',900);
	    		}
	    }
	bar = document.createElement('div');
	bar.style.margin = "0px";
	bar.style.padding = "0px";
	bar.style.display = "inline";
	bar.id = 'priority_bar';
	bar.style.height = "20px";
	bar.style.width = "100px";
	box.appendChild(bar);

        var up = document.createElement('input');
            up.type = "button";
            box.appendChild(up);
            up.value = "+";
            up.onclick = function (){
                        if (this.parentNode.level < 5){
				setPriority( (this.parentNode.level + 1));
				window.setTimeout('savePriority('+ this.parentNode.level+')',900);
            		}
	    }

}else{
	if (parseFloat(box.level) == parseFloat(level)) return;
}
	while (bar.childNodes.length > 0){
		bar.removeChild(bar.childNodes[0]);
	}
	var nlevel = parseFloat(level);
	if (nlevel < 0) nlevel = 0;
	if (nlevel > 5) nlevel = 5;
	box.level = nlevel;
	for (var i = 1; i < 6; i++){

	   if (window.navigator.userAgent.indexOf("MSIE") > -1){
		if (i<=nlevel){

		bar.innerHTML += "<img src=\"/priority.gif\" title=\"" + i + "\" level=\""+i+"\" onclick=\"changePriority(" + i + ");\">";

		}else{
		bar.innerHTML += "<img src=\"/priority_off.gif\" title=\"" + i + "\" level=\""+i+"\" onclick=\"changePriority(" + i + ");\">";
		}
	    }else{
		var image = new Image(1,1); //document.createElement('img');
		image.style.width = "20px";
		image.style.height = "20px";
		image.style.margin = "0px";
		image.level = i;
		image.title = i;
		if (i != nlevel){
			image.onclick = function(){
				setPriority( this.level );
				window.setTimeout('savePriority('+ this.level+')',900);
			}
		}
		bar.appendChild(image);
                if (i<=nlevel){
                image.src = "/priority.gif";
                }else{
                image.src = "/priority_off.gif";
                }
	   }
	}
}

function changePriority(image){
setPriority( image  );
window.setTimeout('savePriority('+ image +')',900);

}


//route options
//links to functions required:
//Show Map: startMap24();
//Add Collection Way Point: Map24.MapApplication.Map.addListener("Map24.Event.MapClick", mapClickedcollect )
//Add Delivery Way Point: Map24.MapApplication.Map.addListener("Map24.Event.MapClick", mapClickeddelivery )
//Optimise Addresses: check_distances()
//Revert to Previous Address Order: revert_addresses()

var defaultWayRegions = new Array();
function PtW ( region, x, y, label, proximity){
	this.region = region;
	this.label = label;
	this.x = x;
	this.y = y;
	this.proximity = proximity;
	return this;
}

function SavedWay (region, x, y, label, proximity) {
	var WayPt = new PtW(region, x, y, label, proximity);
	if (!defaultWayRegions[ region ]){
		defaultWayRegions[ region ] = new Array();
	}
	defaultWayRegions[ region ][ defaultWayRegions[ region ].length ] = WayPt;
}

SavedWay('London','-0.4942','51.4948','M25 Junction (15) / M4 (4b)',500);
SavedWay('London','-0.38520','51.7164','M25 Junction (21) / M1 (6a)',500);
SavedWay('London','-0.53169','51.56079','M25 Junction (16) / M40 (1a)',500);
SavedWay('London','-0.53746','51.4007','M25 Junction (12) / M3 (2)',500);
SavedWay('Midlands','-2.0628','52.3546','M42 Junction (1) / M5 (4a)',500);
SavedWay('Midlands','-1.9750','52.5558','M6 Junction (8) / M5',500);
SavedWay('North West','-2.14023','53.5572','A627(M) Start',100);
SavedWay('Midlands','-1.3228','53.19765','M1 Junction (29) A617 Mansfield',100);
SavedWay('North West','-2.4110','53.3321','A556 (Mere) M56 bypass',100);
SavedWay('Scotland','-3.4033','55.720','A702 (M74 > Edinburgh)',500);

function routeoptionsWaypoints(field){

        var dd = document.getElementById("routeOptionsTab_Way");
	if (dd) document.body.removeChild(dd); //remove existing

	dd = document.createElement("div");
        document.body.appendChild(dd);
        dd.id = "routeOptionsTab_Way";
        dd.style.position = "absolute";
        dd.style.background = "#EEE";
        dd.style.padding = "5px";
        da(field,dd);
        dd.style.left = (parseFloat(dd.style.left) + 190) + "px";
	dd.style.top = (parseFloat(dd.style.top) - 20) + "px";
	dd.style.border = "1px #333 solid";
        dd.style.width = "200px";
        dd.style.color = "#222";
	dd.onmouseouti = function(){
		this.parentNode.removeChild(this);
	}

	for ( var region in defaultWayRegions ){

        var mess = document.createElement("div");
            mess.innerHTML = region;
	    mess.style.fontWeight = "bold";
            dd.appendChild(mess);

	    for ( var i=0;i<defaultWayRegions[ region ].length;i++) { 
           	var WayPt = defaultWayRegions[ region ][i]; 
		var mess = document.createElement("div");
		mess.innerHTML = WayPt.label;
		if (live){
			mess.x = WayPt.x;
			mess.y = WayPt.y
			mess.proximity = WayPt.proximity;
                	mess.style.cursor = "pointer";
                	mess.onclick = function (){
        		WayDisplayBox ("pickup", null,null, this.x, this.y, this.innerHTML, null, this.proximity);        	
			
        			if (document.getElementById('routeOptionsTab')){
                			document.getElementById('routeOptionsTab').close();
        			}
				this.parentNode.close();

			}
            	}else{
                mess.style.color = "#999";
            	}
		dd.appendChild(mess);
	    }

	    dd.close = function(){
		document.body.removeChild(this);
	    }

	}

}

function routeoptions(field){

	if (document.getElementById('routeOptionsTab')){
		field.value = "Show Map Options";
		document.getElementById('routeOptionsTab').close();
		return;
	}
	field.value = "Hide Map Options";
	field.id = "routeoptionbt";
	
	var dd = document.createElement("div");
	document.body.appendChild(dd);
	dd.id = "routeOptionsTab";
	dd.style.position = "absolute";
	dd.style.background = "#EEE";
	dd.style.padding = "5px";
	da(field,dd);
	dd.style.border = "1px #333 solid";
	dd.style.width = "200px";
	dd.style.color = "#222";
	if (live == false){
		var mess = document.createElement("div");
		    mess.innerHTML = "Show Map";
		    dd.appendChild(mess);
		    mess.onclick = function (){
			var mp = document.getElementById('maparea');
			mp.style.width = "500px";
			mp.style.height = "400px";
			startMap24();
			this.parentNode.close();
		    }
		    mess.style.cursor = "pointer";
	}

	var mess = document.createElement("div");
	    mess.innerHTML = "Add Collection Way Point on Map";
	    dd.appendChild(mess);
	    if (live){
	        mess.style.cursor = "pointer";
		mess.onclick = function (){
			Map24.MapApplication.Map.addListener("Map24.Event.MapClick", mapClickedcollect )
			this.parentNode.close();
		}
	    }else{
		mess.style.color = "#777";
	    }
	
        var mess = document.createElement("div");
            mess.innerHTML = "Add Delivery Way Point on Map";
	    dd.appendChild(mess);
            if (live){
	        mess.style.cursor = "pointer";
                mess.onclick = function (){
                        Map24.MapApplication.Map.addListener("Map24.Event.MapClick", mapClickeddelivery )
                	this.parentNode.close()
		}
            }else{
                mess.style.color = "#777";
            }

        var mess = document.createElement("div");
            mess.innerHTML = "Add Default Way Point";
            dd.appendChild(mess);
            if (live || 1==1){
                mess.style.cursor = "pointer";
                mess.onmouseover = function (){
                	routeoptionsWaypoints(this)
		}
            }else{
                mess.style.color = "#777";
            } 
	var rt = document.getElementById('routeoption');
	if (rt){
	
	var dess = document.createElement("div");
	dd.appendChild(dess);

	   if (rt.value == "standard"){

		dess.onclick = function (){
			var rte = document.getElementById('routeoption');
			rte.value = "fast";
			requestRoute(1);
			this.parentNode.close();
		}
		dess.innerHTML = "Re-Route Fastest";

	   }else{
		dess.onclick = function (){
			var rte = document.getElementById('routeoption');
			rte.value = "standard";
			requestRoute(1);
			this.parentNode.close();
		}
            	dess.innerHTML = "Re-Route Shortest";
           }
            dess.style.cursor = "pointer";

	   var ret = document.createElement("div");
	   dd.appendChild(ret);
	   if (rt.value == "standard_return"){
                ret.onclick = function (){
                        var rte = document.getElementById('routeoption');
                        rte.value = "standard";
                        requestRoute(1);
                        this.parentNode.close();
                }
                dess.onclick = function (){
                        var rte = document.getElementById('routeoption');
                        rte.value = "fast_return";
                        requestRoute(1);
                        this.parentNode.close();
                }
		dess.innerHTML = "Re-Route Fastest";
		ret.innerHTML = "One Way Journey";

	   }else if (rt.value == "fast_return"){
                ret.onclick = function (){
                        var rte = document.getElementById('routeoption');
                        rte.value = "fast";
                        requestRoute(1);
                        this.parentNode.close();
                }
                dess.onclick = function (){
                        var rte = document.getElementById('routeoption');
                        rte.value = "standard_return";
                        requestRoute(1);
                        this.parentNode.close();
                }
		dess.innerHTML = "Re-Route Shortest";
                ret.innerHTML = "One Way Journey";
	   }else if (rt.value == "fast"){
                ret.onclick = function (){
                        var rte = document.getElementById('routeoption');
                        rte.value = "fast_return";
                        requestRoute(1);
                        this.parentNode.close();
                }
                ret.innerHTML = "Return Journey to 1st collection pt";
	   }else{
                ret.onclick = function (){
                        var rte = document.getElementById('routeoption');
                        rte.value = "standard_return";
                        requestRoute(1);
                        this.parentNode.close();
                }
                ret.innerHTML = "Return Journey to 1st collection pt";
	   }
	   ret.style.cursor = "pointer";
	}
        var mess = document.createElement("div");
            mess.innerHTML = "Optimise Addresses";
	    dd.appendChild(mess);
	    mess.style.cursor = "pointer";
	    mess.onclick = function (){ check_distances();this.parentNode.close() }
	    
        var mess = document.createElement("div");
            mess.innerHTML = "Revert Address Order";
	    mess.style.cursor = "pointer";
            dd.appendChild(mess);
            mess.onclick = function (){ revert_addresses(); this.parentNode.close() }

	dd.close = function(){
		this.parentNode.removeChild(this);
		document.getElementById('routeoptionbt').value = "Show Map Options";
                if (document.getElementById('routeOptionsTab_Way')){
                    document.getElementById('routeOptionsTab_Way').close();
                }

	}

}


function clearaddress(){
		
		var type = document.getElementById('pickup');
		if (type){
		    if (type.hasChildNodes()){
			while ( type.childNodes.length > 0){
				if (type.childNodes[0]){
					if (type.childNodes[0].mapLocation){
						type.childNodes[0].mapLocation.remove();
					}
					type.removeChild(type.childNodes[0]);
				}
			}
		     }
		}
		var type = document.getElementById('delivery');
		if (type){
		    if (type.hasChildNodes()){	
			while(type.childNodes.length > 0 ){
				if (type.childNodes[0]){
					if (type.childNodes[0].mapLocation){
						type.childNodes[0].mapLocation.remove();
					}
					type.removeChild(type.childNodes[0]);
				}
			}
		    }
		}
}

function clearscreen (){

		if (ref){ ref.reset() }
		dims = new Dimensions();

		clearaddress();

	        var x = document.getElementsByTagName('input');
		for (var i=0;i<x.length;i++) {
			if (x[i].reset){
				x[i].reset();
			}else if (x[i].type != "button"){
				x[i].value = "";
			}
		}
		var x = document.getElementsByTagName('td');
		for (var i=0;i<x.length;i++) {
			if (x[i].reset){ x[i].reset(); }
		}
		CNav.VehicleDropDown( 'vehicleid_box', 'vehicleid', 'vehicle', CNav.conf('vehicleid'));
		setPriority(0);
}




function Currency(num) {
num = num.toString().replace(/0,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
pence = num%100;
num = Math.floor(num/100).toString();
if(pence<10)
pence = "0" + pence;
return (((sign)?'':'-') + num + '.' + pence);
}

