function sendmenow(){
    $("#sendBut").html("Please wait");
    var name = $("#name").val();
    var email = $("#email").val();
    var comment = $("#comment").val();
    var verify = $("#verification").val();
    var dept = $("#department").val();
    var goforit = true;
    var errors = new Array('Your message cannot be sent because:\n\n');
    if (verify != "5"){
        errors.push("Verifcation code not correct");
        goforit = false;
    }
    if (name == ""){
        errors.push("you must enter a Name");
        goforit = false;
    }
    if (email == ""){
        errors.push("you must enter an Email");
        goforit = false;
    }
    if (comment == ""){
        errors.push("you must enter a Comment");
        goforit = false;
    }

    if (goforit){
        $.post("mailer.php", {
            name: name,
            email:email,
            comment:comment,
            dept: dept
        }, function(data){
            alert(data);
            $("#name").val("");
            $("#comment").val("");
            $("#email").val("");
            $("#verification").val("");
            $("#sendBut").html("Send Email");
        });
    }
    else{
        alert (errors.join('\n'));
        $("#sendBut").html("Send Email");
    }
}

function checkEnter(e){ //e is event object passed from function invocation
    var characterCode;
	
    if(e && e.which){ //if which property of event object is supported (NN4)
        e = e;
        characterCode = e.which; //character code is contained in NN4's which property
    }
    else{
        e = event;
        characterCode = e.keyCode; //character code is contained in IE's keyCode property
    }
	
    if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
        return false;
    }
    else{
        return true;
    }
}

function populateprice(){
    var parray = $("#allprice").val().split("\n");
    $("#pricebox").html("");
    for (var i = 0; i < parray.length; i++){
        $("#pricebox").append("<a href=\"javascript: removeitem('" + i + "', 'price')\">"+ parray[i] +"</a><br />");
    }
}

function addprice(){
    var pricedesc = $("#newpriced").val();
    var price = $("#newprice").val();
    var id = $("#id").val();
    $.post("prices.php", {
        pricedesc: pricedesc,
        price: price,
        type: 'new',
        id: id
    }, function(data){
        $("#TB_ajaxContent").load("prices.php?id="+id);
    });
}

function saveprice(num){
    var pricedesc = $("#pricedesc"+num).val();
    var price = $("#price"+num).val();
    var id = $("#id").val();
    $.post("prices.php", {
        pricedesc: pricedesc,
        price: price,
        type: 'update',
        id: id,
        num: num
    }, function(data){
        $("#TB_ajaxContent").load("prices.php?id="+id);
    });
}
function removeprice(num){
    var id = $("#id").val();
    $.post("prices.php", {
        type: 'remove',
        id: id,
        num: num
    }, function(data){
        $("#TB_ajaxContent").load("prices.php?id="+id);
    });
}


function removeitem(num, type){
    if (type == 'price'){
        var carray = $("#allprice").val().split("\n");
        carray.splice(num, 1);
        $("#pricebox").html("");
        for (var i = 0; i < carray.length; i++){
            $("#pricebox").append("<a href=\"javascript: removeitem('" + i + "', 'price')\">"+ carray[i] +"</a><br />");
        }
        $("#allprice").val(carray.join("\n"));
    }
	

}


function swapImg(imgID, newImg){
    $("#" + imgID).attr("src", "images/"+newImg);

}

function drop(list){
    lclose();
    $("#" + list).show(200);
}

function lclose(){
    $(".leftmenublock").hide(0);
}

function pLoad(id){
    $("#textbox").html("<center><img src='images/loading.gif'></center>");
    $("#textbox").load("gettext.php?id="+id);
}


function showdiv(div){
    $("#"+div).show();
}

function hidediv(){
    $(".detailpopup").hide();
}

function goto(page){
    self.location = page;
}

function popbox(width, height, title){
    $("#linker").attr("title", title);
    $("#linker").attr("href", "#TB_inline?height="+height+"&width="+width+"&inlineId=colourpicker");
    $("#linker").click();
}

function add(id){
    var sizes = $("#sizes").val();
    var colours = $("#colours").val();
    addtocart(id, sizes, colours);
    tb_remove();
}

