// JavaScript Document

var site_url = '/';

// this method is called by the event 'onload' of the index page

function setFocus(pageId)
{
	// code for setting the focus.
	
	if (pageId == 'contact-us')
	{
		document.getElementById('txt_name').focus();
	}	
}

//<><><><><> CODE FOR PRODUCT PAGE <><><><><><>//

// function for redirecting the page.

function showUrl()
{
	window.status = '/redirect';
	return true;
}
function hideUrl()
{
	window.status = '';
}

//<><><><><> CODE FOR PRODUCT PAGE <><><><><><>//

//<><><><><><> CODE FOR RIGHT PANEL SEARCH AREA STARTS HERE <><><><><><>//


function searchResult(keyword)
{	
		//Checking valid char
 	    var validChar="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz -";
	    for(i=0;i<keyword.length;i++)
		{
			if(validChar.indexOf(keyword.charAt(i))==-1)
			{
			   	keyword =keyword.replace(keyword.charAt(i),"");											
			}			
		}
		var newkeyword = keyword.replace(/ /g,'-');

        while(newkeyword.indexOf('--') != -1)
        {
            newkeyword = newkeyword.replace(/--/g,'-');
        }

        arr = newkeyword.split('-');
        tempArr = new Array();

        i = 0;
        k = 0;
        while (i < arr.length)
        {
            if (arr[i] != '')
            {
                tempArr[k] = arr[i];
                k++;
            }
            i++;
        }

        var finalstring = tempArr.join('-');
		return finalstring.toLowerCase();	
}

///////////////////********* SEARCH PLANT PRODUCT////////////////////////////////
function clearPlantField(obj)
{
	if (obj.value == 'Enter Plant keywords')
		document.getElementById('txt_searchPlant').value = '';
}

function resetPlantText(obj)
{
	if (obj.value == '')
		document.getElementById('txt_searchPlant').value = 'Enter Plant keywords';
}

function getPlantSearchResult()
{	
	//document.getElementById('txt_keywords').value = 'Enter keywords';
	var palntKeyword = document.getElementById('txt_searchPlant').value;
	if (palntKeyword != '' && palntKeyword != 'Enter keywords')
	{
		var plantSearchValue = searchResult(palntKeyword);		
		document.getElementById('Form_AName').action = site_url+'search/'+plantSearchValue;
		document.Form_AName.submit();		
	}
	else
	{
		return false;	
	}
}


///////////////////********* SEARCH PRODUCT////////////////////////////////
function clearProductField(obj)
{
	if (obj.value == 'Enter keywords')
		document.getElementById('txt_keywords').value = '';
}

function resetProductText(obj)
{
	if (obj.value == '')
		document.getElementById('txt_keywords').value = 'Enter keywords';
}



function getProductSearchResult(name)
{
	/*if(name == 'home' || name == '')
	{
		document.getElementById('txt_searchPlant').value = 'Enter Plant keywords';
	}*/
	var productKeyword = document.getElementById('txt_keywords').value;

	if (productKeyword != '' && productKeyword != 'Enter keywords')
	{
		var productSearchValue = searchResult(productKeyword);		
		document.getElementById('Form_Name').action =site_url+'search/'+productSearchValue;
		document.Form_Name.submit();		
	}
	else
	{
		return false;	
	}
}

function submitProductSearch(event)
{	
	if (event.keyCode == 13)
	{
		var productKeyword = document.getElementById('txt_keywords').value;
		
		if (productKeyword != '' && productKeyword != 'Enter keywords')
		{
			var productSearchValue = searchResult(productKeyword);		
			document.getElementById('Form_Name').action = site_url+'search/'+productSearchValue;
			document.Form_Name.submit();		
		}
		else
		{
			return false;	
		}	
	}
}

function submitPlantSearch(event)
{	
	if (event.keyCode == 13)
	{
		document.getElementById('txt_keywords').value = 'Enter keywords';
		var palntKeyword = document.getElementById('txt_searchPlant').value;
		if (palntKeyword != '' && palntKeyword != 'Enter Plant keywords')
		{
			var plantSearchValue = searchResult(palntKeyword);		
			document.getElementById('Form_Name').action =site_url+'search/'+plantSearchValue;
			document.Form_Name.submit();		
		}
		else
		{
			return false;	
		}	
	}
}

// function for show/hide tab

function showHideTab(Id)
{
	  
	if (Id == 1)
    {
        document.getElementById('searchTab').className = 'active cursor';
        document.getElementById('search_Div').className = 'link-middle-section';
        document.getElementById('subscribeTab').className = 'cursor';
        document.getElementById('subscribeDiv').className = 'link-middle-section hide';
		document.getElementById('txt_email').value='Enter your email';
    }
    else
    {
		document.getElementById('subscribeTab').className = 'active cursor';
        document.getElementById('subscribeDiv').className = 'link-middle-section';
    	document.getElementById('searchTab').className = 'cursor';
        document.getElementById('search_Div').className = 'link-middle-section hide';
		document.getElementById('txt_keywords').value='Enter keywords';
   }
}

///////////////////********* SEARCH PRODUCT////////////////////////////////
function clearEmailField(obj)
{
	if (obj.value == 'Enter your email')
		document.getElementById('txt_email').value = '';
}

function resetEmailText(obj)
{
	if (obj.value == '')
		document.getElementById('txt_email').value = 'Enter your email';
}

// function for validating subscribing form in the right panel

function validateSubscriberEmail()
{
    var emailAddress = document.getElementById('txt_email').value;

	if (emailAddress != '' && emailAddress != 'Enter your email')
	{
        if ((msg = ValidateEmail(document.getElementById('txt_email'))) != 1)
        {
            alert(msg);
            return false;
            
        }
        else
        {
            document.Form_Name.submit();
        }
	}
	else
	{
		return false;
	}
}

function ValidateEmail(Element)
{
	Flag  = 1;
	count = 0;
	msg = '';

	var alp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_@.-";

	if (Element.value.length > 0)
	{
		for (var i=0; i<Element.value.length; i++)
		{
			temp = Element.value.substring(i, i+1);

			if (alp.indexOf(temp) == -1)
			{
				Flag = 0;
				break;
			}
		} // closing the for loop
	}
	else
	{
		msg = 'Please enter email address';
		Flag = 0;
	}

	for(var i=0; i <= Element.value.length; i++)
	{
		if(Element.value.charAt(0)=='@')
		{
			Flag = 0;
			break;
		}

		if(Element.value.charAt(Element.value.length-1)=='@')
		{
			Flag = 0;
			break;
		}

		if(Element.value.charAt(i)=='@')
		{
			count = count + 1;

			if(count>1)
			{
				Flag = 0;
				break;
			}

			if((Element.value.charAt(i-1)=='.') || (Element.value.charAt(i+1)=='.'))
			{
				Flag = 0;
				break;
			}
		}
		if(Element.value.indexOf('@')==-1)
		{
			Flag = 0;
			break;
		}
		if(Element.value.charAt(0)=='.')
		{
			Flag = 0;
			break;
		}
		if(Element.value.indexOf('.')==-1)
		{
			Flag = 0;
			break;
		}
	  } //closing the for loop

	if (Element.value.charAt(Element.value.length-1) == '.')
		Flag = 0;

	if (Flag != 1)
	{
		if (msg == '')
		{
			msg = 'Invalid Email Address.';
		}
		return msg;
	}
	else
		return 1;
}