

function generateform() {

var formdiv,content,nameU,emailU,sname,fname;
sname=""; fname="";
var theForm = document.form1;
formdiv=document.getElementById("multipleform");

content="<div id='Warning_mess'  style='display:none;'><table cellSpacing=0 cellPadding=0 align=center bgColor=#ffffff  border=0><tr><td  borderColor=#ffffff class='stylewarn'>Please correct the highlighted items to continue</td></tr></table></div>";
				
content+="<form id='form1' name='form1'  method='post' action='proc_files/processform.php5'>";

// -------Step1 Case Details--------
content+="<div id='step1'  style='display:block'>";

	content+="<table cellSpacing=0 cellPadding=5 align=center bgColor=#ffffff  border=0><tr><td  borderColor=#ffffff>";
	content+="<table align=center bgColor=#ffffff collspanning=0 cellspaning=0>";
	content+="<tr><td  align='center' colspan=2 height=25px background='Images/bggrey.gif'><div class='heading1'>Step 1 : &nbsp; Tell us what you need legal help with </div></td></tr>";

    content+="<tr><td height=5px> </td></tr>";
	content+="<tr><td height=30px><div class='middleR'>  I need help with  &nbsp;</div></td><td  valign'middle><select name='CaseType' id='CaseType' onChange=this.style.background='#ffffff'><option value='Not Set'>Please select</option><option value='I want to sell my business'>I want to sell my business</option><option value='I want to buy a business'>I want to buy a business</option><option value='Employers advice'>Employers advice</option><option value='Health & Safety advice'>Health & Safety advice</option><option value='Intellectual Property Advice'>Intellectual Property Advice</option></select></td></tr>";
	content+="<tr><td height=20px>&nbsp; </td></tr>";
	content+="<tr><td height=40px><div class='middleR'>  Case Title&nbsp;</div><div class='middleS'><br>(for your reference)</div></td><td  valign='middle'  align=left><input name='CaseRef' id='CaseRef' type='text' size='20' onChange=this.style.background='#ffffff'></div></td></tr>";
	content+="<tr><td height=20px>&nbsp; </td></tr>";
	content+="<tr><td height=40px><div class='middleR'>  Case description<br>(please keep brief)&nbsp;</div></td><td  valign='middle'  align=left><textarea name='CaseDesc' id='CaseDesc' cols='35' rows='4' onChange=this.style.background='#ffffff'></textarea></td></tr>";
	content+="<tr><td height=20px>&nbsp; </td></tr>";
	content+="<tr><td height=30px><div style='font-size:12px;text-align:left;font-weight:normal;padding-left:10px;' valign=middle> </td><td align='left'><input type='button' value='Click to proceed to Step 2' style='margin-right:20px;' class='button' onclick='step1()'></td></tr>";
	content+="</table>"
	content+="</td></tr></table>";
content+="</div>";




//-- Step 2 Personal Details --------
content+="<div id='step2'  style='display:none'>";
	content+="<table cellSpacing=0 cellPadding=5 align=center bgColor=#ffffff  border=0><tr><td  borderColor=#ffffff>";
	content+="<table align=center bgColor=#ffffff collspanning=0 cellspaning=0>";
	content+="<tr><td  align='center' colspan='2' height='25px' background='Images/bggrey.gif'><div class='heading1'>Step 2: &nbsp; Tell us about your companies/your requirements </div></td></tr>";
    content+="<tr><td height=5px> </td></tr>";
	content+="<tr><td height=30px><div class='middleR'> Does the work have a deadline  &nbsp;</div></td><td   valign='top'  align=left><div class='middleL'><select name='Deadline' id='Deadline' class='middleL' onChange=this.style.background='#ffffff'>";
	content+="<option value='Not Set'>Please select</option><option value='asap'>ASAP</option><option value='this week'>This week</option><option value='this month'>This month</option><option value='1 to 3 months'>1 to 3 months</option><option value='3 months +'>3 months +</option></select></td></tr>";
    content+="<tr><td height=5px> </td></tr>";
	content+="<tr><td height=30px><div class='middleR'>Do you have a budget  &nbsp;</div></td><td   valign='top'  align=left><div class='middleL'><select name='Budget' id='Budget' class='middleL' onChange=this.style.background='#ffffff'>";
	content+="<option value='Not Set'>Please select</option><option value='£500 to £1000'>£500 to £1000 </option><option value='£1000 to £2000'>£1000 to £2000</option><option value='£2000 to £3000'>£2000 to £3000</option><option value='£3000 to £4000'>£3000 to £4000</option><option value='£4000 to £5000'>£4000 to £5000</option><option value='£5000+'>£5000+</option></select></td></tr>";


    content+="<tr><td height=20px>&nbsp; </td></tr>";
	content+="<tr><td  valign=middle align=left style='padding-left:10px;'   height=30px><input type='button' value='Back' class='button' onclick='step2back()'> </td><td align='right'><input type='button' value='Click to proceed to Final Step ' style='margin-right:20px;' class='button' onclick='step2()'></td></tr>";

//	content+="<tr><td  colspan=2 height='30px'><div style='background:#f5f5f5;height:30px;line-height:30px;letter-spacing: 0px;font-size:11px;color:#666666;text-align:center;font-weight:normal;'> Privacy Policy: Unless instructed to do so, we will not share your details with any other third parties.</div></td></tr>";
	content+="</table>"
	content+="</td></tr></table>";
content+="</div>";

//-- Final Step Contact Details -----------
content+="<div id='finalstep'  style='display:none;'>";
	content+="<table cellSpacing=0 cellPadding=5 align=center bgColor=#ffffff  border=0><tr><td  borderColor=#ffffff>";
	content+="<table align=center bgColor=#ffffff collspanning=0 cellspaning=0>";
	content+="<tr><td  align='center' colspan='2' height='25px' background='Images/bggrey.gif'><div class='heading1'>Final Step: &nbsp; Contact Details</div></td></tr>";
	content+="<tr><td height='5px'> </td></tr>";
	content+="<tr><td height=30px><div class='middleR'>   First Name &nbsp;</div></td><td  valign='middle' align=left><input name='Firstname' type='text' size='30' style='margin:0;padding:0;font-size:12px;' onChange=this.style.background='#ffffff'></td></tr>";
	content+="<tr><td height=30px><div class='middleR'>   Surname &nbsp;</div></td><td  valign='middle' align=left> <input maxLength=255 size='30' name='Surname' style='margin:0;padding:0;font-size:12px;' onChange=this.style.background='#ffffff'></td></tr>";
	content+="<tr><td height=30px><div class='middleR'>   Job Title &nbsp;</div></td><td  valign='middle' align=left><input name='JobTitle' type='text' size='30' style='margin:0;padding:0;font-size:12px;' onChange=this.style.background='#ffffff'></td></tr>";
	content+="<tr><td height=30px><div class='middleR'>   Company Name &nbsp;</div></td><td  valign='middle' align=left> <input maxLength=255 size='30' name='Company' style='margin:0;padding:0;font-size:12px;' onChange=this.style.background='#ffffff'></td></tr>";
	content+="<tr><td height=30px><div class='middleR'>   Email Address &nbsp;</div></td><td  valign='middle' align=left><input name='EmailAddress' type='text' size='30' style='margin:0;padding:0;font-size:12px;' onChange=this.style.background='#ffffff'></td></tr>";
	content+="<tr><td height=30px><div class='middleR'>  Direct Telephone number &nbsp;</div></td><td  valign='middle' align=left><input name='DirectPhone' type='text'  size='30' style='margin:0;padding:0;font-size:12px;' onChange=this.style.background='#ffffff'></td></tr>";
		content+="<tr><td height=30px><div class='middleR'>  Mobile Telephone number &nbsp;</div></td><td  valign='middle' align=left><input name='MobilePhone' id='MobilePhone' type='text'  size='30' style='margin:0;padding:0;font-size:12px;' onChange=this.style.background='#ffffff'></td></tr>";
		content+="<tr><td height=30px><div class='middleR'>   Address Line 1 &nbsp;</div></td><td  valign='middle' align=left><input name='Address1' id='Address1' type='text' size='30' style='margin:0;padding:0;font-size:12px;' onChange=this.style.background='#ffffff'></td></tr>";

//content+="<tr><td height=30px><div style='font-size:12px;color:#969696;text-align:right;font-weight:normal;padding-left:10px;'>   Address Line 2 &nbsp;</div></td><td  valign='middle' align=left><input name='Address2' type='text' value=''  size='30' style='margin:0;padding:0;font-size:12px;'> </td></tr>";

	content+="<tr><td height=30px><div class='middleR'>   Town &nbsp;</div></td><td  valign='middle' align=left><input name='Town' type='text' value=''  size='30' style='margin:0;padding:0;font-size:12px;' onChange=this.style.background='#ffffff'></td></tr>";
	content+="<tr><td height=30px><div class='middleR'>   County &nbsp;</div></td><td  valign='middle' align=left><input name='County' type='text' value=''  size='30' style='margin:0;padding:0;font-size:12px;' onChange=this.style.background='#ffffff'></td></tr>";
	content+="<tr><td height=30px><div class='middleR'>   Post Code &nbsp;</div></td><td  valign='middle' align=left><input name='Postcode' type='text' value=''  size='30' style='margin:0;padding:0;font-size:12px;' onChange=this.style.background='#ffffff'></td></tr>";
	content+="<tr><td height=30px><div class='middleR'>   Best time to call you &nbsp;</div></td><td  valign='middle' align=left><input name='BestTimeToCall' type='text' size='30' style='margin:0;padding:0;font-size:12px;' onChange=this.style.background='#ffffff'></td></tr>";
    content+="<tr><td height=20px>&nbsp; </td></tr>";
	content+="<tr><td height=30px align='left'><input type='button' value='Back' class='button' onclick='finalstepback()'> </td><td align='right' valign=middle><input type='button' name='askforhelp' id='askforhelp' value='Click for Legal Expert Help' style='margin-right:20px;' class='button' onclick='submit_form()'></td></tr>";
//	content+="<tr><td colspan=2 height='30px'><div style='background:#f5f5f5;height:30px;line-height:30px;letter-spacing: 0px;font-size:11px;color:#666666;text-align:center;font-weight:normal;'>Privacy Policy: Unless instructed to do so, we will not share your details with any other third parties.</div></td></tr>";
	
<!--- REMOVE AFTER TESTING SUBMIT make above button submit------->	
//content+="<tr><td colspan=2 height=30px align='left'><input name='SubmitForm' value='Test Submit Form' class='button' type='button' onclick='submit_form()'></td></tr>";
<!-------------------------------------->	
	content+="</table>";
	content+="</td></tr></table>";

content+="</div>";



content+="</form>";
formdiv.innerHTML=content;
selectedOpt(document.getElementById('landpage').value);



}//end of generate form

