var digits = '0123456789';
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
var bracket=3
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function validateform(theform){
var errors = 0;

  if(theform.fname){
      if(notempty(theform.fname.value)){
         theform.fname.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
      errors = errors + 1;
         theform.fname.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }

  if(theform.county){
      if(notempty(theform.county.value)){
         /*theform.county.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; */
      }else{
      errors = errors + 1;
         /*theform.county.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; */
      }
  }

  if(theform.city){
      if(notempty(theform.city.value)){
         /*theform.city.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; */
      }else{
      errors = errors + 1;
         /*theform.city.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; */
      }
  }


  if(theform.add){
      if(notempty(theform.add.value)){
         theform.add.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
      errors = errors + 1;
         theform.add.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }
  
  if(theform.add2){
  
  }
  
  if(theform.add3){
  
  }
  
  if(theform.county){
      if(notempty(theform.county.value)){
         /*theform.county.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; */
      }else{
      errors = errors + 1;
         /*theform.county.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; */
      }  
  }
  
  if(theform.city){
      if(notempty(theform.city.value)){
         /*theform.city.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; */
      }else{
      errors = errors + 1;
         /*theform.city.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; */
      }
  }
  
  if(theform.postcode){

//      if(get_pcode()){
    if(notempty(theform.geo.value)){
        theform.postcode.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
        errors = errors + 1;
        theform.postcode.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }

  if(theform.postcodep){
      if(notempty(theform.geop.value)){
        theform.postcodep.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
        errors = errors + 1;
        theform.postcodep.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }
  
  if(theform.username){
        if(notempty(theform.username.value)){
        theform.username.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
        errors = errors + 1;
        theform.username.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }

  if(theform.password){
          if(notempty(theform.password.value)){
        theform.password.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
        errors = errors + 1;
        theform.password.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }

  if(theform.password2){
      if( (theform.password.value == theform.password2.value) && (theform.password.value.length > 3) ){
        theform.password2.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
        errors = errors + 1;
        theform.password2.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }


  if(theform.phonenum){
          if(notempty(theform.phonenum.value)){
          if (checkInternationalPhone(theform.phonenum.value))
              {
                  theform.phonenum.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent';
              } else {
				  errors = errors + 1;
                  theform.phonenum.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent';
              }
      }else{
        errors = errors + 1;
        theform.phonenum.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }

  if(theform.email){
      if(checkMail(theform.email.value)){
        theform.email.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
        errors = errors + 1;
        theform.email.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }

  if(theform.website){
  
  }
  
  if(theform.fax){
  
  }

  if(theform.payment){
  
  }




 if( (theform.mortgages)&&(theform.investments)&&(theform.insurance)&&(theform.pensions)&&(theform.loans)&&(theform.tax)&&(document.getElementById('servicetick')) ){
    var tickedcount = 0;
      if( theform.mortgages.checked ){
        tickedcount++;
      }
      if( theform.investments.checked  ){
        tickedcount++;
      }
      if( theform.insurance.checked ){
        tickedcount++;
      }
      if( theform.pensions.checked ){
        tickedcount++;
      }
      if( theform.loans.checked ){
        tickedcount++;
      }
      if( theform.tax.checked ){
        tickedcount++;
      }
    
    if(tickedcount < 1){
      errors = errors + 1;
      document.getElementById('servicetick').innerHTML = '<img src="/js/cross.png" /> Please select at least one service.'; 
      //alert('select a service');
    }else{
      //alert('sevice ok');
      document.getElementById('servicetick').innerHTML = '';    
    }

  }




  if(theform.summary){
      if(CountWords(theform.summary.value,20,40)){
        document.getElementById('summaryval').src = '/js/tick.png';
        document.getElementById('summarycount').innerHTML= '';
      }else{
        errors = errors + 1;
        document.getElementById('summaryval').src = '/js/cross.png';
        document.getElementById('summarycount').innerHTML= returnWords(theform.summary.value,20,30);
      }
  }  

  if(theform.shortdescription){
      if(CountWords(theform.shortdescription.value,20,120)){
        document.getElementById('shortval').src = '/js/tick.png';
        document.getElementById('shortcount').innerHTML= '';
      }else{
        errors = errors + 1;
        document.getElementById('shortval').src = '/js/cross.png';
        document.getElementById('shortcount').innerHTML= returnWords(theform.shortdescription.value,20,100);
      }  
  }

  if(theform.fulldescription){
        if(CountWords(theform.fulldescription.value,100,1200)){
        document.getElementById('fullval').src = '/js/tick.png';
        document.getElementById('fullcount').innerHTML='';
      }else{
        errors = errors + 1;
        document.getElementById('fullval').src = '/js/cross.png';
        document.getElementById('fullcount').innerHTML= returnWords(theform.fulldescription.value,100,1000);
      }
  }  
  
  
  if(errors == 0){
    theform.submit();
  }else{
    alert('Please check your information');
  }

}
///////////////////////////////////

function valEl(theform,elm){

  if(elm == 'fname'){
      if(notempty(theform.fname.value)){
         theform.fname.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
         theform.fname.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }

  if(elm == 'county'){
      if(notempty(theform.county.value)){
         theform.county.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
         theform.county.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }

  if(elm == 'city'){
      if(notempty(theform.city.value)){
         theform.city.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
         theform.city.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }


  if(elm == 'add'){
      if(notempty(theform.add.value)){
         theform.add.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
         theform.add.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }
  
  if(elm == 'add2'){
  
  }
  
  if(elm == 'theform.add3'){
  
  }
  
  if(elm == 'county'){
      if(notempty(theform.county.value)){
         theform.county.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
         theform.county.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }  
  }
  
  if(elm == 'city'){
      if(notempty(theform.city.value)){
         theform.city.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
         theform.city.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }
  
  if(elm == 'postcode'){
      get_pcode();
      if(notempty(theform.geo.value)){
      //alert(theform.geo.value);
        theform.postcode.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
        theform.postcode.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }

  if(elm == 'postcodep'){
      if(notempty(theform.geop.value)){
        theform.postcodep.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
        theform.postcodep.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }
  
  if(elm == 'username'){
        if(notempty(theform.username.value)){
        theform.username.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
        theform.username.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }

  if(elm == 'password'){
      if(notempty(theform.password.value)){
        theform.password.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
        theform.password.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }

  if(elm == 'password2'){
      if( (theform.password.value == theform.password2.value) && (theform.password.value.length > 3) ){
        theform.password2.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
        theform.password2.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }


  if(elm == 'phonenum'){
      if(notempty(theform.phonenum.value)){
          if (checkInternationalPhone(theform.phonenum.value))
              {
                  theform.phonenum.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent';
              } else {
                  theform.phonenum.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
              }
      }else{
        theform.phonenum.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }

  if(elm == 'email'){
      if(checkMail(theform.email.value)){
        theform.email.style.background = 'url("../i/input_box_tick.jpg") no-repeat scroll 0 0 transparent'; 
      }else{
        theform.email.style.background = 'url("../i/input_box_cross.jpg") no-repeat scroll 0 0 transparent'; 
      }
  }

  if(elm == 'website'){
  
  }
  
  if(elm == 'fax'){
  
  }

  if(elm == 'payment'){
  
  }

  if(elm == 'summary'){
      if(CountWords(theform.summary.value,20,30)){
        document.getElementById('summaryval').src = '/js/tick.png';
        document.getElementById('summarycount').innerHTML= '';
      }else{
        document.getElementById('summaryval').src = '/js/cross.png';
        document.getElementById('summarycount').innerHTML= returnWords(theform.summary.value,20,30);
      }
  }  

  if(elm == 'shortdescription'){
      if(CountWords(theform.shortdescription.value,20,100)){
        document.getElementById('shortval').src = '/js/tick.png';
        document.getElementById('shortcount').innerHTML= '';
      }else{
        document.getElementById('shortval').src = '/js/cross.png';
        document.getElementById('shortcount').innerHTML= returnWords(theform.shortdescription.value,20,100);
      }  
  }

  if(elm == 'fulldescription'){
     if(CountWords(theform.fulldescription.value,100,1000)){
        document.getElementById('fullval').src = '/js/tick.png';
        document.getElementById('fullcount').innerHTML='';
      }else{
        document.getElementById('fullval').src = '/js/cross.png';
        document.getElementById('fullcount').innerHTML= returnWords(theform.fulldescription.value,100,1000);
      }
  }  
}

//////////////////////////////////

function notempty(val){  if(val == ''){    return false;  }else{    return true;  }  }

function checkNum(fieldval){
var NumberToTest = fieldval.split(' ').join('');
var IsFound = /^-?\d+$/.test(NumberToTest);
  if((IsFound) && (NumberToTest.length >= 7) && (NumberToTest.length < 12)){
    return false;
  }else{
    return true;
  }
}

function checkMail(value){	var x = value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) 
  return true;
	else return false;
  }
  
  function CountWords (value,min,max) {
      var char_count = value.length;
      var fullStr = value + " ";
      var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
      var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
      var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
      var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
      var splitString = cleanedStr.split(" ");
      var word_count = splitString.length -1;
        if (fullStr.length <2) {
          word_count = 0;
        }

      if( ( word_count >= min ) && ( word_count <= max ) ){
  //    if(  word_count >= min ){
        return true;
      }else{
        return false;
      }
//    	document.getElementById('chars').innerHTML = "Charactors Count:\n" + "    " + char_count +  "\n";
  }
  
    function returnWords (value,min,max) {
      var char_count = value.length;
      var fullStr = value + " ";
      var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
      var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
      var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
      var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
      var splitString = cleanedStr.split(" ");
      var word_count = splitString.length -1;
      if (fullStr.length <2) {
      word_count = 0;
    }
    
    if(  word_count > max  ){
      return '' + (word_count -  max) + ' words too many.';
    }
    
    if( ( word_count >= min ) && ( word_count <= max ) ){
      return 'Ok';  
    }

     if( word_count < min ){
      return '' + (min - word_count) + ' words to go.';
    }
  }


//////////////////////////
/// hide rdr compliant

function comliantRDR(show){
  if(show == 1){
    document.getElementById('rdrcomp').style.display = 'block';
  }
  
  if(show == 0){
    document.getElementById('rdrcomp').style.display = 'none';
  }
}

