function change_product_image(image_path)
{
	if ($('PRODUCT_IMAGE').src != image_path) 
	{ 
		$('PRODUCT_IMAGE').src = image_path; 
	}
}


function display_friend_email_popup()
{
	if (document.getElementById('tell_a_friend_popup').style.display == 'none')
	{
		document.getElementById('TELL_A_FRIEND_SENDER_NAME').value = '';
		document.getElementById('TELL_A_FRIEND_EMAIL').value = '';
		
		var anchor_coords = getAnchorPosition('TELL_A_FRIEND_ANCHOR');
		
		document.getElementById('tell_a_friend_popup').style.display = '';
		document.getElementById('tell_a_friend_popup').style.left = anchor_coords.x + 'px';
		document.getElementById('tell_a_friend_popup').style.top = anchor_coords.y + 'px';
		
		document.getElementById('TELL_A_FRIEND_SENDER_NAME').focus();
	}
}

function display_forgot_password_popup()
{
	if (document.getElementById('forgot_password_popup').style.display == 'none') {
		document.getElementById('forgot_password_popup').style.display = '';
		document.getElementById('FORGOT_PASSWORD_LOGIN_ID').value = '';
		//display_popup_mouse('forgot_password_popup');
		//center_div($('forgot_password_popup'));
		/*
		var anchor_coords = getAnchorPosition('FORGOT_PASSWORD_ANCHOR');
		alert('x: ' + anchor_coords.x + ', y: ' + anchor_coords.y);
		document.getElementById('forgot_password_popup').style.left = anchor_coords.x + 'px';
		document.getElementById('forgot_password_popup').style.top = anchor_coords.y + 'px';
		document.getElementById('forgot_password_popup').style.display = '';
		*/
		document.getElementById('FORGOT_PASSWORD_LOGIN_ID').focus();
	}
}

function edit_gift_message()
{
	if (document.getElementById('gift_message_popup').style.display == 'none')
	{
		document.getElementById('gift_message_popup').style.visibility = 'hidden';
		document.getElementById('gift_message_popup').style.display = '';
	
		var popup_coords = get_screen_center('gift_message_popup');
		document.getElementById('gift_message_popup').style.left = popup_coords.x + 'px';
		document.getElementById('gift_message_popup').style.top = popup_coords.y + 'px';
		
		var gift_message = document.getElementById('GIFT_MESSAGE_LISTING_TEXT').innerHTML;
		gift_message = AddCarriageReturns(gift_message);
		document.getElementById('GIFT_MESSAGE').value = gift_message;
		
		var gift_message_max_chars = document.getElementById('GIFT_MESSAGE_MAX_CHARS').innerHTML - 0;
		document.getElementById('GIFT_MESSAGE_COUNT').innerHTML = gift_message_max_chars - gift_message.length;
		
		document.getElementById('gift_message_popup').style.visibility = 'visible';				
	}
}

function get_screen_center(div_name)
{
	var div_width, div_height;
	var coordinates=new Object();
	
	var frameWidth, frameHeight;
	
	if (self.innerWidth)
	{
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
	}
	
	width = document.getElementById(div_name).offsetWidth;
	height = document.getElementById(div_name).offsetHeight;
	
	coordinates.x = Math.round((frameWidth / 2) - (width / 2));
	coordinates.y = Math.round((frameHeight / 2) - (height / 2));
	
	//alert('w: ' + width + ' - h: ' + height);
	
	return coordinates;
}

function display_edit_billing_popup()
{
	if (document.getElementById('edit_billing_popup').style.display == 'none')
	{
		if (document.getElementById('edit_shipping_popup').style.display != 'none')
		{
			document.getElementById('edit_shipping_popup').style.display = 'none';
		}
		
		// init fields
		document.getElementById('EDIT_BILLING_FIRST_NAME').value = document.getElementById('CURRENT_BILLING_FIRST_NAME').value;
		document.getElementById('EDIT_BILLING_LAST_NAME').value = document.getElementById('CURRENT_BILLING_LAST_NAME').value;
		document.getElementById('EDIT_BILLING_ADDRESS_1').value = document.getElementById('CURRENT_BILLING_ADDRESS_1').value;
		
		
		if (document.getElementById('CURRENT_BILLING_ADDRESS_2'))
		{
			document.getElementById('EDIT_BILLING_ADDRESS_2').value = document.getElementById('CURRENT_BILLING_ADDRESS_2').value;
		}
		
		document.getElementById('EDIT_BILLING_COMPANY_NAME').value = document.getElementById('CURRENT_BILLING_COMPANY_NAME').value;		
		document.getElementById('EDIT_BILLING_CITY').value = document.getElementById('CURRENT_BILLING_CITY').value;		
		document.getElementById('EDIT_BILLING_STATE').selectedIndex = (document.getElementById('CURRENT_BILLING_STATE_INDEX').value - 1);
		document.getElementById('EDIT_BILLING_ZIP').value = document.getElementById('CURRENT_BILLING_ZIP').value;	
		document.getElementById('EDIT_BILLING_PHONE').value = document.getElementById('CURRENT_BILLING_PHONE').value;	
		document.getElementById('EDIT_BILLING_EXTENSION').value = document.getElementById('CURRENT_BILLING_EXTENSION').value;	
		
		
		// center billing popup on screen
		document.getElementById('edit_billing_popup').style.visibility = 'hidden';
		document.getElementById('edit_billing_popup').style.display = '';
		
		var popup_coords = getAnchorPosition('edit_billing_anchor');
		var popup_y_offset =  $('edit_billing_popup').offsetHeight - $('edit_billing_anchor').offsetHeight ;
		document.getElementById('edit_billing_popup').style.left = popup_coords.x + 'px';
		document.getElementById('edit_billing_popup').style.top = (popup_coords.y + 2) - popup_y_offset + 'px';
		
		document.getElementById('edit_billing_popup').style.visibility = 'visible';		
	}
}

