
var styleBackground = '#FFE9E8';
var styleBackgroundNormal = '#FFFFFF';

function ValidateUrl(url) {
	var error = "";
    var v = new RegExp();
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    if (!v.test(url.value)) {
    	url.style.background = styleBackground; 
    	error = "- Inserisci un indirizzo URL valido.\n";
    }else {
        url.style.background = styleBackgroundNormal;
    }
	return error;
} 

function Trim(s) 
{
  // Remove leading spaces and carriage returns
  
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // Remove trailing spaces and carriage returns

  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function isNotNumeric(s)
{
	if(isNaN(s))
	{
		return(true);
	}
	return(false);
}

function echeck(str) 
{
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1)
		{
		   alert("E' stato inserita una e-mail non valida.")
		   return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		{
		    alert("E' stato inserita una e-mail non valida.")
		   return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		{
		     alert("E' stato inserita una e-mail non valida.")
		    return false
		}
		 if (str.indexOf(at,(lat+1))!=-1)
		 {
		     alert("E' stato inserita una e-mail non valida.")
		    return false
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		 {
		     alert("E' stato inserita una e-mail non valida.")
		    return false
		 }
		 if (str.indexOf(dot,(lat+2))==-1)
		 {
		     alert("E' stato inserita una e-mail non valida.")
		    return false
		 }
		 if (str.indexOf(" ")!=-1)
		 {
		     alert("E' stato inserita una e-mail non valida.")
		    return false
		 }
 		 return true					
}

////////validate not selected dropdown

function validateSelect(fld, camponome) {
    var error = "";
  
    if (fld.value == "") {
        fld.style.background = styleBackground; 
        error = "- " + camponome + " risulta non selezionato.\n";
    } else {
        fld.style.background = styleBackgroundNormal;
    }
    return error;   
}




////////validate empty textbox

function validateEmpty(fld, camponome) {
	var error = "";
  	if (fld != null){
		if (Trim(fld.value) == "" ) {
			fld.style.background = styleBackground; 
			error = "- " + camponome + " risulta vuoto.\n";
		} else {
			fld.style.background = styleBackgroundNormal;
		}
	}
    return error;   
}

///////////validate username field

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = styleBackground; 
        error = "- Il nome utente risulta vuoto.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = styleBackground; 
        error = "- Il nome utente deve contenere da 5 a 15 caratteri.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = styleBackground; 
        error = "- Il nome utente contiene dei caratteri non validi.\n";
    } else {
        fld.style.background = styleBackgroundNormal;
    } 
    return error;
}


//////////////validate password field

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (Trim(fld.value) == "") {
        fld.style.background = styleBackground;
        error = "- Non hai inserito la password.\n";
    } else if ((fld.value.length < 6) || (fld.value.length > 14)) {
        error = "- La password deve essere tra 6 e 14 caratteri. \n";
        fld.style.background = styleBackground;
    } else if (illegalChars.test(fld.value)) {
        error = "- La passwrod contiene caratteri non validi.\n";
        fld.style.background = styleBackground;
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "- La password deve contenere almeno una lettera e un numero.\n";
        fld.style.background = styleBackground;
    } else {
        fld.style.background = styleBackgroundNormal;
    }
   return error;
}

function validateRepassword(fld){
    var error = "";
    if (Trim(fld.value) == "") {
        fld.style.background = styleBackground;
        error = "- Ridigita la password.\n";
    }else {
        fld.style.background = styleBackgroundNormal;
    }
   return error;
}
	

///// chech password and confirm password are same

function checkPassword(fld1, fld2)
{	var error = "";

	if(fld1.value != fld2.value)
	{
	error = "- Alcune password sono uguali.\n";
	fld1.style.background = styleBackground;
	fld2.style.background = styleBackground;	
	}
	else
	{
	fld1.style.background = styleBackgroundNormal;
	fld2.style.background = styleBackgroundNormal;
	}
	return error;
}


///////trim the value of the textbox

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

/////////////validate email address, should be entered in the right format ***********Required

function validateEmail(fld, camponome) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (tfld == "") {
        fld.style.background = styleBackground;
        error = "- Il campo " + camponome + " risulta vuoto.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = styleBackground;
        error = "- Indirizzo e-mail non valido.\n";
    } else if (tfld.match(illegalChars)) {
        fld.style.background = styleBackground;
        error = "- L'indirizzo contiene caratteri speciali.\n";
    } else {
        fld.style.background = styleBackgroundNormal;
    }
    return error;
}

/////////////validate email address, should be entered in the right format *********** Not Required

