
var user_valid =/^[a-zA-Z0-9_@.]+$/;
var pass_valid =/^[a-zA-Z0-9]+$/;
var name_exp=/^([A-Za-z]+[A-Za-z. ]+)$/;
var design_exp=/^([A-Za-z]+[A-Za-z. ]+)$/;
var pattern = /[^a-zA-Z0-9,.\-;\_\s]/;
function trim_ws(str)
{
        temp=str.replace(/^\s*/, "");
        temp=temp.replace(/\s*$/,"");
        return temp;
}
function rb_checkUser(user)
{
        if (user.search(/^[a-zA-Z0-9_@.]+$/) == -1)
                return false;
        else
                return true;
}
function rb_checkPassword(pass)
{       if (pass.search(/^[a-zA-Z0-9]+$/) == -1)
                return false;
        else
                return true;
}
//Description: Name Validates Function.
function rb_checkName(str)
{       if(!name_exp.test(str))
                return false;
        else
                return true;
}

//Description: Validates the email field.
function rb_checkEmail(email)
{       if (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1)
                return false;
        else
                return true;
}
//Description: Validates the Website feild
function isValidURL(url) {
	var urlRegxp = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/;
	if (urlRegxp.test(url) != true) {
		return false;
	} else {
		return true;
	}
}
//Description: Validates the Phone field.
function rb_checkPhone(phone)
{       if (phone.search(/^[0-9]+[0-9,\ -]*[0-9]$/) == -1)
                return false;
        else
                return true;
}

