// JavaScript Document
//Email address format checking
function isEmailAddr(email)
{
	var result = false 
	var theStr = new String(email)    
	var index = theStr.indexOf("@");    
	if (index > 0)    
	{      
		var pindex = theStr.indexOf(".",index);      
		if ((pindex > index+1) && (theStr.length > pindex+1)) 	
			result = true;    
		}   
	return result;  
}

 function Is10Digit(StrToCheck,str,str2)
{
		//first remove all spaces using the following regex
		StrToCheck= StrToCheck.replace(/^\s+|\s+$/, '');		
		//then we check for the length of the string if its 0 or not
		
          
		if( StrToCheck.length<10){
		alert(str2);
		return true;
		}
		
		 var reg1=/[A-Za-z]/;
          if(reg1.test(StrToCheck)){
          alert(str);
          return true;      
          }
}

// Check for numbers only fields
function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

//Contact form submit
function submitContactForm()
{
	//alert(document.contactForm.contactType.value);
  document.contactForm.submit();
}

//Validate function called from contact page
function ValidateContact()  
{   
	var errorMsg = "Following information is missing :\n";
	if(document.contactForm.contactType.value == "1")
	{				
			if(document.contactForm.cardNo.value == "")   
			{	
				errorMsg += "\n10 digit card number";	   
			}
			if(!document.contactForm.cardNo.value == "")
			{
				if(!isInteger(document.contactForm.cardNo.value))
				{
					errorMsg += "\nCard number must contain digits only";
				}
			}			
			if (document.contactForm.subject.selectedIndex == 0)  
			{    
				errorMsg += "\nSubject";  
			}
			
			if(document.contactForm.mobile.value == "")   
			{	
				errorMsg += "\nMobile Number";	   
			} 
			
		 
			
			if(!document.contactForm.mobile.value == "")
			{
				if(!isInteger(document.contactForm.mobile.value))
				{
					errorMsg += "\nMobile number must contain digits only";
				}			}
			
			
			
			if (document.contactForm.distCenter.selectedIndex == 0)  
			{    
				errorMsg += "\nDistribution Center";  
			} 
			i=0; 
			/*
			if(document.contactForm.email.value == "")   
			{	
				errorMsg += "\nEmail";	   
				i=1;
			}  
			if(i!=1) */
			if(!document.contactForm.email.value == "")
			{
				if (!isEmailAddr(document.contactForm.email.value))  
				{    
					errorMsg += "\nIncorrect Email format";  
				}  
			}	 
		
			if(document.contactForm.telephone.value == "")   
			{	
				errorMsg += "\nTelephone Number";	   
			} 
			if(!document.contactForm.telephone.value == "")
			{
				if(!isInteger(document.contactForm.telephone.value))
				{
					errorMsg += "\nTelephone number must contain digits only";
				}
			}
			if(document.contactForm.message.value == "")   
			{	
				errorMsg += "\nMessage";	   
			}
			if(errorMsg != "Following information is missing :\n")
			{
				alert(errorMsg)
				return false;
			}
			else
			{			
			  if(Is10Digit(document.contactForm.cardNo.value,"Alphabets are not allowed in Card Number","Kindly enter your 10 digit card number")){								
					document.contactForm.cardNo.focus();          					
					return false;      
				}  
			   if(Is10Digit(document.contactForm.telephone.value,"Alphabets are not allowed in landline number","Kindly enter your 10 digit land line number")){								
					document.contactForm.telephone.focus();          					
					return false;      
				}	
				  if(Is10Digit(document.contactForm.mobile.value,"Alphabets are not allowed in mobile number","Kindly enter your 10 digit Mobile number")){								
					document.contactForm.mobile.focus();          					
					return false;      
				}	
				   return true;	
			}
			
			
			
			
         
	}
	else
	{
			if(document.contactForm.firstName1.value == "")   
			{	
				errorMsg += "\nName";	   
			}
			if(document.contactForm.companyName1.value == "")
			{
				errorMsg += "\nCompany Name";	
			}
		/*	if(document.contactForm.address1.value == "")
			{
				errorMsg += "\nAddress";	
			}
			if(document.contactForm.pinCode1.value == "")
			{
				errorMsg += "\nPincode";	
			}
			if(!document.contactForm.pinCode1.value == "")
			{
				if(!isInteger(document.contactForm.pinCode1.value))
				{
					errorMsg += "\nPincode must contain digits only";
				}
			}*/
			
			if(document.contactForm.telephone1.value == "")   
			{	
				errorMsg += "\nTelephone Number";	   
			}
			if(!document.contactForm.telephone1.value == "")
			{
				if(!isInteger(document.contactForm.telephone1.value))
				{
					errorMsg += "\nTelephone number must contain digits only";
				}
			}
			i=0; 
		
			if(document.contactForm.email1.value == "")   
			{	
				errorMsg += "\nEmail";	   
				i=1;
			}  
			
			if(!document.contactForm.email1.value == "")
			{
				if (!isEmailAddr(document.contactForm.email1.value))  
				{    
					errorMsg += "\nIncorrect Email format";  
				}  
			}			
			
			if(document.contactForm.mobile1.value == "")   
			{	
				errorMsg += "\nMobile Number";	   
			}
			if(!document.contactForm.mobile1.value == "")
			{
				if(!isInteger(document.contactForm.mobile1.value))
				{
					errorMsg += "\nMobile number must contain digits only";
				}
			}
			if (document.contactForm.distCenter1.selectedIndex == 0)  
			{    
				errorMsg += "\nThe Metro centre closest to you is ";  
			}
		/*	if (document.contactForm.subject1.selectedIndex == 0)  
			{    
				errorMsg += "\nSubject";  
			}						
			if(document.contactForm.message1.value == "")   
			{	
				errorMsg += "\nMessage";	   
			}*/
			if(errorMsg != "Following information is missing :\n")
			{
				alert(errorMsg)
				return false;
			}
			else
			{
				
			   if(Is10Digit(document.contactForm.telephone1.value,"Alphabets are not allowed in landline number","Kindly enter your 10 digit land line number")){								
					document.contactForm.telephone1.focus();          					
					return false;      
				}	
				  if(Is10Digit(document.contactForm.mobile1.value,"Alphabets are not allowed in mobile number","Kindly enter your 10 digit Mobile number")){								
					document.contactForm.mobile1.focus();          					
					return false;      
				}	
				   return true;	
			}
	}
}


