// JavaScript Document

function ucfirst(str) {
    var firstLetter = str.substr(0, 1);
    return firstLetter.toUpperCase() + str.substr(1);
}

function serialize( mixed_value ) {
    var _getType = function( inp ) {
        var type = typeof inp, match;
        var key;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            match = cons.match(/(\w+)\(/);
            if (match) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';
    
    switch (type) {
        case "function": 
            val = ""; 
            break;
        case "undefined":
            val = "N";
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
            val = "s:" + mixed_value.length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            var count = 0;
            var vals = "";
            var okey;
            var key;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function") { 
                    continue; 
                }
                
                okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
                vals += serialize(okey) +
                        serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
    }
    if (type != "object" && type != "array") {
      val += ";";
  }
    return val;
}

function msguser(msg){
	$.fn.colorbox({html: "<p>"+msg+"</p>", open: true, opacity: .4, transition: 'none'});	
}

function showcart(){
	$.get(root+"scripts/php/cartfuncs.php", {action: 'showcart'}, function(data){
		$("#sys_shoppingcart").html(data);
	});	
	$.get(root+"scripts/php/cartfuncs.php", {action: 'showcheckout'}, function(data){
		$("#sys_checkout").html(data);
	});	
}

function addToCart(prod){
	var quantity = $("#productQuantityField").val();
	var variants = new Array();
	$.each($(".sys_variant_selectboxes"), function(){
		variants.push($(this).attr('id')+ "#~#" +$(this).val());
	});
	variants = serialize(variants);
	
	if (isNaN(quantity) || quantity < 0){
		msguser("you must enter a number greater than 0");
		return;	
	}
	else {
		$.get(root+"scripts/php/cartfuncs.php", {action: 'add', id: prod, quantity: quantity, variants: variants}, function(data){
			showcart();
		});	
		msguser("Product added to cart");
	}
}

function checkoutRemove(num){
	$.get(root+"scripts/php/cartfuncs.php", {action: 'del', key: num}, function(data){
		showcart();
	});		
	msguser("Product removed from cart");
}
function checkoutUpdate(num){
	var quantity = parseInt($("#sys_checkoutUpdate"+num).val());
	$.get(root+"scripts/php/cartfuncs.php", {action: 'update', key: num, quantity: quantity}, function(data){
		showcart();
	});		
	msguser("Cart Updated");
}

function sys_gopay(){
	var total = parseFloat($("#sys_carttotal").html());
	var deliverynames = new Array();
	var deliveryvalues = new Array();
	var errors = new Array();
	var paymentmethod = $("input[name=sys_paymethod]:checked").val();
	var textalert;
	
	$("#sys_deliverydetails input, #sys_deliverydetails select, #sys_deliverydetails textarea").each(function(){
		if ((($(this).attr('type') == 'checkbox' && $(this).is(":checked")) || $(this).attr('type') != "checkbox") && $(this).attr('id') != "sys_deliveryoptions"){
			deliverynames.push($(this).attr('name'));
			deliveryvalues.push($(this).val());
		}
		if ($(this).attr('rel') == "required" && $(this).val() == ""){
			errors.push($(this).attr('name'));
		}
	});

	if ($("input[name=sys_textalert]").length > 0){
		textalert = $("input[name=sys_textalert]").val();
		if (textalert != ""){
			textalert = textalert.replace(/\D/g, '');
			if (textalert[0] == "3"){
				textalert = textalert.substr(3);
			}
			if (textalert[0] == "0"){
				textalert = textalert.substr(1);
			}
			if (textalert.length != 9 || textalert[0] != "8"){
				errors.push("Incorrect Mobile Phone Number.  This system only accepts ROI mobile numbers");
			}	
		}
	}
	if (!$("#sys_ismember").val()){
		var name = $(".sys_custname").val();
		var email = $(".sys_custemail").val();
		var address= new Array();
		$(".sys_custaddress").each(function(){
			address.push($(this).val());
		});
		address = serialize(address);
	}
	if (total < 0.01){
		errors.push('Your cart may be empty or equal to 0');
	}
	if (errors.length > 0){
		msguser('There are problems with your order:<br /><br />'+errors.join("<br />"));
		return;
	}
	else {
		if ($("#sys_ismember").val()){

			deliverynames.unshift($("#sys_ismember").val());
			deliveryvalues.unshift($("input[name=chosenaddress]:checked").val());
		}
		deliverynames = serialize(deliverynames);
		deliveryvalues = serialize(deliveryvalues);
		$.getScript(root+'paymodules/'+paymentmethod+'/exfuncs.js', function(){
			completepay(total, deliverynames, deliveryvalues, errors, paymentmethod, name, email, address, textalert);
		});
	}
}

function choosepaymentmethod(){
	var method = $("input[name=sys_paymethod]:checked").val();
	$(".sys_paymentbox").hide();
	$("#sys_"+method+"box").show();	
}

function sys_changedelivery(num){
	$.get(root+"scripts/php/cartfuncs.php", {action: 'deliverychange', key: num}, function(data){
		showcart();
	});	
}	

function sys_applyPromoCode(){
	var code = $("#sys_promocode").val();
	if (code == ""){
		msguser("No code was entered");	
		return;
	}
	$.get(root+"scripts/php/cartfuncs.php", {action: 'applypromocode', code: code}, function(data){
		data = data.split("#~#");
		if (data[0] == 1){
			showcart();
			msguser(data[1]);
		}
		else {
			msguser(data[1]);	
			return;
		}
		
	});	
}

function sys_affectprice(){
	var text, number, sign, price, findprice;
	price = $("#sys_origprice").text();

	$(".sys_variant_selectboxes").each(function(){
		text = $("#"+$(this).attr('id')+" option:selected").text().split("(");
		text = text[1];
		if (text){
			number = text.replace (/[^\d\.]/g, "");
		}
		if (isNaN(number) || number == ""){
			number = 0;	
		}
		if (text){
			sign = text.replace(/[^\+\-]/g, "");
		}
		if (sign == "-"){
			price = parseFloat(price) - parseFloat(number);
		}
		else{
			price = parseFloat(price) + parseFloat(number);
		}
		
	});
	$("#sys_price").html("&euro;"+price.toFixed(2));
}

function sys_memberlogin(form){
	var email = $("form[name="+form+"] input[name=memberEmail]").val();
	var password = $("form[name="+form+"] input[name=memberPassword]").val();

	$.ajax({
		async: false,
		type: 'get',
		url: root+'members/loginmember.php',
		data: 'email='+email+'&password='+password,
		success: function(data){
			data = data.split("|");
			if (data[0] == "1"){
				self.location = root+'members/dashboard';	
			}
			else {
				alert(data[1]);
			}
		}
	});	
}

function sys_memberlogout(){
	$.ajax({
		async: false,
		type: 'get',
		url: root+'members/logoutmember.php',
		data: '',
		success: function(data){
			self.location = root;	
		}
	});		
}

function sys_signup(){
	var name = $("input[name=signupmemberName]").val();
	var email = $("input[name=signupmemberEmail]").val();
	if (!name || !email){
		alert("You must enter a name and an email address");
		return;	
	}
	$("#sys_signupbox").dialog({
		title: 'Complete Signup',
		resizable: false,
		buttons: {
			"Sign Up": function(){
				var phone = $("#sys_membertelephone").val();
				var mobile = $("#sys_membermobile").val();
				var address = $("#sys_membermainaddress").val();
				if ((!phone && !mobile) || !address){
					alert("You must enter an address and at least one contact number");
					return;		
				}
				$.ajax({
					async: false,
					url: root+'members/signup.php',
					type: 'post',
					data: 'name='+name+'&email='+email+'&phone='+phone+'&mobile='+mobile+'&address='+address,
					success: function(data){
						data=data.split("|");
						$.fn.colorbox({html: '<p>'+data[1]+'</p>', opacity: .2, transition: 'none'});	
						if (data[0] == "1"){
							$("#sys_signupbox").dialog("close");
						}
						
					}
				});
				
			},
			Cancel: function(){
				$(this).dialog("close");	
			}
		},
		modal: true
	});	
}

function savemydetails(){
	var name, email, phone, mobile, address;
	name = $("input[name=memberName]").val();
	email = $("input[name=memberEmail]").val();
	phone = $("input[name=memberTelephone]").val();
	mobile = $("input[name=memberMobile]").val();
	address = $("textarea[name=memberMainAddress]").val();
	
	if (!name || (!phone && !mobile) || !email || !address){
		msguser("You must enter:<br /><br /> Name<br /> Email<br /> Address<br /> One Telephone Number");
		return;
	}
	
	$.ajax({
		async: false,
		url: root+ 'members/savedetails.php',
		type: 'post',
		data: {
			name: name,
			email: email,
			phone: phone,
			mobile: mobile,
			address: address
		},
		success: function(data){
			msguser(data);
		}
	});
}

function newaddressbox(id){
	var title = "Add Address";
	if(id){
		title = "Edit Address";
	}
	
	if (id){
		$.ajax({
			async:false,
			url: root+'members/getaddress.php',
			type: 'get',
			data: {
				id: id	
			},
			success: function(data){
				$("#sys_address").val(data);
			}
		});
			
	}
		
	$("#addaddress").dialog({
		title: title,
		resizable: false,
		modal:true,
		buttons: {
			Save: function(){
				var address = $("#sys_address").val();
				if (address){
					$.ajax({
						async: false,
						type: 'post',
						url: root+'members/saveaddress.php',
						data: {
							id: id,
							address: address
						},
						success: function(data){
							$(".tabs").tabs("select", 1);
							$("#addaddress").dialog("close");
							$(".tabs").tabs("select", 2);
						}
					});
				}
			},
			Cancel: function(){
				$(this).dialog("close");
			}
		}
	});
}

function changepassword(){
	var oldp, newp, newp2;
	oldp = $("input[name=oldpass]").val();
	newp = $("input[name=newpass]").val();
	newp2 = $("input[name=newpass2]").val();
	
	if (!oldp || !newp){
		msguser("You must enter both existing and new passwords");
		return;
	}
	if (newp != newp2){
		msguser("Your new passwords do not match");
		return;
	}
	
	$.ajax({
		async: false,
		url: root+'members/changepassword.php',
		type: 'post',
		data: {
			oldp: oldp,
			newp: newp,
			newp2: newp2
		},
		success: function(data){
			data = data.split("|");
			msguser(data[1]);
			if (data[0] == 1){
				$("input[name=oldpass]").val('');
				$("input[name=newpass]").val('');
				$("input[name=newpass2]").val('');
			}
		}
	});	
}	


function removeaddress(id){
	var conf = confirm("Are you sure?");
	if (conf){
		$.ajax({
			async:false,
			url: root+'members/removeaddress.php',
			type: 'get',
			data: {
				id: id
			},
			success: function(data){
				$(".tabs").tabs("select", 1);
				$(".tabs").tabs("select", 2);
			}
		});
	}
}

function addtowishlist(type, prod){
	var chosen, title;
	if (type == "choose"){
		chosen = $("#sys_choosewishlist").val();
	}
	else {
		title = $("#sys_newwishlist").val();	
	}
	$.ajax({
		async: false,
		url: root+'members/addtowishlist.php',
		type: 'post',
		data: {
			chosen: chosen,
			title: title,
			prod: prod
		},
		success: function(res){
			msguser(res);		
		}
	});
}

function wishprods(id){
	$.fn.colorbox({
		href: root+'members/viewwishlist.php?id='+id,
		opacity: .4,
		width: 700,
		height: 500
	});
}

function removewishlist(id){
	var conf = confirm("Are you sure you wish to delete this wishlist?");
	if (conf){
		$.ajax({
			async:false,
			type: 'get',
			url: root+ 'members/removewishlist.php',
			data: {
				id: id	
			},
			success: function(data){
				alert(data);
				$(".tabs").tabs({selected: 0});
				$(".tabs").tabs({selected: 3});
			}
		});
	}
}
function setsearchorder(val){
	$.ajax({
		ajax: false,
		type: 'get',
		url: root+'scripts/php/setsearchorder.php',
		data: {
			val: val	
		},
		success: function(data){
			self.location.reload(0);	
		}
	});
}