function addtocart(id){

    var size = $("#size").val();
    var colour = $("#colour").val();
    $.post("docart.php", {
        action: 'add',
        id: id,
        sizes: size,
        colours: colour
    }, function()

    {
            updatecart();
        });
			
    $("#alerter").html("<p>Product added to Cart<br /><br /><a href='cart.php'>Go to Cart</a> &nbsp; <a href='#' onclick='tb_remove();'>Continue Shopping</a></p>");
    $("#linker").click();
}	

function updatecart(){
    $(".cartdetails").load("showcart.php");
}

function clearCart(){
    $.post("docart.php", {
        action: 'clear'
    }, function()

    {
            updatecart();
        });
}	

function removeCart(id){
    $.post("docart.php", {
        action: 'delete',
        id: id
    }, function()

    {
            location.reload(true);
        });
		
}


function updatequant(id){
    var quantity = $('#q'+id).val();
    $("#alerter").html("<p>Quantity Updated</p>");
    $("#linker").click();
    $.post("docart.php", {
        action: 'update',
        quantity: quantity,
        id: id
    }, function()

    {
            location.reload(true);
        });
}


function confirmit(){
    var name = $("#name").val();
    var address = $("#address").val();
    var country = $("#country").val();
    var phone = $("#phone").val();
    var email = $("#email").val();
    var region = $("#region").val();
	
	
    if ((name == "") || (address == "") || (phone == "") || (email == "") || ($('#tandc:checked').size() == 0)){
        $("#linker").attr("title", "Error!");
        $("#alerter").html("<p class='small'>All fields must be filled in</p>");
        $("#linker").click();
        return;
    }
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;


    if (!filter.test(email)){
        $("#linker").attr("title", "Error!");
        $("#alerter").html("<p class='small'>Your email address appears to be incorrect</p>");
        $("#linker").click();
        return;
    }
    $("#alerter").html("<p class='small'>Please Wait.....</p>");
    $("#linker").attr("title", "Please Wait");
    $("#linker").click();
	
    $.post("region.php", {region:region}, function(){
        $("#confirmabox").load("confirmation.php", function(){
            $("#confirmname").html(name);
            $("#confirmaddress").html(address);
            $("#confirmphone").html(phone);
            $("#confirmemail").html(email);
            $("#confirmcountry").html(country);
            $("#securabox").hide();
            $("#confirmabox").show(1, function() {
                tb_remove();
            });
        });
    });
}
function gosecure(orderid){
	
    name = $("#confirmname").html();
    address = $("#confirmaddress").html();
    phone =	$("#confirmphone").html();
    email = $("#confirmemail").html();
    country = $("#confirmcountry").html();
    amount = $("#amount").val();
    $.post("saveorder.php", {
        name: name,
        address: address,
        country: country,
        phone: phone,
        email: email,
        orderid: orderid,
    }, function(data){
        $("#realname").val(name);
        $("#realaddress").val(address);
        $("#realphone").val(phone);
        $("#realemail").val(email);
        $("#realcountry").val(country);
        $("#secureform").submit();
    });
}

function showSubs(id){
    $("#subs").fadeOut(0, function(){
        $("#subs").load("showsubs.php?id="+id, function(){
            $("#subs").fadeIn(0);
        });
    });
}

function removeSub(id, prodid){
    $.post("deletesub.php", {
        id: id
    });
    $("#subs").fadeOut(0, function(){
        $("#subs").load("showsubs.php?id="+prodid, function(){
            $("#subs").fadeIn(0);
        });
    });

	
}

function editsub(id){
    $("#editSub").load("editsubs.php?id="+id, function(){
        $("#linker").click();
    });
}

function changeTo(id){
	$.post("getimage.php", {id: id}, function(data){
	    var zoom = document.getElementById("mainimage");
		MagicZoom.update(zoom, 'products/'+data, 'products/thumbs/'+data, 'zoom-position:inner;zoom-fade: true;');
        $("#colour option[@value="+id+"]").attr("selected","selected");
		
	});
}

function goback(){
    $("#securabox").show();
    $("#confirmabox").hide();
}

function hider(id){
	$.post("hide.php", {id: id});
}
