function isUnsignedInteger(s){
		return (s.toString().search(/^[0-9]+$/) == 0);
		}
function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\0/g,'\0');
	str=str.replace(/\\\\/g,'\\');
	return str;
	}
function calculateTotal(){
	jQuery('.check:checked').each( function() {
	 	var value=parseFloat(jQuery(this).attr('id'));
		quantity=parseFloat(jQuery(this).siblings('.short').val());
		id=jQuery(this).val();
		
		jQuery('#course'+id).show();
		jQuery('#course'+id).children('#quantity').html('');
		jQuery('#course'+id).find('.price').html('');
		jQuery('#course'+id).children('#quantity').html(quantity);
		jQuery('#course'+id).find('.price').html('<strong>'+quantity*value+'</strong>');
		var val1=quantity*value;
		val=val+val1;
		
});
disc=jQuery('#disc').val();
val=val-disc;
jQuery('.total').html('');
jQuery('.total').html('<h3>'+' Total: $'+ val + '</h3>');
jQuery('#amount').val(val);
}

function applyPromoCode(){
	var array=new Array();
	if(jQuery('#promo_code').val()==''){
			jQuery('#discount').removeAttr("disabled");
			alert('Please Enter a promo Code');
		       }else{
		    	   var sign=jQuery('#p_code').val();
				      var inp=jQuery('#promo_code').val();
				      array=sign.split(',');
				      for(var j=1;j<=array.length;j++){
				       if(array[j]== inp){
				       var go=0;
				       break;
				       }else{
				       var go=1;
				       }
				     }
				    if(go==1){
				    	 jQuery('.loader').show();
		    	    code=jQuery('#promo_code').val();
		    	    jQuery.getJSON("../promoCodeVerification.php?code=" + code,function(json) {
		    	    	jQuery('#discount').removeAttr("disabled");
		    	       var p_codes=jQuery('#p_code').val();
		    		   var length=json['course'].length;//gets the courses id in which this promocode is beging applied
		    		   var dis=json['discount'];
		    		   var type=json['type'];
		    		  
		    		   jQuery('#discount_type').val(type);
		    	       	if(dis == 0){
		    	       	 jQuery('.loader').hide();
		            	   alert('Invalid Promo Code');
		    	       	}else{
		    	       			p_codes=p_codes+','+code;
		    	       			jQuery('#p_code').val(p_codes);//keeping a track of what all promo code a user applied so display in email
		    	       			var per=0;
		    	       			for ( var i = 0; i < json['course'].length; i++) {
					                var id=json['course'][i];
					                var type=json['type'];
					                discount=json['discount'];
					                var c_type=json['c_type'][id];

					                if(c_type==1){
					                jQuery('#course'+id).find('#flag'+id).val(1);//to keep a track that a promocode was beign applied to this course
					                	
					                	if(!(jQuery('.check[value='+id+']').is(':checked'))){//If user did not select the checkbox for promo code applied course before
						                    
					                		jQuery('.check[value='+id+']').attr('checked',true);
						                    jQuery('#quantity_'+id).val(1);
						                    jQuery('#course'+id).find('#discount_summary').append('<div class="box"><strong>Promo Code Discount</strong>&nbsp;&nbsp;-<span class="dis_type">'+ type +'</span><span class="dis_amount"> '+ discount+'</span><br/></div>');
						                    jQuery('#course'+id).show();
						                    
						                    if(type=='$'){// if discount type was $
							                    val=parseFloat(val)+ parseFloat(jQuery('.check[value='+id+']').attr('id'))-discount;
							                    per=jQuery('#disc').val();//#disc contains the total discount given so far...so updated every time a discount is applied or taken off
							                    per=parseFloat(per)+parseFloat(discount);
							                    per=parseFloat(per);
							                    jQuery('#disc').val(per);
						                     }else{// if discount tyope was %
							                    var percent=(discount/100)*(parseFloat(jQuery('.check[value='+id+']').attr('id')));
							                    per=jQuery('#disc').val();
							                    per=parseFloat(per)+parseFloat(percent);
							                    per=parseFloat(per);
							                    jQuery('#disc').val(per);
							                    val=parseFloat(val)+ parseFloat(jQuery('.check[value='+id+']').attr('id'))-percent;
				                             }
						                    jQuery('.total').html('');
								    	    jQuery('.total').html('<h3>'+' Total: $'+ val + '</h3>');
						    	       		jQuery('#amount').val(val);
						    	       		jQuery('.loader').hide();
		                   
					                 }else{//if the promocode applicable course was selected before
					                	 
					                	 	var quant= jQuery('#quantity_'+id).val();//get its quantity
						                    var price=parseFloat(jQuery('.check[value='+id+']').attr('id'));
						                 
						                    jQuery('#course'+id).find('#discount_summary').append('<div class="box"><strong>Promo Code Discount</strong>&nbsp;&nbsp;-<span class="dis_type">'+ type +'</span><span class="dis_amount"> '+ discount+'</span><br/></div>');
						                    jQuery('#course'+id).show();
						                   
						                    if(type=='$'){
							                    discount=quant*discount;
							                    val=parseFloat(val)-parseFloat(discount);
							                    per=jQuery('#disc').val();
							                    per=parseFloat(per)+parseFloat(discount);
							                    per=parseFloat(per);
							                    jQuery('#disc').val(per);
						                    }else{
						                       var percent=(discount/100)*(quant*price);
						                       per=jQuery('#disc').val();
						                       per=parseFloat(per)+parseFloat(percent);
						                       per=parseFloat(per);
						                       jQuery('#disc').val(per);
						                       val=parseFloat(val)-parseFloat(percent);
						                    }
						                    jQuery('.total').html('');
								    	    jQuery('.total').html('<h3>'+' Total: $'+ val + '</h3>');
						    	       		jQuery('#amount').val(val);
						    	       	  jQuery('.loader').hide();
					                 }
		    	       			}
		    	       			else{
		    	       				val=0;
		    	       			 jQuery('.loader').show();
		    	       			 jQuery('.retail').find('.check:checked').each( function() {
		    	       			  jQuery(this).attr('checked', false);
                                  var m_id=jQuery(this).val();
                                  jQuery('#quantity_'+m_id).val(0);
				                  jQuery('#course'+m_id).find('#discount_summary').html('');
				                  jQuery('#course'+m_id).hide();
		    	       			 });
		    	       			
		    	       			 
		    	       			 jQuery('#course'+id).find('#flag'+id).val(1);
		    	       				jQuery.getJSON("../courseDetails.php?id="+id,function(json) {
		    	       	                       jQuery('#special').append(
					                    		'<div style="padding-top:10px;width:49%;padding-bottom:10px"><div style="width: 100%; padding-right: 10px; height: 70px;"><div style="float: left; width: 22%; padding-top: 5px;"><input type="checkbox" class="check" id="'+json['price']+'" value="'+json['id']+'" name="day[]"/><input type="text" value="1" class="short" id="quantity_'+json['id']+'" name="quantity_'+json['id']+'" style="width: 30px;"/></div><div style="float: left; width: 75%;"><strong>'+json['name']+' - $'+json['price']+'</strong><br/><em>'+stripslashes(json['desc'])+'</em></div><div style="clear: both;"></div></div></div>'  
					                    		);
					                    jQuery('.check[value='+id+']').attr('checked',true);
					                    jQuery('#quantity_'+id).val(1);
					                    jQuery('#course'+id).find('#discount_summary').append('<div class="box"><strong>Promo Code Discount</strong>&nbsp;&nbsp;-<span class="dis_type">'+ type +'</span><span class="dis_amount"> '+ discount+'</span><br/></div>');
					                    jQuery('#course'+id).show();
					                  
					                        if(type=='$'){// if discount type was $
					                        	 jQuery('.loader').hide();
						                   // val=parseFloat(val)+ parseFloat(json['price'])-discount;
						                    jQuery('#disc').val(0);//#disc contains the total discount given so far...so updated every time a discount is applied or taken off
						                    spd=spd+parseFloat(discount);
						                    spd=parseFloat(spd);
						                    jQuery('#disc').val(spd);
						                    
						                    calculateTotal();
					                     }else{// if discount tyope was %
						                    var percent=(discount/100)*(json['price']);
						                    jQuery('#disc').val(0);
						                    spd=parseFloat(spd)+parseFloat(percent);
						                    spd=parseFloat(spd);
						                    jQuery('.loader').hide();
						                    jQuery('#disc').val(spd);
						                    calculateTotal();
						                   // val=parseFloat(val)+parseFloat(json['price'])-percent;
			                             }
					                 
					                    //jQuery('.total').html('');
							    	 	//jQuery('.total').html('<h3>'+' Total: $'+ val + '</h3>');
					    	       		//jQuery('#amount').val(val);
					    	       		//jQuery('.loader').hide();
		    	       				});
		    	       			}
		    	       	}
    	       		
		    	       	
		    	       	}
		    	       	});
		    	  
		    	    }else {
		    	    	jQuery('#discont').removeAttr("disabled");
		    	    	alert('Promocode Can not be used Twice');
		    	    }
	}
}

