var messageClass;
window.addEvent('load',function(){
	messageClass = new MessageClass();
	var myTips = new Tips('.thisisatooltip',{
		className:'tipz',
		fixed:true,
		hideDelay: 50,
		showDelay:50

	});


	myTips.addEvents({
	'show': function(tip) {
		tip.fade('in');
	},
	'hide': function(tip) {
		tip.fade('out');
	}
	});

});

function closeMessage()
{
	messageClass.closeMessage();
}

function selectOption(quote_item_id,div_id)
{
	var json_array = JSON.decode($('quote_item_json_array').value);
	var is_selected = false;

	if($(quote_item_id).hasClass('imageGrayedOut'))
	{
		return '';
	}

	$('div-'+div_id).getElements('.imageSelected').each(function (el){

		var tmp = json_array[quote_item_id].select_second_option_id.split(',');

		if(tmp.length > 1)
		{
			var found_second_option = false;
			for(i=0; i < tmp.length; i++)
			{
				if(tmp[i] == el.id)
				{
					found_second_option = true;
				}
			}

			if(!found_second_option)
			{
				el.removeClass("imageSelected");
			}
		}
		else if(json_array[quote_item_id].select_second_option_id != el.id)
		{
			el.removeClass("imageSelected");
		}
		if(el.id == quote_item_id)
		{
			is_selected = true;
		}

	});

	if(!is_selected)
	{
		$(quote_item_id).addClass('imageSelected');
	}
	else
	{
		$(quote_item_id).removeClass('imageSelected');
	}
	grayOutOptions(quote_item_id);
	allowedFinishingOptions();
	calcCost();
}
function dealWithOnChange(element_id,changeType)
{
	if(changeType=='hover')
	{
		$(element_id).addClass('imageHover');
	}
	else if(changeType=='leave')
	{
		$(element_id).removeClass('imageHover');
	}
}

function grayOutOptions()
{
	var json_array = JSON.decode($('quote_item_grayed_out').value);

	$('main_div').getElements('.imageGrayedOut').each(
	function(el)
	{
		el.removeClass('imageGrayedOut');
	}
	);

	//gray out all things that should be with what is selected
	$('main_div').getElements('.imageSelected').each(
	function(el)
	{
		try
		{
			for(var i =0; i < json_array[el.id].length; i++)
			{

				$(json_array[el.id][i].grayed_quote_items_id).addClass('imageGrayedOut');
				$(json_array[el.id][i].grayed_quote_items_id).removeClass('imageSelected');
			}
		}
		catch(err)
		{

		}
	}
	);

}

function allowedFinishingOptions()
{
	var json_array = JSON.decode($('quote_finishing_items_allowed').value);

	var finishing_options = JSON.decode($('finishing_options').value);


	for(var i=0; i < finishing_options.length; i++)
	{
		$('finishing_option-'+finishing_options[i].quote_finishing_options_id).disabled = true;
		$('finishing_option-'+finishing_options[i].quote_finishing_options_id).checked = false;
	}

	try
	{
		$('div-1').getElements('.imageSelected').each(function (el){
			var length = json_array[el.id].length;
			for(var i =0; i < length; i++)
			{
				var tmp = $('finishing_option-'+json_array[el.id][i].quote_finishing_options_id);
				tmp.disabled = false;
			}
		});
	}
	catch(err){}
}

