/*------------------------------------------------------------------------
 Function isStringSearchengine                                                       
                                                                         
 This function will not allow the brakets                                
------------------------------------------------------------------------*/
function isStringSearchengine(sStr,Msg)
{
	var denieList = '[]{}/%&?~*$;<>?\'=)(';
	var denieList2 = '[ ]  { } / % & ? ~ * $ ; < > ? \' = ) ('; 
	
	for (var j = 0 ; j < denieList.length ; j++) {
	    if (sStr.value.indexOf(denieList.charAt(j))!=-1 ) {
			if (Msg == "") {
				alert("These characters must not be used :"+denieList2);
			} else {
				alert(Msg+denieList2);
			}			
			sStr.select();
			return false;
		}
	}
	return true;
}	


/*------------------------------------------------------------------------
 Function to check whether a field is empty or not                                                    
                                                                         
 if empty return true else false
------------------------------------------------------------------------*/
function isEmptyTextField(Checkfield) {
	if (Checkfield == "") {
		return true;
	}
	else {
		nochar  = 0;
		for(i=0; i < Checkfield.length; i++){
			if(Checkfield.charAt(i) != " ") {
				nochar = 1;
			}
		}
		if (nochar == 0){
			return true;
		}
		else {
			return false;
		}
	}
}

/*------------------------------------------------------------------------
 Function to check word count                                                    
                                                                         
 if empty return true else false
------------------------------------------------------------------------*/
function CountWords (this_field, max) {
	var fullStr = this_field.value + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length -1;
	if (fullStr.length <2) {
		word_count = 0;
	}
	if (word_count > max) {
		return false;
	} 
	return true;
}


/*------------------------------------------------------------------------
 Function to check Texarea                                                   
                                                                         
 if true returns 1 else -1
------------------------------------------------------------------------*/

function validateText(control ,min , max , mandatory , messages ) {
	if(control) {
		if(mandatory == true) {
			if(isEmptyTextField(control.value))	{
				control.value="";
				control.focus();

				if(messages[0] != "")
					alert(messages[0]);
				return -1;
			}
		}

		if(min > 0)	{
			if(control.value.length < min)
			{
				control.focus();
				if(messages[1] != "")
					alert(messages[1]);
				return -1;
			}
		}

		if(max > 0)	{
			if(control.value.length>max)
			{
				control.focus();
				if(messages[2] != "")
					alert(messages[2]);
				return -1;
			}
		}
		return 1;
	}
}


