var calc_form = new Object;

calc_form.condition = null;
calc_form.options =  new Array();
calc_form.accessories =  new Array();
calc_form.flip = new Object();

var price_multiplier = false;
var category_bonus = false;
var category_bonus_zero = false;
var promotional_item_message = false;
var promotional_modifier = false;
var min_promo_items = 1;

var adding_to_cart = false;

var pay_term = (nx.donate_partner) ? 'Value' : "We'll Pay";
var price_term = (nx.donate_partner) ? 'value' : 'price';

function select_list_item(select_obj,mod_id){
		
	var selected = select_obj.options[select_obj.selectedIndex];
	
	if(!selected.value) {
	
		calc_form.options[mod_id] = null;
	
	}
	else {
	
		calc_form.options[mod_id] = selected.value;
	
	}
	
	
	if(validate_calc()){
		
		if(nx.unboxed) {
			$('show-price-button').addClassName('active');
			reset_unbox_price();
		}
		else {
			update_price();
		}
		
	}
	else {
		
		if(!nx.unboxed) {
		
			reset_calc();
			set_price_content('Please answer all questions at left to get your '+price_term+'.');
			
		}
		
	}
	
}

function reset_price() {
	
}

function click_condition(condition){

	var var_name = 'product-condition';
	
	var current = calc_form.condition;
	
	if(current != condition){
		
		reset_calc();
		
		if(current){
		
			var current_id = var_name + '-' + current;
			
			var a_obj = new getObj(current_id);
			
			a_obj.obj.className = var_name + '-off';
		
		}
		
		var id =  var_name + '-' + condition;
		
		var a_obj = new getObj(id);
		
		a_obj.obj.className = var_name + '-on';
		
		calc_form.condition = condition;
		
		if(validate_calc()){
			
			update_price();
		}
	}
}

function reset_unbox_price() {
	
	set_price_content("<div id='price_header'>"+pay_term+":</div><div id='price_amount'>$--.--</div>");
	
	calc_form.price = false;
	
	$('get-price-button').removeClassName('active');
	
}

function click_option(id, value){
	
	if(nx.unboxed) {
		reset_unbox_price();
	}
	
	var var_name = 'product-option';
	
	var id_yes = var_name + '-' + id + '-yes';
	var id_no = var_name + '-' + id + '-no';
	
	yes_obj = new getObj(id_yes);
	no_obj = new getObj(id_no);
	
	yes_obj.obj.className = 'product-option-yes';
	no_obj.obj.className = 'product-option-no';

	if(value == 'yes'){
		calc_form.options[id] = true;
		yes_obj.obj.className = 'product-option-yes-on';
	}else{
		calc_form.options[id] = false;
		no_obj.obj.className = 'product-option-no-on';
	}
	
	if(validate_calc()){
		
		if(nx.unboxed) {
			$('show-price-button').addClassName('active');
		}
		else {
			update_price();
		}
	}
}

function click_accessory(id, value){
	
	var product_accessory = $('product-accessory-'+id);
	
	if(!product_accessory.hasClassName('product-accessory-on')) {
	
		calc_form.accessories[id] = true;
		
	} else if(product_accessory.hasClassName('product-accessory-on')) {

		calc_form.accessories[id] = false;	
	}
	
	product_accessory.toggleClassName('product-accessory-on');
	
	if(validate_calc()){
		
		update_price();
	}
	
}

function validate_calc(){
	
	if(calc_form.condition == null){
		
		return false;
	}
	
	for(var id in calc_form.options){
		
		var value = calc_form.options[id];
		
		if(value == null){
			
			return false;
		}
	}
	
	// don't update if we've already added an item to the cart.
	if (calc_form.added == true)
		return false;
	
	/*for(var id in calc_form.accessories){
		
		var value = calc_form.accessories[id];
		
		if(value == null){
			
			return false;
		}
	}*/
	
	return true;
}

