function validateFormOnSubmit(theForm) {
var reason = "";

  if(theForm.county){reason += validateCounty(theForm.county);}
  if(theForm.category){reason += validateCategory(theForm.category);}
  if(theForm.business){reason += validateBusiness(theForm.business);}
  if(theForm.title){reason += validateTitle(theForm.title);}
  if(theForm.email){reason += validateEmail(theForm.email,theForm.confirmemail);}
  if(theForm.phone){reason += validatePhone(theForm.phone);}
  
  if(theForm.phone2){reason += validatePhone2(theForm.phone2);}
  if(theForm.userfile){reason += validatePic(theForm.userfile);}
  if(theForm.userfile1){reason += validatePic1(theForm.userfile1);}
  if(theForm.userfile2){reason += validatePic2(theForm.userfile2);}
  if(theForm.userfile3){reason += validatePic3(theForm.userfile3);}
        
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

    return true;
}
function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'red'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateModel(fld) {
    var error = "";
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    if (fld.value == "") {
        fld.style.background = 'red'; 
        error = "You didn't enter a Model Number.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 40)) {
        fld.style.background = 'red'; 
        error = "The Model Number is the wrong length.\n";
    } else if (fld.value.match(illegalChars)) {
       fld.style.background = 'red';
       error = "The Model Number contains illegal characters.\n";      
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateCounty(fld) {
    var error = "";
 
    if (fld.selectedIndex == "0") {
        fld.style.background = 'red';
        error = "You must select an Area.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCategory(fld) {
    var error = "";
 
    if (fld.selectedIndex == "0") {
        fld.style.background = 'red';
        error = "You must select a Catergory.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateBusiness(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'red'; 
        error = "You must enter a Business name.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateTitle(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'red'; 
        error = "You must enter a Title.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld,fld2) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'red';
        error = "You must enter an Email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'red';
        error = "Please enter a valid Email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'red';
        error = "The email address contains illegal characters.\n";
     } else if (!(fld.value == fld2.value)) {
        fld.style.background = 'red';
        fld2.style.background = 'red';
        error = "The Email and Confirm Email do not match.\n";        
    } else {
        fld.style.background = 'White';
        fld2.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value != "") {
   		 if (isNaN(parseInt(stripped))) {
        	error = "The Phone number contains illegal characters.\n";
        	fld.style.background = 'red';
    	} else if (stripped.length < 9) {
        	error = "The Phone number is too short.\n";
        	fld.style.background = 'red';
    	}	 else if (stripped.length > 12) {
        	error = "The Phone number is too long.\n";
        	fld.style.background = 'red'
        }
    } else {
        fld.style.background = 'White';
    }
    return error;
}


function validatePhone2(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value != "") {
   		 if (isNaN(parseInt(stripped))) {
        	error = "The Phone number contains illegal characters.\n";
        	fld.style.background = 'red';
    	} else if (stripped.length < 9) {
        	error = "The Phone number 2 is too short.\n";
        	fld.style.background = 'red';
    	}	 else if (stripped.length > 12) {
        	error = "The Phone number 2 is too long.\n";
        	fld.style.background = 'red'
        }
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePic1(fld) {
    var error = "";
	var dot = fld.value.lastIndexOf(".");
	var extension = fld.value.substr(dot,fld.value.length);
	var lower = extension.toLowerCase();
		if (fld.value.length != 0) {
	    if (lower != ".jpg") {
        fld.style.background = 'red'; 
         	
        error = "Please upload a jpg file in Main Picture! \n";
        }} else {
        fld.style.background = 'White';
    }
    return error;  
}

function validatePic2(fld) {
    var error = "";
	var dot = fld.value.lastIndexOf(".");
	var extension = fld.value.substr(dot,fld.value.length);
	var lower = extension.toLowerCase();
		if (fld.value.length != 0) {
	    if (lower != ".jpg") {
        fld.style.background = 'red'; 
         	
        error = "Please upload a jpg file in Second Picture! \n";
        }} else {
        fld.style.background = 'White';
    }
    return error;  
}

function validatePic3(fld) {
    var error = "";
	var dot = fld.value.lastIndexOf(".");
	var extension = fld.value.substr(dot,fld.value.length);
	var lower = extension.toLowerCase();
		if (fld.value.length != 0) {
	    if (lower != ".jpg") {
        fld.style.background = 'red'; 
         	
        error = "Please upload a jpg file in Third Picture! \n";
        }} else {
        fld.style.background = 'White';
    }
    return error;  
}

function validatePic(fld) {
    var error = "";
	var dot = fld.value.lastIndexOf(".");
	var extension = fld.value.substr(dot,fld.value.length);
	var lower = extension.toLowerCase();
		if (fld.value.length != 0) {
	    if (lower != ".jpg") {
        fld.style.background = 'red'; 
         	
        error = "Please upload a jpg file in Logo Picture! \n";
        }} else {
        fld.style.background = 'White';
    }
    return error;  
}


function textCounter(field,counter,maxlimit,linecounter) {
	// text width//
	var fieldWidth =  parseInt(field.offsetWidth);
	var charcnt = field.value.length;        

	// trim the extra text
	if (charcnt > maxlimit) { 
		field.value = field.value.substring(0, maxlimit);
	}

	else { 
	// progress bar percentage
	var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
	document.getElementById(counter).style.width =  parseInt((fieldWidth*percentage)/100)+"px";
	document.getElementById(counter).innerHTML=percentage+"%"
	// color correction on style from CCFFF -> CC0000
	setcolor(document.getElementById(counter),percentage,"background-color");
	}
}

function setcolor(obj,percentage,prop){
	obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
}