function calcCost()
{
	fixInputBox($('unique_qty'),0);
	displayTurnAroundTime();
	displayQtyTypeOptions();
	var is_replication = false;
	var cost_json = JSON.decode($('cost_break_down').value);
	var quote_items_json = JSON.decode($('quote_item_json_array').value);
	var finishing_options_cost_break_down = JSON.decode($('finishing_options_cost_break_down').value);

	var qty_id = returnQuantityID();
	//var lower_qty = $('quantity').options[$('quantity').selectedIndex].innerHTML;
	var lower_qty = returnLowerQty(qty_id);

	if(qty_id==1)
	{
		fixInputBox($('unique_qty'),0);

		//if($('unique_qty').value > 99)$('unique_qty').value =99;

		var typed_qty = $('unique_qty').value;

		if(typed_qty >= 10 && typed_qty <=49)
		{
			qty_id = 11;
		}
		else if(typed_qty >= 50 && typed_qty <= 99)
		{
			qty_id = 12;
		}
		lower_qty = typed_qty;
	}
	else
	{

		if(qty_id > 4)
		{
			is_replication = true;
		}
	}



	if(qty_id ==1 || qty_id == 11)
	{
		$('12').addClass('imageGrayedOut');
		$('12').removeClass('imageSelected');

		$('13').addClass('imageGrayedOut');
		$('13').removeClass('imageSelected');

		$('14').addClass('imageGrayedOut');
		$('14').removeClass('imageSelected');
	}
	else
	{
		$('12').removeClass('imageGrayedOut');

		$('13').removeClass('imageGrayedOut');

		$('14').removeClass('imageGrayedOut');
	}



	var lower_cost = 0;
	var total_per_unit = 0;

	var dvd_style_jewel_case_picked = false;

	//get the cost of all the items selected (left side of screen)
	$('main_div').getElements('.imageSelected').each(
	function(el)
	{
		var base_cost = (quote_items_json[el.id].quote_item_base_cost *1);
		if(cost_json[el.id][qty_id].dollar_cost > 0 || cost_json[el.id][qty_id].dollar_cost < 0)
		{
			base_cost += (cost_json[el.id][qty_id].dollar_cost *1);
		}
		else if(cost_json[el.id][qty_id].percent_cost > 0 || cost_json[el.id][qty_id].percent_cost < 0)
		{
			var tmp_percent = Math.abs((cost_json[el.id][qty_id].percent_cost / 100));
			var change_amount = (base_cost * tmp_percent);

			change_amount = (change_amount *1).toFixed(2);
			
			base_cost = base_cost.toFloat();
			change_amount = change_amount.toFloat();
				
			if(cost_json[el.id][qty_id].percent_cost > 0)
			{
				
				base_cost += change_amount;
			}
			else
			{
				base_cost -= change_amount;
			}
			
		}

		//dvd or jewel case
		if(el.id == 10 || el.id == 9)
		{
			dvd_style_jewel_case_picked = true;
		}

		base_cost = (base_cost*1);
		base_cost = (base_cost.toFixed(2)*1);
		total_per_unit += base_cost;






	});

	//figure in finishing options

	try
	{
		finishing_options_cost_break_down = JSON.decode($('finishing_options_cost_break_down').value);
		finishing_options = JSON.decode($('finishing_options').value);
	}
	catch(err)
	{

	}
	var selected_finishing_option = returnSelectedFinishingOption();

	try
	{
		base_cost = 0;


		for(var i=0; i < finishing_options.length; i++)
		{
			if(selected_finishing_option == 3 && dvd_style_jewel_case_picked && is_replication)
			{
				base_cost = 0;
			}
			else if(finishing_options[i].quote_finishing_options_id == selected_finishing_option)
			{
				base_cost = (finishing_options[i].base_cost *1);
			}
		}
		base_cost += (finishing_options_cost_break_down[selected_finishing_option][qty_id].dollar_cost *1);

		base_cost =((base_cost *1).toFixed(2) *1);


	}
	catch(err){
	}

	total_per_unit += (base_cost *1);

	//figure in turn around time
	base_cost = 0;
	var turn_around_time_cost_break_down = JSON.decode($('turn_around_time_cost_break_down').value);
	try
	{
		base_cost = (returnSelectedTurnAroundTimeBaseCost()*1);
		base_cost += (turn_around_time_cost_break_down[returnSelectedTurnAroundTime()][qty_id].dollar_cost *1);

	}
	catch(err){
	}

	total_per_unit += (base_cost.toFixed(2) *1);


	//calc print only, no content
	if($('24').checked)
	{
		total_per_unit += (cost_json[24][qty_id].dollar_cost *1);
		total_per_unit = (total_per_unit.toFixed(2) *1);
	}



	other_items_array = JSON.decode($('other_items').value);
	other_items_break_down = JSON.decode($('other_items_cost_break_down').value);

	var other_items_id = 0;
	var base_cost = 0;
	for(i=0; i < other_items_array.length; i++)
	{
		other_items_id = other_items_array[i].other_items_id;
		if($(other_items_id+'-other_items').checked)
		{
			base_cost = other_items_array[i].base_cost;
			if(other_items_break_down[other_items_id][qty_id].percent_cost > 0 || other_items_break_down[other_items_id][qty_id].percent_cost < 0)
			{
				var tmp_percent = Math.abs((other_items_break_down[other_items_id][qty_id].percent_cost / 100));
				var change_amount = (base_cost * tmp_percent);

				change_amount = (change_amount *1).toFixed(2);

				if(other_items_break_down[other_items_id][qty_id].percent_cost > 0)
				{
					base_cost += change_amount;
				}
				else
				{
					base_cost -= change_amount;
				}

			}
			base_cost =(base_cost.toFloat());
			
			total_per_unit = total_per_unit.toFloat();
			total_per_unit += base_cost;
		}
	}

	//display to screen
	if((lower_qty *1) > 100 && $('3').hasClass('imageSelected'))
	{
		$('price_per_unit').innerHTML = "Call For Quote";
		$('total_price').innerHTML = '';
	}
	else
	{
		total_per_unit = (total_per_unit*1).toFixed(2);

		lower_cost = (total_per_unit).toFloat()* ($('unique_qty').value*1);
		total_per_unit = ((total_per_unit).toFloat());


		$('price_per_unit').innerHTML = '$'+(total_per_unit*1).numberFormat();
		$('total_price').innerHTML = '$'+(lower_cost *1).numberFormat();
	}
}

