var g_current_logo;
var g_current_logo_color_r;
var g_current_logo_color_g;
var g_current_logo_color_b;
var g_current_logo_offset_x;
var g_current_logo_offset_y;
var g_current_logo_font_name;
var g_current_logo_font_size;

var g_current_general_url; // Will be initialized at the top of the body

function GetCurrentColor()
{
	return document.getElementById('color')[document.getElementById('color').selectedIndex].value;
}

function GetCurrentColor_FileFriendly()
{
	return GetCurrentColor().toLowerCase().replace(/[^a-z0-9]/g,'_');
}

function GetCurrentLogo()
{
	//return document.getElementById('color')[document.getElementById('color').selectedIndex].value;
	if(g_current_logo)
	{
		return g_current_logo;
	}
	else
	{
		var split_array = document.getElementById('item_main_image').src.split("!")	
		return (split_array[split_array.length-2]);
	}
}

function GetCurrentLogo_FileFriendly()
{
	if(GetCurrentLogo())
	{
		return GetCurrentLogo().toLowerCase().replace(/[^a-z0-9]/g,'_');
	}
}

function Ajax_LogoSwatchImageChangeHandler(logo_swatch_html)
{
	//document.getElementById('tab_body_ajax').innerHTML = test_var;
	//alert(logo_swatch_html);
}

function Ajax_UpdateFromColorDropdown(new_color_value, item_type_code, cart_index)
{
	document.getElementById('item_image_loading_area').style.display = 'block';
	
	var ajaxObj = new Class_Ajax();
	ajaxObj.data_obj.new_color_value = new_color_value;
	if(cart_index >= 0)
		ajaxObj.data_obj.cart_index = cart_index;

	ajaxObj.SendRequest(g_current_general_url+'param=process_item_ajax&item_type_code='+item_type_code+'&operation=update_detailed_display&');
}

function Ajax_UpdateFromSizeDropdown(new_size_value, item_type_code, cart_index)
{
	//document.getElementById('retail_price_row').style.display = 'none';
	document.getElementById('retail_area').innerHTML = 'loading...';
	document.getElementById('price_area').innerHTML = 'loading...';
		
	var ajaxObj = new Class_Ajax();
	ajaxObj.data_obj.new_size_value = new_size_value;
	if(cart_index >= 0)
		ajaxObj.data_obj.cart_index = cart_index;

	ajaxObj.SendRequest(g_current_general_url+'param=process_item_ajax&item_type_code='+item_type_code+'&operation=update_detailed_display&');
}

function Ajax_DetailedDisplayInitialLoad(new_name_value, item_type_code, logo_id, cart_index)
{
	var ajaxObj = new Class_Ajax();
	ajaxObj.data_obj.new_name_value = new_name_value;
	ajaxObj.data_obj.logo_id = logo_id;
	if(cart_index >= 0)
		ajaxObj.data_obj.cart_index = cart_index;

	ajaxObj.SendRequest(g_current_general_url+'param=process_item_ajax&item_type_code='+item_type_code+'&operation=update_detailed_display&');
}

function Ajax_UpdateFromNameInput(new_name_value, item_type_code, cart_index)
{
	//new_name_value = urlencode(new_name_value);

	var ajaxObj = new Class_Ajax();
	ajaxObj.data_obj.new_name_value = new_name_value;
	if(cart_index >= 0)
		ajaxObj.data_obj.cart_index = cart_index;

	ajaxObj.SendRequest(g_current_general_url+'param=process_item_ajax&item_type_code='+item_type_code+'&operation=update_detailed_display&');
}