//Validate function called from send to friend page

//Function to submit Send to friend form
function submitFriendform()
{  
  document.friendForm.submit();
}

//Function to validate Send to friend form
function ValidateSendToFriends()  
{   	
	var errorMsg = "Following information is missing :\n";
	if(document.friendForm.firstName.value == "")   
	{	
		errorMsg += "\nYour Name";	   
	}
	k = 0;
	if(document.friendForm.email.value == "")   
	{	
		errorMsg += "\nYour Email";	   
		k=1;
	}  
	if(k!=1)	
	{
		if (!isEmailAddr(document.friendForm.email.value))  
		{    
			errorMsg += "\nIncorrect Email format";  
		}  
	}
	if (document.friendForm.friendName.value == "")  
	{    
		errorMsg += "\nFriend Name";  
	}
	j = 0;
	if(document.friendForm.friendEmail.value == "")   
	{	
		errorMsg += "\nFriend's Email";	   
		j=1;
	}  
	if(j!=1)
	if(!document.friendForm.friendEmail.value == "")
	{
		if (!isEmailAddr(document.friendForm.friendEmail.value))  
		{    
			errorMsg += "\nIncorrect Friend's Email format";  
		}  
	}
	if(errorMsg != "Following information is missing :\n")
	{
		alert(errorMsg)
		return false;
	}
	else
	{
		return true;
	}
}

//Function for subscription page
function submitEmailForm()
{
  document.emailForm.submit();
}

function validateEsubscription()  
{   	
	var errorMsg = "Following information is missing :\n";
	if(document.emailForm.num1.value == "" || document.emailForm.num2.value == "" || document.emailForm.num3.value == "")   
	{	
		errorMsg += "\nMETRO card number";	   
	}
	if(!document.emailForm.num1.value == "" || !document.emailForm.num2.value == "" || !document.emailForm.num3.value == "")
	{
		if(!isInteger(document.emailForm.num1.value) || !isInteger(document.emailForm.num2.value) || !isInteger(document.emailForm.num3.value))
		{
			errorMsg += "\nMETRO card number must contain digits only";
		}
	}
	k = 0;
	if(document.emailForm.email.value == "")   
	{	
		errorMsg += "\nEmail";	   
		k=1;
	}
	if(k!=1)	
	{
		if (!isEmailAddr(document.emailForm.email.value))  
		{    
			errorMsg += "\nIncorrect Email format";  
		}  
	}
	if(document.emailForm.mobile.value == "")   
	{	
		errorMsg += "\nMobile Number";	   
	}
	if(!document.emailForm.mobile.value == "")
	{
		if(!isInteger(document.emailForm.mobile.value))
		{
			errorMsg += "\nMobile number must contain digits only";
		}
	}	
	if(errorMsg != "Following information is missing :\n")
	{
		alert(errorMsg)
		return false;
	}
	else
	{
		return true;
	}
}


//Function for Login page
function submitLoginForm()
{
  document.loginForm.submit();
}

function validateLoginForm()  
{   	
	var errorMsg = "Following information is missing :\n";
	if(document.loginForm.num1.value == "" || document.loginForm.num2.value == "" || document.loginForm.num3.value == "")   
	{	
		errorMsg += "\nMETRO card number";	   
	}
	if(!document.loginForm.num1.value == "" || !document.loginForm.num2.value == "" || !document.loginForm.num3.value == "")
	{
		if(!isInteger(document.loginForm.num1.value) || !isInteger(document.loginForm.num2.value) || !isInteger(document.loginForm.num3.value))
		{
			errorMsg += "\nMETRO card number must contain digits only";
		}
	}		
	if(errorMsg != "Following information is missing :\n")
	{
		alert(errorMsg)
		return false;
	}
	else
	{
		submitLoginForm();
		return true;
	}
}

