/*-------------------------------------------------------------------------------------------------
rollOver
-------------------------------------------------------------------------------------------------*/
function rollOver(theObj,upOrDown) {
				
	if(upOrDown == 0) {
		theObj.style.left = "0px";
	}
	else {
		theObj.style.left = "-99px";
	}
}




/*-------------------------------------------------------------------------------------------------
resetEverything()
Called body onLoad... used for when user hits back button to make sure everything is right
-------------------------------------------------------------------------------------------------*/
function resetEverything() {

	// Make sure the right step is showing
	toggleStep(document.getElementById("type_hidden").value);
	
	// Make sure it has the actual default values, not what was just last there
	document.getElementById("to_input").value    = document.getElementById("to_input").defaultValue;
	document.getElementById("from_input").value  = document.getElementById("from_input").defaultValue;
	document.getElementById("email_input").value = document.getElementById("email_input").defaultValue;
	document.getElementById("message").value     = "Hope you love your gift card as much as I love you!"; // Have to do it this way b/c defaultValue for textarea is returning what was left, not the actual default value
	
}



 

/*-------------------------------------------------------------------------------------------------
toggleStep - turns on the next step
-------------------------------------------------------------------------------------------------*/
function toggleStep(elecOrPhys) {

	clearAllErrors();
		
	// Toggle between physical or email address
	if(elecOrPhys == "elec") {
		document.getElementById("elecSendTo").style.display  = "";
		document.getElementById("physSendTo").style.display  = "none";
		document.getElementById("elecAmounts").style.display = "block";
		document.getElementById("physAmounts").style.display = "none";
		
		// Toggle left images
		document.getElementById("leftImage_default").style.display = "none";
		document.getElementById("leftImage_instant").style.display = "block";
		document.getElementById("leftImage_mail").style.display    = "none";
		
	}
	else if(elecOrPhys == "phys") {
		document.getElementById("elecSendTo").style.display  = "none";
		document.getElementById("physSendTo").style.display  = "";
		document.getElementById("elecAmounts").style.display = "none";
		document.getElementById("physAmounts").style.display = "block"; 
		
		// Toggle left images
		document.getElementById("leftImage_default").style.display = "none";
		document.getElementById("leftImage_instant").style.display = "none";
		document.getElementById("leftImage_mail").style.display    = "block";
	}
	else {
		document.getElementById("leftImage_default").style.display    = "block";
	}
	
	document.getElementById("type_hidden").value = elecOrPhys;
	
	
	
}




/*-------------------------------------------------------------------------------------------------
livePreview
-------------------------------------------------------------------------------------------------*/
function livePreview(fieldName,theObject) {

	document.getElementById(fieldName + "_livePreview").innerHTML = theObject.value;

}




/*-------------------------------------------------------------------------------------------------
setRadioValue
This populates the hidden amount field with whatever radio was selected
Also generates the hidden option id field
-------------------------------------------------------------------------------------------------*/
function setRadioValue(aOrB,whichThis,optionId) { 

	// Custom - enters a value in the custom field
	//if(custom == 1) { 
	if(1 == 2) {
	
		// Clean up the amount - this will remove any letters and just allow numbers
		var amount = (!isNaN(parseFloat(whichThis.value))) ? parseFloat(whichThis.value) : ""; // Clean up the amount that the user typed in
		document.getElementById("amount").value              = amount; // Put the cleaned up amount back in
		document.getElementById("customAmountRadio").checked = true;   // Check the custom radio
	
	}
	
	document.getElementById("optionId_hidden" + aOrB).value = optionId;         // Store option id in the hidden field
	document.getElementById("amount_hidden" + aOrB).value   = whichThis.value;  // Store the amount in the hidden field
	document.getElementById("amount_livePreview").innerHTML = whichThis.value;	// Inject in live preview
}



