/**
 * @author mukul.das
 */

/*
 * This function will add the simulation to cart
 * parameter : obj simulation_id
 * 
 * Return: Null
 * 
 */

function addToCart(obj){
	//simulation_id
	var sim_id;
	
	//checking whther parameter is objec or not
		if (obj != undefined) {
			if (obj.id == undefined) {
				sim_id = obj;
			}
			else {
				sim_id = obj.id;
			}
		}	
		//creating the query string			
			var str_request = "id=" + sim_id;
			str_request=str_request+"&cart_id= "+ $('#cart_id').val();
			var arr_response = new Array();
			//calling loader Box 
			loaderBox();
			str_url=base_url+"cart/add-item";
			//ajax calling
			$.ajax({
				type: "POST",
				url: base_url+"cart/add-item",
				data: str_request,
				success: function(msg){
					$.modal.close();
					var json = json_parse(msg, 'json');
					//assigng the total simulation
					$('#totalSimulations').html(json.total_simulations);
					
					//assinging the shopping-cart-id
	                $('#cart_id').val(json.shopping_id);
					itemTotalMessage(json.total_simulations);
					
					//calling the alert box after adding the simulation
					alertBox(json.msg);
				}
			});
}

/*
 * This will changes the item or items according the number of the simulations
 * 
 * Parameter: total simulation : int
 * 
 * Return :Null
 */
function itemTotalMessage(total_simulations){
				 if(total_simulations >1){
					 	$('#simItemMsg').html(" Items");
				 }else{
				 	$('#simItemMsg').html(" Item");
				 }
}

/*
 * This is the loader box
 * 
 * Parameter: Null
 * 
 * Return :Null
 * 
 */
function loaderBox(){	
$('#modalBox').html('<div class="loader" id="loader" style="display:none">'
												+'<div class="loaderanim"></div>'
												+'<div class="loadermid"> '
												+'<div class="titletextloading">Adding To Cart.. !</div>'
												+'</div>'												
												+'<div class="loaderright"></div>'
												+'</div>'
												);
			$('#loader').modal({close: false ,position: ["10%","40%"]}) ;
}

/*
 * This is the alert box
 * 
 * Parameter: message to be displayed at the alert box : string 
 * 
 * Return :Null
 * 
 */
function alertBox(message, title){
	if (title == undefined) {
		title = "Add To Cart";
	}
$('#modalBox').html('<div class="liquid-round" id="dialogAlertBox">'
								+'<div class="topalert">'
								+'<div class="topleftalert"></div>'
								+'<div class="topmidalert">'
								+'<div class="titletextalert" id="titletext">' + title +'</div>'
								+'</div>'
								+'<div class="toprightalert"></div>'
								+'</div>'
								+'<div class="midalert">'
								+'<div class="midleftalert"></div>'
								+'<div class="contentmidalert">'
								+'<label  id="contentAlert">'+message+' </label>'
								+'<img src="'+theme_add+'/img/okbutton.gif" alt="Ok" class="controlsalert" style="display: none;cursor:pointer" onclick="resetmodalBox()" />'
								+'</div>'
								+'<div class="midrightalert"></div>'
								+'</div>'
								+'<div class="bottomalert">'
								+'<div class="bottomleftalert"></div>'
								+'<div class="bottommidalert"></div>'
								+'<div class="bottomrightalert"></div>'
								+'</div>'
								+'</div>'	);
					$('#dialogAlertBox').modal({close: false,
							containerCss:{
							backgroundColor:"transparent",
							borderColor:"transparent",
							height:0,
							padding:0,
							width:0
						}
	
					});	
}

/*
 * This is  add simualtion to the cart from the simulation list
 * 
 * Parameter: simulation_id object
 * 
 * Return :null
 * 
 */
function addToCartFromList(obj){	

		//Checking the whether it is object or not
        if (obj.id == undefined) {
            sim_id = obj;
        }
        else {
            sim_id = obj.id;
        }
		//creating query string
        var str_request = "id=" + sim_id;
		str_request=str_request+"&from=fromList";
		str_request=str_request+"&cart_id="+ $('#cart_id').val();
        var arr_response = new Array();
		if(0==$('#cart_id').val() || ""==$('#cart_id').val()){
			str_url=base_url+"cart/create-cart" ;
		}else{
			str_url = base_url+"cart/add-item";
		}
		
		//$.modal.close();
		//loaderBox();
//		$('#modalBox').html(	'<div class="loader" id="loader" >'
//												+'<div class="loaderanim"></div>'
//												+'<div class="loadermid"> '
//												+'<div class="titletext">Loading.. Please Wait!</div>'
//												+'</div>'												
//												+'<div class="loaderright"></div>'
//												+'</div>'
//												);

		//ajax call
        $.ajax({
            type: "POST",
            url: str_url,
            data: str_request,
            success: function(msg){
			var inviteId = "";
                var json = json_parse(msg, 'json');
                $('#totalSimulations').html(json.total_simulations);
				itemTotalMessage(json.total_simulations);
                $('#cart_id').val(json.shopping_id);
				
				//calling to display the list again
				//validInvite($('#i').val(),"Adding To Cart ...!");
				$("#shopping_cart_details").attr("action",base_url+"cart");
				//$('#shopping_cart_details').action(base_url+"")
				$('#shopping_cart_details').submit();
            }
        });    
}

