// JavaScript Document for form validation
function isBlank(val)
{
	if (val=="" || val==" ")
	{
		return true;
	}
	return false;
	
}//End of isBlank()	

/*------------------Function to check blan spaces in string ----------------- */
function isBlankSpace( str){
	 var isNonblank_re    = /^[\S]*$/;
     return String (str).search (isNonblank_re)
}//End of isBlankSpace()	

/*------------------Function to validate for alphaNumeric value ----------------- */
function isAlphaNumeric(val)
{
	if (val.match(/[a-zA-Z]/)){
		if (val.match(/[0-9]/)){
			return true;
		}
		return false;
	}
	return false;
	
}//End of isAlphaNumeric()	

/*------------------Function to validate for Number  ----------------- */
function isNumber(value){
  var iChars = "0123456789";
  for (var i = 0; i < value.length; i++) {
  	if (iChars.indexOf(value.charAt(i)) == -1) 
  	{
  		return false;
  	}
  }	
  return true;
}//End of isNumber()	


/*------------------Function to validate for Number  ----------------- */
function isPhone(value){
  var iChars = "0123456789+-";
  for (var i = 0; i < value.length; i++) {
  	if (iChars.indexOf(value.charAt(i)) == -1) 
  	{
  		return false;
  	}
  }	
  return true;
}//End of isNumber()	


