function changeCountry(obj) {
	top.location = "/" + obj.value + "/";
}

function continueOrder(s) {
	var f = document.forms["order_form"];
	//var buyform = document.forms["BuyForm"];
	switch (s) {
		case 'payment':
			if (validatePayment()) {
				f.action = "order_capture.php?next=payment";
				f.submit();
			} else {
				//
			}
			break;
		case 'shipping':
			if (validateShipping()) {
				f.action = "order_capture.php?next=shipping";
				f.submit();
			}
			break;
		case 'verification':
			if (validateVerification()) {
				f.action = "order_capture.php?next=verification";
				f.submit();
			}
			break;
		case 'confirm':
			if ( confirm("Confirm to proceed this order?") ) {
				f.action = "order_capture.php?next=confirm";
				f.submit();
				//buyform.action = "https://select-test.wp3.rbsworldpay.com/wcc/purchase";
				//buyform.submit();
			}
			break;
		// Modity to confirm1, Cliff, 20100516 1702	
		case 'confirm1':
			if ( confirm("Confirm to proceed this order?") ) {
				f.action = "order_capture.php?next=confirm1";
				f.submit();
				//buyform.action = "https://select-test.wp3.rbsworldpay.com/wcc/purchase";
				//buyform.submit();
			}
			break;
	    // Add confirm2, Cliff, 20100516 1737	
		case 'confirm2':
			if ( confirm("Confirm to proceed this order?") ) {
				f.action = "order_capture.php?next=confirm2";
				f.submit();
				//buyform.action = "https://select-test.wp3.rbsworldpay.com/wcc/purchase";
				//buyform.submit();
			}
			break;
	}
}

function validatePayment() {
	var field = Array("fname", "lname", "address", "country", "daytime_phone", "evening_phone", "email");
	var label = Array("First Name", "Last Name", "Address", "Country", "Daytime Phone", "Evening Phone", "Email");
	//var f = document.forms["order_form"];
	var prompt = false;
	var b = false;
	
	for (var i=0; i<field.length; i++)	{
		var tmp = $("#"+field[i]);
		tmp.removeClass("validation_alert");
	}

	for (var i=0; i<field.length; i++)	{
		var tmp = $("#shipping_"+field[i]);
		tmp.removeClass("validation_alert");
	}

	for (var i=0; i<field.length; i++)	{
		var tmp = $("#"+field[i]);
		if (tmp.val()=="") {
			tmp.addClass("validation_alert");
			prompt = true;
		}
	}
	if ( !$("#copy_address").attr("checked") ) {
		for (var i=0; i<field.length; i++)	{
			var tmp = $("#shipping_"+field[i]);
			if (tmp.val()=="") {
				tmp.addClass("validation_alert");
				prompt = true;
			}
		}
	} 
	//else
	//{
	  //alert('Here!');
	  //for (var i=0; i<field.length; i++)	{
		//var tmp1 = $("#shipping_"+field[i]).val();
		//var tmp2 = $("#"+field[i]).val();
		//tmp1 = tmp2;
		//alert(tmp1);
		//alert(tmp2);
	  //}
	//}

	var patt1 = new RegExp(/\d+/);
	var patt2 = new RegExp(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/);
	if (prompt) {
		$("#validation_desc").addClass("validation_desc");
		$("#validation_desc").text("Please complete the above information before continue");
	} else {
		if(!patt1.test($("#daytime_phone").val())){
			$("#daytime_phone").addClass("validation_alert");
			$("#validation_desc").addClass("validation_desc");
			$("#validation_desc").text("Please enter a number for Daytime Phone");
		}
		else if(!patt1.test($("#evening_phone").val())){
			$("#evening_phone").addClass("validation_alert");
			$("#validation_desc").addClass("validation_desc");
			$("#validation_desc").text("Please enter a number for Evening Phone");
		}
		else if(!patt2.test($("#email").val())){
			$("#email").addClass("validation_alert");
			$("#validation_desc").addClass("validation_desc");
			$("#validation_desc").text("Please enter a valid Emaill Address");
		}
		else{
			$("#validation_desc").removeClass("validation_desc");
			$("#validation_desc").text("");
			b = true;
		}
	}
	/*
	for (var i=0; i<field.length;; i++) {
		var tmp = $("input#"+field[i]).val();
		alert(tmp);
	}
	*/
	return b;
}

