//ROOT_PATH='http://localhost/playgroundaccessories/';
ROOT_PATH='http://www.playgroundaccessories.com.au/';
//ROOT_PATH='http://66.29.157.201/';

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


function formatCurrency(num) {
	num = num.substr(1,num.length);
    num = isNaN(num) || num === '' || num === null ? 0.00 : num;
   return parseFloat(num).toFixed(2);
}
function showDiv(product_id)
{
	document.getElementById('spinner'+product_id).style.display='block';	
	hideMsg(product_id);
}

function hideDiv(product_id)
{
	document.getElementById('spinner'+product_id).style.display='none';	
}

function showMsg(product_id)
{
	//alert(document.getElementById('msg'+product_id)); return false;
	document.getElementById('msg'+product_id).style.display='block';	
	
}

function hideMsg(product_id)
{
	document.getElementById('msg'+product_id).style.display = 'none';	
	document.getElementById('msg'+product_id).innerHTML = "";
}

function showHidePopup()
{
	document.getElementById('delete_error').style.display='none';
	var popup=document.getElementById('popup');	
	if(popup.style.display == 'block')
	popup.style.display='none';
	else
	popup.style.display='block';
}

function getCart(html,product_id)
{
	document.getElementById('cart').innerHTML = html;
	document.getElementById('msg'+product_id).innerHTML = "Product added to cart";
}

function add_to_cart(product_id)
{
	
	var i = Math.random();
    var request_url = ROOT_PATH+'ecommerce/add_to_cart/';
    var quantity = document.getElementById(product_id).value;
    if(quantity == '')
    quantity = 1;
    
    var pars = 'rand_var='+i+'&quantity='+quantity+'&product_id='+product_id;
    //var pars = product_id+'/'+quantity+'/'+i;
    $.ajax({
			url:request_url,
			type: "POST",
			data: pars,
			dataType: 'html',
			beforeSend:showDiv(product_id),
			success: function(html)
			{
				html = trim(html);
				if(html == 'bulky')
				{
					
					alert("We're sorry we don't currently sell this oversize item online.\nIf you would like to order this product please call us on 07 3216 5255 or email us so that we can discuss shipping costs and arrange payment and delivery!");
					hideDiv(product_id);
					return false;
				}
				if(html == 'outofstock')
				{
					alert("We're sorry we don't have enough stock of this product.\nCall 07 3216 5255 or email us to see when they will be restocked!");
					hideDiv(product_id);
					return false;
				}
				
				hideDiv(product_id);
				showMsg(product_id);
				//show_cart_total(html,product_id);
				getCart(html,product_id);	
			},
			timeout: 40000,
		    error: function(){
		        alert('Error: The operation failed to complete');
		        hideDiv(product_id);
		    }

		});

}


//---------------------------Functions for right hand cart--------------------------------------//

function remove(key)
{
	
	var i = Math.random();
    var request_url = ROOT_PATH+'ecommerce/remove/';
    
    var pars = 'rand_var='+i+'&key='+key+'&main_cart=0';
    $.ajax({
			url:request_url,
			type: "POST",
			data: pars,
			dataType: 'html',
			beforeSend:showSpinner('spinner'),
			success: function(html)
			{
				//alert(html); return false;
				hideSpinner('spinner');
				update_cart_after_remove(html);	
			},
			timeout: 40000,
		    error: function(){
		        alert('Error: The operation failed to complete');
		    }

		});

}

function update_cart_after_remove(html)
{
	document.getElementById('cart').innerHTML = html;
	document.getElementById('msg_cart').innerHTML = "Product removed from basket";
	document.getElementById('msg_cart').style.display= 'block';
}

function showSpinner(id)
{
	document.getElementById(id).style.display='block';	
}

function hideSpinner(id)
{
	document.getElementById(id).style.display='none';	
}

function update_cart(key)
{
	
	var i = Math.random();
    var request_url = ROOT_PATH+'ecommerce/update_cart/';
    quantity = document.getElementById('txt_quantity_'+key).value;
    var pars = 'rand_var='+i+'&quantity='+quantity+'&key='+key+'&main_cart=0';
    $.ajax({
			url:request_url,
			type: "POST",
			data: pars,
			dataType: 'html',
			beforeSend:showSpinner('spinner'),
			success: function(html)
			{
				//alert(html); return false;
				hideSpinner('spinner');
				update_cart_after_quantity_change(html);	
			},
			timeout: 40000,
		    error: function(){
		        alert('Error: The operation failed to complete');
		    }

		});

}
function update_cart_after_quantity_change(html)
{
	document.getElementById('cart').innerHTML = html;
	document.getElementById('msg_cart').innerHTML = "Your basket updated";
	document.getElementById('msg_cart').style.display = 'block';
}

//-------------------------------Functions end for Right hand cart--------------------------------------//

//-------------------------------Functions for Main Cart-----------------------------------------------//

function remove_main(key)
{
	
	var i = Math.random();
    var request_url = ROOT_PATH+'ecommerce/remove/';
    
    var pars = 'rand_var='+i+'&key='+key+'&main_cart=1';
    $.ajax({
			url:request_url,
			type: "POST",
			data: pars,
			dataType: 'html',
			beforeSend:showSpinner('spinner_main'),
			success: function(html)
			{
				//alert(html); return false;
				hideSpinner('spinner_main');
				update_main_cart_after_remove(html);	
			},
			timeout: 40000,
		    error: function(){
		        alert('Error: The operation failed to complete');
		    }

		});

}

function update_main_cart_after_remove(html)
{
	document.getElementById('main_cart').innerHTML = html;
	document.getElementById('msg_main_cart').innerHTML = "Product removed from basket";
	document.getElementById('msg_main_cart').style.display= 'block';
}


function update_main_cart(key)
{
	
	var i = Math.random();
    var request_url = ROOT_PATH+'ecommerce/update_cart/';
    quantity = document.getElementById('quantity_'+key).value;
    var pars = 'rand_var='+i+'&quantity='+quantity+'&key='+key+'&main_cart=1';
    $.ajax({
			url:request_url,
			type: "POST",
			data: pars,
			dataType: 'html',
			beforeSend:showSpinner('spinner_main'),
			success: function(html)
			{
				//alert(html); return false;
				hideSpinner('spinner_main');
				update_main_cart_after_quantity_change(html);	
			},
			timeout: 40000,
		    error: function(){
		        alert('Error: The operation failed to complete');
		    }

		});

}
function update_main_cart_after_quantity_change(html)
{
	document.getElementById('main_cart').innerHTML = html;
	document.getElementById('msg_main_cart').innerHTML = "Your basket updated";
	document.getElementById('msg_main_cart').style.display = 'block';
}

//-------------------------------Functions for Main Cart ends------------------------------------------//
