/**
Author: Sanjesh Verma
Date: 21-05-2009

Discription: This js includes all the functions requried for the form validations.

*/


//function to get div object
function getDivObj(name)
 {
	if (document.getElementById)
	 {
		this.obj = document.getElementById(name); 
	 }
	else if (document.all)
	 {
	    this.obj = document.all[name];
	 }
	else if (document.layers)
	 {
	    this.obj = document.layers[name];	
	 }
}// function over


//function to check radio buton of seminar calendar is selected or not
function radioValues()
{	 
  for (var i=0; i < document.cdSeminar.seminarKey.length; i++)
  {
    if (document.cdSeminar.seminarKey[i].checked)
    {
      var radVal = document.cdSeminar.seminarKey[i].value;
      return radVal;
    }

  }
  return false;
}


//function to populate the input box for other field
function fillOtherDiv()
{
  var value = radioValues();
  //alert(value);
  if(value == 'other')
	{
    document.getElementById('otherDiv').innerHTML='<input type="radio" name="seminarKey" id="seminarKey" value="other" onClick="fillOtherDiv()" checked="true" > Other &nbsp;&nbsp;<input type=text name="other1" id="other1" maxlength=25 size=15 onchange="return validateChar(this.id,\'Other\')"> (Other preferred city)';
	
  }

}//function over


//function to remove  the input box for other field
function clearOtherDiv()
{
  document.getElementById('otherDiv').innerHTML='<input type="radio" name="seminarKey" id="seminarKey" value="other" onClick="fillOtherDiv()"  > Other ';

}



//function to populate the seminar calendar
function fillCalendar()
{
var region=document.cdSeminar.state.options[document.cdSeminar.state.selectedIndex].value;
var occup=document.cdSeminar.occup.options[document.cdSeminar.occup.selectedIndex].value;


   if(region == 'select')
	{
	 
   document.getElementById('regionDiv').innerHTML='';
   }
   else
	{

   populateDiv(region,'region',occup);
	}
}// function over

function fillCalendar2()
{
var region=document.cdSeminar.state.options[document.cdSeminar.state.selectedIndex].value;

 if(region == 'select')
 {
   document.getElementById('regionDiv').innerHTML='';
 }
 else
 {
   populateDiv(region,'region','abc');
 }

}// function over


function clearState()
{

//alert("after11111111111");
document.cdSeminar.state.selectedIndex=0;

}


//function to check that values enetered by users are number or not
function checkNumber(field,num)
{
  
  if(isNaN(num))
  {
    alert( field +" can have only numeric values.");
	return false;
  }
  return true;

}



