
function validateEmpty(fld,error_message) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#F7FFAF'; 
        error = error_message;
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateDropDown(fld,no_expect_val,error_message) {
    var error = "";	
    if (fld.selectedIndex==no_expect_val) {
        fld.style.background = '#F7FFAF'; 
        error = error_message;
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateRadioButton(fld) {
    var error = "";	
    if ( ( fld[0].checked == false )
    && ( fld[1].checked == false ) )
    {
        error="Do you agree with our Terms & Conditions ? \n";
    }

    return error;  
}





function validateCheckbox(fld)  {
    var error = "";	

	var total = 0;
	var max = fld.length;
	for(var idx = 0; idx < max; idx++)
	{
		if(eval("fld[" + idx + "].checked") == true)
		{
			total++;
		}
	}
	
  if ( total == 0 )
    {        		
		error="Please check at least one purpose. \n";
	}
    return error;  
}




function validateUsername(fld) {

    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
 
    if (fld.value == "") {
        fld.style.background = '#F7FFAF'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = '#F7FFAF'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#F7FFAF'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = '#F7FFAF';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = '#F7FFAF';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = '#F7FFAF';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = '#F7FFAF';
    } else {
        fld.style.background = 'White';
    }
   return error;
}   

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    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 = '#F7FFAF';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#F7FFAF';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#F7FFAF';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld,phone_type) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    
	
	if (phone_type=="1") {
		error_not_enter="You didn't enter a phone number.\n";
		error_illegal="The phone number contains illegal characters.\n";
		error_wrong_len="The phone number is the wrong length. Make sure you included an area code.\n";
	}
	else {
		error_not_enter="You didn't enter a mobile phone number.\n";
		error_illegal="The mobile phone number contains illegal characters.\n";
		error_wrong_len="The mobile phone number is the wrong length. Make sure you included an area code.\n";		
	}

   if (fld.value == "") {
        error = error_not_enter;
        fld.style.background = '#F7FFAF';
    } else if (isNaN(parseInt(stripped))) {
        error = error_illegal;
        fld.style.background = '#F7FFAF';
    } 
	
/* 	else if (!(stripped.length <=8)) {
        error = error_wrong_len;
        fld.style.background = '#F7FFAF';
    } */
    return error;
}