function checkdt(objName) {
		var strDatestyle = "US"; //United States date style
		//var strDatestyle = "IN";  //British date style
		var strDate;
		var strDateArray;
		var strDay;
		var strMonth;
		var strYear;
		var intday;
		var intMonth;
		var intYear;
		var booFound = false;
		var datefield = objName;
		var strSeparatorArray = new Array("/");
		var intElementNr;
		var err = 0;
		var strMonthArray = new Array(12);
		strMonthArray[0] = "Jan";
		strMonthArray[1] = "Feb";
		strMonthArray[2] = "Mar";
		strMonthArray[3] = "Apr";
		strMonthArray[4] = "May";
		strMonthArray[5] = "Jun";
		strMonthArray[6] = "Jul";
		strMonthArray[7] = "Aug";
		strMonthArray[8] = "Sep";
		strMonthArray[9] = "Oct";
		strMonthArray[10] = "Nov";
		strMonthArray[11] = "Dec";
		strDate = datefield.value;
		if (strDate.length != 8) 
		{
			return false;
		}
		for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) 
		{
			if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) 
			{
				strDateArray = strDate.split(strSeparatorArray[intElementNr]);
				if (strDateArray.length != 3) 
				{
					err = 1;
					return false;
				}
				else 
				{
					strDay = strDateArray[0];
					strMonth = strDateArray[1];
					strYear = strDateArray[2];
					
				}
				booFound = true;
		   }
		}
		if (booFound == false) {
			if (strDate.length>5) 
			{
				strDay = strDate.substr(0, 2);
				strMonth = strDate.substr(2, 2);
				strYear = strDate.substr(4);
			}
		}
		if (strYear.length == 2) 
		{
		strYear = '20' + strYear;
		}
		// US style
		if (strDatestyle == "US")
		{
			strTemp = strDay;
			strDay = strMonth;
			strMonth = strTemp;
		}

		intday = parseInt(strDay, 10);
		if (isNaN(intday)) 
		{
			err = 2;
			return false;
		}
		intMonth = parseInt(strMonth, 10);
		if (isNaN(intMonth)) 
		{
			for (i = 0;i<12;i++) 
			{
				if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) 
				{
					intMonth = i+1;
					strMonth = strMonthArray[i];
					i = 12;
				}
			}
			if (isNaN(intMonth)) 
			{
				err = 3;
				return false;
			}
		}
		intYear = parseInt(strYear, 10);
		if (isNaN(intYear)) 
		{
			err = 4;
			return false;
		}
		if (intMonth>12 || intMonth<1) 
		{
			err = 5;
			return false;
		}
		if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) 
		{
			err = 6;
			return false;
		}
		if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) 
		{
			err = 7;
			return false;
		}
		if (intMonth == 2) 
		{
			if (intday < 1) 
			{
				err = 8;
				return false;
			}
			if (LeapYear(intYear) == true) 
			{
				if (intday > 29) 
				{
					err = 9;
					return false;
				}
			}
			else 
			{
				if (intday > 28) 
				{
					err = 10;
					return false;
				}
			}
		}
	return true;	
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
	if (intYear % 400 == 0) { return true; }
	}
	else {
	if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

function subDate(document)
{
		var flag1 = false;
		var flag2 = false;
	 
		
		if (document.fromDate.value=="" )
		{
			alert("Please Enter the From Date");
			return false;
	 	 
		}
	 
		else if (document.toDate.value=="")
		{	
			alert("Please Enter the To Date");
			return false;
		 
		}
	else if (document.fromDate.value!= null && document.toDate.value!= null)
			{
	 
			var date1 = new Date();
			var date2 = new Date();
			date1 = Date.parse(document.fromDate.value);
			date2 = Date.parse(document.toDate.value)  
	 
	 if (date1 >date2)
		{	
			alert(" To Date must be greater than From Date");
			return false;
		 
		}
	}
	
		return true ;
	}



function subFun(document)
{
	
	flag1=checkdt(document.fromDate);
	flag2=checkdt(document.toDate);
	
	var flagdate = false;

	if(flag1 == true && flag2==true)
	{
		flagdate=subDate(document);		
	}
	else
	{
		alert("Please enter valid  6 digits dates in mm/dd/yy format");
		return false;
	}
}

function verifyKeyword(document)
	{
		var errorText="";
		var errorFlag=false;
		//document.keywordform.productname.value=document.keywordform.product.options[document.keywordform.product.selectedIndex].text;
		if(document.keywords.value=='')//check if the keywords entered are null
		{
			alert("Please enter the keywords");
			return false;
		}
		else
		{
			var keywords = document.keywords.value;
			if(keywords.indexOf(",")==0)// check if the keywords start with ,
			{
				errorText += "The keywords should not start with comma(,) \n";
				errorFlag=true;
			}
			if(keywords.lastIndexOf(",")==(keywords.length)-1)// check if the keyword end with comma
			{
				errorText += "The keywords should not end with comma(,) \n";
				errorFlag=true
			}
			if(keywords.indexOf(",,") != -1)// check if there are consecutive keywords
			{
				errorText += "Consecutive commas not allowed \n";
				errorFlag=true
			}
		}
		
		
		if(keywords.indexOf(" ")  == 0)
		{
			errorText += "Keyword should not start with a space \n";
			errorFlag=true
		}
		
		len = keywords.length

		if(keywords.charAt(len-1)  == ' ')
		{
			errorText += "Keyword should not end with a space \n";
			errorFlag=true
		}
		
		//keyword = keyword.trim();

		keywordArray = keywords.split(" ");
		
		if(keywordArray.length > 6)
		{
			errorText += "Single phrase in keywords entered for a letter \ncan contain maximum up to 6 words only  \nYou have entered a phrase \'"+keywords+"\' \nwhich contains " + keywordArray.length +" words ";
			errorFlag=true
		}
		
		if(errorFlag==true)// if there is error alert the user
		{
			alert(errorText);
			return false;
		}
				
		var productText="";
		var selectedFlag=false;
		
		return true;
	}

function verifyComments(feedForm)
{
	var errorText ="Please fill in all the information \n";
	var errorFlag=false;

	if(feedForm.yourName.value == '')
	{
		errorText += "-Name \n";
		errorFlag=true;
	}
	
	if(feedForm.comment.value == '')
	{
		errorText += "-Comment \n";
		errorFlag=true;
	}
	

	if(feedForm.fromAddress.value == '')
	{
		errorText += "-Email \n";
		errorFlag=true;
	}else
	{
		if(!validEmail2(feedForm.fromAddress.value))
		{	
			errorText += "-Email not valid \n";
			errorFlag=true;
		}
	}
	 
	if(errorFlag)
	{
		alert(errorText);
		return false;
	}
}

function validEmail2 (emailStr)
		 {
		
		/*if(i=0;i<emailStr.length; i++)
		{
			if(email.charAt(i)=="#" || email.charAt(i)=="*" ||  email.charAt(i)=="$" ||  email.charAt(i)=="&" ||  email.charAt(i)=="^" ||  email.charAt(i)=="!")
		}*/
		/* The following pattern is used to check if the entered e-mail address
		   fits the user@domain format.  It also is used to separate the username
		   from the domain. */
		var emailPat=/^(.+)@(.+)$/
		/* The following string represents the pattern for matching all special
		   characters.  We don't want to allow special characters in the address. 
		   These characters include ( ) < > @ , ; : \ " . [ ]    */
		var specialChars="\\(\\)<>#!%^&*$@,;:\\\\\\\"\\.\\[\\]"
		/* The following string represents the range of characters allowed in a 
		   username or domainname.  It really states which chars aren't allowed. */
		var validChars="\[^\\s" + specialChars + "\]"
		/* The following pattern applies if the "user" is a quoted string (in
		   which case, there are no rules about which characters are allowed
		   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
		   is a legal e-mail address. */
		var quotedUser="(\"[^\"]*\")"
		/* The following pattern applies for domains that are IP addresses,
		   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
		   e-mail address. NOTE: The square brackets are required. */
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
		/* The following string represents an atom (basically a series of
		   non-special characters.) */
		var atom=validChars + '+'
		/* The following string represents one word in the typical username.
		   For example, in john.doe@somewhere.com, john and doe are words.
		   Basically, a word is either an atom or quoted string. */
		var word="(" + atom + "|" + quotedUser + ")"
		// The following pattern describes the structure of the user
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		/* The following pattern describes the structure of a normal symbolic
		   domain, as opposed to ipDomainPat, shown above. */
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		
		
		/* Finally, let's start trying to figure out if the supplied address is
		   valid. */
		
		/* Begin with the coarse pattern to simply break up user@domain into
		   different pieces that are easy to analyze. */
		var matchArray=emailStr.match(emailPat)
		if (matchArray==null) {
		  /* Too many/few @'s or something; basically, this address doesn't
			 even fit the general mould of a valid e-mail address. */
			return false
		}
		var user=matchArray[1]
		var domain=matchArray[2]
		
		// See if "user" is valid 
		if (user.match(userPat)==null) {
			// user is not valid
			  return false
		}
		
		/* if the e-mail address is at an IP address (as opposed to a symbolic
		   host name) make sure the IP address is valid. */
		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) {
			// this is an IP address
			  for (var i=1;i<=4;i++) {
				if (IPArray[i]>255) {
					return false
				}
			}
			return true
		}
		
		// Domain is symbolic name
		var domainArray=domain.match(domainPat)
		if (domainArray==null) {
			   return false
		}
		
		/* domain name seems valid, but now make sure that it ends in a
		   three-letter word (like com, edu, gov) or a two-letter word,
		   representing country (uk, nl), and that there's a hostname preceding 
		   the domain or country. */
		
		/* Now we need to break up the domain to get a count of how many atoms
		   it consists of. */
		var atomPat=new RegExp(atom,"g")
		var domArr=domain.match(atomPat)
		var len=domArr.length
		if (domArr[domArr.length-1].length<2 || 
			domArr[domArr.length-1].length>3) {
		   // the address must end in a two letter or three letter word.
			  return false
		}
		
		// Make sure there's a host name preceding the domain.
		if (len<2) {
		   var errStr="This address is missing a hostname!"
		   return false
		}
		
		// If we've gotten this far, everything's valid!
		return true;
	}