function chooseCourses(y){
	val=0;
	if(jQuery(y).is(':checked')){//if checked for the first time
		wait=0;
			jQuery(y).siblings('.short').val(1);
			var allcodes=jQuery('#p_code').val();
			var idchk=jQuery(y).val();
			var currdis=parseFloat(jQuery('#disc').val());
			var d=0;
			
			if(allcodes){
				 jQuery('.loader1').show();
				 jQuery.getJSON("../promocodeDetails.php?code=" + allcodes +"&id="+idchk,function(json) {
					 if(json['amount'] != 0){	
						 d=currdis+parseFloat(json['amount']);
						 for ( var i = 0; i < json['discount'].length; i++){
						 jQuery('#course'+idchk).find('#discount_summary').append('<div class="box"><strong>Promo Code Discount</strong>&nbsp;&nbsp;-<span class="dis_type">'+ json['type'][i] +'</span><span class="dis_amount"> '+ json['discount'][i]+'</span><br/></div>');
						 }
						 jQuery('.loader1').hide();
						 jQuery('#disc').val(d);
						 calculateTotal();
					 }else{
						 calculateTotal();
						 jQuery('.loader1').hide();
					 }
					
				 });
			}
			else {
				 calculateTotal();
			}
		
			
			}else{//if unchecked
				wait=1;
				var j=0;
				id=jQuery(y).val();
				if(jQuery('#course'+id).find('#flag'+id).val()==1){//if there was a discount associated with the course
					
					jQuery('#course'+id).find('#discount_summary').find('.box').each(function(){//for each discount given to the course
					distype=jQuery(this).find('.dis_type').html();
					disamount= parseFloat(jQuery(this).find('.dis_amount').html());
					if(distype=='%'){
					var qty=jQuery('#quantity_'+id).val();
					var cost=jQuery('.check[value='+id+']').attr('id');
					disc= parseFloat(qty)*(disamount/100*parseFloat(cost));
					disc=parseFloat(jQuery('#disc').val())-parseFloat(disc);//reduce total discount given for the course
					jQuery('#disc').val(disc);
					}else{
						var qty=jQuery('#quantity_'+id).val();
						var cost=jQuery('.check[value='+id+']').attr('id');
						disc= parseFloat(qty)*(parseFloat(disamount));
						disc=parseFloat(jQuery('#disc').val())-parseFloat(disc);
						jQuery('#disc').val(disc);
					}
					
			});	
		}
               
				jQuery(y).siblings('.short').val(0);
				jQuery('#course'+id).find('#discount_summary').html('');
				jQuery('#course'+id).hide();
				jQuery('#course'+id).children('#quantity').html('');
				jQuery('#course'+id).find('.price').html('');

	}
	
		if(wait==1){
	    calculateTotal();
		}
		

}
function quantityUpdate(x){
	val=0;
	disc=jQuery('#disc').val();//get discount so far..
	input=jQuery(x).val();
	
	if(isUnsignedInteger(input)==false){
		alert ('Input Field Must be Integer');
		jQuery(x).val(0);
	} 
	
	id=jQuery(x).siblings('.check').val();//get the checkbox id
	//check if any promocode was active for the course


	if(jQuery('#course'+id).find('#flag'+id).val()==1){
      
		jQuery('#course'+id).find('#discount_summary').find('.box').each(function(){ 
		distype=jQuery(this).find('.dis_type').html();
		disamount= parseFloat(jQuery(this).find('.dis_amount').html());
		
		if(distype=='%'){
			
		var qty=jQuery('#quantity_'+id).val();
		var cost=jQuery('.check[value='+id+']').attr('id');
        var earlier=parseFloat(jQuery('#course'+id).children('#quantity').html());
        //reduce the discount given for this course before
        var chance=earlier*(disamount/100*parseFloat(cost));

         disc=parseFloat(disc)-parseFloat(chance);
     
         //add the discount given for the this course
 		var disc1= parseFloat(qty)*(disamount/100*parseFloat(cost));
 
 		netdisc1=parseFloat(netdisc1)+parseFloat(disc1);
 	 
      	   	}else{
			var qty=jQuery('#quantity_'+id).val();
			var cost=jQuery('.check[value='+id+']').attr('id');
			var earlier=parseFloat(jQuery('#course'+id).children('#quantity').html());
		    var chance=earlier*(disamount);
		    disc=parseFloat(disc)-parseFloat(chance);
		
            var disc2= parseFloat(qty)*parseFloat(disamount);
           	netdisc2=parseFloat(netdisc2)+parseFloat(disc2);
           
       	}
		
});	
		disc=disc+netdisc1+netdisc2;
		
       jQuery('#disc').val(disc);
       netdisc1=0;netdisc2=0;
		}
	  calculateTotal()
}
function validate(){
	var flag=1;

	if(jQuery('#first_name').val()==''){
     alert('Please Provide your First name'); 
     flag=0;
 	}
	if(jQuery('#last_name').val()==''){
	     alert('Please Provide your Last name'); 
	     flag=0;
	}
	if(jQuery('#email').val()==''){
	     alert('Please Provide your Email Address'); 
	     flag=0;
	}
	if(jQuery('#conf_email').val()==''){
	     alert('Please Confirm the Email'); 
	     flag=0;
	}
	if(jQuery('#email').val() !=jQuery('#conf_email').val()){
		 alert('Both The Emails Are Not Same'); 
	     flag=0;

	}
	if(jQuery('#address').val()==''){
	     alert('Please Provide your  Address'); 
	     flag=0;
	}
	if(jQuery('#city').val()==''){
	     alert('Please Provide your city'); 
	     flag=0;
	}
	if(jQuery('#state_providence').val()==''){
	     alert('Please Provide your state/providence Address'); 
	     flag=0;
	}
	if(jQuery('#phone_number').val()==''){
	     alert('Please Provide your state/providence Address'); 
	     flag=0;
	}
	if(jQuery('#card_number').val()==''){
	     alert('Please Enter A CARD NUMBER'); 
	     flag=0;
	}
	if(jQuery('#exp_month').val()==0){
	     alert('Please Select Expire Month of your CARD'); 
	     flag=0;
	}
	if(jQuery('#exp_year').val()==0){
	     alert('Please Select Expire Year of your CARD'); 
	     flag=0;
	}
	if(jQuery('#cvc_code').val()==''){
	     alert('Please Enter The CVC Code'); 
	     flag=0;
	}
	if(!(jQuery('input[name=card_type]').is(':checked'))) {
		  alert('Please Select Your Card Type'); 
		     flag=0;
		}

	if(flag==0)
		return false;
	else{
      jQuery('input[type=checkbox]').removeAttr('disabled');
		return true;
	}
	
	
}

