function echeck(str) 
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	var lastDot=str.lastIndexOf(dot);
	if (str.indexOf(at)==-1){
	   alert("Please enter a valid E-mail Id");
	   return false;
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Please enter a valid E-mail Id");
	   return false;
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("Please enter a valid E-mail Id");
		return false;
	}
	
	 if (str.indexOf(at,(lat+1))!=-1){
		alert("Please enter a valid E-mail Id");
		return false;
	 }
	
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("Please enter a valid E-mail Id");
		return false;
	 }
	
	 if (str.indexOf(dot,(lat+2))==-1){
		alert("Please enter a valid E-mail Id");
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		alert("Please enter a valid E-mail Id");
		return false;
	 }
	if (lstr-1 == lastDot){
	   alert("Please enter a valid E-mail Id");
	   return false;
	}
	return true	;						
}

function checkURL(value)
{
	var urlregex = new RegExp("^(http:\/\/www.|https:\/\/www.|www.){1}[0-9A-Za-z\.\-]*\.[0-9A-Za-z\.\-]*$");
	if(urlregex.test(value))
	{
		return true;
	}
	return false;
}

function isCharsInBag (s, bag)
{  
    var i;
    for (i = 0; i < s.length; i++)
    {   
       
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) return false;
    }
    return true;
}

function ext(s)
{
        var i,pos=0;
        for (i = 0; i < s.length; i++)
        {
                var c = s.charAt(i);
                if (c==".") pos=i;
        }
        var extValue="";
        for (i=pos+1;i<=s.length;i++)
        {
                extValue =extValue+s.charAt(i);
        }

        if (extValue != 'jpeg' && extValue != 'JPEG' && extValue != 'jpg' && extValue != 'JPG' && extValue != 'png' && extValue != 'PNG')
        return false;
    else
  		document.register.ftype.value=extValue;
	return true;
}
function trimString (str)
{
	str = this != window? this : str;
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
function isCharsInBag (s, bag)
{
    var i;
    for (i = 0; i < s.length; i++)
    {

        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) return false;
    }
    return true;
}

function isFloatValue(s)
{
		var i,counter=0;
		for (i = 0; i < s.length; i++)
		{
			var c = s.charAt(i);
			if (c==".") counter++;
		}
		if (counter>1)
		return false;
		else
		return true;
}

// Function to create XMLHTTP Request object
function createobject()
{
	var browser=navigator.appName;// Browset type
	//alert(browser);
	// If browser is Microsoft Internet Explorer
	if (browser=="Microsoft Internet Explorer")
	{
		var obj=new ActiveXObject("Microsoft.XMLHTTP");
	}
	// If browser is not Microsoft Internet Explorer
	else
	{
		var obj=new XMLHttpRequest();
	}
	return obj; // Return object
}