function display_edit_shipping_popup()
{
	if (document.getElementById('edit_shipping_popup').style.display == 'none')
	{
		if (document.getElementById('edit_billing_popup').style.display != 'none')
		{
			document.getElementById('edit_billing_popup').style.display = 'none';
		}
		
		// init fields
		document.getElementById('EDIT_SHIPPING_FIRST_NAME').value = document.getElementById('CURRENT_SHIPPING_FIRST_NAME').value;
		document.getElementById('EDIT_SHIPPING_LAST_NAME').value = document.getElementById('CURRENT_SHIPPING_LAST_NAME').value;
		document.getElementById('EDIT_SHIPPING_ADDRESS_1').value = document.getElementById('CURRENT_SHIPPING_ADDRESS_1').value;
		
		
		if (document.getElementById('CURRENT_SHIPPING_ADDRESS_2'))
		{
			document.getElementById('EDIT_SHIPPING_ADDRESS_2').value = document.getElementById('CURRENT_SHIPPING_ADDRESS_2').value;
		}
		
		document.getElementById('EDIT_SHIPPING_COMPANY_NAME').value = document.getElementById('CURRENT_SHIPPING_COMPANY_NAME').value;		
		document.getElementById('EDIT_SHIPPING_CITY').value = document.getElementById('CURRENT_SHIPPING_CITY').value;		
		document.getElementById('EDIT_SHIPPING_STATE').selectedIndex = (document.getElementById('CURRENT_SHIPPING_STATE_INDEX').value - 1);
		document.getElementById('EDIT_SHIPPING_ZIP').value = document.getElementById('CURRENT_SHIPPING_ZIP').value;	
		document.getElementById('EDIT_SHIPPING_PHONE').value = document.getElementById('CURRENT_SHIPPING_PHONE').value;	
		document.getElementById('EDIT_SHIPPING_EXTENSION').value = document.getElementById('CURRENT_SHIPPING_EXTENSION').value;	
		
		
		// center shipping popup on screen
		document.getElementById('edit_shipping_popup').style.visibility = 'hidden';
		document.getElementById('edit_shipping_popup').style.display = '';
		
		var popup_coords = getAnchorPosition('edit_shipping_anchor');
		var popup_y_offset =  $('edit_shipping_popup').offsetHeight - $('edit_shipping_anchor').offsetHeight ;
		document.getElementById('edit_shipping_popup').style.left = popup_coords.x + 'px';
		document.getElementById('edit_shipping_popup').style.top = (popup_coords.y + 2) - popup_y_offset + 'px';
				
		document.getElementById('edit_shipping_popup').style.visibility = 'visible';		
	}
}

function send_friend_email(product_id)
{
	var error_str = '';
	
	var sender_name = trim(document.getElementById('TELL_A_FRIEND_SENDER_NAME').value);
	var email = trim(document.getElementById('TELL_A_FRIEND_EMAIL').value);
	
	if (!check_email(email)) { error_str += 'Invalid Email Address\n'; }	
	if (sender_name.length < 1) { error_str += 'Your Name is blank'; }
	
	if (error_str.length) { alert('Please correct the following: \n\n' + error_str); }
	else
	{
		var url = "library/functions/send_friend_email.php";
		url = url + "?q=";
		url = url + "&ajax=1";
		url = url + "&sender_name=" + sender_name;
		url = url + "&email=" + email;
		url = url + "&product_id=" + product_id;
		url = url + "&sid=" + Math.random();		
		
		xmlreqGET(url);		
	}
}

function send_password()
{
	var error_str = '';
	
	var email = trim(document.getElementById('FORGOT_PASSWORD_LOGIN_ID').value);
	
	if (!check_email(email)) { error_str += 'Invalid Email Address\n'; }	
	
	if (error_str.length) { alert('Please correct the following: \n\n' + error_str); }
	else
	{
		var url = "library/functions/send_password.php";
		url = url + "?q=";
		url = url + "&ajax=1";
		url = url + "&email=" + email;
		url = url + "&sid=" + Math.random();		
		
		xmlreqGET(url);		
	}
}

function ConvertCarriageReturns(str)
{
	str = str.replace(/\n/gi, "<br>");
	return str;
}

function AddCarriageReturns(str)
{
	str = str.replace(/<br>/gi, "\n");	
	return str;
}

