var xmlhttp

function checkAlphaNumerics(checkString) {

        var regExp = /^[A-Za-z0-9 ,]$/;

        if(checkString!= null && checkString!= ""){

          for(var i = 0; i < checkString.length; i++){

            if (!checkString.charAt(i).match(regExp)){
				return false;
			}

          }

        }else{
			return false;
        }
		
		return true;
}

 
function searchPlants(str)
{
	
	if(checkAlphaNumerics(str)==false){
	
		$('#search_code').css('background-color', '#f7b3b6');
		$('#error_message').html('<span style="color:red;font-family:Lucida Grande,Lucida Sans,Arial;font-weight:normal;">! Allow letters only (no numbers or special chars).</span>');
		
	}else if(str.length < 3){
	
		$('#search_code').css('background-color', '#f7b3b6');
		$('#error_message').html('<span style="color:red;font-family:Lucida Grande,Lucida Sans,Arial;font-weight:normal;">! Please enter 3 or more letters to begin search.</span>');
		

	}else{

		$('#search_code').css('background-color', '#fff');
		$('#error_message').html('');
		
		if (document.getElementById('search_code').value != '') {
					
			xmlhttp=GetXmlHttpObject();
			if (xmlhttp==null)
			{
			  alert ("Your browser does not support AJAX!");
			  return;
			}
			var url="index.php?option=com_searchplants&view=searchplants&task=searchflowers&format=raw";
			url=url+"&searchLogic="+str;
			url=url+"&sid="+Math.random();
			url += '&search_code=' + encodeURI(document.getElementById("search_code").value);
			
			//document.getElementById("searchHint").innerHTML = '<img src="components/com_searchplants/ajax.gif" border="0">';
			xmlhttp.onreadystatechange=stateChanged;
			xmlhttp.open("GET",url,true);
			xmlhttp.send(null);
			
			document.getElementById("searchHint").innerHTML=xmlhttp.responseText;
	

			
			
		}
	}
}

function stateChanged()
{

if (xmlhttp.readyState==4)
  {
  
	document.getElementById("searchHint").innerHTML=xmlhttp.responseText;
	
	$(function() {
	
				 var url = $('#default-url').val() + "component/searchplants?plantname=" + $('#search_string').val();


				$('#share-url').val(url);
				
				$("#ire_pagination").paginate({
					count 		: $('#total_pages_counted').val(),
					start 		: 1,
					display     : 10,
					border					: true,
					border_color			: '#BEF8B8',
					text_color  			: '#68BA64',
					background_color    	: '#E3F2E1',	
					border_hover_color		: '#68BA64',
					text_hover_color  		: 'black',
					background_hover_color	: '#CAE6C6', 
					images					: false,
					mouse					: 'press',
					onChange     			: function(page){
					
												$('div.div_dialogs').remove();
												
												postvars = 'selected_page=' + page;
												postvars += '&search_code='+$('#search_string').val();
											
												$('#searchList').html('<div style="height:30px;padding:10px;"><img src="/components/com_searchplants/loader.gif"><br/>&nbsp;downloading search results...</div><br/>');
												
												
												
												$.ajax({
													url: 'index.php?option=com_searchplants&task=plantSearchList&format=raw',
													type: 'POST',
													data: postvars,
													error: function(xhr, desc, exceptionobj) {

                                                                                                                 desc = 'We were unable to process your request. Wait a few moments and try again.';
														$('#searchList').html(desc);
													},
													
													
													success: function(desc) {

														$('#searchList').html(desc);
														$('#show_page').html('<small>Showing Page ' + page + ' of ' + $('#total_pages_counted').val() + '</small>');
													}
												});

											  }
				});
			});
  }else{
  
	$('#searchHint').html('<div style="height:30px;padding:10px;"><img src="/components/com_searchplants/loader.gif"><br/>&nbsp;downloading search results...</div><br/>');
  
  }
}


function AddFlowers(str)
{
	if (document.getElementById(str).value != '') {
		xmlhttp=GetXmlHttpObject();
		if (xmlhttp==null){
		  alert ("Your browser does not support AJAX!");
		  return;
		}
		var url="index.php?option=com_searchplants&view=searchplants&task=addflowers&format=raw";
		url=url+"&add="+str;
		url += '&add_code=' + encodeURI(document.getElementById(str).value);
		das = str+'_' + encodeURI(document.getElementById(str).value);

		xmlhttp.onreadystatechange=FlowerChange;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
}

function FlowerChange(){
if (xmlhttp.readyState==4){
  document.getElementById(das).innerHTML=xmlhttp.responseText;
  }else{
  
  document.getElementById(das).innerHTML = '<img src="/components/com_searchplants/add-ajax.gif" border="0">';
 
  }
}

function saveNotes(str){
 
  xmlhttp=GetXmlHttpObject();
  if (xmlhttp==null)
  {
    alert ("Your browser does not support AJAX!");
    return;
  }
  
  var url="index.php?option=com_plants&view=plants&tmpl=component";
  url=url+"&noteSave="+str;
  url=url+"&sid="+Math.random();
 
     for(i = 1 ; i < 4 ; i++){
   plantsNote = 'plantsNote_' + i; 
   url += '&values'+ i +'=' + encodeURI(document.getElementById(plantsNote).value);
  }
  
  url += '&id=' + encodeURI(document.getElementById(str).id);
  
  samukaoi = 'hint_' +encodeURI(document.getElementById(str).id);
  
  document.getElementById(samukaoi).innerHTML = '<img src="components/com_searchplants/ajax.gif" border="0">';
  xmlhttp.onreadystatechange=notes;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}

function notes()
{
if (xmlhttp.readyState==4)
  {
  document.getElementById(samukaoi).innerHTML=xmlhttp.responseText;
  }
}

function searchLimit(str)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	}
	var url="index.php?option=com_searchplants&view=searchplants&task=limitflowers&format=raw";
	url=url+"&searchLimitfilter="+str;
	url=url+"&sid="+Math.random();		
	
	xmlhttp.onreadystatechange=LimitChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	
}

