Store = Class.create();
Store.prototype = {
	
	initialize: function() {
		if (navigator.language) {
			if (navigator.language.toUpperCase().indexOf("US") != -1)
				$('currency').setValue("USD");
			else
				$('currency').setValue("EUR");
		} else if (navigator.browserLanguage) {
			if (navigator.browserLanguage.toUpperCase().indexOf("US") != -1)
				$('currency').setValue("USD");
			else
				$('currency').setValue("EUR");
		}
		
		$('currency').observe('change', this.changedCurrency.bindAsEventListener(this));
		
		$('quantity_1').observe('change', this.respondToChange);
		$('quantity_1').observe('keyup', this.respondToChange);
		$('quantity_2').observe('change', this.respondToChange);
		$('quantity_2').observe('keyup', this.respondToChange);
		$('quantity_3').observe('change', this.respondToChange);
		$('quantity_3').observe('keyup', this.respondToChange);
		$('quantity_4').observe('change', this.respondToChange);
		$('quantity_4').observe('keyup', this.respondToChange);
		if ( $('quantity_5') && $('upgrade_text')) {
			$('quantity_5').observe('change', this.respondToChange);
			$('quantity_5').observe('keyup', this.respondToChange);
			
			$('quantity_5').observe('click', this.showOldSerialNumberFrom);
			
			if ( $('old_serial_number').getValue().length > 0 ) {
				this.showOldSerialNumberFrom();
			}
			
			/*
			$('quantity_5').setStyle( {cursor: "pointer"} );
			$('quantity_5').observe('click', this.showOldSerialNumberFrom);
			$('upgrade_text').setStyle( {cursor: "pointer"} );
			$('upgrade_text').observe('click', this.showOldSerialNumberFrom);
			*/
		}
		if ( $('quantity_6') ) {
			$('quantity_6').observe('change', this.respondToChange);
			$('luo').observe('click', function(event) { 
				$('quantity_6').checked = !$('quantity_6').checked;
				this.respondToChange();
			}.bindAsEventListener(this));
			$('luo').setStyle( {cursor: "pointer"} );
		}
		
		if ($('upgrade_a')) {
			$('upgrade_a').observe('click', this.showOldSerialNumberFrom);
			$('upgrade_t').observe('click', this.showOldSerialNumberFrom);
			$('upgrade_t').setStyle( {cursor: "pointer"} );
		}
		
		$('submit').observe('click', this.purchase.bindAsEventListener(this));
		
		this.changedCurrency();
		
		if ( $('old_serial_number') ) {
			$('old_serial_number').observe('change', this.checkOldSerialNumber.bindAsEventListener(this));
		}
		
		if ( $('coupon') ) {
			$('coupon').observe('change', this.coupon.bindAsEventListener(this));
			if ( $('coupon').getValue().length > 0 ) {
				this.coupon();
			}
		}
		
		if ( $('old_serial_number').getValue().length > 0 ) {
			$('quantity_1').setValue("");
			this.checkOldSerialNumber();
		}
	},
	
	respondToChange: function(event) {
		var quantities = 0;
		var sum = 0;
		if ($('quantity_1').getValue() != Number.NaN && $('amount_1').innerHTML != Number.NaN) {
			sum += $('quantity_1').getValue() * $('amount_1').innerHTML;
			quantities += $('quantity_1').getValue() * 1;
		}
		if ($('quantity_2').getValue() != Number.NaN && $('amount_2').innerHTML != Number.NaN) {
			sum += $('quantity_2').getValue() * $('amount_2').innerHTML;
			quantities += $('quantity_2').getValue() * 1;
		}
			
		if ($('quantity_3').getValue() != Number.NaN && $('amount_3').innerHTML != Number.NaN) {
			sum += $('quantity_3').getValue() * $('amount_3').innerHTML;
			quantities += $('quantity_3').getValue() * 1;
		}
			
		if ($('quantity_4').getValue() != Number.NaN && $('amount_4').innerHTML != Number.NaN) {
			sum += $('quantity_4').getValue() * $('amount_4').innerHTML;
			quantities += $('quantity_4').getValue() * 1;
		}
			
		if ($('quantity_5') && $('quantity_5').getValue() != Number.NaN && $('amount_5').innerHTML != Number.NaN) {
			sum += $('quantity_5').getValue() * $('amount_5').innerHTML;
			quantities += $('quantity_5').getValue() * 1;
		}
			
		if ($('quantity_6') && $('quantity_6').checked) {
			sum += quantities * $('amount_6').innerHTML;
		}
		
		if (  $('coupon')  && $('coupon').getValue() == "MW2010IDS" ) {
			sum = sum * 0.8;
		}
		
		$('total').innerHTML = sum.toFixed(2);
		
		$('submit').disabled = sum > 0 ? false : true;

		
	},
	
	changedCurrency: function(event) {
		var currencys = $$('strong.currency');
		currencys.each(function(item) {
			if ( $('currency').getValue() == "EUR" )
				item.innerHTML = "&euro;";
			else
				item.innerHTML = "$";
		});
		
		
		if ( $('currency').getValue() == "EUR" ) {
			$('amount_1').innerHTML = "19.99";
			$('amount_2').innerHTML = "34.99";
			$('amount_3').innerHTML = "69.99";
			$('amount_4').innerHTML = "125.99";
			if ($('quantity_5'))
				$('amount_5').innerHTML = "9.99";
			if ($('quantity_6'))
				$('amount_6').innerHTML = "14.99";
		} else {
			$('amount_1').innerHTML = "29.95";
			$('amount_2').innerHTML = "49.95";
			$('amount_3').innerHTML = "99.95";
			$('amount_4').innerHTML = "179.95";
			if ($('quantity_5'))
				$('amount_5').innerHTML = "14.95";
			if ($('quantity_6'))
				$('amount_6').innerHTML = "19.95";
		}
		
		this.respondToChange();
	},
	
	purchase: function(event) {
		var lang;
		if (location.href) {
			if (location.href.indexOf("/de/") != -1)
				lang = "de";
			else
				lang = "en";
		} else {
				lang = "en";
		}
	
		var url = "https://www.paypal.com/cgi-bin/webscr?";
				
		url += "cmd=_cart";
		url += "&upload=1";
		url += "&business=sales@restoroot.com";
		url += "&no_shipping=2";
		url += "&cn=Message";
		url += "&currency_code="+$('currency').getValue();
		url += "&ah=0";
		url += "&cancel_return=http://www.restoroot.com/Store/index.php";
		url += "&return=http://www.restoroot.com/Store/successfulPayment.php";
		url += "&rm=2";
		url += "&lc=US";
		
		var discount = 1;
		if (  $('coupon')  && $('coupon').getValue() == "MW2010IDS" ) {
			discount = 0.8;
		}
		
		var quantities = 0;
		
		var num = 1;
		if ($('quantity_1').getValue() != Number.NaN && $('quantity_1').getValue() > 0) {
			url += "&item_name_"+num+"=" + "1 User Letter Opener License";
			url += "&quantity_"+num+"=" + $('quantity_1').getValue();
			var amount = ($('amount_1').innerHTML * discount).toFixed(2);
			url += "&amount_"+num+"=" + amount;
			
			if (  $('coupon')  && $('coupon').getValue() == "MW2010IDS" ) {
				url += "&on0_"+num+"=coupon code";
				url += "&os0_"+num+"="+ $('coupon').getValue();
			}
			
			quantities += $('quantity_1').getValue() * 1;
			num++;
		}
		
		if ($('quantity_2').getValue() != Number.NaN && $('quantity_2').getValue() > 0) {
			url += "&item_name_"+num+"=" + "2 Users Letter Opener License";
			url += "&quantity_"+num+"=" + $('quantity_2').getValue();
			var amount = ($('amount_2').innerHTML * discount).toFixed(2);
			url += "&amount_"+num+"=" + amount;
			
			if (  $('coupon')  && $('coupon').getValue() == "MW2010IDS" ) {
				url += "&on0_"+num+"=coupon code";
				url += "&os0_"+num+"="+ $('coupon').getValue();
			}
			
			quantities += $('quantity_2').getValue() * 1;
			num++;
		}
			
		if ($('quantity_3').getValue() != Number.NaN && $('quantity_3').getValue() > 0) {
			url += "&item_name_"+num+"=" + "5 Users Letter Opener License";
			url += "&quantity_"+num+"=" + $('quantity_3').getValue();
			var amount = ($('amount_3').innerHTML * discount).toFixed(2);
			url += "&amount_"+num+"=" + amount;
			
			if (  $('coupon')  && $('coupon').getValue() == "MW2010IDS" ) {
				url += "&on0_"+num+"=coupon code";
				url += "&os0_"+num+"="+ $('coupon').getValue();
			}
			
			quantities += $('quantity_3').getValue() * 1;
			num++;
		}
			
		if ($('quantity_4').getValue() != Number.NaN && $('quantity_4').getValue() > 0) {
			url += "&item_name_"+num+"=" + "10 Users Letter Opener License";
			url += "&quantity_"+num+"=" + $('quantity_4').getValue();
			var amount = ($('amount_4').innerHTML * discount).toFixed(2);
			url += "&amount_"+num+"=" + amount;
			
			if (  $('coupon')  && $('coupon').getValue() == "MW2010IDS" ) {
				url += "&on0_"+num+"=coupon code";
				url += "&os0_"+num+"="+ $('coupon').getValue();
			}
			
			quantities += $('quantity_4').getValue() * 1;
			num++;
		}
			
		if ($('amount_5') && $('quantity_5').getValue() != Number.NaN && $('quantity_5').getValue() > 0) {
			url += "&item_name_"+num+"=" + "Upgrade Letter Opener License";
			url += "&quantity_"+num+"=" + $('quantity_5').getValue();
			var amount = ($('amount_5').innerHTML * discount).toFixed(2);
			url += "&amount_"+num+"=" + amount;
			url += "&on0_"+num+"=registration code";
			url += "&os0_"+num+"="+ $('old_serial_number').getValue();
			
			if (  $('coupon')  && $('coupon').getValue() == "MW2010IDS" ) {
				url += "&on1_"+num+"=coupon code";
				url += "&os1_"+num+"="+ $('coupon').getValue();
			}
			
			quantities += $('quantity_5').getValue() * 1;
			num++;
		}
			
		if ($('quantity_6') && $('quantity_6').checked) {
			url += "&item_name_"+num+"=" + "Letter Opener Lifetime Upgrades Option";
			url += "&quantity_"+num+"=" + quantities;
			var amount = ($('amount_6').innerHTML * discount).toFixed(2);
			url += "&amount_"+num+"=" + amount;
			
			if (  $('coupon')  && $('coupon').getValue() == "MW2010IDS" ) {
				url += "&on0_"+num+"=coupon code";
				url += "&os0_"+num+"="+ $('coupon').getValue();
			}
			
			num++;
		}
		
		$('process_indicator').show();
		
		this.disableForm();
		
		if (typeof _uacct != "undefined")
			urchinTracker('/Store/Purchase');
		location.href = url;
	},
	
	checkOldSerialNumber: function(event) {

		if ( $('old_serial_number').getValue().length == 0 ) {
			$('old_serial_number_validate').hide();
			$('old_serial_number_not_validate').hide();
			$('quantity_5').setValue("");
			$('quantity_5').readOnly = true;
			this.respondToChange();
			return;
		}
		
		new Ajax.Request('/OMiC/php/oldSerialNumber.php',
  				{
					method:'get',
					parameters: {email: $('old_serial_number').getValue()},
					onSuccess: this.onSuccessCheckOldSerialNumber.bindAsEventListener(this),
					onFailure: function() {
						alert('Something went wrong...')
					}
		});	
	},
	
	onSuccessCheckOldSerialNumber: function(transport) {
		if (!transport.responseText)
			return;
		
		var response = transport.responseText.split(";");
		
		if (response[1] != "3444042c14bbe105")
			return;
		
		$('quantity_5').setValue(response[0]);
		this.respondToChange();
		
		if (response[0] > 0) {
			$('old_serial_number_validate').show();
			$('old_serial_number_not_validate').hide();
			$('quantity_5').readOnly = false;
		} else {
			$('old_serial_number_validate').hide();
			$('old_serial_number_not_validate').show();
			$('quantity_5').readOnly = true;
		}
	
	},
	
	coupon: function(event) {
		if ( $('coupon').getValue() == "MW2010IDS" ) {
			$('coupon_validate').show();
			$('coupon_not_validate').hide();
		} else {
			$('coupon_validate').hide();
			$('coupon_not_validate').show();
		}
		
		this.respondToChange();
	},
	
	disableForm: function(event) {
		$('currency').disabled = true;
		$('quantity_1').disabled = true;
		$('quantity_2').disabled = true;
		$('quantity_3').disabled = true;
		$('quantity_4').disabled = true;
		if ($('old_serial_number'))
			$('old_serial_number').disabled = true;
		$('submit').disabled = true;
	},
	
	showOldSerialNumberFrom: function(event) {
		if (!$('old_serial_number_form').visible()) {
			Effect.BlindDown( $('old_serial_number_form'), {duration: 0.25} );
			$('upgrade_a').src = "../../images/arrow1.png";
			/*
			$('upgrade_text').setStyle( {cursor: "auto"} );
			$('upgrade_text').stopObserving('click', this.showOldSerialNumberFrom);
			$('quantity_5').setStyle( {cursor: "auto"} );
			$('quantity_5').stopObserving('click', this.showOldSerialNumberFrom);
			*/
			$('old_serial_number').focus();
		} else {
			Effect.BlindUp( $('old_serial_number_form'), {duration: 0.25} );
			$('upgrade_a').src = "../../images/arrow.png";
		}
	}
}

// LOCAL
Event.observe(window, 'load', function() { 
	new Store(); 
}, false);