function returnSelectedFinishingOption()
{
	var finishing_options = JSON.decode($('finishing_options').value);

	var tmp = null;
	for(var i=0; i < finishing_options.length; i++)
	{
		tmp = $('finishing_option-'+finishing_options[i].quote_finishing_options_id);
		if(tmp.checked && !tmp.disabled)
		{
			return finishing_options[i].quote_finishing_options_id;
		}
	}
	return 0;
}
function returnSelectedTurnAroundTime()
{
	var turn_around_time = JSON.decode($('turn_around_time').value);

	var tmp = null;
	for(var i=0; i < turn_around_time.length; i++)
	{
		tmp = $(turn_around_time[i].turn_around_time_id+'_day');
		if(tmp.checked && !tmp.disabled)
		{
			return turn_around_time[i].turn_around_time_id;
		}
	}
	return 0;
}

function returnSelectedTurnAroundTimeBaseCost()
{
	var turn_around_time = JSON.decode($('turn_around_time').value);

	var tmp = null;
	for(var i=0; i < turn_around_time.length; i++)
	{
		tmp = $(turn_around_time[i].turn_around_time_id+'_day');
		if(tmp.checked && !tmp.disabled)
		{
			return turn_around_time[i].base_cost;
		}
	}
	return 0;
}





function displaySendOptions(option)
{
	var func ='';
	var header = '';
	var height='';
	switch(option)
	{
		case 1:
		func = 'returnEmailQuote';
		header = 'Email Quote';
		height = 300;
		break;
		case 2:
		func = 'returnRequestACall';
		header = 'Request a Call';
		height = 380;
		break;
		case 3:
		func = 'returnRequestAnEmail';
		header = 'Request an Email';
		height = 325;
		break;
	}

	new Request({
		method: 'post',
		url: PAGE,
		onComplete : function()
		{
			$('message_body').innerHTML = this.response.text;
			messageClass.showMessage(header,height);
		}
	}).send("func="+func);
}

function returnOtherOptions()
{
	var tmp_object = new Object();
	var failed = false;

	$('message_box_div').getElements('.errorOutline').each(
	function(el)
	{
		el.removeClass('errorOutline');
	}
	);

	if($('first_name').value =='')
	{
		$('first_name').addClass('errorOutline');
		failed = true;
	}

	tmp_object.finishing_option_id = returnSelectedFinishingOption();
	tmp_object.turn_around_time_id = returnSelectedTurnAroundTime();
	tmp_object.qty = returnQty();
	tmp_object.price_per_unit = $('price_per_unit').innerHTML;
	tmp_object.total_price = $('total_price').innerHTML;
	tmp_object.quantity_id = returnQuantityID();

	tmp_object.first_name = $('first_name').value;
	tmp_object.last_name = $('last_name').value;
	tmp_object.company = $('company').value;
	tmp_object.copy_protection = $('1-other_items').checked;

	try
	{
		tmp_object.email = $('email').value;

		if($('email').value=='')
		{
			$('email').addClass('errorOutline');
			failed = true;
		}

	}
	catch(err){}

	try
	{
		tmp_object.phone_number = $('phone_number').value;
		if($('phone_number').value=='')
		{
			$('phone_number').addClass('errorOutline');
			failed = true;
		}
	}
	catch(err){}

	if(failed)
	{
		return '-1';
	}

	return tmp_object;

}