function validateShipping() {
	/*
	var field = Array("payment_cc_type", "payment_cc_no", "payment_cc_scode");
	var label = Array("Credit Card Type", "Credit Card Number", "Security Code");
	
	var prompt = false;
	var b = false;
	for (var i=0; i<field.length; i++)	{
		var tmp = $("#"+field[i]);
		if (tmp.val()=="") {
			tmp.addClass("validation_alert");
			prompt = true;
		}
	}

	if (prompt) {
		$("#validation_desc").addClass("validation_desc");
		$("#validation_desc").text("Please complete the above information before continue");
	} else {
		$("#validation_desc").removeClass("validation_desc");
		$("#validation_desc").text("");
		b = true;
	}
	*/

	return true;
}

function validateVerification() {
	var field = Array("payment_cc_type", "payment_cc_no", "payment_cc_scode");
	var label = Array("Credit Card Type", "Credit Card Number", "Security Code");
	
	var prompt = false;
	var b = false;
	for (var i=0; i<field.length; i++)	{
		var tmp = $("#"+field[i]);
		if (tmp.val()=="") {
			tmp.addClass("validation_alert");
			prompt = true;
		}
	}

	if (prompt) {
		$("#validation_desc").addClass("validation_desc");
		$("#validation_desc").text("Please complete the above information before continue");
	} else {
		$("#validation_desc").removeClass("validation_desc");
		$("#validation_desc").text("");
		b = true;
	}

	return b;
}

function add2Cart(next) {
	//alert('Coming Soon!');
	//return;

	//var f = document.forms["cart_form"];
	//var sss = "code=" + f.code.value + "&quantity=" + f.quantity.value + "&currency=hkd";
	var quantity =  $("select#product_quantity").val()=='more' ? $("input#product_quantity_text").val() : $("select#product_quantity").val();
	var sss = "action=add&product_id=" + $("input#product_id").val() + "&code=" + $("input#product_code").val() + "&quantity=" +quantity+ "&currency=" + $("input#product_currency").val() + "&color=" + $("input#product_color").val();

	//alert (sss);
	//alert ($("a#buy_now").val());

	$.ajax({
		url		: 'cart_capture.php',
		cache	: false,
		dataType: 'html',
		type	:'GET',
		data	: sss,
		error	: function(xhr) {
			alert('Ajax request');
		},
		success: function(response) {
			/*$('#msg').html(response);*/
			if (next!='') {
				top.location = next;
			} else {
				viewShoppingCart();
			}
		}
	});
}

// For accessories page
function add2Cart_acc(code) {
	//alert('Coming Soon!');
	//return;
	next = "";
	//alert ( code + " : " + $("input#product_id").val() );
	
	var quantity =  $("select#product_quantity_"+code).val()=='more' ? $("input#product_quantity_text_"+code).val() : $("select#product_quantity_"+code).val();
	var sss = "action=add&product_id=" + $("input#product_id_"+code).val() + "&code=" + $("input#product_code_"+code).val() + "&quantity=" +quantity+ "&currency=" + $("input#product_currency_"+code).val() + "&color=" + $("input#product_color_"+code).val();

	$.ajax({
		url		: 'cart_capture.php',
		cache	: false,
		dataType: 'html',
		type	:'GET',
		data	: sss,
		error	: function(xhr) {
			alert('Ajax request');
		},
		success: function(response) {
			//$('#msg').html(response);
			if (next!='') {
				top.location = next;
			} else {
				viewShoppingCart();
			}
		}
	});
}

// For popup shopping cart
function add2Cart_adapter() {
	//alert('Coming Soon!');
	//return;
	next = "";
	
	code = $("select#adapter").val();
	
	//var quantity =  $("select#product_quantity_"+code).val()=='more' ? $("input#product_quantity_text_"+code).val() : $("select#product_quantity_"+code).val();
	//var sss = "action=add&code=" + $("input#product_code_"+code).val() + "&quantity=" +quantity+ "&currency=" + $("input#product_currency_"+code).val() + "&color=" + $("input#product_color_"+code).val();

	var sss = "action=add&product_id=" + code + "&quantity=1&currency=HKD&color=0";

	$.ajax({
		url		: 'cart_capture.php',
		cache	: false,
		dataType: 'html',
		type	:'GET',
		data	: sss,
		error	: function(xhr) {
			alert('Ajax request');
		},
		success: function(response) {
			//$('#msg').html(response);
			if (next!='') {
				top.location = next;
			} else {
				viewShoppingCart();
			}
		}
	});
}