//function for corporate form to check that mandatory fields are filled by users or not 
function validateCorp()
{
 
 if(document.cdSeminar.fullName.value == "")
 {
	alert("Please enter name.");
	document.cdSeminar.fullName.focus();
	return false;
 }
 else
 {
	  if(!validateChar('fullName','Name'))
	  {
	    document.cdSeminar.fullName.focus();
		return false;
	  }
  
 }

 if( document.cdSeminar.industry.options[document.cdSeminar.industry.selectedIndex].value == "select" )
  {
	  alert("Please select the industry.");
		document.cdSeminar.industry.focus();
		return false;
  
  }

 if(document.cdSeminar.turnOver.value != "")
 {
    if(!checkNumber('Turn over',document.cdSeminar.turnOver.value))
	 {
	   document.cdSeminar.turnOver.focus();
	   return false;
	 }   
 
 }

 if(document.cdSeminar.forex.value != "")
 {
   
  if(!checkNumber('Forex exposure',document.cdSeminar.forex.value))
	 {
	   document.cdSeminar.forex.focus();
	   return false;
	 }   
 
 }

if(document.cdSeminar.title.options[document.cdSeminar.title.selectedIndex].value == "select")
  {
		alert("Please select the tittle.");
		document.cdSeminar.title.focus();
		return false;
  }

  if(document.cdSeminar.cpName.value == "")
  {
		alert("Please enter contact person name.");
		document.cdSeminar.cpName.focus();
		return false;
  }
  else
  {
	  if(!validateChar('cpName','Contact person'))
	  {
	    document.cdSeminar.cpName.focus();
		return false;
	  }
  
  }

 if(document.cdSeminar.designation.value != "")
 {
   if(!validateChar('designation','Designation'))
	 {
	   document.cdSeminar.designation.focus();
	   return false;
	 }   
 
 }

  

  //alert(document.cdSeminar.address.value);
  if(document.cdSeminar.address.value == "")
  {
		alert("Please enter the address.");
		document.cdSeminar.address.focus();
		return false;
  }
  else
  {
	  if(!validateAlphaNumeric('address', 'Address'))
	  {
	    document.cdSeminar.address.focus();
		return false;
	  }
  
  }
  //alert(document.cdSeminar.city.value);
  if(document.cdSeminar.city.value == "")
  {
		alert("Please enter the city.");
		document.cdSeminar.city.focus();
		return false;
  }
  else
  {
	  if(!validateChar('city','City'))
	  {
	    document.cdSeminar.city.focus();
		return false;
	  }
  
  }
//alert(document.cdSeminar.state.options[document.cdSeminar.state.selectedIndex].value);
if(document.cdSeminar.state.options[document.cdSeminar.state.selectedIndex].value == "select")
  {
		alert("Please select the state.");
		document.cdSeminar.state.focus();
		return false;
  }
  //alert(document.cdSeminar.pinCode.value);
  if(document.cdSeminar.pinCode.value == "")
  {
		alert("Please enter the pin.");
		document.cdSeminar.pinCode.focus();
		return false;
  }
  else
  { 
	 if(!checkNumber('Pincode',document.cdSeminar.pinCode.value))
	 {
	   document.cdSeminar.pinCode.focus();
	   return false;
	 }
	 
	 var pin =  document.cdSeminar.pinCode.value;
     if(pin.length<6)
	  {
	     alert("Please enter a pin of 6 digit.");
	     document.cdSeminar.pinCode.focus();
	 	 return false;
	  }
	 
  }

  if(document.cdSeminar.mobile.value != "")
  {
	if(!checkNumber('Mobile',document.cdSeminar.mobile.value))
	{
	    document.cdSeminar.mobile.focus();
		return false;
	}
	else
	{
	  var mob =  document.cdSeminar.mobile.value;
      if(mob.length<10)
	  {
	    alert("Please enter a 10 digit mobile No.");
	    document.cdSeminar.mobile.focus();
		return false;
	  }
    }//else
  }//if

  

  //to chek the semninar calendar
  
 if(document.cdSeminar.seminarKeyFlag.value == "false")
  {
 //alert("inside ifff");
    if(radioValues() == false )
    {
      alert("Please select the seminar calendar");
      return false;
    }
    else
    {
	  var value=radioValues();
	  //alert("value of radion buttin is "+value);
      if(value == 'other' )
	  {
	     //alert("value of radion buttin is "+value);
		 if(document.cdSeminar.other1.value == "")
		 {
		     alert("Please fill the other feild.");
			 return false;
		 }
		 else
		 {
		   if(!validateChar('other1','Other'))
	       {
	          document.cdSeminar.other1.focus();
		      return false;
	       }
		 
		 }
	  }
    }//else

  }//if over
  else
  { 

	  //alert("inside else");

	  if(document.cdSeminar.other1.value == "")
	  {
	    alert("Please fill the other feild.");
		return false;
	  
	  }
	  else
	  {
	    
		if(!validateChar('other1','Other'))
	    {
	      document.cdSeminar.other1.focus();
		  return false;
	    }
	  
	  }
		
  
 
  }
	

  
  
  var std=document.cdSeminar.std.value;
  if( std == "")
  {
		alert("Please enter the STD code.");
		document.cdSeminar.std.focus();
		return false
  }
  else
  {
    if(!checkNumber('Std',document.cdSeminar.std.value))
	{
	    document.cdSeminar.std.focus();
		return false;
	}
  }
		var phone=document.cdSeminar.phone.value;
  if(phone == "")
  {
		alert("Please enter the telephone No.");
		document.cdSeminar.phone.focus();
		return false
  }
  else
  { 
	  if(!checkNumber('Phone No.',document.cdSeminar.phone.value))
	{
	    document.cdSeminar.phone.focus();
		return false;
	}
  
  }


  var phoneNo = std+phone;
  //alert(phoneNo);
  if(phoneNo.length != 11)
  {
		
	alert("Please enter a 11 digit Telephone No.");
	return false;

  }

  
if(  document.cdSeminar.personNo.options[document.cdSeminar.personNo.selectedIndex].value == "select" )
  {
	  alert("Please select the No of person attending seminar.");
		document.cdSeminar.personNo.focus();
		return false;
  
  }

if(document.cdSeminar.email.value == "")
  {
		alert("Please enter the email.");
		document.cdSeminar.email.focus();
		return false;
  }
  else
  {
	if(!validateEmailAdd('email'))
	{
      document.cdSeminar.email.focus();
	  return false;
	}
	
  }

  if(document.cdSeminar.cEmail.value == '' )
  {
	 alert("Please enter the confirm email.");
	 document.cdSeminar.cEmail.focus();
	 return false;
  }
  else
  {
  	 if(!validateEmailAdd('cEmail'))
	 {
	   document.cdSeminar.cEmail.focus();
	   return false;
     }
  }
			

  if(document.cdSeminar.enteredCaptchaStr.value == "")
  {
		alert("Please enter the validation String.");
		document.cdSeminar.enteredCaptchaStr.focus();
		return false;
  }
  else
  {
     if(valCaptcha())
	 {
		 return true;
	 }
	 else
	 {
		 return false;
	 }
  }
	
  return true;
	    	
}//function over