function submitEmailQuote()
{
	var selected_array = returnAllSelectedOptions();
	tmp_object = returnOtherOptions();

	if(tmp_object != '-1')
	{

		new Request({
			method: 'post',
			url: PAGE,
			onComplete : function()
			{
				$('message_body').innerHTML = this.response.text;
				//messageClass.showMessage(header,height);
			}
		}).send("func=submitEmailQuote&selected_array="+JSON.encode(selected_array)+"&other_vars="+JSON.encode(tmp_object));
	}
}

function submitRequestCall()
{
	var selected_array = returnAllSelectedOptions();
	tmp_object = returnOtherOptions();
	tmp_object.request_a_call = 'true';
	tmp_object.best_time_to_call = '';

	if(tmp_object != '-1')
	{

		$('message_body').getElements('.search_best_time_to_call').each(function (el)
		{
			if(el.checked)
			{
				tmp_object.best_time_to_call = $('best_time_'+el.value).innerHTML;
			}

		});

		new Request({
			method: 'post',
			url: PAGE,
			onComplete : function()
			{
				$('message_body').innerHTML = this.response.text;
			}
		}).send("func=submitEmailQuote&selected_array="+JSON.encode(selected_array)+"&other_vars="+JSON.encode(tmp_object));
	}
}

function submitRequestAnEmail()
{
	var selected_array = returnAllSelectedOptions();
	tmp_object = returnOtherOptions();
	tmp_object.request_a_email = 'true';

	if(tmp_object != '-1')
	{

		new Request({
			method: 'post',
			url: PAGE,
			onComplete : function()
			{
				$('message_body').innerHTML = this.response.text;
			}
		}).send("func=submitEmailQuote&selected_array="+JSON.encode(selected_array)+"&other_vars="+JSON.encode(tmp_object));
	}
}

function returnQty()
{
/*	var qty_id = returnQuantityID();
	//var lower_qty = $('quantity').options[$('quantity').selectedIndex].innerHTML;
	var lower_qty = returnLowerQty(returnQuantityID());

	if(qty_id==1)
	{
		//unique_qty
		fixInputBox($('unique_qty'),0);

		//if($('unique_qty').value > 99)$('unique_qty').value =99;

		var typed_qty = $('unique_qty').value;

		if(typed_qty >= 10 && typed_qty <=49)
		{
			qty_id = 11;
		}
		else if(typed_qty >= 50 && typed_qty <= 99)
		{
			qty_id = 12;
		}
		lower_qty = typed_qty;
	}
	*/
	
	fixInputBox($('unique_qty'),0);
	
	return $('unique_qty').value;
}

function returnLowerQty(quote_quantity_id)
{
	//returnQuantityID()
	var current_qty = $('unique_qty').value;
	var quantity_options = JSON.decode($('quantity_options').value);

	for(i=0; i < quantity_options.length; i++)
	{
		if(quantity_options[i].quote_quantity_ == quote_quantity_id)
		{
			return quantity_options[i].quantity_lower_range;
		}
	}
	return 0;

}




function returnAllSelectedOptions()
{
	var json_array = JSON.decode($('quote_item_json_array').value);
	var return_array = Array();
	var count = 0;

	$('main_div').getElements('.imageSelected').each(function (el){

		var tmp_object = new Object();

		tmp_object.quote_item_name = json_array[el.id].quote_item_name;
		return_array[count] = tmp_object;
		count++;

	});
	return return_array;

}

function returnQuantityType()
{
	var quantity_id = returnQuantityID();
	var quantity_options = JSON.decode($('quantity_options').value);

	for(i=0; i < quantity_options.length; i++)
	{
		if(quantity_options[i].quote_quantity_id == quantity_id)
		{
			return quantity_options[i].drop_down_type;
		}
	}
	return 'Duplication';
}

