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

  reason += validateEmpty(theForm.apiname);
  reason += validateURL(theForm.apiurl);
  reason += validateEmail(theForm.apiemail);
  reason += validateEmpty(theForm.apidesc);
  reason += validateCheckbox(theForm.agree);
  
    
 
  if (reason != "") {
    //alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validatePostForm(theForm) {
var reason = "";
 // reason += validateUsername(theForm.username);
 // reason += validatePassword(theForm.pwd);
 // reason += validateEmail(theForm.email);
 // reason += validatePhone(theForm.phone);
  reason += validateEmpty(theForm.empName);
  reason += validateEmpty(theForm.empWebSite);
  reason += validateEmail(theForm.empEmail);
//  reason += validateSelect(theForm.jobType);
//  reason += validateSelect(theForm.jobCategory);
  reason += validateEmpty(theForm.jobLocation);
  reason += validateEmpty(theForm.jobTitle);
  reason += validateEmpty(theForm.jobDescription);
  reason += validateEmpty(theForm.jobApply);
  reason += validateCheckbox(theForm.toc);

  //validate stupid radio inputs- job type
  if($('fulltime').checked != true && $('freelance').checked != true && $('internship').checked != true){
  		//set the labels to error class
	    $('fulltimeLabel').className = 'error';   
	    $('freelanceLabel').className = 'error';   
	    //show error msg
		$('jobTypeError').show();  
		//add something to the error counter (reason) to stop the form from submitting
		reason += "jobType";
  } else {
  		//reset any error classes/text  
	    $('fulltimeLabel').className = 'radio';   
	    $('freelanceLabel').className = 'radio';   
	    $('internshipLabel').className = 'radio';   
        $('jobTypeError').hide();  
  }

  //validate stupid radio inputs- job category
  if($('Developer').checked != true && $('Design').checked != true && $('Support').checked != true){
  		//set the labels to error class  
	    $('DeveloperLabel').className = 'error';   
	    $('DesignLabel').className = 'error';   
	    $('SupportLabel').className = 'error';	    
	    //show error msg
		$('jobCategoryError').show();  
		//add something to the error counter (reason) to stop the form from submitting		
		reason += "jobType";
  } else {
  		//reset any error classes/text
	    $('DeveloperLabel').className = 'radio';   
	    $('DesignLabel').className = 'radio';   
	    $('SupportLabel').className = 'radio';	  
        $('jobCategoryError').hide();  
  }
    
  //if the word count totals are greater then limit
  var jobDescTotal  = $('dtotal').innerHTML;
  var jobApplyTotal = $('atotal').innerHTML; 
  if(jobDescTotal > 400){  
  	$('jobDescription').className = 'error'; 
  	$('dtotal').className = 'error';
  	$('jobDescriptionWordCountError').show();
  	reason += 'job description';
  } else {
  	if(!reason){
	  	$('jobDescription').className = ''; 
  		$('dtotal').className = '';  
	  	$('jobDescriptionWordCountError').hide();  	
	}
  }
  if(jobApplyTotal > 30){ 
  	$('jobApply').className = 'error';   
  	$('atotal').className = 'error';
  	$('jobApplyWordCountError').show();
  	reason += 'job apply';  	  	
  } else {
	if(!reason){
	  	$('jobApply').className = '';   
	  	$('atotal').className = '';  
	  	$('jobApplyWordCountError').hide();  	
	}  	  	
  }
    
//  if($('dtotal').innerHTML)

      
  if (reason != "") {
    //alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}


function validateEmpty(fld) {
    var error = "";
	var id = fld.id.toString();

    if (fld.value.length == 0) {
	    	
        //fld.style.background = 'Yellow'; 
        //error = "The required field has not been filled in.\n"
        fld.className = 'error';   
		$(id+'Error').show();  
        error = fld.id;

    } else {
        //fld.style.background = 'White';
        fld.className = 'text'; 
        $(id+'Error').hide();  
    }
    return error;   
}


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

function validateEmail(fld) {
    var error="";
	var id = fld.id.toString();
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.className = 'error';   
		$(id+'Error').show();  
        error = fld.id;

    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.className = 'error';   
		$(id+'Error').show();  
        error = fld.id;	

    } else if (fld.value.match(illegalChars)) {
        fld.className = 'error';   
		$(id+'Error').show();  
        error = fld.id;
        
    } else {
        fld.className = 'text'; 
        $(id+'Error').hide(); 
    }
    return error;
}

function validateSelect(fld){
	var error="";
	var value = fld.value;
	var id = fld.id.toString();

	if(value == '0'){
        fld.className = 'error';   
		$(id+'Error').show();  
        error = fld.id;		
	} else {
        fld.className = ''; 
        $(id+'Error').hide();	
	}
	

	return error;


}

function validateCheckbox(fld){
	var error="";
	var value = fld.value;
	var id = fld.id.toString();
	
	if(value != "on"){
        //fld.className = 'error';   
		$(id+'Label').className = 'error';   
        error = fld.id;	

   	} else {
        $(id+'Label').className = '';   
        //$(id+'Error').hide();	
	}

	return error;

}

function validateRadio(fld){
	var error="";
	var value = fld.value;
	var id = fld.id.toString();

/*
	if(value != "on"){
        fld.className = 'error';   
		$(id+'Error').show();  
        error = fld.id;	

   	} else {
        fld.className = ''; 
        $(id+'Error').hide();	
	}

	return error;
*/

}


function validateURL(fld){ //need to finish this one
    var error = "";

	var id = fld.id.toString();

    if (fld.value.length == 0) {
        //fld.style.background = 'Yellow'; 
        //error = "The required field has not been filled in.\n"
        fld.className = 'error';   
		$(id+'Error').show();  
        error = fld.id;

    } else {
        //fld.style.background = 'White';
        fld.className = ''; 
        $(id+'Error').hide();  
    }
    return error;  
}