//function for individual form to check that mandatory fields are filled by users or not 

function validateIndi()
{
 //alert(document.cdSeminar.title.options[document.cdSeminar.title.selectedIndex].value);
  

  if( document.cdSeminar.title.options[document.cdSeminar.title.selectedIndex].value == "select")
  {
		alert("Please select the tittle.");
		document.cdSeminar.title.focus();
		return false;
  }

  if(  document.cdSeminar.fullName.value == "")
  {
		alert("Please enter your name.");
		document.cdSeminar.fullName.focus();
		return false;
  }
  else
  {
	  if(!validateChar('fullName','Name'))
	  {
	    document.cdSeminar.fullName.focus();
		return false;
	  }
  
  }
 
  if(document.cdSeminar.occup.options[document.cdSeminar.occup.selectedIndex].value == "select")
  {
		alert("Please select the Occupation.");
		document.cdSeminar.occup.focus();
		return false;
  }

  //alert(document.cdSeminar.address.value);
  if(document.cdSeminar.address.value == "")
  {
		alert("Please enter the address.");
		document.cdSeminar.address.focus();
		return false;
  }
  else
  {
	  
	  if(!validateAlphaNumeric('address', 'Address'))
	  {
	    document.cdSeminar.address.focus();
		return false;
	  }
  
  }
  //alert(document.cdSeminar.city.value);
  if(document.cdSeminar.city.value == "")
  {
		alert("Please enter the city.");
		document.cdSeminar.city.focus();
		return false;
  }
  else
  {
	  if(!validateChar('city','City'))
	  {
	    document.cdSeminar.city.focus();
		return false;
	  }
  
  }
//alert(document.cdSeminar.state.options[document.cdSeminar.state.selectedIndex].value);
if(document.cdSeminar.state.options[document.cdSeminar.state.selectedIndex].value == "select")
  {
		alert("Please select the state.");
		document.cdSeminar.state.focus();
		return false;
  }
  //alert(document.cdSeminar.pinCode.value);
  if(document.cdSeminar.pinCode.value == "")
  {
		alert("Please enter the pin.");
		document.cdSeminar.pinCode.focus();
		return false;
  }
  else
  { //alert("elseqqqqqqqq");
  //alert(checkNumber('Pincode',document.cdSeminar.pinCode.value));
	 if(!checkNumber('Pincode',document.cdSeminar.pinCode.value))
	 {
	   //alert("else2222");
	   document.cdSeminar.pinCode.focus();
	   return false;
	 }
	 
	 //alert("else3333");
	 var pin =  document.cdSeminar.pinCode.value;
     if(pin.length<6)
	 {
	    alert("Please enter a pin of 6 digit.");
	    document.cdSeminar.pinCode.focus();
	  return false;
	 }
	 
  }

  
  if(document.cdSeminar.mobile.value != "")
  {
	if(!checkNumber('Mobile',document.cdSeminar.mobile.value))
	{
	    document.cdSeminar.mobile.focus();
		return false;
	}
	else
	{
	  var mob =  document.cdSeminar.mobile.value;
      if(mob.length<10)
	  {
	    alert("Please enter a 10 digit mobile No.");
	    document.cdSeminar.mobile.focus();
		return false;
	  }
    }//else
  }//if


  



 if(document.cdSeminar.seminarKeyFlag.value == "false")
  {
 //alert("inside ifff");
    if(radioValues() == false )
    {
      alert("Please select the seminar calendar");
      return false;
    }
    else
    {
	  var value=radioValues();
	  //alert("value of radion buttin is "+value);
      if(value == 'other' )
	  {
	     //alert("value of radion buttin is "+value);
		 if(document.cdSeminar.other1.value == "")
		 {
		     alert("Please fill the other feild.");
			 return false;
		 }
		 else
		 {
		   if(!validateChar('other1','Other'))
	       {
	          document.cdSeminar.other1.focus();
		      return false;
	       }
		 
		 }
	  }
    }//else

  }//if over
  else
  { 

	  //alert("inside else");

	  if(document.cdSeminar.other1.value == "")
	  {
	    alert("Please fill the other feild.");
		return false;
	  
	  }
	  else
	  {
	    
		if(!validateChar('other1','Other'))
	    {
	      document.cdSeminar.other1.focus();
		  return false;
	    }
	  
	  }
		
  
 
  }
	



  
  var std=document.cdSeminar.std.value;
  if( std == "")
  {
		alert("Please enter the STD code.");
		document.cdSeminar.std.focus();
		return false
  }
  else
  {
    if(!checkNumber('Std',document.cdSeminar.std.value))
	{
	    document.cdSeminar.std.focus();
		return false;
	}
  }
		var phone=document.cdSeminar.phone.value;
  if(phone == "")
  {
		alert("Please enter the telephone No.");
		document.cdSeminar.phone.focus();
		return false
  }
  else
  { 
	  if(!checkNumber('Phone No.',document.cdSeminar.phone.value))
	{
	    document.cdSeminar.phone.focus();
		return false;
	}
  
  }


  var phoneNo = std+phone;
  //alert(phoneNo);
  if(phoneNo.length != 11)
  {
		
	alert("Please enter a 11 digit Telephone No.");
	return false;

  }

	

  if(document.cdSeminar.email.value == "")
  {
		alert("Please enter the email.");
		document.cdSeminar.email.focus();
		return false;
  }
  else
  {
	if(!validateEmailAdd('email'))
	{
      document.cdSeminar.email.focus();
	  return false;
	}
	
  }

  if(document.cdSeminar.cEmail.value == '' )
  {
	 alert("Please enter the confirm email.");
	 document.cdSeminar.cEmail.focus();
	 return false;
  }
  else
  {
  	 if(!validateEmailAdd('cEmail'))
	 {
	   document.cdSeminar.cEmail.focus();
	   return false;
     }
  }
			

  if(document.cdSeminar.enteredCaptchaStr.value == "")
  {
		alert("Please enter the validation String.");
		document.cdSeminar.enteredCaptchaStr.focus();
		return false;
  }
  else
  {
     if(valCaptcha())
	 {
		 return true;
	 }
	 else
	 {
		 return false;
	 }
  }
	
  return true;

}//function over