function returnQuantityID()
{
	var current_qty = ($('unique_qty').value *1);
	var quantity_options = JSON.decode($('quantity_options').value);

	var selected_qty_id = 0;
	for(i=0; i < quantity_options.length; i++)
	{
		//alert(current_qty +'>='+ quantity_options[i].quantity_lower_range +'&&'+current_qty +'<='+ quantity_options[i].quantity_higher_range );
		if(current_qty >= quantity_options[i].quantity_lower_range && current_qty <= quantity_options[i].quantity_higher_range )
		{
			
			selected_qty_id = quantity_options[i].quote_quantity_id;
			if(quantity_options[i].quote_quantity_id ==4 || quantity_options[i].quote_quantity_id == 5)
			{
				if($('duplication_qty_type').checked)
				{
					selected_qty_id = 4;
				}
				else
				{
					selected_qty_id = 5
				}
			}
			return selected_qty_id;
			
		}
	}
	return 0;


}




function displayTurnAroundTime()
{
	var turn_around_time = JSON.decode($('turn_around_time').value);

	var qty_type = returnQuantityType();

	var duplication_fx = new Fx.Tween($('scroll_turn_around_time_duplication'), {
		duration: 500,
		property: 'height',
		transition: Fx.Transitions.Quad.easeOut,
		onStart: function(el)
		{
			el.setStyle('overflow','hidden');
			el.setStyle('display','');
		},
		onComplete: function(el)
		{
			if(el.getStyle('height') == '0px')el.setStyle('display','none');
		}
	});

	var cd_replication_fx = new Fx.Tween($('scroll_turn_around_time_cd_replication'), {
		duration: 500,
		property: 'height',
		transition: Fx.Transitions.Quad.easeOut,
		onStart: function(el)
		{
			el.setStyle('overflow','hidden');
			el.setStyle('display','');

		},
		onComplete: function(el)
		{
			if(el.getStyle('height') == '0px')el.setStyle('display','none');
		}
	});
	var dvd_replication_fx = new Fx.Tween($('scroll_turn_around_time_dvd_replication'), {
		duration: 500,
		property: 'height',
		transition: Fx.Transitions.Quad.easeOut,
		onStart: function(el)
		{
			el.setStyle('overflow','hidden');
			el.setStyle('display','');
		},
		onComplete: function(el)
		{
			if(el.getStyle('height') == '0px')el.setStyle('display','none');
		}
	});

	var printed_sleeve_duplication_fx = new Fx.Tween($('scroll_turn_around_time_printed_sleeve'), {
		duration: 500,
		property: 'height',
		transition: Fx.Transitions.Quad.easeOut,
		onStart: function(el)
		{
			el.setStyle('overflow','hidden');
			el.setStyle('display','');
		},
		onComplete: function(el)
		{
			if(el.getStyle('height') == '0px')el.setStyle('display','none');
		}
	});



	if($('2').hasClass('imageSelected') && qty_type == 'Duplication')
	{
		$('1-other_items').disabled = false;

	}
	else
	{
		$('1-other_items').disabled = true;
		$('1-other_items').checked = false;
	}


	//if($('1').hasClass('imageSelected') || $('2').hasClass('imageSelected') || $('3').hasClass('imageSelected'))

	if(qty_type == 'Duplication')
	{

		if($('13').hasClass('imageSelected') || $('12').hasClass('imageSelected') || $('14').hasClass('imageSelected'))
		{
			if($('scroll_turn_around_time_printed_sleeve').getStyle('height') == '0px')
			{
				printed_sleeve_duplication_fx.start(0,70);
				if($('scroll_turn_around_time_duplication').getStyle('height') != '0px')duplication_fx.start(70,0);
				if($('scroll_turn_around_time_dvd_replication').getStyle('height') != '0px')dvd_replication_fx.start(90,0);
				if($('scroll_turn_around_time_cd_replication').getStyle('height') != '0px')cd_replication_fx.start(90,0);

			}
		}

		else if($('scroll_turn_around_time_duplication').getStyle('height') == '0px')
		{
			duplication_fx.start(0,70);
			if($('scroll_turn_around_time_dvd_replication').getStyle('height') != '0px')dvd_replication_fx.start(90,0);
			if($('scroll_turn_around_time_cd_replication').getStyle('height') != '0px')cd_replication_fx.start(90,0);
			if($('scroll_turn_around_time_printed_sleeve').getStyle('height') != '0px')printed_sleeve_duplication_fx.start(70,0);
		}
	}
	else
	{
		if($('scroll_turn_around_time_cd_replication').getStyle('height') == '0px' && $('1').hasClass('imageSelected'))
		{
			if($('scroll_turn_around_time_duplication').getStyle('height') != '0px')duplication_fx.start(70,0);
			if($('scroll_turn_around_time_dvd_replication').getStyle('height') != '0px')dvd_replication_fx.start(90,0);
			if($('scroll_turn_around_time_printed_sleeve').getStyle('height') != '0px')printed_sleeve_duplication_fx.start(70,0);
			cd_replication_fx.start(0,90);
		}
		else if($('scroll_turn_around_time_dvd_replication').getStyle('height') == '0px' && $('2').hasClass('imageSelected'))
		{
			if($('scroll_turn_around_time_duplication').getStyle('height') != '0px')duplication_fx.start(70,0);
			dvd_replication_fx.start(0,90);
			if($('scroll_turn_around_time_cd_replication').getStyle('height') != '0px')cd_replication_fx.start(90,0);
			if($('scroll_turn_around_time_printed_sleeve').getStyle('height') != '0px')printed_sleeve_duplication_fx.start(70,0);
		}
		else if($('3').hasClass('imageSelected'))
		{
			if($('scroll_turn_around_time_duplication').getStyle('height') == '0px')duplication_fx.start(0,70);
			if($('scroll_turn_around_time_dvd_replication').getStyle('height') != '0px')dvd_replication_fx.start(90,0);
			if($('scroll_turn_around_time_cd_replication').getStyle('height') != '0px')cd_replication_fx.start(90,0);
			if($('scroll_turn_around_time_printed_sleeve').getStyle('height') != '0px')printed_sleeve_duplication_fx.start(70,0);
		}
	}
}