/*-------------------------------------------------------------------------------------------------
toggleStep
-------------------------------------------------------------------------------------------------*/
function addGiftCardToCart() {
	
	// Tabula rasa
		clearAllErrors();
		var error = false; var scrollToTop = false;
	
	// Variable objects
		
		var type     = document.getElementById("type_hidden");
		
		// If it's elec, grab from the hidden fields with A at the end, if phys grab from B
		var aOrBfields = (type.value == "elec") ? "A" : "B";
		var amount     = document.getElementById("amount_hidden" + aOrBfields);
		var optionId   = document.getElementById("optionId_hidden" + aOrBfields);
		var message    = document.getElementById("message");
		var email      = document.getElementById("email_input");	
		var to         = document.getElementById("to_input");
		var from       = document.getElementById("from_input");
	
	// Make sure they selected type
		if(type.value == "") {
			errorField("type",false,"Don't forget to choose<br/> which kind you'd like",0,false); 
			error = true;
			var scrollToTop = true;
		}
	
	// Validate email (only if it's elec)
		if(type.value == "elec" || type.value == "") {
			if(email.value == "" || email.value == email.defaultValue) {
				errorField("email",false,"Where should we<br/> email it to?",1,true); 
				error = true;
			}
			else {
				var emailValidate = checkEmail(email.value); 
				if(!emailValidate) {
					errorField("email",false,"This email looks funny.",1,true);
					error = true;
				}
			}
		}			

	// Make sure to / from isn't blank	
		if((to.value == "" || to.value == to.defaultValue) && (from.value == "" || from.value == from.defaultValue)) { 
			errorField("to",false,"Who From?<br/>Who To?",1,true,-35);
			document.getElementById("from_input").style.backgroundImage = "url(/store/checkout/images/field-status-pink.jpg)";
			error = true; 
		}
		else if(to.value == "" || to.value == to.defaultValue) { 
			errorField("to",false,"Who's is it to?",1,true,-35); 
			error = true; 
		}
		else if(from.value == "" || from.value == from.defaultValue) { 
			errorField("to",false,"Who is it from?",1,true,-35); 
			error = true; 
		}

		
		
	// Passed -- add to cart
	if(error == false) {
		
		// Turn on loading message
		document.getElementById("loading").style.display = "block";
		
		// Send it to ajax to create a custom id
		params     = new Array();i = 0;
		params[i]  = "action=addGiftCardToCart"; i++;	
		params[i]  = "amount="         + amount.value; i++;
		params[i]  = "optionId="       + optionId.value; i++;
		params[i]  = "type="           + type.value; i++;
		params[i]  = "to="             + encodeURIComponent(to.value); i++;
		params[i]  = "from="           + encodeURIComponent(from.value); i++;
		params[i]  = "message="        + encodeURIComponent(message.value); i++;
		params[i]  = "emailTo="        + email.value; i++;
		putResults = "results_addToCart_giftCardBA";
		ajaxMagic('/store/gift-card/giftCard_ajax.php',params,putResults,'','doNotFade','doNotLoad',postAddGiftCardToCart);
	}
	else if(scrollToTop == true) {
		scroll(0,0);
	}

}


// VV

/*-------------------------------------------------------------------------------------------------
postAddGiftCardToCart()
-------------------------------------------------------------------------------------------------*/
function postAddGiftCardToCart() {

	var customId = document.getElementById("customId_hiddenAjax").value; // This is created in the ajax that is called by the above function
	var optionId = document.getElementById("optionId_hiddenAjax").value; 
	
	changeQuantity(optionId, customId, 1, 'product', 'giftCardPA'); // Last variable is where the results were go, will actually be results_addToCart_giftCardPA
	// Note that change quantity will take care of forwarding user to cart 
	
}




/*-------------------------------------------------------------------------------------------------
clearAllErrors()
-------------------------------------------------------------------------------------------------*/
function clearAllErrors() {
	errorField("email",true);
	errorField("to",true);
	errorField("from",true);
	errorField("type",true);
	document.getElementById("from_input").style.backgroundImage = "";
}




/*-------------------------------------------------------------------------------------------------
checkEmail()
-------------------------------------------------------------------------------------------------*/
function checkEmail(email){
	var filter=/^.+@.+\..{2,3}$/
	
	// Pass
	if (filter.test(email)) {
		return true;
	}
	// Fail
	else {
		return false;
	}
}
	

