//function to add item to shopping cart
function addItem(itemID) {
     goCart();
	 var itemID = itemID;
	 var action = 'AddToCart';
	 //var thisqty = 'qty'+itemID;
	 var qty = $('#prodqty').val();
	 if(qty==0){ qty=1; }
     $.get("control.php", { action: action, itemID: itemID, qty: qty },
         function(data){
			 //do smething else
			if(data==1){
				//$('#hint').fadeIn(100);
				//$('#hint').html('<div style="color:green">Item Added</div>');
				//$('#hint').animate({opacity: 1.0}, 500).fadeOut(1500);
				$('#displaycart').fadeIn(1000).load('cartsummary.php');
				//showAlert('<div align=center><p class=hdr>Artwork Added To List</p></div>');
				location.href='cart.php';
				//$('#'+thisqty).val("");
			}else if(data==2){
				showAlert('<div align=center><p class=hdr>Item Already Selected</p>If you wish to buy more, <br />click <a href=cart.php style=color:#000>here</a> to access your shopping cart.</b></div>');
			}else if(data==3){
				showAlert('<div align=center><p class=hdr>Item Not Available</p>Please <a href=index.php style=color:#000>here</a> to continue browsing our store.<br />This item could be out-of-stock or not up for pre-orders yet.</div>');
			}else if(data==0){
				showAlert('<div align=center><p class=hdr>Item Not Available</p>Please <a href=index.php style=color:#000>choose another item</a>.</div>');
			}
     });
}

function updateCart(cartID) {
     goCart();
	 var cartID = cartID;
	 var action = 'updateCart';
	 //var thisqty = 'qty'+itemID;
	 var qty = $('#prodqty_'+cartID).val();
	 if(qty==0){ qty=1; }
     $.get("control.php", { action: action, cartID: cartID, qty: qty },
         function(data){
			 //do smething else
			if(data==1){
				//$('#hint').fadeIn(100);
				//$('#hint').html('<div style="color:green">Item Added</div>');
				//$('#hint').animate({opacity: 1.0}, 500).fadeOut(1500);
				//$('#displaycart').fadeIn(1000).load('cartsummary.php');
				//showAlert('<div align=center><p class=hdr>Artwork Added To List</p></div>');
				location.href='cart.php?i=quantity_updated';
				//$('#'+thisqty).val("");
				//showAlert('<div align=center><p class=hdr>Quantity Updated</p>Click ok to continue.</div>');
			}else if(data==0){
				showAlert('<div align=center><p class=hdr>Item Not Available</p>Please <a href=index.php style=color:#000>choose another item</a>.</div>');
			}
     });
}
//function to add item to shopping cart
function addGiftList(itemID) {
     goProfile();
	 var itemID = itemID;
	 var action = 'AddToCart';
	 //var thisqty = 'qty'+itemID;
	 var qty = 1;
     $.get("control.php", { action: action, itemID: itemID, qty: qty },
         function(data){
			 //do smething else
			if(data==1){
				//$('#hint').fadeIn(100);
				//$('#hint').html('<div style="color:green">Item Added</div>');
				//$('#hint').animate({opacity: 1.0}, 500).fadeOut(1500);
				$('#displaycart').fadeIn(1000).load('cartsummary.php');
				//showAlert('<b>Artwork Added To List</b>');
				location.href='profile.php?i=add_gifts&id='+itemID;
				//$('#'+thisqty).val("");
			}else if(data==2){
				jAlert('<div align=center><p class=hdr>Artwork Already Selected</p>Please choose another artwork.</b></div>','Shopping Cart');
			}else if(data==3){
				jAlert('<div align=center><p class=hdr>Artwork Sold</p>Please choose another artwork.</div>','Shopping Cart');
			}else if(data==0){
				jAlert('<div align=center><p class=hdr>Artwork Not Available</p>Please choose another artwork.</div>','Shopping Cart');
			}
     });
}



//show jsAlert
function showAlert(msg){
	jAlert(msg, 'Shopping Cart');
}

function showWarning(msg,title){
	jAlert(msg, title);
}

function showSWarning(msg,title){
	$().ready(function() {
	jAlert(msg, title);
	});
}

//function to add item to shopping cart
function clearAllItems() {
	 var action = 'ClearAllItems';
     $.get("control.php", { action: action },
         function(data){
			 //do smething else
			if(data==1){
				//$('#hint').fadeIn(100);
				//$('#hint').html('<div style="color:black">All Items Removed</div>');
				//$('#hint').animate({opacity: 1.0}, 500).fadeOut(1500);
				$('#displaycart').fadeIn(1000).load('cartsummary.php');
				//jAlert('All Items Removed From Cart', 'Shopping Cart V2');
			}
     });
}
//clear text field on click
function clearText(field){
    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;} 

//load data into displaycart div, on load of page
$.get("cartsummary.php",
         function(data){
			 $('#displaycart').fadeIn(100).load('cartsummary.php');
     });

//update currency
function currencyUpdate(){
	 var currency = document.cur.cursel.value;
	 var action = 'currencyUpdate';
     $.get("currency.php", { action: action, currency: currency },
         function(data){
			 //do smething else
			if(data==1){
				$('#displaycart').fadeIn(1000).load('cartsummary.php');
				location.reload(true);
			}
     });
}
//Javascript addslashes function, like PHP
function addslashes(str) {
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\0/g,'\\0');
return str;
}
//Javascript stripslashes function, like PHP
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
str=str.replace(/\\0/g,'\0');
return str;
}

function goProfile(){
	var action='gotoProfile';
    var s;
    $.get("expresscheckout.php", { action:action }, function(data){
		    if(data==1){
			//alert('ok');
			s='ok';
			}
	});
	//alert('parsed');
}

function goCart(){
	var action='gotoCart';
    var s;
    $.get("expresscheckout.php", { action:action }, function(data){
		    if(data==1){
			//alert('ok');
			s='ok';
			}
	});
	//alert('parsed');
}

function changeSortOrder(value1,value2){
   var action='sortcode';
   var th1=value1;
   var th2=value2;
   var sv= $('#sortselector').val();
   $.get("setsortorder.php", {action: action, sv: sv, g: th1, c: th2 }, function(data){
		    if(data==1){
			//alert('ok');
			location.reload(true);
			}
	});
}

//funzcentre ajax functions
function viewAll(c, g, p){
   var c = c;  //cat
   var g = g;  //type
   var p = p;  //page number 0, means page 1
   var action= 'paging';
   //alert("ok");
   $.get("pagingcookie.php", { action:action, c: c , p: p, g: g}, function(data){
		    if(data==1){
			location.reload(true);
			}
	});
}
