//================================================

function checkbrowser()
{
if (navigator.userAgent.indexOf("Netscape") > 0)
	{ 
	var browser='NS';
	}

else if (navigator.userAgent.indexOf("Firefox") > 0)
	{
	var browser='FF';
	}

else if (navigator.userAgent.indexOf("MSIE") > 0 &&
navigator.userAgent.indexOf("Opera") < 0)
	{
	var browser='IE';
	}

else if (navigator.userAgent.indexOf("Opera") > 0)
	{
	var browser='OP';
	}
	return browser;
}

//================================================

function openhelp (url) 
{
	helpwindow = window.open(url, "helpwindow", "width=792,height=600,left=0,top=190,scrollbars=yes,resizable=yes ");
	helpwindow.focus();
}

//================================================
/* sets the pair of 50% boxes to the same height */

function resizeboxes(box1, box2, pageCorrection) {
	if (pageCorrection == null) {
			pageCorrection = 0;
	}
	
	var varContentBox1 = document.getElementById('contentbox' + box1);
	var varContentBox2 = document.getElementById('contentbox' + box2);
	
  if(varContentBox1 != null && typeof varContentBox1 != 'undefined' && 
     varContentBox2 != null && typeof varContentBox2 != 'undefined') {
	  var varcheckbrowser = checkbrowser();
	  if (varcheckbrowser == 'IE' || varcheckbrowser == 'NS') {
	    varContentBox1.style.height = '1%';
	    varContentBox2.style.height = '1%';
	    varCorrection = 0;
	    pageCorrection = 0;
	  } else {
	    varContentBox1.style.height = '';
	    varContentBox2.style.height = '';
	    /* changed from FF 1.5.02 to 1.5.03 or so. So I left it here for your convenience ;-) */
	    varCorrection = 0;
	    pageCorrection = 0;
	  }
	
	  /*	get height of the elements	*/
	  var ContentBox1Height = varContentBox1.clientHeight;
	  var ContentBox2Height = varContentBox2.clientHeight;

	  /* compare and resize if neccesary */
	  if(ContentBox1Height < ContentBox2Height) {
	    varContentBox1.style.height = ContentBox2Height + varCorrection + pageCorrection + "px";
	  } else if (ContentBox2Height < ContentBox1Height) {
	    varContentBox2.style.height = ContentBox1Height + varCorrection + pageCorrection + "px";
	  }
  }
}

/*====================================================================*/

function preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

function findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function swapImage() { //v3.0
  var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
	
function swapImgRestore() { //v3.0
  var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

/*====================================================================*/

/*simple static version hides the known selects on regestration form*/

function hideselect(m)

{
	for(var i=m;i <= 9;i++)
	{
	document.getElementById("select_0"+i).style.visibility="hidden";
	document.getElementById("select_0"+i).style.position="absolute";
	}
}

function shownextselect(id)

{
	var whichDealer = 'dealerType'+id;
	whichDealer = document.getElementById(whichDealer);
	
	var whatDealer = 'dealerFor'+id;
	whatDealer = document.getElementById(whatDealer);
	
	if ((whichDealer.selectedIndex != 0) && (whatDealer.selectedIndex != 0)) {
		document.getElementById(id).style.visibility="visible";
		document.getElementById(id).style.position="relative";
		document.getElementById(id).style.height="100%";
	}
}

/*====================================================================*/
/* selects all checkboxes on a form */

var checkflag = "false";

function check(field,newValue,oldValue) 
{
	if (checkflag == "false") 
	{
		for (i = 0; i < field.length; i++) 
		{
		field[i].checked = true;
		}
		checkflag = "true";
		return newValue; 
	}
	else 
	{
		for (i = 0; i < field.length; i++) 
		{
		field[i].checked = false; 
		}
		checkflag = "false";
		return oldValue; 
	}
}


/*	toogle form field visibility	*/
	
function toggleCountySelection () {
	if (document.getElementById('country')) {
		if (document.getElementById('countySelect')) {
			hideFormField ('countySelect');
		}
		var countryField = document.getElementById('country');
		var countryFieldSelect = countryField.getElementsByTagName('select')[0];
		if (countryFieldSelect.value == 'GB' || countryFieldSelect.value == "IT") {
			showFormField ('countySelect');
		}
		countryFieldSelect.onchange = function () {
			if (countryFieldSelect.value == 'GB' || countryFieldSelect.value == "IT") {
				showFormField ('countySelect');
			} else {
				hideFormField ('countySelect');
				document.getElementById('countyValue').value = '';
			}
		}
	}
}

function toggleRegionCode () {
	var frSelectsCount = 0;

	if (document.getElementById('faxNumber')) 
	{
		if (document.getElementById('faxNumber').getElementsByTagName('select')[0].value == 'FR')
		{
			frSelectsCount = frSelectsCount+1;
		}
		doTheRegionCodeToggle('fax', frSelectsCount);
	}
	if (document.getElementById('mobileNumber')) 
	{
		if (document.getElementById('mobileNumber').getElementsByTagName('select')[0].value == 'FR')
		{
			frSelectsCount = frSelectsCount+1;
		}
		doTheRegionCodeToggle('mobile', frSelectsCount);
	}
	if (document.getElementById('phoneOneNumber')) 
	{
		if (document.getElementById('phoneOneNumber').getElementsByTagName('select')[0].value == 'FR')
		{
			frSelectsCount = frSelectsCount+1;
		}	
		doTheRegionCodeToggle('phoneOne', frSelectsCount);
	}
	if (document.getElementById('phoneTwoNumber')) 
	{
		if (document.getElementById('phoneTwoNumber').getElementsByTagName('select')[0].value == 'FR')
		{
			frSelectsCount = frSelectsCount+1;
		}
		doTheRegionCodeToggle('phoneTwo', frSelectsCount);
	}

}

function doTheRegionCodeToggle (whichCode, frSelectsCount) 
{
	var field = document.getElementById(whichCode+'Number');
	var fieldSelect = field.getElementsByTagName('select')[0];
	var fieldInput = document.getElementById(whichCode+'RegionCode');
	var phoneHintFR = document.getElementById('infolayer');

	if (fieldSelect.value == 'FR') 
	{
		firstlayer = 'false';
		secondLayer = 'false';
		if ((fieldInput.id == 'faxRegionCode') || (fieldInput.id == 'mobileRegionCode'))
		{
			firstLayer = 'true';
		}
		if (fieldInput.id == 'phoneOneRegionCode' || fieldInput.id == 'phoneTwoRegionCode')
		{
			secondLayer = 'true';
		}
		hideFormField (whichCode+'RegionCode');
		if (frSelectsCount > 0 && firstLayer == 'true')
		{
		showFormField ('infolayer');
		}
		if (frSelectsCount > 0 && secondLayer == 'true')
		{
		showFormField ('infolayer2');
		}
	}

	fieldSelect.onchange = function () 
	{
		if (fieldSelect.value == 'FR')
		{
			fieldInput.value = '';
			hideFormField (whichCode+'RegionCode');
		}
		 else 
		 {
			showFormField (whichCode+'RegionCode');
		}
		
		frSelectsCount = 0;
		secondFrSelectsCount = 0;
		if (document.getElementById('mobileNumber').getElementsByTagName('select')[0].value == 'FR')
			{
				frSelectsCount = frSelectsCount+1;
			}
		if (document.getElementById('faxNumber').getElementsByTagName('select')[0].value == 'FR')
			{
				frSelectsCount = frSelectsCount+1;
			}
		if (document.getElementById('phoneTwoNumber').getElementsByTagName('select')[0].value == 'FR')
			{
				secondFrSelectsCount = secondFrSelectsCount+1;
			}
		if (document.getElementById('phoneOneNumber').getElementsByTagName('select')[0].value == 'FR')
			{
				secondFrSelectsCount = secondFrSelectsCount+1;
			}
		if (frSelectsCount <= 0)
			{
			hideFormField ('infolayer');
			}
		else if (frSelectsCount > 0)
			{
			showFormField ('infolayer');
			}
		if (secondFrSelectsCount <= 0)
			{
			hideFormField ('infolayer2');
			}
		else if (secondFrSelectsCount > 0)
			{
			showFormField ('infolayer2');
			}			
	}

}
	
function showFormField (whichField) {
	document.getElementById(whichField).style.display='inline';
}

function hideFormField (whichField) {
	document.getElementById(whichField).style.display='none';
}


/*	add functions to call onload	*/

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

addEvent(window, 'load', toggleCountySelection);
addEvent(window, 'load', toggleRegionCode);

function toggleDisplay(checkbox,targetID){
  var targetElement = document.getElementById(targetID);
  if(targetElement != null && typeof targetElement != "undefined"){
    if(checkbox.checked){
      targetElement.style.display = "block";
    }else{
      targetElement.style.display = "none";
    }
  }
}
function toggleEnabled(checkbox,targetID){
  var targetElement = document.getElementById(targetID);
  if(targetElement != null && typeof targetElement != "undefined"){
    if(!checkbox.checked){
      targetElement.value = "";
      targetElement.style.backgroundColor = "#DDD";
    }else{
      targetElement.style.backgroundColor = "#FFF";
    }
    targetElement.readOnly = !checkbox.checked;
  }
}
  
function showVehicleDescription(){
  var elementToShow = document.getElementById("standardVehicleDescription");
  var elementToHide = document.getElementById("customTemplateDescription");
  var standardTab = document.getElementById("standardDescriptionTab");
  var htmlTab = document.getElementById("htmlDescriptionTab");
  elementToShow.style.display = "block";
  elementToHide.style.display = "none";
      
  standardTab.setAttribute("class", "active");
  standardTab.setAttribute("className", "active");
  htmlTab.setAttribute("class", "");
  htmlTab.setAttribute("className", "");
}
function showHtmlDescription(){
  var elementToShow = document.getElementById("customTemplateDescription");
  var elementToHide = document.getElementById("standardVehicleDescription");
  var standardTab = document.getElementById("standardDescriptionTab");
  var htmlTab = document.getElementById("htmlDescriptionTab");
  elementToShow.style.display = "block";
  elementToHide.style.display = "none";
    
  htmlTab.setAttribute("class", "active");
  htmlTab.setAttribute("className", "active");
  standardTab.setAttribute("class", "");
  standardTab.setAttribute("className", "");
}
function getXMLHttpObject(){
    var xmlHttpObject = false;
    if(typeof XMLHttpRequest != 'undefined'){
      xmlHttpObject = new XMLHttpRequest();
    }
    if(!xmlHttpObject){
      try{
        xmlHttpObject = new ActiveXObject("Msxml2.XMLHTTP");
      }catch(e){
        try{
          xmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(e){ xmlHttpObject = null; }
      }
    }
    return xmlHttpObject;
}
function ajaxPost(formId, target, displayDivId, serializeFields, executeOnSuccess, executeOnFailure) {
  var form = $(formId);
  var displayDiv = $(displayDivId);
  var parameters = form.serialize();
  if (serializeFields != null) {
    parameters = serializeFields.toQueryString();	
  }
  new Ajax.Request(target,
    {
	  method:'post',
	  parameters: parameters,
	  onCreate: function() {},
	  onSuccess: function(transport) {
	    var response = transport.responseText || "no response text";
	    if(displayDiv){
	      displayDiv.innerHTML = response;
	    }
	    if (executeOnSuccess) {
	      eval(executeOnSuccess);
	    }
      },
      onFailure: function(){
        if (executeOnFailure) {
          eval(executeOnFailure);
        }
      }
    });
}
var DRAFT_SAVE_DEFAULT_ID = 'draftSaveDefault';
var DRAFT_SAVE_PROGRESS_ID = 'draftSaveProgress' ;
var DRAFT_SAVE_SUCCESS_ID = 'draftSaved';
function showDraftSaveFeedback(resultId){
  $(DRAFT_SAVE_PROGRESS_ID).style.display = 'none';
  $(resultId).style.display = 'block';
  if(resultId == DRAFT_SAVE_SUCCESS_ID){
	  updateLastSavedDraftWithCurrentTime();
  }
  window.setTimeout("showDraftSaveDefault('" + resultId + "')",2000);
}
function showDraftSaveDefault(resultId){
  $(resultId).style.display = 'none';
  $(DRAFT_SAVE_DEFAULT_ID).style.display = 'block';
}
function updateLastSavedDraftWithCurrentTime(){
  updateLastSavedDraft(Date.parse(new Date()));
}
function updateLastSavedDraft(lastSavedInMillis){
	try{
		if(typeof(lastSavedInMillis) != 'number' && typeof(lastSavedDraft) == 'number'){
			lastSavedInMillis = lastSavedDraft;
		}
		if(typeof(lastSavedInMillis) == 'number'){
			var lastSaved = new Date(lastSavedInMillis);
			var lsdHour = lastSaved.getHours();
			var lsdMinute = lastSaved.getMinutes();
			var ampm;
			if (0 <= lsdHour && lsdHour < 12) {
				ampm = 'AM';
				if (lsdHour == 0) lsdHour = 12;
			} else {
				ampm = 'PM';
				lsdHour = lsdHour - 12;
				if (lsdHour == 0) lsdHour = 12;
			}
			if (lsdHour < 10) lsdHour = '0' + lsdHour;
			if (lsdMinute < 10) lsdMinute = '0' + lsdMinute;
			var lsdTime = lsdHour + ":" + lsdMinute + " " + ampm;
			$('draftLastSaved').innerHTML = "Last Saved: " + lsdTime;
		}
	}catch(e){}
}
function saveDraft(){
	$(DRAFT_SAVE_DEFAULT_ID).style.display = 'none';
	$(DRAFT_SAVE_PROGRESS_ID).style.display = 'block';
	var target = "saveDraft.html";
	if(typeof(draftTarget) != 'undefined'){
		target = draftTarget;
	}
	ajaxPost('descForm',target,null,null,'showDraftSaveFeedback("draftSaved")','showDraftSaveFeedback("draftSaveFailed")');
}
function autoSaveDraft(target){
	try{
	  var target = "saveDraft.html";
	  if(typeof(draftTarget) != 'undefined'){
		target = draftTarget;
	  }	
	  ajaxPost('descForm',target,null,null,'updateLastSavedDraftWithCurrentTime()',null);
	}catch(e){}
}