/*-------------------------------------------------------------------------------------------------
geolocate
-------------------------------------------------------------------------------------------------*/
function geolocate() {
	params     = new Array(); i = 0;
	params[i]  = "action=geolocate"; i++;	
	putResults = "geolocate_results";	
	ajaxMagic('/store/cart/cart_ajax.php',params,putResults,'','doNotFade','doNotLoad',postGeolocate);
}


 

/*-------------------------------------------------------------------------------------------------
postGeolocate
-------------------------------------------------------------------------------------------------*/
function postGeolocate() {
	
	var geolocateDebug = $("geolocate_debug").value;
		
	// International	
	if(geolocateDebug != "" && geolocateDebug.indexOf("fresh:US") == -1) { // (geolocate_debug is printed out from cart_ajax.php:geolocate)
		
		// Flip ship info icon
		document.getElementById("shippingInfo_domestic").style.display = "none";
		document.getElementById("shippingInfo_intl").style.display     = "block";	
		
		// Country bubble
		var country = document.getElementById("geolocate_countryForBubble").value;
		
		if(country != "United States" && country != "") {
			document.getElementById("countryBubble_country").innerHTML = country;
			
			// Where is the logo? We want to bounce down relative to that?
			xPosOfLogo = findPosX(document.getElementById("storeLogo"));
			yPosOfLogo = findPosY(document.getElementById("storeLogo"));
		
			// Setup animation object
			var attributes = {
				 points: { to: [xPosOfLogo - 100, yPosOfLogo], control: [ [xPosOfLogo - 150, yPosOfLogo + 40] ] }
			};
			var anim = new YAHOO.util.Motion('countryBubble', attributes, .5, YAHOO.util.Easing.elasticBoth);
				
			// Set duration
			anim.duration = .75; 
			
			// Set ease
			anim.method = YAHOO.util.Easing.easeOut;
		
			// Move it!
			anim.animate();
			
			// DEV - Let me know if this gets called, because it shouldn't! 8/19/10
			params     = new Array(); i = 0;
			params[i]  = "action=notifyAboutBubble"; i++;	
			params[i]  = "country=" + country; i++;	
			putResults = "geolocate_results";	
			ajaxMagic('/store/cart/cart_ajax.php',params,putResults,'','doNotFade','doNotLoad');
			
		}		
		
	}
	// Domestic
	else {
		// Flip ship info icon
		document.getElementById("shippingInfo_domestic").style.display = "block";
		document.getElementById("shippingInfo_intl").style.display     = "none";
	}		
}




/*-------------------------------------------------------------------------------------------------
inputClearer
-------------------------------------------------------------------------------------------------*/
function inputClearer(whichObj) {	
	if(whichObj.value == whichObj.defaultValue) {
		whichObj.value = "";
		whichObj.style.color = "#000000";
	}
}




/*--------------------------------------------------------------------------------------------------
browserDisposal
pass in 4 different values for each browser. If blank, it'll return $macFirefox as default
--------------------------------------------------------------------------------------------------*/
function browserDisposal(macFirefox,safari,ie,winFirefox) {
	 
	 // Get the mac and browser name from the hidden field. It's put there by the php version of this function
	 if(document.getElementById("browserDisposal_hidden")) {
	 	var browserAndOs = document.getElementById("browserDisposal_hidden").value; // Should be something like Mac:firefox
	 	
	 	// Split it to seperate the os and browser
	 	browserAndOs = browserAndOs.split(":");
	 	var os 		= browserAndOs[0];
	 	var browser = browserAndOs[1];

	 }
	 
	 if(browser == "firefox") {
	 	if(os == "Mac") {
	 		returnValue = macFirefox;
	 	}
	 	else {
	 		returnValue = winFirefox;
	 	}
	 }
	 else if(browser.indexOf("ie") >= 0) {
	 	returnValue = ie;
	 }
	  else if(browser.indexOf("ie") >= 0) {
	 	returnValue = ie;
	 }
	  else if(browser.indexOf("ie") >= 0) {
	 	returnValue = ie;
	 }
	 else {
	 	returnValue = macFirefox;
	 }
	 
	return returnValue;
}



/*-------------------------------------------------------------------------------------------------
Find position functions
-------------------------------------------------------------------------------------------------*/
function findPosX(obj) {
	var curleft = 0;
	if(obj.offsetParent)
		while(1) 
		{
		  curleft += obj.offsetLeft;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	if (obj==undefined) return 0;
	var curtop = 0;
	if(obj.offsetParent)
		while(1)
		{
		  curtop += obj.offsetTop;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.y) 
		curtop += obj.y;
	return curtop;
}






/*-------------------------------------------------------------------------------------------------
Check enter
-------------------------------------------------------------------------------------------------*/
function checkEnter(e,submitFunction){ 
// Put this in the last field of a form like this : <input type="text" onKeyPress="checkEnter(event,hey)"> 
// e is the event.. its passed in just as event... It looks like this : keyup charCode=0, keyCode=13
// submit function is the function we'll call, pass it in as a string! 
// if params are needed for the submitFunction they get passed in too - as many as are needed. we'll invoke arguments to fetch all of them. 

// example - function with no paramters :  onKeyPress='checkEnter(event,\"checkZip\")'
// example - function with paramaters :    onkeyup='checkEnter(event,\"changeQuantity\",$optionId,this.value,\"cartText\")'  optionId,this.value,cartText are the paramaters to changeQuantity



	 // Build our paramaters for the submitFunction (if there are any)	
	 var theseParams = "";
	
	 // Go through all the params after the first 2 (first two are e and submitFunction)
	 for (var i = 2; i < checkEnter.arguments.length; i++) {
     	theseParams += "'" + checkEnter.arguments[i] + "'";
     	
     	// If this is the last one, don't add the comma at the end
     	if(i != checkEnter.arguments.length - 1) {
     		theseParams += ",";
     	}
     }
     
   	
	try{ // If i don't use this try statement, it'll throw an error when tab is pushed
		var characterCode;
		if(e && e.which){ //if which property of event object is supported (NN4)
			e = e
			characterCode = e.which //character code is contained in NN4's which property
		}
		else{
			e = event
			characterCode = e.keyCode //character code is contained in IE's keyCode property
		}
		
		if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
			
			// Run our submit function with any paramaters
			eval(submitFunction + '('+theseParams+')');
			return false; 
		}
		else{
			return true;
		}
	}
	catch(err) {
	
	}

}

