var field_notes = new Array();
field_notes["title_prompt"] = "Please enter your personal title eg Mr";
field_notes["title_warn"] = "Mandatory field";
field_notes["forename_prompt"] = "Please enter a Forename";
field_notes["surname_prompt"] = "Please enter a Surname";
field_notes["jobtitle_prompt"] = "Please enter a Jobtitle";
/*field_notes["jobtitle_warn"] = "Mandatory field";*/
field_notes["company_prompt"] = "Please enter a Company";
field_notes["add1_prompt"] = "Please enter the first line of your address";
field_notes["add1_warn"] = "Mandatory field";
field_notes["add2_prompt"] = "Please enter the second line of your address (if available)";
field_notes["add2_warn"] = "";
field_notes["add3_prompt"] = "Please enter the third line of your address (if available)";
field_notes["add4_prompt"] = "Please enter a Town";
field_notes["add5_prompt"] = "Please enter a County";
field_notes["telephone_prompt"] = "Please enter valid UK Telephone landline number (starting 01 or 02)";
field_notes["telephone_warn"] = "Please enter valid UK Telephone landline number (starting 01 or 02)";
field_notes["mobile_prompt"] = "Please enter a Mobile number";
field_notes["email_prompt"] = "Please enter your email address";
field_notes["email_warn"] = "Valid email address required";

field_notes["bt_customer_prompt"] = "Are you an existing BT customer";
field_notes["bt_customer_warn"] = "Please answer yes or no";
field_notes["num_of_employees_prompt"] = "How many people does your company employ?";
field_notes["num_of_employees_warn"] = "Please select a value";
field_notes["event_urn_prompt"] = "Please select the time your require";
field_notes["event_urn_warn"] = "Please select a value";
field_notes["btlb_ter_prompt"] = "Please select which territory you are in";
field_notes["btlb_ter_warn"] = "Please select a value";
field_notes["tie_break_prompt"] = "What question would you put to Peter Jones?";

field_notes["btbe2009_how_hear_prompt"] = "How did you hear about BT Business Experience?";
field_notes["btbe2009_how_hear_warn"] = "Please select a value";

field_notes["btbe2009_sites_prompt"] = "How many business premises does your organisation operate from?";
field_notes["btbe2009_sites_warn"] = "Please select a value";

field_notes["diet_req_prompt"] = "Do you have any special dietary requirements?  If not please enter 'none'";
field_notes["diet_req_warn"] = "Please enter a value";

//