function ValidateClient()
{	  //Description: Validates the Email Id field.	
        var msg='';
		var error=0;
        var user=trim_ws(document.getElementById("username").value);
		if(user == '')
        {       msg+='Username is compulsary.<br>';
				document.getElementById("username").style.background="#FFDFEF";
			    error=error+1;
		}
        else if(!rb_checkUser(user))
        {       msg+="'"+user+"' is not a valid UserName.Please enter a valid UserName.<br>";
				document.getElementById("username").style.background="#FFDFEF";
				error=error+1;
        }
		else if( (user.length<8) || (user.length>50) )
        {       msg+='Username should be between 8 - 50 characters long.<br>';
				document.getElementById("username").style.background="#FFDFEF";
			    error=error+1;
        }
	    else
		{		document.getElementById("username").style.background="#FFFFFF";
		}
		//Description: Validates the Password field.	
		var pass=trim_ws(document.getElementById("userpass").value);
		var cpass=trim_ws(document.getElementById("compass").value);
	    if(pass == '')
        {       msg+='Password filled cant be left blank.<br>';
				document.getElementById("userpass").style.background="#FFDFEF";
			    error=error+1;
        }
        else if(!rb_checkPassword(pass))
        {       msg+="'"+pass+"' is not a valid Password.Please enter a valid Password.<br>";
				document.getElementById("userpass").style.background="#FFDFEF";
				error=error+1;
        }
		else if( ( pass.length < 8) || (pass.length > 24) ) 
        {       msg+='Password  should be between 8 - 24 characters long.<br>';
				document.getElementById('userpass').style.background="#FFDFEF";
				error=error+1;
        }
		else
		{		document.getElementById("userpass").style.background="#FFFFFF";
		}
		//Description: Validates the Confirm Password field.	
		if(cpass == '')
        {       msg+='Please enter a Confirm Password.<br>';
				document.getElementById("compass").style.background="#FFDFEF";
			    error=error+1;
        }
        else if(!rb_checkPassword(cpass))
        {       msg+="'"+cpass+"' is not a valid confirm password.Please enter a valid Confirm Password.<br>";
				document.getElementById("compass").style.background="#FFDFEF";
				error=error+1;
        }
	    else if( (cpass.length < 8) || (cpass.length > 24) )
        {       msg+='Confirm password  should be between 8 - 24 characters long.<br>';
				document.getElementById('compass').style.background="#FFDFEF";
				error=error+1;
        }
		else if(pass != cpass)
		{	    msg+='Password and Confirm Password should match.<br>';
				document.getElementById("userpass").style.background="#FFDFEF";
				document.getElementById("compass").style.background="#FFDFEF";
				error=error+1;
		}
		else
		{		document.getElementById("userpass").style.background="#FFFFFF";
				document.getElementById("compass").style.background="#FFFFFF";
		}
		var ctperson=trim_ws(document.getElementById("contperson").value);
		if(ctperson == '')
        {       msg+='Please enter a Contact person name.<br>';
				document.getElementById("contperson").style.background="#FFDFEF";
			    error=error+1;
        }
		else if(!rb_checkName(ctperson)) 
        {       msg+='Please enter a valid Contact person name.<br>';
			    document.getElementById("contperson").style.background="#FFDFEF";
                error=error+1;
        }
		else
		{		document.getElementById("contperson").style.background="#FFFFFF";			
		}
	    var email=trim_ws(document.getElementById("email_id").value);
        if(email == '')
        {	    msg+='Please enter your Primary Email.<br>';
				document.getElementById("email_id").style.background="#FFDFEF";
 				error=error+1;
        }
        else if(!rb_checkEmail(email))
        {       msg+="'"+email+"' is not a valid Primary Email Id.Please enter a valid Primary Email Id<br>";
  			    document.getElementById("email_id").style.background="#FFDFEF";
                error=error+1;
        }
		else
		{		document.getElementById("email_id").style.background="#FFFFFF";
		}	
		 var sec_email=trim_ws(document.getElementById("sec_email").value);
        if(sec_email != '')
        {	 if(!rb_checkEmail(sec_email))
			{       msg+="'"+sec_email+"' is not a valid Secondary  Email Id.Please enter a valid Secondary Email Id<br>";
					document.getElementById("sec_email").style.background="#FFDFEF";
					error=error+1;
			}
			else
			{		document.getElementById("sec_email").style.background="#FFFFFF";
			}
		}
		var country=trim_ws(document.getElementById("country").value);
		if(country == 'none')
        {       msg+='Please select a country.<br>';
				document.getElementById("country").style.background="#FFDFEF";
			    error=error+1;
        }
		else
		{		document.getElementById("country").style.background="#FFFFFF";			
		}
		var state=trim_ws(document.getElementById("State").value);
		if(state == '')
        {       msg+='Please select a State.<br>';
				document.getElementById("State").style.background="#FFDFEF";
			    error=error+1;
        }
		else
		{		document.getElementById("State").style.background="#FFFFFF";			
		}
		if(document.getElementById('State').value=='0')
		{	 	var other_state =(document.getElementById('otherstate').value);
				if(other_state == '')
				{	 msg+='Specify other state name.<br>'
					 document.getElementById('otherstate').style.background="#FFDFEF";
					 error=error+1;
				}
				else if(pattern.test(other_state))
				{	msg+='In State special characters other than (Dot Comma Hyphen UnderScore And Semicolon) are not allowed<br>';				document.getElementById("otherstate").style.background="#FFDFEF";
					error=error+1;
				}
				else
				{	document.getElementById('otherstate').style.background="#FFFFFF";
				}
		}
		//Description: Validates the City  field.	
		var city_id=trim_ws(document.getElementById("City").value);
		if(city_id == '')
        {       msg+='Please select a City.<br>';
				document.getElementById("City").style.background="#FFDFEF";
			    error=error+1;
        }
		else
		{		document.getElementById("City").style.background="#FFFFFF";			
		}
		if(document.getElementById('City').value=='0')
		{	 	var other_city =(document.getElementById('othercity').value);
				if(other_city == '')
				{	 msg+='Specify other city name.<br>'
					 document.getElementById('othercity').style.background="#FFDFEF";
					 error=error+1;
				}
				else if(pattern.test(other_city))
				{	msg+='In City special characters other than (Dot Comma Hyphen UnderScore And Semicolon) are not allowed<br>';				document.getElementById("othercity").style.background="#FFDFEF";
					error=error+1;
				}
				else
				{	document.getElementById('otherstate').style.background="#FFFFFF";
				}
		}
		//Description: Validates the Company Name field.	
		var comp_name=trim_ws(document.getElementById("comp_name").value);
		if(comp_name == '')
        {       msg+='Specify a Company name.<br>';
				document.getElementById("comp_name").style.background="#FFDFEF";
			    error=error+1;
        }
		else
		{		document.getElementById("comp_name").style.background="#FFFFFF";			
		}
	    //Description: Validates the Designation field.	
	    var desig=trim_ws(document.getElementById("designation").value);
		if(desig == '')
        {       msg+='Please enter a Designation.<br>';
				document.getElementById("designation").style.background="#FFDFEF";
			    error=error+1;
        }
		else if(pattern.test(desig))
	 	{	msg+='In Designation special characters other than (Dot Comma Hyphen UnderScore And Semicolon) are not allowed<br>';
			document.getElementById("designation").style.background="#FFDFEF";
			error=error+1;
	 	}
		else
		{		document.getElementById("designation").style.background="#FFFFFF";			
		}
		var contactno=trim_ws(document.getElementById("contact_no").value);
		var mobileno=trim_ws(document.getElementById("mobile_no").value);
	 	var cotcode=trim_ws(document.getElementById("countrycode").value);
		var stcode=trim_ws(document.getElementById("stdcode").value);
		if(contactno == '' && mobileno == '')
        {       msg+='Please enter atleast one contact no.<br>';
				document.getElementById("stdcode").style.background="#FFDFEF";
				document.getElementById("contact_no").style.background="#FFDFEF";
				document.getElementById("mobile_no").style.background="#FFDFEF";
                error=error+1;
        }
		else
		{		document.getElementById("stdcode").style.background="";
				document.getElementById("contact_no").style.background="";			
				document.getElementById("mobile_no").style.background="";			
		}
	 if(contactno != '')
        {	if(cotcode == '')
			{		msg+='Please enter a Country Code.<br>';
					document.getElementById("countrycode").style.background="#FFDFEF";
					error=error+1;
			}
			else if(cotcode.length < 2)
			{		msg+='Please Country code atleast 2 digit long.<br>';
					document.getElementById("countrycode").style.background="#FFDFEF";
					error=error+1;
			}
			else
			{		document.getElementById("countrycode").style.background="";
			}
		}
		if(contactno != '')
        {			
		   if(!rb_checkPhone(contactno))
			{		msg+='Please enter a Contact No.<br>';
					document.getElementById("contact_no").style.background="#FFDFEF";
					error=error+1;
			}
			else if(contactno.length < 6)
			{		msg+='Please Contact no atleast 6 digit long.<br>';
					document.getElementById("contact_no").style.background="#FFDFEF";
					error=error+1;
			}
			else if(stcode == '')	
        	{	 msg+='Please enter a STD Code.<br>';
  			     document.getElementById("stdcode").style.background="#FFDFEF";
                 error=error+1;
        	}
		   else if(!rb_checkPhone(stcode))
			{		msg+='Please enter a valid STD code.<br>';
					document.getElementById("stdcode").style.background="#FFDFEF";
					error=error+1;
			}
			else if(stcode.length < 3)
			{		msg+='Please STD Code atleast 3 digits  long including 0.<br>';
					document.getElementById("stdcode").style.background="#FFDFEF";
					error=error+1;
			}
			else
			{	  	document.getElementById("contact_no").style.background="";
					document.getElementById("stdcode").style.background="";
			}
		}
	 if(mobileno != '')
        {
			if(!rb_checkPhone(mobileno))
			{		msg+='Please enter a valid Mobile no.<br>';
					document.getElementById("mobile_no").style.background="#FFDFEF";
					error=error+1;
			}
			else if(mobileno.length < 10)
			{		msg+='Please Mobile no atleast 10 digit long.<br>';
					document.getElementById("mobile_no").style.background="#FFDFEF";
					error=error+1;
			}
			else
			{		document.getElementById("mobile_no").style.background="";
			}
		}
	//Description: Validates the Address field.	
	   var addr=trim_ws(document.getElementById("address").value);
	   if(addr == '')
        {       msg+='Please enter Home address.<br>';
				document.getElementById("address").style.background="#FFDFEF";
			    error=error+1;
        }
		else
		{		document.getElementById("address").style.background="#FFFFFF";			
		}
	 
		 //Description: Validates the Website  field.	
        var wsite=trim_ws(document.getElementById("website").value);
		if(wsite != '')
        {      if(!isValidURL(wsite))
        		{
                msg+="'"+wsite+"' is not a valid Website url.Please enter a valid Website url.<br>";
				document.getElementById("website").style.background="#FFDFEF";
				error=error+1;
        		}
				else
				{
				document.getElementById("website").style.background="#FFFFFF";
				}
		}
	    if(error>0)
		{
		//alert(msg);
		 $.prompt(msg,{buttons:{Ok:true}, prefix:'cleanblue'})
			return false;
		}
		else
        return true;
}


//Others List
function addOthrsState(obj) 	
{
   if(obj  == '0')
	      document.getElementById('ostate').style.display='';
   else
		  document.getElementById('ostate').style.display='none';
}

function addOthrsCity(obj) 	
{
   if(obj  == '0')
	      document.getElementById('ocity').style.display='';
   else
		  document.getElementById('ocity').style.display='none';
}