var msg = 'Please correct the highlighted items before continuing ...\n';
var field;var alpha = /\w/;
var numeric = /[^0-9^\s]/;var invalid = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
var valid = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
var dateformat1=/^\d{4}\-\d{2}\-\d{2}$/; var dateformat2=/^\d{4}\ \d{2}\ \d{2}$/;
var alphanumeric = /^[a-zA-Z 0-9\-\/\,\n\r\&\.\']+$/;
var elem = document.getElementById(field);
var err1 = false;var err2=false;var errSubmit=false;
var theForm = document.form1;
var paaemail=/^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;


// -------step 1 verification--------
function step1(){
var theForm = document.form1;
err1=false;
var msg = 'Please correct the following to continue ...\n';var field;var alpha = /\w/;var numeric = /[^0-9^\s]/;var invalid = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;var valid = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;

	
if ((theForm.CaseType.value) =="Not Set"){
	field = theForm.CaseType;
	highlite(field);
	msg += '\n\Please select an option from the "I need help with" box';
	err1 = true;
	}
	//else{
	///var butMess="Click to receive\n ";
	//caseID=document.getElementById('CaseType').selectedIndex;
	//switch (caseID)
	//{
	//case 1:
  	//butMess+="Help with selling a Business";
  	//break
	//case 2:
  	//butMess+="Help with buying a Business";
  	//break
	//default:
  	//butMess+=theForm.CaseType.value;
//	}
	
	//theForm.askforhelp.value=butMess;
	//}
	
	
	
	
if (strPresent(theForm.CaseRef.value) && !alphanumeric.test(theForm.CaseRef.value)){
	field = theForm.CaseRef;
	highlite(field);
	msg += '\n\Case Reference contains invalid characters';
	err1 = true;
	}


if (!strPresent(theForm.CaseDesc.value)){
	field = theForm.CaseDesc;
	highlite(field);
	msg += '\n\Please enter a brief Case Description';
	err1 = true;
	}else{

	if (strPresent(theForm.CaseDesc.value) && !alphanumeric.test(theForm.CaseDesc.value)){
		field = theForm.CaseDesc;
		highlite(field);
		msg += '\n\Case Description contains invalid characters';
		err1 = true;
	}
}


	
	if(err1==true)  {
		show("Warning_mess");
		alert(msg);
		field.focus();
		
	}else {
	
	hide("step1");
	show("step2");		
	hide("Warning_mess");
	if(err2==true) {show("Warning_mess");
	}else{
		hide("Warning_mess");}
	
	
	
	}


}
// -------step2 verification--------


function step2(){
var theForm = document.form1;
err2=false;
var msg = 'Please correct the following to continue ...\n';var field;var alpha = /\w/;var numeric = /[^0-9^\s]/;var invalid = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;var valid = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;

if ((theForm.Deadline.value) =="Not Set"){
	field = theForm.Deadline;
	highlite(field);
	msg += '\n\Please select a "Deadline"';
	err2 = true;
	}

if ((theForm.Budget.value) =="Not Set"){
	field = theForm.Budget;
	highlite(field);
	msg += '\n\Please select a "Budget"';
	err2 = true;
	}



if(err2==true)  {
			alert(msg);
			field.focus();
			show("Warning_mess");
		}else {
		hide("step2");
		show("finalstep");
			if(errSubmit==true) {show("Warning_mess");
			}else{
			hide("Warning_mess");
			}
		}
}


function submit_form(){
	errSubmit = false;
	var theForm = document.form1;
	var msg = 'Please correct the highlighted items before continuing ...\n';

			for (var i=0; i< theForm.length; i+=1){
			if (theForm[i].type=="text"){
				//alert(i+"  "+theForm[i].name+" : "+ theForm[i].value);
				theForm[i].value=trimInput(theForm[i].value);
					if(!strPresent(theForm[i].value)){
						field = theForm[i];
						if(field.name!="EmailAddress" && field.name!="MobilePhone" && field.name!="CaseRef" && field.name!="Company" && field.name!="CaseRef" && field.name!="JobTitle"){
							highlite(field);
							msg += '\n\ '+ field.name;
									errSubmit = true;}	   
																   
										   }
								}
			}





if (strPresent(theForm.EmailAddress.value) && (!paaemail.test(theForm.EmailAddress.value))){
	field = theForm.EmailAddress;
	highlite(field);
	msg += '\n\Valid email address (name@domain.com/co.uk etc)';
	errSubmit = true;
		}	




if (checkPostCode(theForm.Postcode.value)){
theForm.Postcode.value=checkPostCode(theForm.Postcode.value);	
}else{
field = theForm.Postcode;
highlite(field);
	msg += '\n\Valid UK Postcode';
	errSubmit = true;
}

if (strPresent(theForm.DirectPhone.value) && !checktelNo(theForm.DirectPhone.value)){
field = theForm.DirectPhone;
highlite(field);
	msg += '\n\Valid Telephone No.';
	errSubmit = true;
}


if (strPresent(theForm.Firstname.value) && !alphanumeric.test(theForm.Firstname.value)){
field = theForm.Firstname;
highlite(field);
	msg += '\n\Firstname';
	errSubmit = true;
}

if (strPresent(theForm.Surname.value) && !alphanumeric.test(theForm.Surname.value)){
field = theForm.Surname;
highlite(field);
	msg += '\n\Surname';
	errSubmit = true;
}

if (strPresent(theForm.JobTitle.value) && !alphanumeric.test(theForm.JobTitle.value)){
field = theForm.JobTitle;
highlite(field);
	msg += '\n\JobTitle';
	errSubmit = true;
}

if (strPresent(theForm.Company.value) && !alphanumeric.test(theForm.Company.value)){
field = theForm.Company;
highlite(field);
	msg += '\n\Company';
	errSubmit = true;
}

if (strPresent(theForm.MobilePhone.value) && !alphanumeric.test(theForm.MobilePhone.value)){
field = theForm.MobilePhone;
highlite(field);
	msg += '\n\MobilePhone';
	errSubmit = true;
}

if (strPresent(theForm.Address1.value) && !alphanumeric.test(theForm.Address1.value)){
field = theForm.Address1;
highlite(field);
	msg += '\n\Address1';
	errSubmit = true;
}

if (strPresent(theForm.Town.value) && !alphanumeric.test(theForm.Town.value)){
field = theForm.Town;
highlite(field);
	msg += '\n\Town';
	errSubmit = true;
}

if (strPresent(theForm.County.value) && !alphanumeric.test(theForm.County.value)){
field = theForm.County;
highlite(field);
	msg += '\n\County';
	errSubmit = true;
}
if (strPresent(theForm.BestTimeToCall.value) && !alphanumeric.test(theForm.BestTimeToCall.value)){
field = theForm.BestTimeToCall;
highlite(field);
	msg += '\n\BestTimeToCall';
	errSubmit = true;
}


		if(errSubmit==true)  {
			show("Warning_mess");
			alert(msg);
			field.focus();
		}else {
		hide("Warning_mess");	
		//alert("Form would have been submitted OK");
		//Strip numbers
		//theForm.DirectPhone.value=stripInput(theForm.DirectPhone.value,"-");
		//theForm.DirectPhone.value=stripInput(theForm.DirectPhone.value," ");
		//theForm.MobilePhone.value=stripInput(theForm.MobilePhone.value,"-");
		//theForm.MobilePhone.value=stripInput(theForm.MobilePhone.value," ");
		if (theForm.EmailAddress.value==""){theForm.EmailAddress.value="no@email.com";}
		//var elem=document.getElementById("merchID").value;
		//document.getElementById("merchID2").value=elem;
		//var elem2=document.getElementById("merchID2").value;
		//alert (elem2);
		theForm.submit();
		}
	
}//end of submit





function finalstepback(){
		hide("finalstep");
		show("step2");
		hide("Warning_mess");
}
function step2back(){
		hide("step2");
		show("step1");
		hide("Warning_mess");
}



//function showltv()
//{
//	var display_text = '';
//	var theForm = document.form1;
//
//	if (document.all || document.layers) {
//		MM_setTextOfLayer(window.ltvdesc,'',display_text);
//
//	} else {
//		MM_setTextOfLayer("ltvdesc",'',display_text);
//	}
//}

function KeyPress(iField,e,max,action) {
	if(document.layers){	if(e.target.value.length >= max) eval(action);}
		else if(document.all){ if(iField.value.length > (max-1)) eval(action);}
			else if(document.getElementById){if(iField.value.length >= max)	eval(action);}
}

function formatnum(amount,X) {
	// rounds number to X decimal places, defaults to 2
	//X = (!X ? 2 : X);
	//return Math.round(amount*Math.pow(10,X))/Math.pow(10,X);
	return Math.ceil(amount)
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	var ck = name+"="+value+expires+"; path=/";
	// if (days != -1) alert('Cookie\n' + ck + '\ncreated');
	document.cookie = ck;
}


function trimInput(sStr)
{
//return str.replace(/^\s*|\s*$/,"");

while (sStr.substring(0,1) == ' ')
{
sStr = sStr.substring(1, sStr.length);
}
while (sStr.substring(sStr.length-1, sStr.length) == ' ')
{
sStr = sStr.substring(0,sStr.length-1);
}
return sStr;
}

function stripInput(Str,sStr){
	sStr=eval("/"+sStr+"/gi");
	Str=Str.replace(sStr, "");
	//tmpStr=tmpStr.replace(/-/gi, "");
	return Str;
	}


function checkNumber(field){

var elem = document.getElementById(field);

if ((isNaN(elem.value)) || (!alpha.test(elem.value))){
	return true;
		}else{
	return false;
		}
}

function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  

  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var validPC = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      validPC = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (validPC) {return postCode;} else return false;
}

function strPresent(toCheck){
	var emptyString = /^\s*$/ ;
	
	if((toCheck!='') || (!emptyString.test(toCheck))){
	return true;
	}else{
	return false;
	}
}

function checktelNo(toCheck){
    var telnr = /^\+?[0-9 ()-]+[0-9]$/  ;
	var tmpStr;
	tmpStr=stripInput(toCheck," ");
	tmpStr=stripInput(tmpStr,"-");
	
	if (telnr.test(toCheck)&& tmpStr.length>3 && tmpStr.length<21) {
	return true;		
	}else{return false;}
}


function highlite(field){
field.style.background="#FFB3B3";
}

function hide(id){
	elem=document.getElementById(id);
    if(elem)
    	elem.style.display='none';
}
function show(id){
    elem=document.getElementById(id);
    if(elem)
    	elem.style.display='block';
}

function selectedOpt(id){
	elem=document.getElementById('CaseType');
	elem.selectedIndex=id;
	}