function addLoadEvent(func) {
  var oldonload = window.onload;
  if(getURLVar("debug")=="true"){
   	alert("old onload=" + onload + " new onload=" + func);
  }
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function prepareInputsForHints() {
  if(getURLVar("debug")=="true"){
  	alert("PPI4H");
  }
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
    // see if there is a lable I can stuff things in
    //alert(inputs[i].name);
  	label_name = "label_" + inputs[i].name;
    elem = document.getElementById(label_name);
    if (elem==null){
    	continue;
    }
		if (field_notes[inputs[i].name +"_prompt"]){
      elem.innerHTML = '</div><div style="display:inline-table;width:140px;">' + elem.innerHTML + '</div><div id=hint_' + inputs[i].name + ' class="myhint"><div class=myhint-top><div class=myhint-top-left><div class=myhint-top-right></div></div></div><div class=myhint-left><span>' + field_notes[inputs[i].name +"_prompt"] + '</span></div><div class=myhint-base><div class=myhint-base-left><div class=myhint-base-right></div></div></div></div><div id=warn_' + inputs[i].name + ' class="warn">';
		} else {
      elem.innerHTML = '</div><div style="display:inline-table;width:140px;">' + elem.innerHTML + '</div><div id=hint_' + inputs[i].name + ' class="myhint"><div class=myhint-top><div class=myhint-top-left><div class=myhint-top-right></div></div></div><div class=myhint-left><span>Enter a ' + inputs[i].name + '</span></div><div class=myhint-base><div class=myhint-base-left><div class=myhint-base-right></div></div></div></div><div id=warn_' + inputs[i].name + ' class="warn">';
		}
		
		// test to see if the hint span exists first
		if (document.getElementById("hint_" + inputs[i].name)) {
			// the span exists!  on focus, show the hint and hide the warning
			inputs[i].onfocus = function () {
				document.getElementById("warn_" + this.name).style.display = "none";
				document.getElementById("hint_" + this.name).style.display = "inline";
			  this.style.backgroundColor="#ffffff";
			}
			// when the cursor moves away from the field, hide the hint, check if there is a validation option set - if so show the warning and change the field background
			inputs[i].onblur = function () {
				if (!this.value || (this.validationset && this.validationset.validate())  ){
					if (this.validationset){
						if (field_notes[this.name +"_warn"]){
					    document.getElementById("warn_" + this.name).innerHTML = "<span>" + field_notes[this.name +"_warn"] + "</span>";
					    //document.getElementById("warn_" + this.name).innerHTML = "<span>&nbsp;</span>";
						} else {
					    document.getElementById("warn_" + this.name).innerHTML = "<span>" + this.validationset.validate() + "</span>";
					    //document.getElementById("warn_" + this.name).innerHTML = "<span>&nbsp;</span>";
					  }
  				  this.style.backgroundColor="#ffe0cc";
					} else {
					  document.getElementById("warn_" + this.name).innerHTML = "";
					}
				  document.getElementById("warn_" + this.name).style.display = "inline";
				}
				document.getElementById("hint_" + this.name).style.display = "none";
			}
		}
	}
	// maybe we need to repeat the same tests as above for selects - BT don't seem to
	//var selects = document.getElementsByTagName("select");
	var selects = document.getElementsByTagName("select");
	for (var i=0; i<selects.length; i++){
    // see if there is a lable I can stuff things in
  	label_name = "label_" + selects[i].name;
    elem = document.getElementById(label_name);
    if (elem==null){
    	continue;
    }
		if (field_notes[selects[i].name +"_prompt"]){
      elem.innerHTML = '</div><div style="display:inline-table;width:140px;">' + elem.innerHTML + '</div><div id=hint_' + selects[i].name + ' class="myhint"><div class=myhint-top><div class=myhint-top-left><div class=myhint-top-right></div></div></div><div class=myhint-left><span>' + field_notes[selects[i].name +"_prompt"] + '</span></div><div class=myhint-base><div class=myhint-base-left><div class=myhint-base-right></div></div></div></div><div id=warn_' + selects[i].name + ' class="warn">';
		} else {
      elem.innerHTML = '</div><div style="display:inline-table;width:140px;">' + elem.innerHTML + '</div><div id=hint_' + selects[i].name + ' class="myhint"><div class=myhint-top><div class=myhint-top-left><div class=myhint-top-right></div></div></div><div class=myhint-left><span>Enter a ' + selects[i].name + '</span></div><div class=myhint-base><div class=myhint-base-left><div class=myhint-base-right></div></div></div></div><div id=warn_' + selects[i].name + ' class="warn">';
		}
		
		// test to see if the hint span exists first
		if (document.getElementById("hint_" + selects[i].name)) {
			// the span exists!  on focus, show the hint and hide the warning
			selects[i].onfocus = function () {
				document.getElementById("warn_" + this.name).style.display = "none";
				document.getElementById("hint_" + this.name).style.display = "inline";
			  this.style.backgroundColor="#ffffff";
			}
			// when the cursor moves away from the field, hide the hint, check if there is a validation option set - if so show the warning and change the field background
			selects[i].onblur = function () {
				if (!this.selectedIndex  || (this.validationset && this.validationset.validate())){ //!this.selectedIndex
					if (this.validationset){
						if (field_notes[this.name +"_warn"]){
					    document.getElementById("warn_" + this.name).innerHTML = "<span>" + field_notes[this.name +"_warn"] + "</span>";
					    //document.getElementById("warn_" + this.name).innerHTML = "<span>&nbsp;</span>";
						} else {
					    document.getElementById("warn_" + this.name).innerHTML = "<span>" + this.validationset.validate() + "</span>";
					    //document.getElementById("warn_" + this.name).innerHTML = "<span>&nbsp;</span>";
					  }
  				  this.style.backgroundColor="#ffe0cc";
					} else {
					  document.getElementById("warn_" + this.name).innerHTML = "";
					}
				  document.getElementById("warn_" + this.name).style.display = "inline";
				}
				document.getElementById("hint_" + this.name).style.display = "none";
			}
		}
	}


	var textareas = document.getElementsByTagName("textarea");
	for (var i=0; i<textareas.length; i++){
    // see if there is a lable I can stuff things in
  	label_name = "label_" + textareas[i].name;
    elem = document.getElementById(label_name);
    if (elem==null){
    	continue;
    }
		if (field_notes[textareas[i].name +"_prompt"]){
      elem.innerHTML = '</div><div style="display:inline-table;width:140px;">' + elem.innerHTML + '</div><div id=hint_' + textareas[i].name + ' class="myhint"><div class=myhint-top><div class=myhint-top-left><div class=myhint-top-right></div></div></div><div class=myhint-left><span>' + field_notes[textareas[i].name +"_prompt"] + '</span></div><div class=myhint-base><div class=myhint-base-left><div class=myhint-base-right></div></div></div></div><div id=warn_' + textareas[i].name + ' class="warn">';
		} else {
      elem.innerHTML = '</div><div style="display:inline-table;width:140px;">' + elem.innerHTML + '</div><div id=hint_' + textareas[i].name + ' class="myhint"><div class=myhint-top><div class=myhint-top-left><div class=myhint-top-right></div></div></div><div class=myhint-left><span>Enter a ' + textareas[i].name + '</span></div><div class=myhint-base><div class=myhint-base-left><div class=myhint-base-right></div></div></div></div><div id=warn_' + textareas[i].name + ' class="warn">';
		}
		
		// test to see if the hint span exists first
		if (document.getElementById("hint_" + textareas[i].name)) {
			// the span exists!  on focus, show the hint and hide the warning
			textareas[i].onfocus = function () {
				document.getElementById("warn_" + this.name).style.display = "none";
				document.getElementById("hint_" + this.name).style.display = "inline";
			  this.style.backgroundColor="#ffffff";
			}
			// when the cursor moves away from the field, hide the hint, check if there is a validation option set - if so show the warning and change the field background
			textareas[i].onblur = function () {
				if (!this.value  || (this.validationset && this.validationset.validate())){ //!this.selectedIndex
					if (this.validationset){
						if (field_notes[this.name +"_warn"]){
					    document.getElementById("warn_" + this.name).innerHTML = "<span>" + field_notes[this.name +"_warn"] + "</span>";
					    //document.getElementById("warn_" + this.name).innerHTML = "<span>&nbsp;</span>";
						} else {
					    document.getElementById("warn_" + this.name).innerHTML = "<span>" + this.validationset.validate() + "</span>";
					    //document.getElementById("warn_" + this.name).innerHTML = "<span>&nbsp;</span>";
					  }
  				  this.style.backgroundColor="#ffe0cc";
					} else {
					  document.getElementById("warn_" + this.name).innerHTML = "";
					}
				  document.getElementById("warn_" + this.name).style.display = "inline";
				}
				document.getElementById("hint_" + this.name).style.display = "none";
			}
		}
	}


}