function reset_calc(){
	
	calc_form.added = false;
	calc_form.price = false;
	
	var a_obj = new getObj('get-price-button');
	a_obj.obj.className = 'hidden';
	
	var b_obj = new getObj('trade-in-button');
	if(b_obj.obj){
		b_obj.obj.className = 'hidden';
	}
}

function show_waiting(){
	
	set_price_content('<img src="/images/public/calc_loader.gif" />');
}

function set_price_content(content){
	
	var price_obj = new getObj('price');
		
	price_obj.obj.innerHTML = '' + content;
}

function set_button_content(content){
	
	var button_content = $('get-price-button-content');
	
	if(button_content) {
	
		button_content.innerHTML = content;
		
		var price_button = $('get-price-button');
		if(price_button) {
			price_button.addClassName('active');
		}
	
	}
	else {
	
		var a_obj = new getObj('get-price-button');
	
		a_obj.obj.className = 'off';
		
		if(!nx.v3) {
			a_obj.obj.innerHTML = content;
		}
	}
}

function price_callback(price){
	
	//alert(price.debug);
	
	if(price.ref_id){
		calc_form.ref_id = price.ref_id;
		price = price.price;
	}
	
	calc_form.added = false;
		
	if(!price){
		price = 0;
	}
	
	price = parseFloat(price);
			
	var pre_promo_price = price.formatMoney();
		
	if(price_multiplier){
			
		price += price * price_multiplier;
	}
	
	if(promotional_modifier){
		
		price = eval(price + promotional_modifier);
	}
	
	var new_price = price.formatMoney();
		
	calc_form.price = price;
	calc_form.pre_promo_price = pre_promo_price;
	
	if(!nx.unboxed
	&& !nx.v3) {
	
		var standard_span = new getObj('standard-price-message');
		var zero_span = new getObj('zero-price-message');
	
	}
	
	if(new_price == '0.00'){
		
		if(!nx.unboxed
		&& !nx.v3) {
			standard_span.obj.className = 'hidden';
		}
		
		if(live.instore) {	
			$('get-price-button').hide();
		}

		if(category_bonus_zero){	
				
			set_price_content('Sorry, this item has no cash value.<br/><span>'+category_bonus_zero+'</span>');
			set_button_content('Recycle it!');
			
			if(!nx.unboxed
			&& !nx.v3) {
				zero_span.obj.className = 'visible';
			}
		
		}else if(promotional_item_message){
			
			set_price_content(promotional_item_message);
			
			set_button_content('Add to cart');
				
		}else{
				
			set_price_content('Sorry, this item has no cash value.');
			set_button_content('Recycle it!');

			if(!nx.unboxed
			&& !nx.v3) {
				zero_span.obj.className = 'visible';
			}
		}
		
	}
	else
	{
		$('get-price-button').show();
		
		var pre_promo_price_curr = convertCurrency(pre_promo_price, nx.currency);
		var new_price_curr = convertCurrency(new_price, nx.currency);
		
		if((price_multiplier || promotional_modifier) && !calc_form.added)
		{
			if(nx.unboxed) {
				set_price_content("<div id='price_header'>Normally: "+pre_promo_price_curr+"<br/>"+pay_term+"</div><div id='price_amount'>"+new_price_curr+"</div>");
			}
			else {
				set_price_content('<span>Normally: ' + pre_promo_price_curr + '</span><br/>'+pay_term+':<br />' + new_price_curr);
			}
			
			set_button_content('Add to cart');
		}
		else if(promotional_item_message)
		{
			set_price_content(promotional_item_message);
			set_button_content('Add to cart');
		}
		else if(category_bonus && !calc_form.added)
		{
			set_price_content(pay_term + ': ' + new_price_curr + '<br/><span>'+category_bonus+'</span>');
			set_button_content('Add to cart');
		}
		else if (!calc_form.added)
		{
			if(nx.unboxed) {
				set_price_content("<div id='price_header'>"+pay_term+":</div><div id='price_amount'>"+new_price_curr+"</div>");
				set_button_content('Add to cart');
				
			}
			else {
				set_price_content(pay_term + ': ' + new_price_curr);
				set_button_content('Add to Cart');
			}
		}
		
		if(!nx.unboxed
		&& !nx.v3) {
			
			standard_span.obj.className = 'visible';
			zero_span.obj.className = 'hidden';
			
		}
	}
}

