function Butterfly(){};

var WindowObjectReference;

Butterfly.showDetailPage = function(inputType)
{
	var x = document.getElementById(inputType); 
	var butterfly= x[x.selectedIndex].value;

	if(butterfly == "")
	{
		return;
	}
	window.open('./butterfly/'+butterfly+'/index.html','_self');
}

Butterfly.chgVal = function(ptVal){
	//A little slight of hand to record the plant type value
	$('ptVal').value = ptVal;
}


Butterfly.addDivTag = function(namecode,commonname,plantType,botanticalName)
{
    if(plantType == 'n')
    {
		var divTag = 'nectarPlants';    	
    }else if(plantType == 'h'){
		var divTag = 'hostPlants';    	   		
    } else {
    	return;
    }
    
    var divTagId = plantType+"_"+namecode;
	var existingData = $(divTag).innerHTML;    	
	
	if(!$(divTagId)){
		var x = Butterfly.AJAXcall(namecode,commonname,plantType,botanticalName);
		$(divTag).innerHTML = existingData+"<div id='"+divTagId+"'><a onclick=\"Butterfly.removeShoppingListItem('"+divTagId+"');return false;\" id=\""+namecode+"\">"+commonname+"</a> <em>"+botanticalName+"</em></div>";		    
	}
	return;
	
}

Butterfly.AJAXcall = function(namecode,commonname,plantType,botanticalName)
{

    var url = 'http://www.gardenswithwings.com/butterfly/AJplantlist.php?nc='+namecode+'&cn='+commonname+'&pt='+plantType+'&bn='+botanticalName;

	new Ajax.Request(url,{
					method: 'post',
					onSuccess: function(transport){
						var rCode = transport.responseText;
					},
					OnFailure: function(){return 0;}
					});	
}


Butterfly.validatePostalCode = function()
{
     var objPostalCode = $('zipcode');
     return false;
     
     var sPostalCode = objPostalCode.value;
     var errMsg = "Postal codes must contain at least five numeric values.  e.g. 10001"; 
	 var patt1 = /\D/;
 
	 var result = objPostalCode.match(patt1);

     if(sPostalCode.length < 5){
       alert(errMsg);
       document.getElementById("zipcode").focus();
       return false;
     } else if (sPostalCode.length > 5)
     {
       alert("Postal codes must not contain more than five numeric values.  e.g. 10001");
       return false;
     } else if (result.length > 0)
     {
     	alert("Postal codes must only contain numeric values.  e.g. 10001");
     	return false;
     }else
     {
       return true;
     }    
}

//Fun little function that deletes an anchor tag and removes the namecode from SimilarTo hidden field.
Butterfly.removeShoppingListItem = function(TagId)
{

//	y = $('div_'+nc);
	y = $(TagId);
	
	if(TagId.slice(0,1) == 'n')
	{
	  x = $('nectarPlants');		
	} else 
	{
	  x = $('hostPlants');		
	}
	
	
    //Remove the butterfly from the list
	x.removeChild(y);
	
	//Remove the plant from the session - another AJAX call.
	//Pass TagId to processing page
    var url = 'http://www.gardenswithwings.com/butterfly/AJplantlist.php?r_nc='+TagId;

	new Ajax.Request(url,{
					method: 'get',
					onSuccess: function(transport){
						var rCode = transport.responseText;
						if(rCode == 1){
							//alert('Deleted');
						}

					},
					OnFailure: function(){return 0;}
					});	
	
}

Butterfly.openPrint = function()
{
    $('screen').media = "print";
    $('screen').href = "http://www.gardenswithwings.com/includes/print.css";
    $('mainnav').style.display = 'none';
    $('email').style.display = 'none';
    $('rightcol').style.display = 'none';
    $('utilnav').style.display = 'none';
    //$('footer').style.display = 'none';

	window.print();
   // window.history.back();
}

Butterfly.openMistake = function()
{
	var url = location.pathname;
	WindowObjectReference = window.open('http://www.gardenswithwings.com/mistake.php?sURL='+url,"winMistakeForm","height=530, width=540,toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no");
	
}

Butterfly.openEmailaFriend = function()
{
	var url = location.pathname;
	WindowObjectReference = window.open('http://www.gardenswithwings.com/emailafriend.php?sURL='+url,"winMistakeForm","height=530, width=540,toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no");
	
}

Butterfly.submitForm = function(formType)
{
	var email = $('emailAddress');
	var name = $('yourName');
	var URL = $('URL');
    var sMessage = $('message');
    var submitButton = $('submitBtn');
    //alert(email.value+name.value+URL.value+sMessage.value);
    var captchaRFld = $('recaptcha_response_field').value;
    var captchaCFld = $('recaptcha_challenge_field').value;

    if(formType == 'emfriend')
	{
		var toEmail = $('toEmailAddress');
		
		var objFields = {emailAddress:email.value, yourName:name.value, sURL:URL.value, message:sMessage.value, toEmailAddress:toEmail.value, rCF:captchaCFld, rRF:captchaRFld};
		var sTarget = 'http://www.gardenswithwings.com/emailafriend.php';
	} else {
		var objFields = {emailAddress:email.value, yourName:name.value, sURL:URL.value, message:sMessage.value, rCF:captchaCFld, rRF:captchaRFld};
		var sTarget = 'http://www.gardenswithwings.com/mistake.php';
	}

//$('errMsg').innerHTML = '';
submitButton.disabled = true;

    new Ajax.Request(
			sTarget,
			{
				method: 'post',
				parameters: objFields,
				onSuccess: function(transport){
					var rCode = transport.responseText;
					
					if( rCode == 0){
						if(formType == 'emfriend')
						{
						alert('Your message was successfully sent.');	
						} else {
						alert('Your message was successfully sent to the Gardens With Wings staff.');							
						}

						//Close Window
						window.close(WindowObjectReference);
						return;
					} 
					
					if(rCode == 1){
						

				      submitButton.disabled = false;
					  alert('Required information is missing.  Please complete form and submit again.');
					  $('yourName').focus();
					}
					
					if(rCode == 2){
						
				      submitButton.disabled = false;
					  alert('Invalid e-mail address.  Please correct and submit again.');
					  $('emailAddress').selected();
					  $('emailAddress').focus();
						
					}
					
					if(rCode == 3){				      
					 alert('reCaptcha code was incorrect.  Please correct and submit again.');
					 submitButton.disabled = false;
					 $('recaptcha_response_field').focus();

					}
				},
				OnFailure: function(){alert(rCode);}
			}
		);	
}