/* Need to be move to other file*/
/*
 * This is to move to registration page from simulation list file
 * 
 * Parameter: null
 * 
 * Return :Null
 * 
 */
function gotoNextPage(){
//        var arr_response = new Array();
////		str_request="";
//		var str_request="cart_id= "+ $('#cart_id').val();
//		$.modal.close();
//		$('#modalBox').modal({close: false ,position: ["10%","40%"]}) ;
//		//loaderBox();
//		
//		//loading part
//		$('#modalBox').html(	'<div class="loader" id="loader" >'
//												+'<div class="loaderanim"></div>'
//												+'<div class="loadermid"> '
//												+'<div class="titletextloading">Loading.. Please Wait!</div>'
//												+'</div>'												
//												+'<div class="loaderright"></div>'
//												+'</div>'
//												);
//        //calling teh ajax
//		$.ajax({
//            type: "POST",
//            url: base_url+"register/simulation-player-register",
//            data: str_request,
//            success: function(msg){			
//				$.modal.close();	
//                $('#modalBox').html(msg);
//				$('#modalBox').modal({close: false , position: ["5%","15%"]});
//            }
//        });	

	//	$("#shopping_cart_details").attr("action",base_url+"register/simulation-player-register");
	
	var  coupon_code=$('#coupon_code_temp').val();	
	$('#coupon_code').val(coupon_code);		
	if (checkDiscountCoupon(coupon_code)) {
		$("#shopping_cart_details").attr("action", base_url + "cart/sign-up");
		//$('#shopping_cart_details').action(base_url+"")
		$('#shopping_cart_details').submit();
	}
}


/*
 * This will execute before deleting the simulation from list
 * 
 * Parametr: simulation id : int
 * 
 * Return :null
 * 
 */
 function deleteFromCart(str_sim_id){
 	
	//Checking whether it is object or nots
       if (str_sim_id.id == undefined) {
            sim_id = str_sim_id;
        }
        else {
            sim_id = str_sim_id.id;
        }
		
		//creating query string
        var str_request = "id=" + sim_id;
		str_request=str_request+"&cart_id= "+ $('#cart_id').val();
		
		//ajax call
        $.ajax({
            type: "POST",
            url:base_url+"/cart/delete-item",
            data: str_request,
            success: function(str_msg){

				var json = json_parse(str_msg, 'json');
					$('#totalSimulations').html(json.total_simulations);
					itemTotalMessage(json.total_simulations);
					
					//calling to display the list after deleting
					//validInvite($('#i').val(), "Removing From Cart ...!");
				$("#shopping_cart_details").attr("action",base_url+"cart");
				//$('#shopping_cart_details').action(base_url+"")
				$('#shopping_cart_details').submit();
			}
        });
		
 }

/*
 * This is to close the modal and reset the modalBox div..
 * 
 */


 function resetmodalBox(){
			  $.modal.close(); 			
              $('#modalBox').html("");
 }
 
 
 
 /*
 * This is to close the modal and reset the modalBox div..
 * 
 */

 function confirmation(){
//			  $.modal.close(); 			
//              $('#modalBox').html("");
		cart_id=$('#cart_id').val();
		 window.location=base_url+"/cart/delete-cart?cart_id="+cart_id;
 }
 
 /*
 *The function name 'demo()' is changed to 'freeDemoSignup()'. 
 * As the demo functionality will launch the demo simulation,
 * We are changing name of the function.
 */ 
 function freeDemoSignup(){	
	 window.location=base_url+"index/demo";
 }
 
 function ValidateCouponCode(){
	var  coupon_code=$('#coupon_code_temp').val();
	if(coupon_code != ""){
			if (checkDiscountCoupon(coupon_code)) {
				$('#coupon_code').val(coupon_code);		
				$("#shopping_cart_details").attr("action", base_url + "cart/review");
				//$('#shopping_cart_details').action(base_url+"")
				$('#shopping_cart_details').submit();
			}
	}else{
		
		$("#discount_coupon_msg").html("Coupon code cannot be empty.");
	}
}