function LimitChanged()
{
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
   
    document.getElementById("searchlimit").innerHTML=xmlhttp.responseText;
	
  }else{
  
	document.getElementById("searchlimit").innerHTML = "<img src='/components/com_searchplants/loader.gif'/><br>&nbsp;&nbsp;please wait downloading plants list...";
  
  }
}

function showHint(txt,plantid,ajaxMsgDiv,myoption)
{
	xmlhttp=GetXmlHttpObject();

	if (xmlhttp==null) {
	  alert ("Your browser does not support XMLHTTP!");
	  return;
	}

	var url="index.php?option=com_plants&view=plants&task=savenote&format=raw";
	url += '&txt=' + encodeURI(document.getElementById(txt).value);
	url += '&Myplantid=' + encodeURI(document.getElementById(plantid).value);
	url += '&ajaxMsg=' + encodeURI(document.getElementById(ajaxMsgDiv).value);
	url += '&myoption=' + encodeURI(document.getElementById(myoption).value);
	hint = 'txtHint_'  + encodeURI(document.getElementById(ajaxMsgDiv).value);
	document.getElementById(hint).innerHTML = 'saving...';
	xmlhttp.onreadystatechange=ajaxHintResponse;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	
}

function ajaxHintResponse()
{
if (xmlhttp.readyState==4)
  {
  document.getElementById(hint).innerHTML=xmlhttp.responseText;
  }
}