function removeFromCart(id, code, color, next) {
	var sss = "action=remove&product_id="+id+"&code="+code+"&color="+color;

	$.ajax({
		url		: 'cart_capture.php',
		cache	: false,
		dataType: 'html',
		type	:'GET',
		data	: sss,
		error	: function(xhr) {
			alert('Ajax request');
		},
		success: function(response) {
			/*$('#msg').html(response);*/
			/* top.location = next; */
			if (next!='') {
				top.location = next;
			} else {
				closeShoppingCart();
				viewShoppingCart();
			}
		}
	});
}

function send_offer_email() {
	var to_email = $("input#offer_email").val();
	var patt2 = new RegExp(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/);
	if(!patt2.test(to_email)){
		alert("Put email address here");
		return;
	}
	//var url = "email_special_offers.php?to_email="+to_email;
	var url = "send_special_offers.php?to_email=" +to_email;
	
	var ajaxRequest;
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Error occurs on your browser's connection. Please close and re-open your browser to try again. Thank you very much.");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			if(ajaxRequest.status == 200){
				if(ajaxRequest.responseText=="true")
					alert("Thank you for your signing up. A welcome email will be sent in a few minutes for the confirmation.");
				else
					alert("Error occurs returned. Please close and re-open your browser to try again. Thank you very much.");
			}
		}
	}
	ajaxRequest.open("GET", url, true);
	ajaxRequest.send(null); 

	/*
	$.ajax({
		url		: 'email_special_offers.php',
		cache	: false,
		dataType: 'html',
		type	:'GET',
		data	: sss,
		error	: function(xhr) {
			alert('Ajax request');
		},
		success: function(response) {
			alert("123");
		}
	});
	//*/
	
}

$(function() {   
  $("#add_to_cart").click(function() {
	  add2Cart('');
  });
  
  $("#buy_now").click(function() { 
	  add2Cart('');
  });
  
  $("#product_quantity").change(function() {
	  if ( isNaN($(this).val()) ) {
	  } else {
		  var price = $("input#product_price").val();
		  var quantity = $(this).val();
		  var currency = $("input#product_currency").val();
		  var amount = price * quantity;

		  $("div#amount").html ( "Amount: " + currency + " " + formatPrice(amount) );
	  }
  }); 
  $("#product_quantity_text").change(function() {
	  if ( isNaN($(this).val()) ) {
	  } else {
		  var price = $("input#product_price").val();
		  var quantity = $(this).val();
		  var currency = $("input#product_currency").val();
		  var amount = price * quantity;

		  $("div#amount").html ( "Amount: " + currency + " " + formatPrice(amount) );
	  }
  }); 

  
  $("#continue_order_payment").click(function() { 
	  continueOrder('payment');
	  //continueOrder('shipping');
  }); 
  
  $("#continue_order_shipping").click(function() { 
	  continueOrder('shipping');
  }); 
  
  
  $("#continue_order_verification").click(function() { 
	  continueOrder('verification');
  }); 

});
function add_to_cart_acc(p_code){
	add2Cart_acc(p_code);
}

function product_qty(v,p_code){
	if ( isNaN(v) || p_code=="" ) {
	} else {
	  var price = $("input#product_price_"+p_code).val();
	  var quantity = v;
	  var currency = $("input#product_currency_"+p_code).val();
	  var amount = price * quantity;

	  $("div#amount_"+p_code).html ( "Amount: " + currency + " " + formatPrice(amount) );
	}
}

function product_qty_text(){
}

function submit2Cart() {
}

function formatPrice(s) {
	s = s + "";

	switch (s.indexOf("."))
	{
		case -1:	s += ".00";
					break;
		case 0:		s += "00";
					break;
		case 1:		s += "0";
					break;
	
	}
	return s;
}

function selectColor(i) {
	var f = document.forms['cart_form'];
	f.color.value = i;
}

function popExternal(src) {
		var f = document.forms["press_list"];
		src = unescape(src);
		src = src.substring( src.lastIndexOf('/')+1, src.length );
		src = src.replace(/ /g, '_');
		if (f.elements["x" + src].value!="") {
			window.open(f.elements["x" + src].value,'_blank','location=1, menubar=1, resizable=1, scrollbars=1, status=1, titlebar=1, toolbar=1');
		}
		
}