function validateQuickQuery()
{
	var frmName=document.frmQuickQuery;
	var contactNameVal=trimString(frmName.contactName.value);
	var contactEmailVal=trimString(frmName.contactEmail.value);
	var contactPhoneVal=trimString(frmName.contactPhone.value);
	var contactMessageVal=trimString(frmName.contactMessage.value);
	contactMessageVal=contactMessageVal.replace("&","and");
	var howKnowUsVal=trimString(frmName.howKnowUs.value);
	
	/******** Name Validation Starts Here ********/
	if (contactNameVal=="")
	{
		alert("Please enter the Name");
		frmName.contactName.value=contactNameVal;
		frmName.contactName.focus();
		return false;
	}
	/*---------------------If any special character---------------------*/	
	if (!isCharsInBag(contactNameVal, " '.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))
	{
		alert("Invalid characters found in Name" );
		frmName.contactName.value=contactNameVal;
		frmName.contactName.focus();
		return false;
	}
	/*----------------------If length of name if greater than 25 and less than 2 --------------------*/	
	contactNameLen=contactNameVal.length
	if (contactNameLen < 2 || contactNameLen > 200)
	{
		alert( "Name should be minimum of 2 characters and maximum of 200 characters");
		frmName.contactName.value=contactNameVal;
		frmName.contactName.focus();
		return false;
	}
	
	 var contactNameFirstChar = contactNameVal.charAt(0);
	 /*---------------------If any special character for the first letter---------------------*/	
	if (!isCharsInBag(contactNameFirstChar, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))
	{
		alert("First character should be alphabet in Name" );
		frmName.contactName.value=contactNameVal;
		frmName.contactName.focus();
		return false;
	}
	/******** Name Validation Ends Here ********/
	
	/******** Email Id Validation Starts Here ********/
	if (contactEmailVal=="")
	{
		alert("Please enter the E-mail Id");
		frmName.contactEmail.value=contactEmailVal;
		frmName.contactEmail.focus();
		return false;
	}
	/*---------------------If any special character---------------------*/	
	if (!isCharsInBag(contactEmailVal, "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@.0123456789"))
	{
		alert("Invalid characters found in E-mail Id" );
		frmName.contactEmail.value=contactEmailVal;
		frmName.contactEmail.focus();
		return false;
	}
	/*----------------------If length of name if greater than 25 and less than 2 --------------------*/	
	contactEmailLen=contactEmailVal.length
	if (contactEmailLen < 6 || contactEmailLen > 200)
	{
		alert( "E-mail Id should be minimum of 6 characters and maximum of 200 characters");
		frmName.contactEmail.value=contactEmailVal;
		frmName.contactEmail.focus();
		return false;
	}
	
	 var contactEmailFirstChar = contactEmailVal.charAt(0);
	 /*---------------------If any special character for the first letter---------------------*/	
	if (!isCharsInBag(contactEmailFirstChar, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"))
	{
		alert("First character should be alphabet or numeric in E-mail Id" );
		frmName.contactEmail.value=contactEmailVal;
		frmName.contactEmail.focus();
		return false;
	}
	/*---------------------Email Id format ---------------------*/	
	if (!echeck(contactEmailVal))
	{
		frmName.contactEmail.value=contactEmailVal;
		frmName.contactEmail.focus();
		return false;
	}
	
	/******** Email Id Validation Ends Here ********/
	
	/******** Phone Number Validation Starts Here ********/
	if (contactPhoneVal=="")
	{
		alert("Please enter the Phone Number");
		frmName.contactPhone.value=contactPhoneVal;
		frmName.contactPhone.focus();
		return false;
	}
	/*---------------------If any special character---------------------*/	
	if (!isCharsInBag(contactPhoneVal, " -()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))
	{
		alert("Invalid characters found in Phone Number" );
		frmName.contactPhone.value=contactPhoneVal;
		frmName.contactPhone.focus();
		return false;
	}
	/*----------------------If length of name if greater than 25 and less than 2 --------------------*/	
	contactPhoneLen=contactPhoneVal.length
	if (contactPhoneLen < 5 || contactPhoneLen > 20)
	{
		alert( "Phone Number should be minimum of 5 characters and maximum of 20 characters");
		frmName.contactPhone.value=contactPhoneVal;
		frmName.contactPhone.focus();
		return false;
	}
	/******** Phone Number Validation Ends Here ********/
	
	/******** Message Validation Starts Here ********/
	if (contactMessageVal=="")
	{
		alert("Please enter the Query");
		frmName.contactMessage.value=contactMessageVal;
		frmName.contactMessage.focus();
		return false;
	}
	/******** Message Validation Ends Here ********/
	
	/******** How did you know about us Validation Starts Here ********/
	if (howKnowUsVal=="")
	{
		alert("Please select \"How did you know about us?\"");
		frmName.howKnowUs.value=howKnowUsVal;
		frmName.howKnowUs.focus();
		return false;
	}
	if (howKnowUsVal!="")
	{
		var howKnowUsOtherVal="";
		/******** If Other Search Engine ********/
		if (howKnowUsVal=="Other Search Engine")
		{
			howKnowUsOtherVal=trimString(frmName.howKnowUsOtherOSE.value);
			var howKnowUsOtherAlert="Other Search Engine Name";
			
			if (howKnowUsOtherVal=="")
			{
				alert("Please enter the "+howKnowUsOtherAlert);
				frmName.howKnowUsOtherOSE.value=howKnowUsOtherVal;
				frmName.howKnowUsOtherOSE.focus();
				return false;
			}
			if (howKnowUsOtherVal!="")
			{
				/*---------------------If any special character---------------------*/	
				/*if (!isCharsInBag(howKnowUsOtherVal, " '.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"))
				{
					alert("Invalid characters found in "+howKnowUsOtherAlert );
					frmName.howKnowUsOtherOSE.value=howKnowUsOtherVal;
					frmName.howKnowUsOtherOSE.focus();
					return false;
				}*/
				/*----------------------If length of name if greater than 25 and less than 2 --------------------*/	
				howKnowUsOtherValLen=howKnowUsOtherVal.length
				if (howKnowUsOtherValLen < 2 || howKnowUsOtherValLen > 200)
				{
					alert( howKnowUsOtherAlert+" should be minimum of 2 characters and maximum of 200 characters");
					frmName.howKnowUsOtherOSE.value=howKnowUsOtherVal;
					frmName.howKnowUsOtherOSE.focus();
					return false;
				}
			}
		}
		
		/******** If Print Ad or Newspaper or Magazine ********/
		if (howKnowUsVal=="Print Ad")
		{
			howKnowUsOtherVal=trimString(frmName.howKnowUsOtherPAD.value);
			var howKnowUsOtherAlert="Newspaper or Magazine Name";
			
			if (howKnowUsOtherVal=="")
			{
				alert("Please enter the "+howKnowUsOtherAlert);
				frmName.howKnowUsOtherPAD.value=howKnowUsOtherVal;
				frmName.howKnowUsOtherPAD.focus();
				return false;
			}
			
			if (howKnowUsOtherVal!="")
			{
				/*---------------------If any special character---------------------*/	
				/*if (!isCharsInBag(howKnowUsOtherVal, " '.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))
				{
					alert("Invalid characters found in "+howKnowUsOtherAlert );
					frmName.howKnowUsOtherPAD.value=howKnowUsOtherVal;
					frmName.howKnowUsOtherPAD.focus();
					return false;
				}*/
				/*----------------------If length of name if greater than 25 and less than 2 --------------------*/	
				howKnowUsOtherValLen=howKnowUsOtherVal.length
				if (howKnowUsOtherValLen < 2 || howKnowUsOtherValLen > 200)
				{
					alert( howKnowUsOtherAlert+" should be minimum of 2 characters and maximum of 200 characters");
					frmName.howKnowUsOtherPAD.value=howKnowUsOtherVal;
					frmName.howKnowUsOtherPAD.focus();
					return false;
				}
			}
		}
		
		/******** If Link from another website ********/
		if (howKnowUsVal=="Link from another website")
		{
			howKnowUsOtherVal=trimString(frmName.howKnowUsOtherLOW.value);
			var howKnowUsOtherAlert="Website URL";
			if (howKnowUsOtherVal=="")
			{
				alert("Please enter the "+howKnowUsOtherAlert);
				frmName.howKnowUsOtherLOW.value=howKnowUsOtherVal;
				frmName.howKnowUsOtherLOW.focus();
				return false;
			}
			
			if (howKnowUsOtherVal!="")
			{
				/*---------------------If any special character---------------------*/	
				if (!checkURL(howKnowUsOtherVal))
				{
					alert("Invalid "+howKnowUsOtherAlert);
					frmName.howKnowUsOtherLOW.value=howKnowUsOtherVal;
					frmName.howKnowUsOtherLOW.focus();
					return false;
				}
			}
		}
		
		/******** If Other ********/
		if (howKnowUsVal=="Other")
		{
			howKnowUsOtherVal=trimString(frmName.howKnowUsOtherOTH.value);
			var howKnowUsOtherAlert="Other text";
			if (howKnowUsOtherVal=="")
			{
				alert("Please enter How did you know about us");
				frmName.howKnowUsOtherOTH.value=howKnowUsOtherVal;
				frmName.howKnowUsOtherOTH.focus();
				return false;
			}
			
			if (howKnowUsOtherVal!="")
			{
				/*---------------------If any special character---------------------*/	
				/*if (!isCharsInBag(howKnowUsOtherVal, " '.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))
				{
					alert("Invalid characters found in "+howKnowUsOtherAlert );
					frmName.howKnowUsOtherOTH.value=howKnowUsOtherVal;
					frmName.howKnowUsOtherOTH.focus();
					return false;
				}*/
				/*----------------------If length of name if greater than 25 and less than 2 --------------------*/	
				howKnowUsOtherValLen=howKnowUsOtherVal.length
				if (howKnowUsOtherValLen < 2 || howKnowUsOtherValLen > 200)
				{
					alert( howKnowUsOtherAlert+" should be minimum of 2 characters and maximum of 200 characters");
					frmName.howKnowUsOtherOTH.value=howKnowUsOtherVal;
					frmName.howKnowUsOtherOTH.focus();
					return false;
				}
			}
		}
	}
	/******** How did you know about us Validation Ends Here ********/
	
	if (contactNameVal!="" && contactEmailVal!="" && contactPhoneVal!="" && contactMessageVal!="" && howKnowUsVal!="")
	{
		var params = "contactName="+contactNameVal+"&contactEmail="+contactEmailVal+"&contactPhone="+contactPhoneVal+"&contactMessage="+contactMessageVal+"&howKnowUs="+howKnowUsVal+"&howKnowUsOther="+howKnowUsOtherVal;
		var http=createobject(); // Calling function to create XMLHTTP Request object
		//alert(http);
		http.open('POST','quickQueryAjax.php',true); // Calling the Ajax page
		//Send the proper header information along with the request
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
		http.onreadystatechange=getresponse; // Calling the function to get response
		http.send(params); // Send the XMLHTTP request to the server
		return false;
	}
	
	// Function to get response for XMLHTTP Request
	function getresponse()
	{
		// If Ready state is 4
		if (http.readyState<4)
		{
			document.getElementById("quickQueryLoaderId").style.display='block';
			document.getElementById("quickQueryId").style.display='none';
		}
		if (http.readyState==4)
		{
			var response=trimString(http.responseText); // Final Response Text
			//alert(response);
			if (response=="mailsent")
			{
				validateCancelQuickQuery();
				document.getElementById("quickQuerySuccessId").style.display="block";
				document.getElementById("quickQueryLoaderId").style.display='none';
				document.getElementById("quickQueryId").style.display='block';
			}
			if (response=="mailnotsent")
			{
				document.getElementById("quickQueryFailerId").style.display="block";
				document.getElementById("quickQueryLoaderId").style.display='none';
				document.getElementById("quickQueryId").style.display='block';
			}
			if (response=="queryfailed")
			{
				document.getElementById("quickQueryFailerId").style.display="block";
				document.getElementById("quickQueryLoaderId").style.display='none';
				document.getElementById("quickQueryId").style.display='block';
			}
			//frmCheck.specialNotation.value="";
		}
	}
	return false;
	//frmName.submit();
	//return true;
}

function showHideOtherBox(howKnowUsVal)
{
	if (howKnowUsVal!="Other Search Engine" && howKnowUsVal!="Print Ad" && howKnowUsVal!="Link from another website" && howKnowUsVal!="Other")
	{
		if(document.getElementById("OSE"))
		{
			document.getElementById("OSE").style.display="none";
		}
		if(document.getElementById("PAD"))
		{
			document.getElementById("PAD").style.display="none";
		}
		if(document.getElementById("LOW"))
		{
			document.getElementById("LOW").style.display="none";
		}
		if(document.getElementById("OTH"))
		{
			document.getElementById("OTH").style.display="none";
		}
	}
	
	/******** If Other Search Engine ********/
	if (howKnowUsVal=="Other Search Engine")
	{
		document.getElementById("OSE").style.display="inline";
		if(document.getElementById("PAD"))
		{
			document.getElementById("PAD").style.display="none";
		}
		if(document.getElementById("LOW"))
		{
			document.getElementById("LOW").style.display="none";
		}
		if(document.getElementById("OTH"))
		{
			document.getElementById("OTH").style.display="none";
		}
	}
	/******** If Print Ad or Newspaper or Magazine ********/
	if (howKnowUsVal=="Print Ad")
	{
		document.getElementById("PAD").style.display="inline";
		if(document.getElementById("OSE"))
		{
			document.getElementById("OSE").style.display="none";
		}
		if(document.getElementById("LOW"))
		{
			document.getElementById("LOW").style.display="none";
		}
		if(document.getElementById("OTH"))
		{
			document.getElementById("OTH").style.display="none";
		}
	}
	/******** If Link from another website ********/
	if (howKnowUsVal=="Link from another website")
	{
		document.getElementById("LOW").style.display="inline";
		if(document.getElementById("OSE"))
		{
			document.getElementById("OSE").style.display="none";
		}
		if(document.getElementById("PAD"))
		{
			document.getElementById("PAD").style.display="none";
		}
		if(document.getElementById("OTH"))
		{
			document.getElementById("OTH").style.display="none";
		}
	}
	/******** If Other ********/
	if (howKnowUsVal=="Other")
	{
		document.getElementById("OTH").style.display="inline";
		if(document.getElementById("OSE"))
		{
			document.getElementById("OSE").style.display="none";
		}
		if(document.getElementById("PAD"))
		{
			document.getElementById("PAD").style.display="none";
		}
		if(document.getElementById("LOW"))
		{
			document.getElementById("LOW").style.display="none";
		}
	}
}

function validateCancelQuickQuery()
{
	var frmName=document.frmQuickQuery;
	frmName.reset();
	if(document.getElementById("OSE"))
	{
		document.getElementById("OSE").style.display="none";
	}
	if(document.getElementById("PAD"))
	{
		document.getElementById("PAD").style.display="none";
	}
	if(document.getElementById("LOW"))
	{
		document.getElementById("LOW").style.display="none";
	}
	if(document.getElementById("OTH"))
	{
		document.getElementById("OTH").style.display="none";
	}
	return false;
}