function MattAlert(sometext){
  alert('Matt' + sometext);
}
function checkSubmit() {
	var inputs = document.getElementsByTagName("input");
	mgv_retval = true;
	firstfield = 0;
	for (var i=0; i<inputs.length; i++){
		if (inputs[i].validationset){
      if (!inputs[i].value  || inputs[i].validationset.validate() ){
      	if (firstfield==0){
      		firstfield=i;
      	}
      	mgv_retval = false;
        if (field_notes[inputs[i].name +"_warn"]){
           document.getElementById("warn_" + inputs[i].name).innerHTML = "<span>" + field_notes[inputs[i].name +"_warn"] + "</span>";
           //document.getElementById("warn_" + inputs[i].name).innerHTML = "<span>&nbsp;</span>";
        } else {
           document.getElementById("warn_" + inputs[i].name).innerHTML = "<span>" + inputs[i].validationset.validate() + "</span>";
           //document.getElementById("warn_" + inputs[i].name).innerHTML = "<span>&nbsp;</span>";
        }
        document.getElementById("warn_" + inputs[i].name).style.display = "inline";
        inputs[i].style.backgroundColor="#ffe0cc";
      } else {
      	//mgv_retval = inputs[i].validationset.validate();
      	//alert(inputs[i].name + ":" + mgv_retval);
      }
    }
  }
	var selects = document.getElementsByTagName("select");
	for (var i=0; i<selects.length; i++){
		if (selects[i].validationset){
      if (!selects[i].selectedIndex  ){
      	if (firstfield==0){
      		firstfield=i;
      	}
      	mgv_retval = false;
        if (field_notes[selects[i].name +"_warn"]){
           document.getElementById("warn_" + inputs[i].name).innerHTML = "<span>" + field_notes[inputs[i].name +"_warn"] + "</span>";
           //document.getElementById("warn_" + selects[i].name).innerHTML = "<span>&nbsp;</span>";
        } else {
           document.getElementById("warn_" + inputs[i].name).innerHTML = "<span>" + inputs[i].validationset.validate() + "</span>";
           //document.getElementById("warn_" + selects[i].name).innerHTML = "<span>&nbsp;</span>";
        }
        document.getElementById("warn_" + selects[i].name).style.display = "inline";
        selects[i].style.backgroundColor="#ffe0cc";
      }
    }
  }
	var textareas = document.getElementsByTagName("textarea");
	for (var i=0; i<textareas.length; i++){
		if (textareas[i].validationset){
      if (!textareas[i].value ){
      	if (firstfield==0){
      		firstfield=i;
      	}
      	mgv_retval = false;
        if (field_notes[textareas[i].name +"_warn"]){
           document.getElementById("warn_" + inputs[i].name).innerHTML = "<span>" + field_notes[inputs[i].name +"_warn"] + "</span>";
           //document.getElementById("warn_" + textareas[i].name).innerHTML = "<span>&nbsp;</span>";
        } else {
           document.getElementById("warn_" + inputs[i].name).innerHTML = "<span>" + inputs[i].validationset.validate() + "</span>";
           //document.getElementById("warn_" + textareas[i].name).innerHTML = "<span>&nbsp;</span>";
        }
        document.getElementById("warn_" + textareas[i].name).style.display = "inline";
        textareas[i].style.backgroundColor="#ffe0cc";
      }
    }
  }
  
  inputs[firstfield].focus();
  return mgv_retval;    
}	
function MattValidator(frmname){

  if(getURLVar("debug")=="true"){
  		alert("mV");
  }
  this.formobj=document.forms[frmname];
	if(!this.formobj)
	{
	  //showDialog('Error',"BUG: couldnot get Form object "+frmname,'error',4);
		return;
	}
	if(this.formobj.onsubmit)
	{
	 this.formobj.old_onsubmit = this.formobj.onsubmit;
	 this.formobj.onsubmit=null;
	}
	else
	{
	 this.formobj.old_onsubmit = null;
	}
	this.formobj.onsubmit=checkSubmit; //form_submit_handler;
}

function getURLVar(urlVarName) {
//divide the URL in half at the '?'
  var urlHalves = String(document.location).split('?');
  var urlVarValue = '';
  if(urlHalves[1]){
    //load all the name/value pairs into an array
    var urlVars = urlHalves[1].split('&');
    //loop over the list, and find the specified url variable
    for(i=0; i<=(urlVars.length); i++){
      if(urlVars[i]){
      //load the name/value pair into an array
      var urlVarPair = urlVars[i].split('=');
        if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
        //I found a variable that matches, load it's value into the return variable
        urlVarValue = urlVarPair[1];
        }
      }
    }
  }
  return urlVarValue;   
}

  if(getURLVar("debug")=="true"){
  	alert("debug is on");
  }
  addLoadEvent(prepareInputsForHints);
