function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}


function show_error_dialog(obj, msg) {
	
	var pos = findPos(obj);	
	$("#eror_dialog").css("top", pos[1] - 10);
	$("#eror_dialog").css("left", pos[0] + 20);
	$("#eror_dialog").text(msg);
	$("#eror_dialog").fadeIn("slow");	
}

function close_error_dialog() {
	$("#eror_dialog").fadeOut("slow");	
}


function check_intl_state(obj) {
	var i = obj.selectedIndex;
	if (obj.options[i].value == 'None'){
		alert("Please fill the name of your state or province in the 'Region' field below");
	}
	else {
	}
}

function check_intl_country(obj) {

	var country_code = $(obj).val();
	var id           = $(obj).attr('id');

	var is_shipping  = false;
	if(id.indexOf('s_') > -1 ) {
		is_shipping  = true;
	}

	/** 
	 * If Puerto Rico , Guam
	 */
	if(country_code == 'PR' || country_code == 'GU') {
		//Auto Select United States 
		//Auto Select Puerto Rico as a state
		$(obj).val('US');
		
		if(is_shipping) {
			$("#s_state").val(country_code);
		}
		else {
			$("#b_state").val(country_code);
		}
	}
}

function CheckSelect(obj, orig_status) {

	/** Get Value **/
	var index = obj.selectedIndex;
	var val   = obj.options[index].value;
	
	if(val == 'X') {
		var rslt = confirm('Cancelling this order will refund applied Blood Monies back to the user do you want to continue?');
		if(!rslt) {
			/** set the status back **/
			for(i = 0; i < obj.options.length; i++) {
				if(obj.options[i].value == orig_status) {
					obj.selectedIndex = i;
				}
			}
		}
	}
}


function CheckGoogleCancel(obj, orig_status) {
	var index = obj.selectedIndex;
	var val   = obj.options[index].value;
	
	if(val == 'X') {
		var rslt = confirm('Cancelling this order will cancel in Google Checkout (which is not reversible), do you want to continue?');
		if(!rslt) {
			/** set the status back **/
			for(i = 0; i < obj.options.length; i++) {
				if(obj.options[i].value == orig_status) {
					obj.selectedIndex = i;
				}
			}
		}
	}
}