/*------------------Function to validate email  ----------------- */
function trim(str)
{ 
	return((""+str).replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') ); 
}

function isValidEmail(value){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if(filter.test(trim(value)))
		return true;
	else
		return false;
	
}//End of isValidEmail()


function showError(msg,obj){
	$("#" + obj).parent().find("strong.errorMsg").remove();
	$("#" + obj).parent().addClass("error");
	$("#" + obj).before("<strong class='errorMsg'>" + msg + "</strong>");
	$("#" + obj).focus();
	return false;
}

function changeProper(id){
	$("form dd").find("strong.errorMsg").remove();
	$("form dd").removeClass("error");
}//End of changeProper()	
/*---------- FUnction for Search Box ----------*/

function validateLogin(){
	var userName = document.getElementById('userName').value;
	var password = document.getElementById('password').value;
		
	if(isBlank(userName)){
		showError("User Name cannot be left blank","userName");
		return false;
	}else{
		changeProper('userName');
	}
	
	if(isValidEmail(userName)){
		changeProper('userName');
	}else{
		showError("Enter Proper Email ID","userName");
		return false;
	}
	
	if(isBlank(password)){
		showError("User Name cannot be left blank","password");
		return false;
	}else{
		changeProper('password');
	}
	
	return true;
}

function validateContact(){
	var name = document.getElementById('name').value;
	var mobile = document.getElementById('mobile').value;
	var email = document.getElementById('email').value;
	var subject = document.getElementById('subject').selectedIndex;
	var message = document.getElementById('message').value;
	
	if((isBlank(name)) || (name=='Full Name')){
		showError("Please enter Name","name");
		return false;
	}else{
		changeProper('name');
	}
	
	if(isBlank(mobile)){
		showError("Mobile Number cannot be left blank","mobile");
		return false;
	}else{
		changeProper('mobile');
	}
	
	if(isPhone(mobile)){
		changeProper('mobile');
	}else{
		showError("Only numbers, - &amp; + allowed","mobile");
		return false;
	}
	
	if(isBlank(email)){
		showError("Email ID cannot be left blank","email");
		return false;
	}else{
		changeProper('email');
	}
	
	if(isValidEmail(email)){
		changeProper('email');
	}else{
		showError("Enter Proper Email ID","email");
		return false;
	}
	
	if(subject<1){
		showError("Please select a subject","subject");
		return false;
	}else{
		changeProper('subject');
	}
	
	if(isBlank(message)){
		showError("Please enter your message","message");
		return false;
	}else{
		changeProper('message');
	}
	return true;
}

function validateRegister(){
	var fname = document.getElementById('fname').value;
	var lname = document.getElementById('lname').value;
	var emailId = document.getElementById('emailId').value;
	var address = document.getElementById('address').value;
	var state = document.getElementById('state').selectedIndex;
	var city = document.getElementById('city').selectedIndex;
	var pincode = document.getElementById('pincode').value;
	var mobileno = document.getElementById('mobileno').value;
	var agree = document.getElementById('agree').checked;
	if((isBlank(fname)) || (fname=='First Name')){
		showError("Please enter First Name","fname");
		return false;
	}else{
		changeProper('fname');
	}
	
	if((isBlank(lname)) || (lname=='Last Name')){
		showError("Please enter Last Name","fname");
		return false;
	}else{
		changeProper('lname');
	}
	
	if(isBlank(emailId)){
		showError("Email ID cannot be left blank","emailId");
		return false;
	}else{
		changeProper('emailId');
	}
	
	if(isValidEmail(emailId)){
		changeProper('emailId');
	}else{
		showError("Enter Proper Email ID","emailId");
		return false;
	}
	
	if((isBlank(address)) || (address=='Postal Address - Line 1')){
		showError("Please enter Address","address");
		return false;
	}else{
		changeProper('address');
	}
	
	if(state<1){
		showError("Please select a state","state");
		return false;
	}else{
		changeProper('state');
	}
	
	if(city<1){
		showError("Please select a city","city");
		return false;
	}else{
		changeProper('city');
	}
	
	if((isBlank(pincode)) || (pincode=='ex: 400309')){
		showError("Please enter proper Pincode","pincode");
		return false;
	}else{
		changeProper('pincode');
	}
	
	if(isNumber(pincode)){
		changeProper('pincode');
	}else{
		showError("Only numbers allowed","pincode");
		return false;
	}
	
	if(isBlank(mobileno)){
		showError("Mobile Number cannot be left blank","mobileno");
		return false;
	}else{
		changeProper('mobileno');
	}
	
	if(isPhone(mobileno)){
		changeProper('mobileno');
	}else{
		showError("Only numbers, - &amp; + allowed","mobileno");
		return false;
	}
	
	if(agree!='true'){
		showError("Please agree to Terms & Conditions of website","agreeLabel");
		return false;
	}else{
		changeProper('agreeLabel');
	}
	
	return true;
}

function getQuote(){
	var companyName = document.getElementById('companyName').value;
	if((isBlank(companyName)) || (companyName=='Company Name')){
		alert("Please enter the Company Name");
		$("#companyName").focus();
		return false;
	}
		var left = (screen.width/2)-(780/2);
		var top = (screen.height/2)-(480/2);
		window.open("http://destimoney.accordfintech.com/CompanyProfile/CompanyList.aspx?id=71&Todate=&fromdate=&ChartType=D&SrchQuote="+companyName, '', 'toolbar=no, location=false, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=780, height=480, top='+top+', left='+left);
		//window.open("/tab-table.htm", '', 'toolbar=no, location=false, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=780, height=480, top='+top+', left='+left);
	return false;
}

$(function(){
		   $("#fname").focus(function(){
									  if($(this).val()=='First Name')
									  	{
											$(this).val('');
										}
									  });
		   $("#fname").blur(function(){
									   if($(this).val()=='')
									  	{
											$(this).val('First Name');
										}
									   });
		   $("#lname").focus(function(){
									  if($(this).val()=='Last Name')
									  	{
											$(this).val('');
										}
									  });
		   $("#lname").blur(function(){
									   if($(this).val()=='')
									  	{
											$(this).val('Last Name');
										}
									   });
		   $("#address").focus(function(){
									  if($(this).val()=='Postal Address - Line 1')
									  	{
											$(this).val('');
										}
									  });
		   $("#address").blur(function(){
									   if($(this).val()=='')
									  	{
											$(this).val('Postal Address - Line 1');
										}
									   });
		   $("#address1").focus(function(){
									  if($(this).val()=='Line 2 - (optional)')
									  	{
											$(this).val('');
										}
									  });
		   $("#address1").blur(function(){
									   if($(this).val()=='')
									  	{
											$(this).val('Line 2 - (optional)');
										}
									   });
		   $("#address2").focus(function(){
									  if($(this).val()=='Line 3 - (optional)')
									  	{
											$(this).val('');
										}
									  });
		   $("#address2").blur(function(){
									   if($(this).val()=='')
									  	{
											$(this).val('Line 3 - (optional)');
										}
									   });
		   $("#pincode").focus(function(){
									  if($(this).val()=='ex: 400309')
									  	{
											$(this).val('');
										}
									  });
		   $("#pincode").blur(function(){
									   if($(this).val()=='')
									  	{
											$(this).val('ex: 400309');
										}
									   });
		   $("#ext").focus(function(){
									  if($(this).val()=='ext')
									  	{
											$(this).val('');
										}
									  });
		   $("#ext").blur(function(){
									   if($(this).val()=='')
									  	{
											$(this).val('ext');
										}
									   });
		   
		   $("#name").focus(function(){
									  if($(this).val()=='Full Name')
									  	{
											$(this).val('');
										}
									  });
		   $("#name").blur(function(){
									   if($(this).val()=='')
									  	{
											$(this).val('Full Name');
										}
									   });
		   
		   });