function validateEmailCheck(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
   if ((!emailFilter.test(tfld))&&(tfld!='')) {              //test email for illegal characters
        fld.style.background = styleBackground;
        error = "- Please enter a valid email address.\n";
    } else if (tfld.match(illegalChars)) {
        fld.style.background = styleBackground;
        error = "- The email address contains illegal characters.\n";
    } else {
        fld.style.background = styleBackgroundNormal;
    }
    return error;
}


////////// validate phone number, should contain numbers**************** Required
function validatePhone(fld) {
    var error = "";
    var illegalChars= /[\<\>\;\:\\\"\[\]\@\_\sa-zA-Z]/ ;   

   if  (Trim(fld.value) == "") {
        error = "- " + fld.id + " has not been filled in.\n";
        fld.style.background = styleBackground;
    } else if (fld.value.match(illegalChars)) {
        error = "- " + fld.id + " contains illegal characters.\n";
        fld.style.background = styleBackground;
    } else if (fld.value!="") {
        if (fld.value.length < 7) {
        error = "- " + fld.id + " must contain atleast 7 numbers.\n";
        fld.style.background = styleBackground;
    }
	}
	else {
        fld.style.background = styleBackgroundNormal;
    } 
    return error;
}


////////// validate phone number, should contain numbers**************** Not Required
function validatePhoneCheck(fld) {
    var error = "";
    var illegalChars= /[\<\>\;\:\\\"\[\]\@\_\sa-zA-Z]/ ;   

   if (fld.value.match(illegalChars)) {
        error = "- " + fld.id + " contains illegal characters.\n";
        fld.style.background = styleBackground;
    } else if (fld.value!="") {
        if (fld.value.length < 7) {
        error = "- " + fld.id + " must contain atleast 7 numbers.\n";
        fld.style.background = styleBackground;
    }
	}
	else {
        fld.style.background = styleBackgroundNormal;
    } 
    return error;
}





//////validate numeric field, like Zip Code/************* Required

function validateNumber(fld) {
    var error = "";
	var illegalChars= /[\(\)\<\>\;\$\:\\\"\[\]\@\_\s\-\.a-zA-Z]/ ;
 
   if (Trim(fld.value) == "") {
        error = "- " + fld.id + " has not been filled in.\n";
        fld.style.background = styleBackground;
    } else if (fld.value.match(illegalChars)) {
        error = "- Only numbers are allowed in " + fld.id + ".\n";
        fld.style.background = styleBackground;
    } 
	else {
        fld.style.background = styleBackgroundNormal;
    } 
    return error;
}


//////validate set up fee/ ************* Required

function validateSetupFee(fld) {
    var error = "";
	var illegalChars= /[\(\)\<\>\;\$\:\\\"\[\]\@\_\s\-a-zA-Z]/ ;
 
   if (fld.value == "") {
        error = "- " + fld.id + " has not been filled in.\n";
        fld.style.background = styleBackground;
    } else if (fld.value.match(illegalChars)) {
        error = "- Only numbers are allowed in " + fld.id + ".\n";
        fld.style.background = styleBackground;
    } 
	else {
        fld.style.background = styleBackgroundNormal;
    } 
    return error;
}

//////validate numeric field, like Zip Code, year built, it will contain only numbers/************* Not Required

function validateNumberCheck(fld) {
    var error = "";
	var illegalChars= /[\(\)\<\>\;\:\\\"\[\]\@\_\s\-\.a-zA-Z]/ ;
 
   if (fld.value.match(illegalChars)) {
        error = "- Only numbers are allowed in " + fld.id + ".\n";
        fld.style.background = styleBackground;
    } 
	else {
        fld.style.background = styleBackgroundNormal;
    } 
    return error;
}


//////validate alphabetic field, this field could not contain numbers*/**************** Required  

function validateAlphabet(fld) {
    var error = "";
	var illegalChars= /[\(\)\<\>\;\:\\\"\[\]\@\_\-0-9]/ ;
 
   if (Trim(fld.value) == "") {
        error = "- " + fld.id + " has not been filled in.\n";
        fld.style.background = styleBackground;
    } else if (fld.value.match(illegalChars) && fld.value!="") {
        error = "- Numbers not allowed in " + fld.id + ".\n";
        fld.style.background = styleBackground;
    } 
	else {
        fld.style.background = styleBackgroundNormal;
    } 
    return error;
}


//////validate alphabetic field, this field could not contain numbers*/**************** Not Required  

function validateAlphabetCheck(fld) {
    var error = "";
	var illegalChars= /[\(\)\<\>\;\:\\\"\[\]\@\_\-0-9]/ ;
 
   if (fld.value.match(illegalChars)) {
        error = "- Numbers not allowed in " + fld.id + ".\n";
        fld.style.background = styleBackground;
    } 
	else {
        fld.style.background = styleBackgroundNormal;
    } 
    return error;
}


////////// validate price may contain dot(.) ****Required field
function validatePrice(fld) {
    var error = "";
    var illegalChars= /[\(\)\<\>\;\:\\\"\,\[\]\@\_\s\-a-zA-Z]/ ;

   if (fld.value == "") {
        error = "- " + fld.id + " has not been filled in.\n";
        fld.style.background = styleBackground;
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = styleBackground;
        error = "- The "+ fld.id +" contains illegal characters.\n";
    }  
	else {
        fld.style.background = styleBackgroundNormal;
    } 
    return error;
}



////////// validate price,sq feet may contain comma(,), that fields contain comma(,) like sq feet. ****Not Required field
function validatePriceCheck(fld) {
    var error = "";
    var illegalChars= /[\(\)\<\>\;\:\\\"\[\]\@\_\s\-a-zA-Z]/ ;

   if (fld.value.match(illegalChars)) {
        fld.style.background = styleBackground;
        error = "- The "+ fld.id +" contains illegal characters.\n";
    }  
	else {
        fld.style.background = styleBackgroundNormal;
    } 
    return error;
}

//// validate bedroom, bathroom field, it contain numbers and decimal(.) only. **** Required Field

function validateBedroom(fld) {
     var error = "";
    var illegalChars= /[\(\)\<\>\;\:\\\"\[\]\@\_\-\s\,a-zA-Z]/ ;

   if (fld.value == "") {
        error = "- " + fld.id + " has not been filled in.\n";
        fld.style.background = styleBackground;
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = styleBackground;
        error = "- The "+ fld.id +" contains illegal characters.\n";
    }  
	else {
        fld.style.background = styleBackgroundNormal;
    } 
    return error;
}



//// validate bedroom, bathroom field, it contain numbers and decimal(.) only. **** Not Required Field

function validateBedroomCheck(fld) {
     var error = "";
    var illegalChars= /[\(\)\<\>\;\:\\\"\[\]\@\_\-\s\,a-zA-Z]/ ;

   if (fld.value.match(illegalChars)) {
        fld.style.background = styleBackground;
        error = "- The "+ fld.id +" contains illegal characters.\n";
    }  
	else {
        fld.style.background = styleBackgroundNormal;
    } 
    return error;
}


/////////////// validate Rating field, check field length must be between 1 to 5.

function validateRating(fld) {
     var error = "";
    var illegalChars= /[\(\)\<\>\;\:\\\"\[\]\@\_\-\,a-zA-Z]/ ;

   if (fld.value.match(illegalChars)) {
        fld.style.background = styleBackground;
        error = "- The "+ fld.id +" contains illegal characters.\n";
    }  else if(fld.value!="")
	{
		if ((fld.value < 1) || (fld.value > 5)) {
        error = "- The Rating should be between 1 to 5. \n";
        fld.style.background = styleBackground;
	}
	}
	else {
        fld.style.background = styleBackgroundNormal;
    } 
    return error;
}

////////// validate event date in right  format....




function checkdate(input){
var error = "";
var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
//var returnval=false
if (!validformat.test(input.value))
	{
error = "Invalid Date Format. Please correct and submit again. \n";
input.style.background = styleBackground;
	}
else
   { //Detailed check for valid date ranges
var monthfield=input.value.split("/")[0]
var dayfield=input.value.split("/")[1]
var yearfield=input.value.split("/")[2]
var dayobj = new Date(yearfield, monthfield-1, dayfield)

if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
	{
error = "Invalid Day, Month, or Year range detected. Please correct and submit again. \n";
input.style.background = styleBackground;
	}
	else
	{
	input.style.background = styleBackgroundNormal;
	}
   }
return error;
}

function matchpassword(val1,val2)
{
	var error="";
	
	if(val1.value!=val2.value)
	{
	 error = "- Password is not matched.\n";	
	 val1.style.background = styleBackground;
	 val2.style.background = styleBackground;
	}
	
	return error;
}

//// validate subdomain name
function validateSubdomain(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = styleBackground;
        error = "- Please enter subdomain.\n";
    } else if ((fld.value.length < 4) || (fld.value.length > 14)) {
        error = "- The subdomain must contain 4-14 characters. \n";
        fld.style.background = styleBackground;
    } else if (illegalChars.test(fld.value)) {
        error = "- The subdomain contains illegal characters.\n";
        fld.style.background = styleBackground;
    } else {
        fld.style.background = styleBackgroundNormal;
    }
   return error;
} 