
function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'White'; 
        error = "Enter your username";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = 'White'; 
        error = "The username should be minimum 5 and maximum 15 characters";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'White'; 
        error = "The username contains illegal characters";
    } 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 = 'White';
        error = "Enter Your password<br>";
    } else if ((fld.value.length < 3) || (fld.value.length > 12)) {
        error = "The password length should be between 3 and 12 characters";
        fld.style.background = 'White';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters";
        fld.style.background = 'White';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral";
        fld.style.background = 'White';
    } else {
        fld.style.background = 'White';
    }
   return error;
}   

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

function isEmpty(fld,message){
     var error="";
     var name = trim(fld.value);
     if(name==''){
         fld.style.background = 'White';
          error = message+"\n";
     }else{
         fld.style.background = 'White';
     }
     return error;
}

function isMax(fld,message){
     var error="";
     var name = trim(fld.value);
     if(name==''){
         fld.style.background = 'White';
          error = message+"\n";
     }if(name.length<15){
          fld.style.background = 'White';
          error = "Enter atleast 5 words!\n";
     }else{
         fld.style.background = 'White';
     }
     return error;
}

function home_category(URL,short_name){
    if(short_name!=''){
         window.location = URL+'/'+short_name;
    }else{
         return false;
    }
}

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 = 'White';
        error = "Enter your email address\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'White';
        error = "Please enter a valid email address\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'White';
        error = "Please enter a valid email address\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    
	var phon=/^[0-9]*$/;
   if (fld.value == "") {
        error = "Enter your phone number<br>";
        fld.style.background = 'White';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters";
        fld.style.background = 'White';
    } else if (!(stripped.length > 5 )) {
        error = "The phone number is the wrong length";
        fld.style.background = 'White';
    }else if(!phon.test(fld.value)){
		 error = "The phone number contains illegal characters";
		  fld.style.background = 'White';
	}
    return error;
}

function validatecountrycode(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    
	var phon=/^[0-9]*$/;
   if (fld.value == "") {
        error = "Enter your country code<br>";
        fld.style.background = 'White';
    }
    return error;
}

function validateareacode(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    
	var phon=/^[0-9]*$/;
   if (fld.value == "") {
        error = "Enter your Area code<br>";
        fld.style.background = 'White';
    }
    return error;
}

function checkphonevalidity(str)
{
	 var error = "";
var phon=/^[0-9]*$/;
if(!phon.test(str))
 error = "The phone number contains illegal characters";
 return error;
}