function update_price(){
	
	if(validate_calc()){
		
		show_waiting();
		
		var calc = new CalcService();
		
		calc.get_price.call(calc_form);
		calc.get_price.result = price_callback;
		
	}else{
		
		window.alert('Please answer all questions about the condition of your item, thanks!');
	}
}

/* separate update price function for flipswap products */
function update_flipswap_price(){

	var flip_hash = new Hash();

	var flip_calc_form = $('flipswap_questions');

	var form_inputs = flip_calc_form.getElements();
	
	var valid = true;
	
	var working = true;
	
	form_inputs.each(function(input)
	{
		
		if(input.name) {
			
			//store selected values in calc_form array so they can be passed to add_to_cart later
			eval("calc_form.flip." + input.name + ".value = $F(input)");
			
		}
		
		//get the text value of the selected dropdown item
		if(input.options){
			
			if(!$F(input))
			{
				valid = false;
			}
			
			var option_text = input.options[input.selectedIndex].text;
			eval("calc_form.flip." + input.name + ".value_text = option_text");
			
			//alert(input.name + ' : ' + option_text);
			
			if(input.name == 'CELL_PHONE_DAMAGE_DROPDOWN' && option_text == 'Yes'){
				
				working = false;
			}
			
			if(input.name == 'CELL_PHONE_WATER_DAMAGE' && option_text == 'Yes'){
				
				working = false;
			}
			
			if(input.name == 'CELL_PHONE_POWER_UP_DROPDOWN' && option_text == 'No'){
				
				working = false;
			}
			
			if(input.name == 'CELL_PHONE_LCD_FUNCTIONALITY_DROPDOWN' && option_text == 'No'){
				
				working = false;
			}
			
			if(input.name == 'CELL_PHONE_POWER_UP_DROPDOWN' && option_text == 'Yes, battery not included'){
				
				working = false;
			}


		}
	
		//enter yes or no for accessory checkboxes
		if(input.name.search("accessory") != -1){
			if($F(input)){
				eval("calc_form.flip." + input.name + ".value_text = 'Yes'");
			}else{
				eval("calc_form.flip." + input.name + ".value_text = 'No'");
			}
		}
		
		flip_hash.set(input.name, $F(input));
		if(input.name.search("REQ") != -1)
		{
			//if questions is required, but not answered
			if(!$F(input))
			{
				//if required text field, but answer is blank, set value to 11111111111 (this is used to catch a blank IMEI answer from user)
				if(input.name.search("FLTEXT") != -1)
				{
					eval("calc_form.flip." + input.name + ".value = '11111111111'");
					flip_hash.set(input.name, "11111111111");
				}
				//else if required field, but not a text field and answer is blank, return false validation
				else
				{
					valid = false;
				}
			}
		}
	});
	
	if(promotional_item_message && !working){
			
		window.alert('The phone must be in working condition in order to qualify for the promotion.');
		
		return false;

	}
	
	if(valid){
		
		show_waiting();
	
		var flip_json = flip_hash.toObject();
		var flip_calc = new CalcService();
			
		flip_calc.get_flipswap_price.call(flip_json);
		flip_calc.get_flipswap_price.result = price_callback;
		
	}else{
		
		window.alert('Please answer all questions about the condition of your item, thanks!');
	}
}