function Ajax_UpdateFromLogoThumb(logo_id, item_type_code, cart_index)
{
	document.getElementById('item_image_loading_area').style.display = 'block';
	
	document.getElementById('current_logo_swatch_name_area').innerHTML = '<div style="color: #009; font-size: 10px; background-color: #eee">loading...</div>';
	
	var ajaxObj = new Class_Ajax();
	ajaxObj.data_obj.logo_id = logo_id;
	if(cart_index >= 0)
		ajaxObj.data_obj.cart_index = cart_index;

	ajaxObj.SendRequest(g_current_general_url+'param=process_item_ajax&item_type_code='+item_type_code+'&operation=update_detailed_display&');
}

String.prototype.replaceAll = function(regular_expression, replacement_text, reg_ex_delim_char)
{
	return eval('this.replace('+reg_ex_delim_char+regular_expression+reg_ex_delim_char+'g,"'+replacement_text+'")');
}

/********************************************************************************************
NOTE: This is an interesting function but doesn't escape single quotes the way "escape" does.

function urlencode(str)
{
	var div = document.createElement('div');
	var text = document.createTextNode(str);
	div.appendChild(text);
	return div.innerHTML;
}
*********************************************************************************************/


function urlencode(str)
{
	str = escape(str);
	str = str.replace(/\+/g, '%2B');
	//str = str.replace(/%20/g, '+');
	str = str.replace(/\*/g, "%2A");
	str = str.replace(/(\/)/g, '%2F');
	str = str.replace(/@/g, '%40');
	//str = str.replace(/\s/g, '%20');
	
	return str;
}
/*
function urldecode(str)
{
	str = str.replace(/\+/g, ' ');
	str = unescape(str);
	return str;
}
*/

function Ajax_UpdateDetailedDisplayHandler(overlay_text, item_image_path, logo_swatch_image_path, xl_image_full_path, logo_name, new_our_price, new_retail_price, new_sku, new_logo_id)
{

	if(document.getElementById("custom_text"))
	{
		var custom_text = urlencode(document.getElementById("custom_text").value.toUpperCase());
		
		if(overlay_text != custom_text)
		{
			//alert("["+overlay_text+"] is not ["+custom_text+"]");
			
			return false; // Cancel the handler (it's not the newest one)
		}
	}

	if(new_retail_price)
	{
		document.getElementById('retail_price_row').style.display = '';
		document.getElementById('retail_area').innerHTML = new_retail_price;
	}
	else
	{
		document.getElementById('retail_price_row').style.display = 'none';
		document.getElementById('retail_area').innerHTML = 'n/a';
	}
	
	document.getElementById('item_image_loading_area').style.display = 'none';
	
	document.getElementById('price_area').innerHTML = new_our_price;
	document.getElementById('item_main_image').src = item_image_path;
	
	document.getElementById('full_logo_image').src = logo_swatch_image_path;

	pbSrc['item_main_image'] = new Image();
	pbSrc['item_main_image'].src = xl_image_full_path;

	document.getElementById('current_logo_swatch_name_area').innerHTML = logo_name;
	
	document.getElementById('item_sku_area').innerHTML = new_sku;
	
	document.getElementById("hidden_logo_id_input").value = new_logo_id;
	
}

function Ajax_ChangeLogoCategoryState(logo_category_id, item_type_code)
{
	document.getElementById('logo_thumbs_area_expanded!'+logo_category_id).style.display	=(document.getElementById('logo_thumbs_area_expanded!'+logo_category_id).style.display=='none'?'inline':'none');
	document.getElementById('logo_thumbs_area_collapsed!'+logo_category_id).style.display	=(document.getElementById('logo_thumbs_area_collapsed!'+logo_category_id).style.display=='none'?'inline':'none');
	document.getElementById('expanded_marker!'+logo_category_id).style.display		=(document.getElementById('expanded_marker!'+logo_category_id).style.display=='none'?'inline':'none');
	document.getElementById('collapsed_marker!'+logo_category_id).style.display		=(document.getElementById('collapsed_marker!'+logo_category_id).style.display=='none'?'inline':'none');
	
	var ajaxObj = new Class_Ajax();
	ajaxObj.data_obj.logo_category_id = logo_category_id;
	
	if(document.getElementById('logo_thumbs_area_expanded!'+logo_category_id).style.display=='none')
	{
		ajaxObj.data_obj.status = "closed";
	}
	else
	{
		ajaxObj.data_obj.status = "open";
	}
	
	ajaxObj.SendRequest(g_current_general_url+'param=process_item_ajax&item_type_code='+item_type_code+'&operation=update_logo_category_state&');
}

