function contactValidiation(frm){
	//alert(frm);
    var error = "";
    $("#"+frm +" :input").each(function(){
        
        if($(this).attr('type') == "text" && $(this).attr('mandatory') == "true"){
            error += emptyCheck($(this));
        }else if($(this).is('textarea') && $(this).attr('mandatory') == "true"){
            error += emptyCheck($(this));
        }else if($(this).is('select') && $(this).attr('mandatory') == "true"){
            if($(this).val() == 0){
                error += "You didn't select "+ $(this).attr("title") +".\n";
            }
        }else if($(this).attr('type') == "checkbox" && $(this).attr('mandatory') == "true"){
            if(!$(this).is(':checked')){
                error += "You didn't select "+ $(this).attr("title") +".\n";
            }
        }
		 if($(this).attr("title")=='Email'){            
             var tfld = contacttrim($(this).val());
            if (tfld != "") {
               error += validateContactEmail($(this));
             }            
        }
		if($(this).attr("title")=='Telephone Number'){
			
			  var tfld = contacttrim($(this).val());
			 //alert(tfld);
			       if (tfld != "") {
               error += validatephone($(this));
             }            
        }
		
        
		
    });

    if (error == ""){
        return true;
    }
    alert(error);
    return false;
}
function emptyCheck(fld) {
    var error = "";
    var tfld = trim(fld.val());
    if (tfld == "") {
        error = "You didn't enter "+ fld.attr("title") +".\n";
    }
    return error;
}

function trim(s) {
	return s.replace(/^\s+|\s+$/, '');
}

function validateEmpty(fld) {
    var error = "";
    //alert(fld.name);
    if (fld.value.length == 0) {
        //fld.style.background = 'Yellow';
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'none';
    }
    if(fld.type == 'select-one'){
    	if(fld.value == 0){
    		//fld.style.background = 'Yellow';
            error = "The required field has not been filled in.\n"
    	}else{
    		fld.style.background = 'none';
    	}    	
    }
    return error;
}
function contacttrim(s) {
    return s.replace(/^\s+|\s+$/, '');
}

function validateContactEmail(fld) {
    var error = "";
    var tfld = contacttrim(fld.val());
   // var emailFilter = new RegExp('^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$');
    var emailFilter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;

    if (tfld == "") {
        error = "You didn't enter "+ fld.attr("title") +".\n";
    } else if (!emailFilter.test(tfld)) { // test email for
        // illegal characters
        error = "Please enter a valid "+ fld.attr("title") + ".\n";
    } else if (tfld.match(illegalChars)) {
        error = "The " +fld.attr("title") + " contains illegal characters.\n";
    }

    return error;
}


function validateConfirmEmail(fld) {
    var error="";
    var tfld = trimchk(fld.value);                        // value of field with
														// whitespace trimmed
														// off
    // var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    //var emailFilter = new RegExp('^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$');
	var emailFilter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    // var emailFilter =
	// /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
    // var emailFilter = new
	// RegExp('[a-z0-9._%+-]+@(?:[a-z0-9-]+\.)+[a-z]{2,4}');
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (tfld == "") {
        //fld.style.background = 'Yellow';
        error = "You didn't enter an confirm email address.\n";
    } else if (!emailFilter.test(tfld)) {              // test email for
														// illegal characters
        //fld.style.background = 'Yellow';
        error = "Please enter a valid confirm email address.\n";
    } else if (fld.value.match(illegalChars)) {
        //fld.style.background = 'Yellow';
        error = "The confirm email address contains illegal characters.\n";
    } else {
        var st;
    	fld.style.background = 'none';
        
        /*alert(chk);*/
    }
    return error;
}

function validatephone(fld) {
           
	    var error = "";
	   // var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');
		var tfld = contacttrim(fld.val());
			   if (tfld == "") {
	        error = "You didn't enter a phone number.\n";
	        //fld.style.background = 'Yellow';
	    } else if (isNaN(tfld)) {
			
	        error = "The phone number contains illegal characters.\n";
			//return false;
	        //fld.style.background = 'Yellow';
	    } else if (!(tfld.length == 10)) {
	        error = "The phone number is the wrong length.\n";
			//return false;
	        //fld.style.background = 'Yellow';
	    }
	    return error;
	}
