/*
* Shopping cart javascript class
* version 1.0
* For more details please read ShoppingCart.txt
*/
function ShoppingCart()
{
    var xhttp = null; // ajax obect
    
    /* config parameters */
    this.action_url = 'http://ethnostyling.com/php/payment.php';
    this.customer_id = null;
    /* _config parameters */
    
    /* callback functions */
    var callback_success = null;
    var callback_error = null;
    /* _callback funtions */
    
    /* is now loading */
    var loading = false;
    /* _is now loading*/
    
    /* set callback_success function */
    this.setCallbackSuccess = function(function_name)
    {
        if(function_name)
        {
            callback_success = function_name;
        }
    }
    /* _set callback_success function */
    
    /* set callback_error function */
    this.setCallbackError = function(function_name)
    {
        if(function_name)
        {
            callback_error = function_name;
        }
    }
    /* _set callback_error function */
    
    /* Start request */
    this.startRequest = function(params)
    {
        if(!loading)
        {
            loading = true;
            this.sendRequest(params);
        }
    }
    /* _Start request */
    
    /* Send request  */
    this.sendRequest = function(params)
    {
        try
    	{
    		xhttp=$.ajax(
    		{ 
    			type: 		"POST", 
    			url: 		this.action_url, 
    			data: 		params, 
    			success: 	on_post_success,
    			error: 		on_post_error
    		} 
    			);
    	} 
    	catch(e)
    	{
    		//alert("start_request;"+"AJAX call"+e.message);
            if(callback_error && typeof(callback_error) == 'function' )
            {
                callback_error();
                callback_error=null;
                callback_success=null;
            }
        }
    };
    /* _Send request */

    /* Parse success answer */
    var on_post_success = function(data)
    {
        loading = false; // ajax request is complete now
        // parse XML
        parse_xml(data);            
        // _parse XML
    }
    /* _Parse success answer */
    
    /* Parse XML function */
    var parse_xml = function(data)
    {	
    	try
    	{
    		var status = $("status",data).text();
            var content = $("content",data);
            switch(status) 
            {
                case "success":				
                        if(callback_success && typeof(callback_success) == 'function' )
                        {	
                            callback_success(content)
                            callback_success=null;
                        }
                    
                break;
                
                case "error":
                default:
                    if(callback_error && typeof(callback_error) == 'function' )
                    {
                        callback_error();
                        callback_error=null;
                        callback_success=null;
                    }
            }
            
    	}
    	catch(e)
    	{
            //alert("parse_xml; "+"parsing xml object; ",  e.message);        
            if(callback_error && typeof(callback_error) == 'function' )
            {
                callback_error();
                callback_error=null;
                callback_success=null;
            }
    	}	
    }
    /* _ Parse XML function */
      
    /* Parse error output */
    var on_post_error = function()
    {
        loading = false; // ajax request is complete now
        if(typeof(callback_error == 'function'))
        {
            callback_error();
        }
    }
    /* _Parse error output*/
    
    /* Initialize some config values */
    this.init = function(proxy_url,customer_id)
    {
        if(proxy_url!='undefined')
        {
            this.action_url = proxy_url;
        }
        if(customer_id!='undefined')
        {
            this.customer_id = customer_id;
        }
    }
    /* _Initialize some config values */
    
    /* Get shopping cart content */
    this.getCart = function()
    {
        var params = {'customer_id':this.customer_id,'action_code':3};
        this.startRequest(params);
    }
    /* _Get shopping cart content  */
    
    /* Delete product from shopping cart  */
    this.deleteFromCart = function(product_id)
    {
        var params = {'customer_id':this.customer_id,'product_id':product_id,'action_code':2};
        this.startRequest(params);
    }
    /* Delete product from shopping cart  */
    
    /* Add product to shopping cart  */
    this.addToCart = function(name,price,description)
    {
        var params = {'customer_id':this.customer_id,'action_code':1,'product_name':name,'product_price':price,'product_description':description};
        this.startRequest(params);
    }
    /* Add product to shopping cart  */
}