function  chooseOffer(x){
	val=0;
	if(jQuery(x).is(':checked')){//if checked for the first time
		jQuery(x).siblings('.short').val(1);
		}else{//if unchecked
			id=jQuery(x).val();
			jQuery(x).siblings('.short').val(0);
			jQuery('#course'+id).find('#discount_summary').html('');
			jQuery('#course'+id).hide();
			jQuery('#course'+id).children('#quantity').html('');
			jQuery('#course'+id).find('.price').html('');
	}
	 offerTotal();
}
function offerUpdate(x){
	val=0;
	input=jQuery(x).val();
	if(isUnsignedInteger(input)==false){
		alert ('Input Field Must be Integer');
		jQuery(x).val(0);
	} else{
	offerTotal();
	}

}
function offerTotal(){
	jQuery('.check:checked').each( function() {
	 	var value=parseFloat(jQuery(this).attr('id'));
		quantity=parseFloat(jQuery(this).siblings('.short').val());
		id=jQuery(this).val();
		jQuery('.boxes').hide();
		jQuery('#course'+id).show();
		jQuery('#course'+id).children('#quantity').html('');
		jQuery('#course'+id).find('.price').html('');
		jQuery('#course'+id).children('#quantity').html(quantity);
		jQuery('#course'+id).find('.price').html('<strong>'+quantity*value+'</strong>');
		var val1=quantity*value;
		val=val+val1;
		
});
jQuery('.total').html('');
jQuery('.total').html('<h3>'+' Total: $'+ val + '</h3>');
jQuery('#amount').val(val);
}




