function addtocart(id,quantity) {
    $.post("cart/updatecart.php",{product:id,quantity:quantity,'do':1},function(data){

        cartstatus(); cartcontents();
        window.location='index.php?r=cart';
        //	$('#cart_holder').animate({ backgroundColor : 'aqua'},2000);
    });
}
function updatecart(id,quantity) {
    if ( quantity >= 1 ) {
        $.post("cart/updatecart.php",{product:id,quantity:quantity,'do':2},function(data){window.location.reload();});
    }else{
        deletefromcart(id);
    }
}
function deletefromcart(id) {
    $.post("cart/updatecart.php",{product:id,'do':3},function(data){window.location.reload();});
}
function cartcontents(esc) {
    if(esc) {
        //		$.get("cart/cartcontents.php",function(data){document.getElementById('cart_content').innerHTML = data;});
    } else {
        //		$.get("cart/cartcontents.php",function(data){document.getElementById('cart_content').innerHTML = data; cartshow()});
    }
}
function cartstatus() {
    $.get("cart/cartstatus.php?rand=<?php print rand(1, 20000); ?>",function(data){$('#totalitems').html('Has ' + data + ' items');});

}
function cartshow() {
    $('#cart').slideDown();
    window.location = '#';
}
function carthide() {
    $('#cart').slideUp();
}
function cartclick() {
    $('#memberbox').slideUp();
    $('#cart').slideToggle();
}
function memberclick() {
    $('#cart').slideUp();
    $('#memberbox').slideToggle();
}
function memberhide() {
    $('#memberbox').slideUp();
}
$(function () {
    cartcontents(1);
    cartstatus();

});


function contentPopup(id) {
    /*$.post("productpopup.php",{product:id},function(data){
        jQuery.modal(data);
        //alert(data);
    });
    */
    
    $.ajax({
        url: 'productpopup.php',
        type: 'POST',
        data: ({product: id}),
        success: function(data) {jQuery.modal(data);}
    })
    
}

function pop_page(pagename)
{
    $.get('index.php?r=terms', function(data) {
        jQuery.modal(data);
    });
}

function adddealtocart(id) {
    //alert(id);
        if (id != '')
        {
            //alert(id);
            $.post("cart/updatecart.php",{product:id,quantity:1,'do':1},function(data){
                //alert(data);
                cartstatus(); cartcontents();
        		//window.location='cart.php';
        	//	$('#cart_holder').animate({ backgroundColor : 'aqua'},2000);
        	});
        }
    }

function validateForm(){
    var error=true;
    var string="";
    if(document.getElementById('g_name').value==""){
        string="Name\n";
        error=false;
    }

    if(document.getElementById('emails').value==""){
        string=string+"Email\n";
        error=false;
    }

    if(document.getElementById('date_day').value==""){
        string=string+"Purchase Date";
        error=false;
    }
    if(document.getElementById('emails').value!=""){
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        if(reg.test(document.getElementById('emails').value)==false){
            alert('Email should be in the form of john@bing.com');
            return false;
        }
    }
     

    if(error==false){
        alert("Please Fill the following fields:"+"\n"+string)
    }

    return error;
}

function validate2(form) {
    valid = true;
    if(form.email.value == '') {
        valid = false;
    } else if(form.firstname.value == '') {
        valid = false;
    } else if(form.lastname.value == '') {
        valid = false;
    } else if(form.address1.value == '') {
        valid = false;
    } else if(form.country.value == '-------------------' || form.country.value == 'Please Select') {
        valid = false;
    } else if(form.city.value == '') {
        valid = false;
    } else if(form.province.value == '') {
        valid = false;
    } else if(form.zip.value == '') {
        valid = false;
    } else if(form.phone.value == '') {
        valid = false;
    } //else if(!form.terms.value == 1) {
    //	alert("You must agree to the Terms of Service");
    //	return false;
    //}


    if(!valid) {
        alert("Please complete the form. All fields marked with * are required");
        return false;
    }

    return true;
}

function v (val){
    alert(val);
}


function validate(form) {

    var shipping;
    for(i = 0 ; i < form.shipping.length; i++) {
        if(form.shipping[i].checked) {
            shipping = form.shipping[i].value;
        }
    }
    valid = true;

    if(form.email2.value == '') {
        valid = false;
    } else if(form.firstname2.value == '') {
        valid = false;
    } else if(form.lastname2.value == '') {
        valid = false;
    } else if(form.address12.value == '') {
        valid = false;
    } else if(form.country2.value == '-------------------' || form.country2.value == 'Please Select') {
        valid = false;
    } else if(form.city2.value == '') {
        valid = false;
    } else if(form.province2.value == '') {
        valid = false;
    } else if(form.zip2.value == '') {
        valid = false;
    } else if(form.phone2.value == '') {
        valid = false;
    }

    if(shipping == 2) {
        if(form.sfirstname2.value == '') {
            valid = false;
        } else if(form.slastname2.value == '') {
            valid = false;
        } else if(form.saddress12.value == '') {
            valid = false;
        } else if(form.scountry2.value == 'Please Select' || form.scountry2.value == '-------------------') {
            valid = false;
        } else if(form.scity2.value == '') {
            valid = false;
        } else if(form.sprovince2.value == '') {
            valid = false;
        } else if(form.szip2.value == '') {
            valid = false;
        } else if(form.sphone2.value == '') {
            valid = false;
        }
    }



    if(!valid) {
        alert("Please complete the form. All fields marked with * are required");
        return false;
    } else {
        if(form.country2.value != 'Australia' && form.country2.value != 'New Zealand') {

            if(shipping != 2) {
                alert("The Shipping Country Must be Australia or New Zealand.");
                return false;

            } else {
                if(form.scountry2.value != 'Australia' && form.scountry2.value != 'New Zealand') {
                    alert("The Shipping Country Must be Australia or New Zealand.");
                    return false;
                }
            }

        }
    }


}

function poptastic(id)
{
    var width  = 1200;
    var height = 500;
    var left   = (screen.width  - width)/2;
    var top    = (screen.height - height)/2;
    newwindow=window.open('productpopup.php?product=' + id,'name','height=' + height + ',width=' + width + ',scrollbars=yes,resizable=yes,top=' + top + ',left=' + left);
    newwindow.moveTo(500, 200);
    if (window.focus) {newwindow.focus()}
}

function disableEnterKey(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     
    
     if ((key == 13) && ($('input[name="product"]').val() != ''))
        addtocart($('input[name="product"]').val(), 1);
        
     return (key != 13);
}