//function to validate email
function validateEmailAdd(fieldName)
{
  var atRate = 0;
  var dot = 0;
  fieldObj = new getDivObj(fieldName);
  var fieldVal = fieldObj.obj.value;

  
  if(fieldVal == '')
	{
 alert("Please enter Email-id.");
 return false;
 }
  if (fieldVal != '')
  {
	//alert("inside");
	for(var i = 0; i < fieldVal.length; i++) 
	{
	  
	  var myChar = fieldVal.charAt(i);
      //alert("for loop "+myChar);
	  if ((myChar >= '0' && myChar <= '9') 	|| (myChar >= 'a' && myChar <= 'z') 	|| (myChar >= 'A' && myChar <= 'Z') || myChar == '_' 	|| myChar == '@' || myChar == '.' || myChar == '-') 
	  {
		  
		  //alert("inside if for loop "+myChar);
		  
		  if(myChar == '@')
		     atRate++;
		  if(myChar == '.')
		     dot++;
	  }
	  else 
	  {
		 alert('Invalid Character at Email Address. Valid Characters ' +	'are Numbers(0-9),underscore,hyphen, a-z, A-Z,@ and .(period)');
		 fieldObj.obj.focus();
		 return false;
	  }	
	  
	 
   }//for loop
	
 }//if
 
 
 //alert("at --" +atRate +"dot"+dot);
 if (atRate = 1 && dot >= 1)
 {
	   //alert('Invalid Email Address.Please check');
	   //return true;

   if(document.cdSeminar.cEmail.value != '' && document.cdSeminar.cEmail.value !== document.cdSeminar.email.value)
   {
     alert("Email-id & Confirm email-id are not matching.");
     return false;
 
    }	
 }
 else
 {
	alert('Invalid Email Address.Please check');
	fieldObj.obj.focus();
	return false;
 }

 

	return true;
}


