var xmlHttp;

function GetXmlHttpObject()
	{
	var xmlHttp=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch(e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch(e)
		{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	return xmlHttp;
	}

function isName(val)
{
	var nameExpr = /^[a-zA-Z.\']+$/
	res = nameExpr.test(val);
	return res;
}

function isEmail(val)
{
 //var emailRegExpr = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9]+\.(([a-zA-Z]{2,3})|([a-zA-Z]{2,2}\.[a-zA-Z]{2,2}))$/
 var emailRegExpr=/[\w.]+\@[\w]+\.(([a-zA-Z]{2,4})|([a-zA-Z]{2,2}\.[a-zA-Z]{2,2}))/
 res = emailRegExpr.test(val);
 return res;
}

function isAlphabets(val)
{
	var nameExpr = /^[a-zA-Z\s\'.]+$/
	res = nameExpr.test(val);
	return res;
}

function isNumber(s)
{
	    var i;
		var bag="1234567890"
	    for (i = 0; i < s.length; i++)
	    {   
	        var c = s.charAt(i);
	        if (bag.indexOf(c) == -1) return false;
	    }
	    return true;
}

function txtArea(val)
{
	var numExpr = /^[a-zA-Z0-9\s]+$/;
	res = numExpr.test(val);
	return res;
}
function isCompany(val)
{
	var numExpr = /^[a-zA-Z0-9\s.\@\#\'_-]+$/;
	res = numExpr.test(val);
	return res;
}
function isAlphanumeric(val)
{
	var regexpr = /^[a-zA-Z0-9\s]+$/;
	res = regexpr.test(val);
	return res;

}
function isMobileno(val)
{
	var regexpr = /^(\+)([0-9]{2,2}[0-9]+)$/;
	res = regexpr.test(val);
	return res;
}

/*************************************************Subscribe Starts ***************************************************/

function doSubscribe()
{
	if(document.frmSubscribe.Subscribe_txt.value.length != 0)
	{
		if(!isEmail(document.frmSubscribe.Subscribe_txt.value))
		{
			document.getElementById('SubscribeAlert').innerHTML = "Please enter valid Email Address."
			document.frmSubscribe.Subscribe_txt.focus();
			return;
		}
	}
	else
	{
			document.getElementById('SubscribeAlert').innerHTML = "Please enter Email Address."
			document.frmSubscribe.Subscribe_txt.focus();
			return;
	}

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  } 
	Subscribe_txt = document.frmSubscribe.Subscribe_txt.value
	xmlHttp.open("POST",'../../scripts/ajax_subscribe.asp',true);
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttp.onreadystatechange=showItem;
	var multiVal  = 'subscribe='+Subscribe_txt;
	multiVal += '&sid='+Math.random();
	xmlHttp.send(multiVal);			
}

function showItem() 
	{ 
			if(xmlHttp.readyState==4)
			{
			 if(xmlHttp.responseText=="You have subscribed Successfully.")
				 document.frmSubscribe.Subscribe_txt.value = "Stay updated";
			 document.getElementById('SubscribeAlert').innerHTML=xmlHttp.responseText;
			}
	}


/***************************************subscribe Ends***************************************/

/***************************************Cleat Text box Starts***************************************/
	function clearTxtBox(objVal)
	{
		if(objVal.value == "Search Suguna Website" || objVal.value == "Stay updated")
		{
			objVal.value = "";
		}		
	}

	function blurTxtBox(obj)
	{
		 if (obj.value.replace(/ /g,'')=="")
		 {
			 if (obj.name=="txtsearch")
			 {
               obj.value = "Search Suguna Website"
			 }

			  if (obj.name=="Subscribe_txt")
			 {
               obj.value = "Stay updated"
			 }

		}
	}

/***************************************Cleat Text box Ends***************************************/


/**************************************Site Search Function********************************/
function doSearch()
{
	searchVal = document.getElementById("txtsearch");
	if(searchVal.value.length == 0 || searchVal.value == "Search Suguna Website")
	{
		alert("Please Enter text for search");
		searchVal.focus();
		return;
	}
	if(searchVal.value.charAt(0) == " ")
	{
		alert("White space are not allowed");
		searchVal.focus();
		return;
	}
	 document.frmSearch.action="../../article_search.asp"
	 document.frmSearch.submit()
}

/*************************************************************Pagination For Search****************************/
function SearchArticle(pageid)
{
	 document.frmPageArticle.submit();
}

/*************************************************************Pagination Previous/Next**************************/
function Searchpage(pageid,previousval)
{
	 document.frmPageArticle.pagearticle.value=pageid
	 //document.frmPageArticle.hdprevious.value = previousval
	 document.frmPageArticle.submit();
}

/***************************************Search Ends***************************************/

/*****************************************Enquiry starts***********************************/
function doEnquire(Parentfolder,formname,Enquiretype,Sublevel,contactus)
{
  document.getElementById('parentfolder').value=Parentfolder;
  document.getElementById('formname').value=formname;
   document.getElementById('Enquiretype').value=Enquiretype;
   document.getElementById('Enquiresublevel').value=Sublevel;
   document.getElementById('Enquirecontact').value=contactus;
  document.EnquiryFolders.action="../../enquiry_form.asp";
  document.EnquiryFolders.submit();
}

/******************************************Enquiry ends************************************/

/***************************************** Enquiry Farmers view****************************/

function doShowOption(selval)
{
	 if (selval=='Farmer')
	 {
       if(document.getElementById('trFarmer'))
	   document.getElementById('trFarmer').style.display='';
	 }
	 else
	{
		 if(document.getElementById('trFarmer'))
		 document.getElementById('trFarmer').style.display='none';
	}
}

function dohideOption(selval)
{
	 if (selval=='Corn' || selval=='Soya')
	 {
		  document.getElementById('trFarmer').style.display='none';
	 }
}
/*********************************************Farmers View Ends******************************/
/*********************************************Submit Enquiry Form*****************************/
function doSubmitEnquiry()
{
	var alertMessages = "";
	var objFocus = "";	
	doc=document.enquiryform;
	if(doc.select_description.value == "")
	{
		alertMessages = alertMessages + "Please choose your description.\n"
		objFocus = "doc.select_description.focus()";
		//return;
	}
	if(doc.firstname.value.length == 0)
	{
		alertMessages = alertMessages + "Please enter your first name.\n";
		if(objFocus == "")
			objFocus = "doc.firstname.focus()";
		//return;
	}
	else
	{
		if(!isAlphabets(doc.firstname.value))
		{
			alertMessages = alertMessages + "Please do not use special characters in first name.\n";
			if(objFocus == "")			
				objFocus = "doc.firstname.focus()";
			//return;
		}
		if(doc.firstname.value.charAt(0) == " ")
		{
			alertMessages = alertMessages + "Please do not use space in first name.\n";
			if(objFocus == "")
				objFocus = "doc.firstname.focus()";
			//return;
		}
	}

	if(doc.lastname.value.length == 0)
	{
		alertMessages = alertMessages + "Please enter your last name.\n";
		if(objFocus == "")
			objFocus = "doc.lastname.focus()";
		//return;
	}
	else
	{
		if(!isAlphabets(doc.lastname.value))
		{
			alertMessages = alertMessages + "Please do not use special characters in last name.\n";
			if(objFocus == "")
				objFocus = "doc.lastname.focus()";
			//return ;
		}
		if(doc.lastname.value.charAt(0) == " ")
		{
			alertMessages = alertMessages + "Please do not use space in last name.\n";
			if(objFocus == "")
				objFocus = "doc.lastname.focus()";
			//return;
		}
	}

	if(doc.company.value.length == 0)
	{
		alertMessages = alertMessages + "Please enter your company / individual name.\n";
		if(objFocus == "")
			objFocus = "doc.company.focus()";
		//return;
	}
	else
	{
		if(!isCompany(doc.company.value))
		{
			alertMessages = alertMessages + "Please do not use special characters in company / individual name.\n";
			if(objFocus == "")
				objFocus = "doc.company.focus()";
			//return;
		}
		if(doc.company.value.charAt(0) == " ")
		{
			alertMessages = alertMessages + "Please do not use space in company / individual name.\n";
			if(objFocus == "")
				objFocus = "doc.company.focus()";
			//return;
		}
	}

	if(doc.jobtitle.value.length == 0)
	{
		alertMessages = alertMessages + "Please enter Job Title / Designation.\n";
		if(objFocus == "")
			objFocus = "doc.jobtitle.focus()";
		//return;
	}
	else
	{
		if(doc.jobtitle.value.charAt(0) == " ")
		{
			alertMessages = alertMessages + "Please do not use space in Job Title / Designation.\n";
			if(objFocus == "")
				objFocus = "doc.jobtitle.focus()";
			//return;
		}
	}

	if(doc.address.value.length == 0)
	{
		alertMessages = alertMessages + "Please enter your address.\n";
		if(objFocus == "")
			objFocus = "doc.address.focus()";
		//return;
	}
	else
	{
		if(doc.address.value.charAt(0) == " ")
		{
			alertMessages = alertMessages + "Please do not use space in address.\n";
			if(objFocus == "")
				objFocus = "doc.address.focus()";
			//return;
		}
	}

	if(doc.state.value == "")
	{
		alertMessages = alertMessages + "Please choose your state of residence.\n"
		if(objFocus == "")
			objFocus = "doc.state.focus()";
		//return;
	}

	if(doc.eMail.value.length == 0)
	{
		alertMessages = alertMessages + "Please enter your email id.\n";
		if(objFocus == "")
			objFocus = "doc.eMail.focus()";
		//return;
	}
	else
	{
		if(!isEmail(doc.eMail.value))
		{
			alertMessages = alertMessages + "Please enter a valid email id.\n";
			if(objFocus == "")
				objFocus = "doc.eMail.focus()";
			//return;
		}
	}

	if(doc.Phone1.value.length == "")
	{
		alertMessages = alertMessages + "Please enter your country code.\n";
		doc.Phone1.value=""
		if(objFocus == "")
			objFocus = "doc.Phone1.focus()";
		//return;
	}
	else
	{
		if(!isNumber(doc.Phone1.value))
		{
			alertMessages = alertMessages + "Please enter only numbers for your country code.\n";
			if(objFocus == "")
				objFocus = "doc.Phone1.focus()";
			doc.Phone1.value=""
			//return;
		}
	}
	if(doc.Phone2.value.length == "")
	{
		alertMessages = alertMessages + "Please enter your STD code.\n";
		if(objFocus == "")
			objFocus = "doc.Phone2.focus()";
		doc.Phone2.value=""
		//return;
	}
	else
	{
		if(!isNumber(doc.Phone2.value))
		{
			alertMessages = alertMessages + "Please enter numbers for the STD code.\n";
			doc.Phone2.value=""
			if(objFocus == "")
				objFocus = "doc.Phone2.focus()";
			//return;
		}
	}

	if(doc.Phone3.value.length == "" )
	{
		alertMessages = alertMessages + "Please enter phone number.\n";
			doc.Phone3.value=""
		if(objFocus == "")
			objFocus = "doc.Phone3.focus()";
		//return;
	}
	else
	{
		if(!isNumber(doc.Phone3.value))
		{
			alertMessages = alertMessages + "Please enter only numbers for your phone number.\n";
			if(objFocus == "")
				objFocus = "doc.Phone3.focus()";
			doc.Phone3.value=""
			//return;
		}
	}


if(doc.mobileno.value.length == "" )
	{
		alertMessages = alertMessages + "Please enter your mobile number.\n";
			doc.mobileno.value=""
		if(objFocus == "")
			objFocus = "doc.mobileno.focus()";
		//return;
	}
	else
	{
		if(!isMobileno(doc.mobileno.value))
		{
			alertMessages = alertMessages + "Please enter a valid mobile number.\n";
			if(objFocus == "")
				objFocus = "doc.mobileno.focus()";
			doc.mobileno.value=""
			//return;
		}
	}


	//************************** Fax text box validation start ***************************
	/*if(doc.Fax1.value.length != 0)
	{
		
		if(!isNumber(doc.Fax1.value))
		{
			alertMessages = alertMessages + "Please enter only numbers for your fax number.\n";
			if(objFocus == "")
				objFocus = "doc.Fax1.focus()";
			//return;
		}
	}*/
	Tempfaxchk = false;
	faxchkalert = false;
	tempvalidationchk = false;
	for(i=1;i<4;i++)
	{
		if(eval("doc.Fax"+i+".value.length") != 0)
			Tempfaxchk = true;
		else
			faxchkalert = false;
	}
	if(Tempfaxchk)
	{
		for(i=1;i<4;i++)
		{
			
			if(eval("doc.Fax"+i+".value.length") == 0)
			{
				faxchkalert = true
				if(objFocus == "")
					objFocus = "doc.Fax"+i+".focus()";
			}			
			if(!isNumber(eval("doc.Fax"+i+".value")))
			{
				tempvalidationchk = true
				if(objFocus == "")
					objFocus = "doc.Fax"+i+".focus()";
			}			
				
		}
	}
	if(faxchkalert)
	{
		alertMessages = alertMessages + "Please enter fax number.\n";
		
	}
	if(tempvalidationchk)
	{
		alertMessages = alertMessages + "Please enter only numbers for your fax number.\n";		
	}
	/*if(doc.Fax1.value.length != 0 || )
	{
		if(doc.Fax2.value.length == 0 || doc.Fax3.value.length == 0)
		{
			alertMessages = alertMessages + "Please enter fax number.\n";
			
			//return;
		}
		else if(!isNumber(doc.Fax2.value) || (!isNumber(doc.Fax3.value)))
		{
			alertMessages = alertMessages + "Please enter only numbers for your fax number.\n";
			if(objFocus == "")
				objFocus = "doc.Fax1.focus()";
			//return;
		}			

	}*/
		//************************** Fax text box validation Stop ***************************
	if(alertMessages != "")
		{
			alert(alertMessages);
			eval(objFocus);
			return;
		}	
	
	document.enquiryform.action="../../scripts/enquiry_formDAO.asp"
	document.enquiryform.submit();


}
/***************************************************Submit Ends********************************/

/*********************************************Limit TestArea *****************************/
function doLimit(obj,limit)
{
 if(obj.value.length>=limit)
 {
   alert("You cannot enter more than "+limit+" characters for "+obj.title)
   obj.value=obj.value.substr(0,(limit-1))
   obj.focus()
   return;
 }
}
/***************************************************Submit Ends********************************/
function disTxtBox(objval)
{
	res = objval.options[objval.selectedIndex].text;
	if (res == "Others")
		document.getElementById("hearabtothers").style.display = '';
	else
		document.getElementById("hearabtothers").style.display= "none";
}
 function validateDate(val)
 {
	var tempres = false;
	var chkval;
	splitval = val.split("/")
	if(splitval.length == 3)
	 {
		for(i=0;i<3;i++)
		 {
			if(parseInt(splitval[i]) == "NaN")
				tempres = false;
			else
				tempres = true;		 
			if(i != 2)
			 {
				if(splitval[i].length > 2 || splitval[i].length == 0)
					tempres = false				
			 }
			 else
			 {
				if(splitval[i].length == 4)
					tempres = true
			 }			 
		}
		if(parseInt(splitval[0]) > 31)
			tempres = false;
		if(parseInt(splitval[1]) > 12)
			tempres = false;
		curryear =new Date();
		currmonth=curryear.getMonth()+1;
		currday=curryear.getDate();
		curryear = curryear.getFullYear();	
		
		temp_chkyear = parseInt(splitval[2]);
		if(parseInt(splitval[2]) < parseInt(1900) || parseInt(splitval[2]) > curryear)
		 {
		   tempres = false;
         }
		if(parseInt(splitval[2]) == curryear && parseInt(splitval[1]) > currmonth)
		 {
			tempres = false;
         }
		if(parseInt(splitval[2]) == curryear && parseInt(splitval[1]) == currmonth && parseInt(splitval[0])>currday)
		 {
			tempres = false;
		 }
	 }		 
	 else
		tempres = false;

		if (tempres)
		{
			 tempres = IsValidDate(splitval[0],splitval[1],splitval[2])
		}
		
	 if(tempres)
	 {
			tempfullDate = splitval[1] + "/" + splitval[0] + "/"+ splitval[2];			
			doc.hdnewdob.value = tempfullDate;	
	 }

	 return tempres
 }

function doSubmitPassKey()
{
	 username=document.XMLPass.username.value
	 username=username.replace(/ /g,'')
	 if (username=="")
	 {
		 alert("Please Enter Your User Name")
		  return
	 }
	 passkey=document.XMLPass.XMLPasskey.value
	 passkey=passkey.replace(/ /g,'')
	 if (passkey=="")
	 {
		 alert("Please Enter Your Passkey")
		  return
	 }
		 
		 document.XMLPass.submit();
}
function doDownload()
{
   document.XMLPass.action="download.asp"
  document.XMLPass.target="_blank"
  document.XMLPass.submit();
  document.XMLPass.target=""
}
function doLogout()
{
	  document.XMLPass.action=""
  document.XMLPass.submit();
  document.XMLPass.target="" 
}

function IsValidDate(Day,Mn,Yr){
    var DateVal = Mn + "/" + Day + "/" + Yr;
    var dt = new Date(DateVal);

    if(dt.getDate()!=Day){
        return(false);
        }
    else if(dt.getMonth()!=Mn-1){
    //this is for the purpose JavaScript starts the month from 0
        return(false);
        }
    else if(dt.getFullYear()!=Yr){
        return(false);
        }
        
    return(true);
 }