function click_price(){
	
	if(adding_to_cart) {
		return;
	}
	else {
		adding_to_cart = true;
	}
	
	var my_cart_service = new CartService();
	
	if(calc_form.added){
		
		window.location = "/checkout/";
	
	}else if(calc_form.price != undefined && calc_form.price !== false){
		
		//if we have a price, add to cart
		
		if($('serial_number')){
			
			calc_form.serial_number = $('serial_number').value;
		}
		
		if(nx.unboxed) {
			$('get-price-button-content').innerHTML = 'Adding Item to Cart...';
		}
		else {
			set_button_content('<img src="/images/public/checkout_button_waiting.gif" />');
		}
		
		my_cart_service.add_to_cart.result = function(success) {
			
			//alert(success);
			
			adding_to_cart = false;
				
			if (success.reason == "duplicate_item") {
				
				if(nx.unboxed) {
					set_price_content("<div id='price_header'>Whoops!<br />You can only sell one video game title per order.</div>");
				}
				else {
					set_button_content('Whoops!<br /><span>You can only sell one video<br />game title per order.</span>');
				}
			
			}else if(success.reason == "promo_perfect_value") {
				
				if(nx.unboxed) {
					set_price_content("<div id='price_header'>Whoops! Item has to be in good condition to qualify for this promotion.</div>");
				}
				else {
					set_button_content('Whoops!<br /><span>Item has to be in good condition<br />to qualify for this promotion.</span>');
				}
			
			}else if(success.reason == "promo_max_items") {
				
				if(nx.unboxed) {
					set_price_content("<div id='price_header'>Whoops! Only "+ min_promo_items +" promotion item"+(min_promo_items > 1 ? 's' : '')+" per order "+ (min_promo_items > 1 ? 'are' : 'is') +" allowed for this promotion.</div>");
				}
				else {
					set_button_content('Whoops!<br /><span>Only '+ min_promo_items +' promotion item'+(min_promo_items > 1 ? 's' : '')+' per order<br />'+ (min_promo_items > 1 ? 'are' : 'is')  +' allowed for this promotion.</span>');
				}
				
			}else if(success){
				
				calc_form.added = true;
				
				if(nx.v3) {
				
					$('get-price-button').addClassName('success-checkout-button');
					
					// fix me
					if(nx.partner_id!=36) {
						
				
						if($('trade-in-button').hasClassName('hidden')) {
							$('trade-in-button').removeClassName('hidden');
						}
					}
					
				} else {
					set_button_content('Success!<br/><span>Click to checkout.</span>');
				}
				if(!nx.unboxed) {
					var b_obj = new getObj('trade-in-button');
					b_obj.obj.className = 'off';
				}
				
				nx.cart.get_cart_items();
				
				// Opens the "trade in at a store near you" dialog
				
				/*if(nx.partner_id==36) {
				
					$('get-price-button').setAttribute('onclick',function(){ return false; });
					
					$('get-price-button').observe('click', function() {
					
					   calc_form.added = false;
					
						nx.request_content({
							content_id: 'target_continue_order', 
							title:'Want to speed this up?',
							modal:true,
							width:600,
							height:300
						});	
						
						
					});
					
					nx.cart.cart_link.href="#";
					nx.cart.cart_dropdown_link.href="#";
					
					nx.cart.cart_link.observe('click', function() {
					
					   calc_form.added = false;
					
						nx.request_content({
							content_id: 'target_continue_order', 
							title:'Want to speed this up?',
							modal:true,
							width:600,
							height:300
						});	
						
						
					});
					
					nx.cart.cart_dropdown_link.observe('click', function() {
					
						nx.cart.cart_dropdown_link.href="#";
					
					   	calc_form.added = false;
					
						nx.request_content({
							content_id: 'target_continue_order', 
							title:'Want to speed this up?',
							modal:true,
							width:600,
							height:300
						});	
						
						
					});
					
				}*/
				
			}else{
				if (success.reason == "duplicate_item")
					set_button_content('Whoops!<br /><span>You can only sell one video<br />game title per order.</span>');
				else
					set_button_content('Whoops!<br /><span>That didn\'t work, try again?</span>');
			}
		}
		
		my_cart_service.add_to_cart.call(calc_form);
		
	}else{
		
		adding_to_cart = false;
		
		//get price
		if(!nx.unboxed) {
			update_price();
		}
	}
}