//function to validate alphanumberic characters
function validateAlphaNumeric(fieldName,tempFname)
{
  //alert("value is1111"+fieldName);
  //alert("value is1111"+fieldName.value);
  fieldObj = new getDivObj(fieldName);
  var fieldVal = fieldObj.obj.value;
  if (fieldVal != '') 
  {
	for(var i = 0; i < fieldVal.length; i++) 
	{
	  var myChar = fieldVal.charAt(i);
	  if ((myChar >= 0 && myChar <= 9) || (myChar >= "a" && myChar <= "z") || (myChar >= "A" && myChar <= "Z") || myChar == " " || myChar == "," || myChar == ".") 
	  {
	  } 
	  else
	  {
	    alert('Invalid Character at field : '+ tempFname +'. Valid characters are Numbers(0-9), a-z, A-Z, Period(.), comma(,) & space.');
		fieldObj.obj.focus();
		return false;
	  }					
    }//for

  }
	
  return true;
}


//function to validate alphabetic characters
function validateChar(fieldName,tempFname)
{
	//alert(fieldName);
	fieldObj = new getDivObj(fieldName);
	//alert(fieldObj.obj.name);
	var fieldVal = fieldObj.obj.value;
	//alert("value is222 "+fieldVal);
  //alert("value is1111"+fieldName+.value);
  if (fieldVal != '') 
  {
	for(var i = 0; i < fieldVal.length; i++) 
	{
	  var myChar = fieldVal.charAt(i);
	  if ((myChar >= "a" && myChar <= "z") ||   (myChar >= "A" && myChar <= "Z") || myChar == " " ) 
	  {
		
	  } 
	  else
	  {
		alert('Invalid Character at field : '+ tempFname +'. Valid characters are a-z, A-Z & space.');
		fieldObj.obj.focus();
		return false;
	  }
	 
	}//for

  }//if
return true;
}


function valCaptcha()
{
	
	var imgValueArry = new Array("g5396","12345","34837","56n8p","9dh58","42680","d24u1","ta480","hke31","sbx42","a19z0","b28y1","c37x2","d46w3","e55v4","f64u5","g73t6","h82s7","j91r8","k19q9","l28p9","m37o8","n46n7","o55m6","p64l5","q73k4","r82j3","s91h2","t19g1","u28f0","v37e0","w46d9","x55c8","y64b7","z73a6","9z0a1","8y1b2","7x2c3","6w3d4","5v4e5","4u5f6","3t67g","2s78h","1r89i","0q90j","9p01k","8o92l","7n83m","6m74n","5l65o","4k56p","3l47q","2j38r","1i29s","0h10t","1g09u","2f98v","3e87w","4d76x","5c65y","6b54z");

	var enteredText = document.getElementById("enteredCaptchaStr").value; 
	var imageSrNo = document.getElementById("imgSrNo").value; 

	
	if(imgValueArry[imageSrNo].toUpperCase() == enteredText.toUpperCase())
	{
		return true;
	}
	else
	{
		alert("The characters entered by you, are not the same as the characters shown.");
		getImage();
		return false;
	}
}// function over

function getImage() 
{ 
  var imgSrNo= Math.round((Math.random() * 60)); 
   
  var  imgObj = document.getElementById("captchaImg"); 
  imgObj.src="/images/mynse/AutoRegCheckImg"+imgSrNo+".jpg"; 
 
  var  imgSrNoObj = document.getElementById("imgSrNo"); 
  imgSrNoObj.value=imgSrNo; 
} // function over
