
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// the following functions are part of the ajax used to display and populate the selection lists on the business form.
// GetXmlHttpObject()  -- used for all ajax parts..
// showDivision(str)  and stateChangedDivision()  -- act as a pair and refer php file that produces the code and a division in the form that will display on selection..
// showGroup(str)  and stateChangedGroup)
// showClass(str)  and stateChangedClass()

var xmlHttp;
////////////////////////////////////////////////
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
///////////////////////////////////////////////
// find and show divisions.. /////////////////////////////////////////////////////////
function showDivision(str)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
var url="ajax/get-division-ajax.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChangedDivision;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChangedDivision() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("show_divisions").innerHTML=xmlHttp.responseText;
 } 
}
// end of   //////////////////////////////////////////////////////////////////////////

// find and show groups.. /////////////////////////////////////////////////////////
function showGroup(str)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
var url="ajax/get-group-ajax.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChangedGroup;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChangedGroup() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("show_groups").innerHTML=xmlHttp.responseText;
 } 
}
// end of   ///////////////////////////////////////////////////////////////////////

// find and show classes.. /////////////////////////////////////////////////////////
function showClass(str)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
var url="ajax/get-class-ajax.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChangedClass;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChangedClass() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("show_classes").innerHTML=xmlHttp.responseText;
 } 
}
// end of   //////////////////////////////////////////////////////////////////////////

// find and show classification confirmation.. /////////////////////////////////////////////////////////
function showClassification(str)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
var url="ajax/get-classification-ajax.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChangedClassification;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChangedClassification() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("show_classification").innerHTML=xmlHttp.responseText;
 } 
}
// end of   //////////////////////////////////////////////////////////////////////////

function verifyMe(state){
var msg='';
// state 1=add, 2=edit, 3=admin edit.

if(document.getElementById('BusinessName').value==''){
	msg+='- Business Name\n\n';}

if(document.getElementById('BusinessAdd1').value==''){
	msg+='- Address 1\n\n';}

if(document.getElementById('BusinessTown').value==''){
	msg+='- Town\n\n';}

if(document.getElementById('BusinessPC').value==''){
	msg+='- Post Code\n\n';}

//if(document.getElementById('BusinessLocation').value==''){
//	msg+='- Location\n\n';}

if(document.getElementById('BusinessTel').value==''){
	msg+='- Telephone\n\n';}

if (state!=3) 	{
if(document.getElementById('BusinessEmail').value==''){
	msg+='- email\n\n';}
				}
				
//if(document.getElementById('BusinessWWW').value==''){
//	msg+='- www\n\n';}

if(document.getElementById('BusinessContact').value==''){
	msg+='- Contact Name\n\n';}

if(document.getElementById('BusinessEmployed').value==''){
	msg+='- Total Employed\n\n';}

if(document.getElementById('BusinessEstablishedYear').value==''){
	msg+='- Year Established\n\n';}

if(document.getElementById('BusinessDescription').value==''){
	msg+='- Description\n\n';}

if(document.getElementById('BusinessSearchWords').value==''){
	msg+='- Search Words\n\n';}
	
if(document.getElementById('BusinessSIC').value==''){
	msg+='- Business Classification Code\n\n';}
	
//if(document.getElementById('BusinessDescription').value.length > 155){
//	msg+='- Business Description too long, limited to 150 characters\n\n';}
	
//if(document.getElementById('BusinessSearchWords').value.length > 155){
//	msg+='- Search Words too long, limited to 150 characters\n\n';}
	
if (state==1) 	{ 	
if(document.getElementById('BusinessAcceptTerms').checked == 0){
	msg+='- Please tick the box to accept our terms\n\n';}
				}
if ((state==2) || (state==3))	{
if(document.getElementById('BusinessKey').value==''){
	msg+='- Business Key Code\n\n';}
								}

if(msg!=''){
	alert('The following fields are empty or invalid:\n\n'+msg);
	return false
}else{
	return true }

}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
function verifyMePassword(){
var msg='';
	
if(document.getElementById('BusinessPassword1').value==''){
	msg+='- Business Password\n\n';}

if(document.getElementById('BusinessPassword1').value.length < 6){
	msg+='- Business Password too short\n\n';}
	
if(document.getElementById('BusinessPassword1').value.length > 20){
	msg+='- Business Password too long\n\n';}
	
if(document.getElementById('BusinessPassword2').value==''){
	msg+='- Business Password Repeat\n\n';}
	
if((document.getElementById('BusinessPassword1').value)!=(document.getElementById('BusinessPassword2').value)){
	msg+='- Business Password and Repeat do not match\n\n';}

if(msg!=''){
	alert('The following fields are empty or invalid:\n\n'+msg);
	return false
}else{
	return true }

}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function limitChars(textarea, limit, infodiv)

{
var text = textarea.value; 
var textlength = text.length;
var info = document.getElementById(infodiv);

if(textlength > limit)

	{
	info.innerHTML = 'You cannot write more then '+limit+' characters!';
	textarea.value = text.substr(0,limit);
	return false;
	}
else
	{
	info.innerHTML = 'You have '+ (limit - textlength) +' characters left.';
	return true;
	}
}

function popWindowURL(URL)
{
// apply as follows... <a href="javascript:popWindowURL('".$maplink."')">
	features = 'width=800,height=600,toolbar=no,location=no,directories=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no';
	pop = window.open(URL,"window",features);
	if(pop.focus){ pop.focus(); }			
}

function popWindowURL2(URL)
{
// apply as follows... <a href="javascript:popWindowURL2('".$maplink."')">
	features = 'width=1000,height=700,toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes';
	pop = window.open(URL,"window",features);
	if(pop.focus){ pop.focus(); }			
}

function popCloseRefresh()
{
//alert("Settings have been saved successfully.");

opener.history.go(0);
opener.focus();
self.close();
}

function popClose()
{
//alert("Settings have been saved successfully.");
//opener.history.go(0);
opener.focus();
self.close();
}


function postit(){ //check postcode format is valid
 test = document.getElementById('BusinessPC').value; size = test.length
 test = test.toUpperCase(); //Change to uppercase
 while (test.slice(0,1) == " ") //Strip leading spaces
  {test = test.substr(1,size-1);size = test.length
  }
 while(test.slice(size-1,size)== " ") //Strip trailing spaces
  {test = test.substr(0,size-1);size = test.length
  }
 document.getElementById('BusinessPC').value = test; //write back to form field
 if (size < 6 || size > 8){ //Code length rule
  alert(test + " is not a valid postcode - wrong length");
  document.getElementById('BusinessPC').focus();
  return false;
  }
 if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
   alert(test + " is not a valid postcode - cannot start with a number");
   document.getElementById('BusinessPC').focus();
   return false;
  }
 if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
   alert(test + " is not a valid postcode - alpha character in wrong position");
   document.getElementById('BusinessPC').focus();
   return false;
  }
 if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
   alert(test + " is not a valid postcode - number in wrong position");
   document.getElementById('BusinessPC').focus();
   return false;
  }
 if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
   alert(test + " is not a valid postcode - number in wrong position");
   document.getElementById('BusinessPC').focus();
   return false;
  }
 if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
   alert(test + " is not a valid postcode - no space or space in wrong position");
   document.getElementById('BusinessPC').focus();
   return false;
   }
 count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
 if (count1 != count2){//only one space rule
   alert(test + " is not a valid postcode - only one space allowed");
   document.getElementById('BusinessPC').focus();
   return false;
  }
//alert("Postcode Format OK");
return true;
}
//  End -->