function displayQtyTypeOptions()
{
	var qty_id =returnQuantityID();
	
	var qty_type = returnQuantityType();
	
	if(qty_type =='Duplication')
	{
		$('duplication_qty_type').checked = true;
		if(qty_id == 4)
		{
			$('replication_qty_type').disabled = false;
			$('duplication_qty_type').disabled = false;
		}
		else
		{
			$('replication_qty_type').disabled = true;
			$('duplication_qty_type').disabled = false;
		}
	}
	else
	{
		$('replication_qty_type').checked = true;
		if(qty_id == 5)
		{
			$('duplication_qty_type').disabled = false;
			$('replication_qty_type').disabled = false;
		}
		else
		{
			$('duplication_qty_type').disabled = true;
			$('replication_qty_type').disabled = false;
		}
	}
	
	
	
	{
		
	}
	
	
	
	
}




Number.implement({

	numberFormat : function(decimals, dec_point, thousands_sep) {
		decimals = Math.abs(decimals) + 1 ? decimals : 2;
		dec_point = dec_point || '.';
		thousands_sep = thousands_sep || ',';
		var matches = /(-)?(\d+)(\.\d+)?/.exec((isNaN(this) ? 0 : this) + '');

		var dec_value = (String(((+matches[3] || 0)))).substr(2,2);
		if((dec_value *1) < 0)dec_value ='00';

		if(dec_value.length ==1)dec_value = dec_value +'0';
		else if(dec_value.length ==0) dec_value = '00';

		var remainder = matches[2].length > 3 ? matches[2].length % 3 : 0;
		return (matches[1] ? matches[1] : '') + (remainder ? matches[2].substr(0, remainder) + thousands_sep : '') + matches[2].substr(remainder).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep) +
		(dec_point + dec_value);
	}


});

function fixInputBox(i,fixedPlace)
{
	i.value = Math.abs(i.value*1);
	i.value = (i.value*1).toFixed(fixedPlace);
	if(i.value == "NaN")
	{
		i.value = "1";
	}
	return i;
}