function SendMsg(recipientEmail,senderName,senderEmail,subjct,plantid,baseUrl)
{
	xmlhttp=GetXmlHttpObject();

	if (xmlhttp==null) {
	  alert ("Your browser does not support XMLHTTP!");
	  return;
	}
	emailString = document.getElementById(recipientEmail).value;	
	emailpat = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
	if( !emailpat.test( emailString )  ) {
		document.getElementById('txtHinterror').innerHTML = 'Invalid email address';
	}else{
		var url="index.php?option=com_plants&view=plants&tmpl=component";
		url += '&recipientEmail= ' + encodeURI(document.getElementById(recipientEmail).value);
		url += '&senderName= 	 ' + encodeURI(document.getElementById(senderName).value);
		url += '&senderEmail= 	 ' + encodeURI(document.getElementById(senderEmail).value);
		url += '&subjct= 		 ' + encodeURI(document.getElementById(subjct).value);
		url += '&plantidX= 		 ' + encodeURI(document.getElementById(plantid).value);
		url += '&baseUrl= 		 ' + encodeURI(document.getElementById(baseUrl).value);
		document.getElementById('ajaxMsgSend').innerHTML = 'An email has been sent to \n ' + encodeURI(document.getElementById(recipientEmail).value);
		xmlhttp.onreadystatechange=HintResponseMsg;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
}

function HintResponseMsg()
{
if (xmlhttp.readyState==4)
  {
  //document.getElementById('ajaxMsgSend').innerHTML=xmlhttp.responseText;
 // document.getElementById('ajaxMsgSend').innerHTML = 'An email has been sent to \n ' + recipientEmail;
  }
}



function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

$(function() {

	$('#search_code').keypress(function(event) {
		if(event.keyCode=='13'){
			var search_string;
			search_string = $('#search_code').val();
			searchPlants(search_string);
		}
	});
	
	$('#attribute_search_code').keypress(function(event) {
		if(event.keyCode=='13'){
			var search_string;
			search_string = $('#attribute_search_code').val();
			showAttributesSearchResults(search_string, 1);
		}
	});

});

function displaySearchResult()
{
if (xmlhttp.readyState==4)
  {
  
	document.getElementById("searchHint").innerHTML=xmlhttp.responseText;
	
	$(function() {
	
				$('#attribute_search_code').css('background-color', '#fff');
				$('#error_message').html('');

				$('#attributes_button').css('display','block');

				$('#search_buttons').css('width','510px');
				$('#attribute_search_code').css('width','375px');
				
				var thisControls;
				
				thisControls = '<input class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" name="btnSearch" value="Go!" title="Search the Mulch plant database" onclick="return showAttributesSearchResults(document.getElementById(\'attribute_search_code\').value, 1)">';
				thisControls += '<input class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" name="btnReset" value="Clear" title="Clear search criteria and start a new search" onclick="window.location=\'index.php?option=com_searchplants&view=searchplants&task=attibutes\';">';
				thisControls += '<input class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"type="button"  name="btnHelp" value="Help" title="Search instructions" onclick="toggleRow(\'search_instructions\');">';
				
				$('#search_buttons').html(thisControls);
				
				$("#ire_pagination").paginate({
					count 		: $('#total_pages_counted').val(),
					start 		: 1,
					display     : 10,
					border					: true,
					border_color			: '#BEF8B8',
					text_color  			: '#68BA64',
					background_color    	: '#E3F2E1',	
					border_hover_color		: '#68BA64',
					text_hover_color  		: 'black',
					background_hover_color	: '#CAE6C6', 
					images					: false,
					mouse					: 'press',
					onChange     			: function(page){
					
												$('div.div_dialogs').remove();
												
												postvars = 'selected_page=' + page;
												postvars += '&search_code='+$('#search_string').val();
											
												$('#searchList').html('<div style="height:30px;padding:10px;"><img src="/components/com_searchplants/loader.gif"><br/>&nbsp;downloading search results...</div><br/>');
												
												
												
												$.ajax({
													url: 'index.php?option=com_searchplants&task=plantSearchList&format=raw',
													type: 'POST',
													data: postvars,
													error: function(xhr, desc, exceptionobj) {

                                                                                                                desc = 'We were unable to process your request. Wait a few moments and try again.';
														$('#searchList').html(desc);
													},
													
													
													success: function(desc) {

														$('#searchList').html(desc);
														$('#show_page').html('<small>Showing Page ' + page + ' of ' + $('#total_pages_counted').val() + '</small>');
													}
												});

											  }
				});
			});
  }else{
  
	$('#searchHint').html('<div style="height:30px;padding:10px;"><img src="/components/com_searchplants/loader.gif"><br/>&nbsp;downloading search results...</div><br/>');
  
  }
}

function showAttributesSearchResults(str, search_type)
{
	
  if(search_type==1){
  
		if(checkAlphaNumerics(str)==false){
		
			$('#attribute_search_code').css('background-color', '#f7b3b6');
			$('#error_message').html('<span style="color:red;font-family:Lucida Grande,Lucida Sans,Arial;font-weight:normal;">! Allow letters only (no numbers or special chars).</span>');
			
		}else if(str.length < 3){
		
			$('#attribute_search_code').css('background-color', '#f7b3b6');
			$('#error_message').html('<span style="color:red;font-family:Lucida Grande,Lucida Sans,Arial;font-weight:normal;">! Please enter 3 or more letters to begin search.</span>');
			
		}else{
		
			$('#btn_attributes').attr('value','Show Attributes');
			
			$('#attributes_button').css('display','none');
			$('#hidethis').css('display','none');
				
			if (document.getElementById('attribute_search_code').value != '') {
						
				xmlhttp=GetXmlHttpObject();
				if (xmlhttp==null)
				{
				  alert ("Your browser does not support AJAX!");
				  return;
				}
				var url="index.php?option=com_searchplants&view=searchplants&task=searchflowers&format=raw";
				url=url+"&searchLogic="+str;
				url=url+"&sid="+Math.random();
				url += '&search_code=' + encodeURI(document.getElementById("attribute_search_code").value);
				
				//document.getElementById("searchHint").innerHTML = '<img src="components/com_searchplants/ajax.gif" border="0">';
				xmlhttp.onreadystatechange=displaySearchResult;
				xmlhttp.open("GET",url,true);
				xmlhttp.send(null);
			}
		}
	}
}

$(document).ready(function() {
	
	 var url = $('#default-url').val() + "component/searchplants?plantname=" + $('#search_string').val();


	$('#share-url').val(url);
				
	$("#ire_pagination-from-url").paginate({
		count 		: $('#total_pages_counted').val(),
		start 		: 1,
		display     : 10,
		border					: true,
		border_color			: '#BEF8B8',
		text_color  			: '#68BA64',
		background_color    	: '#E3F2E1',	
		border_hover_color		: '#68BA64',
		text_hover_color  		: 'black',
		background_hover_color	: '#CAE6C6', 
		images					: false,
		mouse					: 'press',
		onChange     			: function(page){
					
									$('div.div_dialogs').remove();
												
									postvars = 'selected_page=' + page;
									postvars += '&search_code='+$('#search_string').val();
											
									$('#searchList').html('<div style="height:30px;padding:10px;"><img src="/components/com_searchplants/loader.gif"><br/>&nbsp;downloading search results...</div><br/>');
												
												
												
									$.ajax({
										url: 'index.php?option=com_searchplants&task=plantSearchList&format=raw',
										type: 'POST',
										data: postvars,
										error: function(xhr, desc, exceptionobj) {

                                                                                                                 desc = 'We were unable to process your request. Wait a few moments and try again.';
											$('#searchList').html(desc);
										},
													
													
											success: function(desc) {

											$('#searchList').html(desc);
											$('#show_page').html('<small>Showing Page ' + page + ' of ' + $('#total_pages_counted').val() + '</small>');
										}
									});

									}
	});
});