function GetCurrentOffsetX()
{
	return g_current_logo_offset_x;
}
function GetCurrentOffsetY()
{
	return g_current_logo_offset_y;
}
function GetCurrentLogoFontName()
{
	return g_current_logo_font_name;
}
function GetCurrentLogoFontSize()
{
	return g_current_logo_font_size;
}


// resets the referral tab to its original state
function Ajax_ReferralTab_Reset(item_id, item_type_code)
{
	var ajaxObj = new Class_Ajax();
	ajaxObj.data_obj.item_id = item_id;
	ajaxObj.data_obj.item_type_code = item_type_code;

	ajaxObj.SendRequest(g_current_general_url+'param=process_item_ajax&item_type_code='+item_type_code+'&operation=reset_referral_tab&');
}

// submits the data contained in the referral tab
function Ajax_ReferralTab_Submit(item_id, item_type_code)
{
	var error_array = Array();
	var to_email = document.getElementById('tellafriend_to_email').value;
	var from_email = document.getElementById('tellafriend_from_email').value;
	var from_name = document.getElementById('tellafriend_from_name').value;

	if(from_name.length <= 0)
		error_array.push('You must enter your name');
	if(from_email.length <= 0)
		error_array.push('You must enter your email address');
	if(to_email.length <= 0)
		error_array.push('You must enter your friend\'s email address');

	if(error_array.length)
		ShowErrorBox('tellafriend', error_array);
	else
	{
		var ajaxObj = new Class_Ajax();
		ajaxObj.data_obj.item_id = item_id;
		ajaxObj.data_obj.item_type_code = item_type_code;
		ajaxObj.data_obj.to_email = to_email;
		ajaxObj.data_obj.from_email = from_email;
		ajaxObj.data_obj.from_name = from_name;
		ajaxObj.SendRequest(g_current_general_url+'param=process_item_ajax&item_type_code='+item_type_code+'&operation=submit_referral&');
	}
	
}

// updates the display of the referral tab to indicate a successful submission
function Ajax_ReferralTab_UpdateSuccess(item_id, item_type_code)
{
	document.getElementById('referral_tab_body').innerHTML = '<div style="text-align:center;">You have successfully told your friend about this item.  <a href="javascript:void 0;" onclick="Ajax_ReferralTab_Reset(\''+item_id+'\', \''+item_type_code+'\');">Click here</a> to tell another.</div>';
}



// =======================================================================
// tabs
// =======================================================================
var current_tab_id = 1;

function Item_v2_FN_TabOnClick(id)
{
	document.getElementById('tab_'+current_tab_id).className = 'tab_inactive';
	document.getElementById('tab_body_'+current_tab_id).className = 'tab_body_inactive';
	
	document.getElementById('tab_'+id).className = 'tab_active';
	document.getElementById('tab_body_'+id).className = 'tab_body_active';
	current_tab_id = id;
}
function Item_v2_FN_TabOnMouseOver(id)
{
	if(id == current_tab_id)
	{
		document.getElementById('tab_'+current_tab_id).className = 'tab_active_highlight';
	}
	else
	{
		document.getElementById('tab_'+id).className = 'tab_inactive_highlight';
	}
}
function Item_v2_FN_TabOnMouseOut(id)
{
	if(id == current_tab_id)
	{
		document.getElementById('tab_'+id).className = 'tab_active';
	}
	else
	{
		document.getElementById('tab_'+id).className = 'tab_inactive';
	}
}