var qtyFlag =0;

function addtocart(product_id,base_path,theme,count,quantity,productname,addressid,shippingtime) {
	
		
	jQuery("#errormsg").html('');
	if(product_id == ''){
		 document.getElementById('success_info').style.display= "none";
		document.getElementById('error_info').style.display= "block";
		document.getElementById('number_info').style.display= "none";
		jQuery("#error_info").html("Please Select Product");
		return false;
	}
	if(count!=1){
		var values = document.getElementById('qty').value;	
	
		if(values!=0)
		{
			
			document.getElementById('error_info').style.display= "none";
			document.getElementById('number_info').style.display= "block";
			
			var i;
			for (i = 0; i < values.length; i++)
			 {
				var c = values.charAt(i);
				 if (((c < "0") || (c > "9"))){
					 jQuery("#number_info").html("Please Enter Numbers Only");
					 return false;
				 }
				 else{
					 

					 document.getElementById('success_info').style.display= "none";
					 jQuery("#success_info").html('"'+ productname+'"'+" was successfully added to your shopping cart.");
					 document.getElementById('number_info').style.display= "none";
				 }				 
			 }		
		}
		else {
					
			document.getElementById('error_info').style.display= "block";
			document.getElementById('number_info').style.display= "none";
			jQuery("#error_info").html("Please Enter Product Quantity");
		    post_file = base_path+"product/view/id/"+product_id;
		    jQuery("#qty").focus();
		    return false;
		}	
		count = values;
		var status;
	    post_file = base_path+"product/checkqtyavail/id/"+product_id+"/qty/"+quantity+"/count/"+count;
	}
	else{
		var status;
		post_file = base_path+"product/checkqtyavail/id/"+product_id+"/qty/"+quantity+"/count/1";
	}
	
	
    jQuery.post(post_file, { },
        function(data)
            {
	    	var opt = "";
	    	var pname = "";
	    	  	
	    	
	        if(data.length)
	        {	        	
	        	opt = data[0].name;	        	
	        	       	
			    if(opt!=0)
			    {
			    	
			    	if(quantity >= count)
			    	{
			    		
			    		jQuery("#shoppingbag").html("<div align='center'><img src='"+base_path+"frontend/themes/default/images/ajax-loader.gif'></div>");
			    		if(addressid){
			    			var giftfrom = document.getElementById('gift_from_'+addressid+'_'+product_id).value;
			    			var giftmessage = document.getElementById('gift_message_'+addressid+'_'+product_id).value;
			    			
			    			giftfrom = giftfrom.replace(/#/g,"%23");
			    			giftfrom = giftfrom.replace(/@/g,"%40");
			    			giftfrom = giftfrom.replace(/!/g,"%21");
			    			giftfrom = giftfrom.replace(/%/g,"%25");
			    			giftfrom = giftfrom.replace(/&/g,"%26");
			    			//giftmessage = giftmessage.replace(/$/g,"%24");
			    			
			    			
			    						    			
			    			giftfrom = giftfrom.split("\n");
			    			
			    		
			    			
			    			var giftfrom1 = '';
			    			for(var b=0;b<giftfrom.length;b++){
			    				
			    				giftfrom1 = giftfrom1 + giftfrom[b]+'_';
			    				
			    			}			    			
			    			giftfrom1 = giftfrom1.slice(0, -1); 
			    			
			    			
			    			
			    			
			    			
			    			giftmessage = giftmessage.replace(/#/g,"%23");
			    			giftmessage = giftmessage.replace(/@/g,"%40");
			    			giftmessage = giftmessage.replace(/!/g,"%21");
			    			giftmessage = giftmessage.replace(/%/g,"%25");
			    			giftmessage = giftmessage.replace(/&/g,"%26");
			    			//giftmessage = giftmessage.replace(/$/g,"%24");
			    			
			    			
			    						    			
			    			giftmessage = giftmessage.split("\n");
			    			
			    		
			    			
			    			var giftmessage1 = '';
			    			for(var b=0;b<giftmessage.length;b++){
			    				
			    				giftmessage1 = giftmessage1 + giftmessage[b]+'_';
			    				
			    			}			    			
			    			giftmessage1 = giftmessage1.slice(0, -1); 
			    			
			    			
			    			
			    			
			    			
			    			
			    			post_file = base_path+"product/addcart/id/"+product_id+"/count/"+count+"/addressid/"+addressid+"/giftfrom/"+giftfrom1+"/giftmessage/"+giftmessage1+"/shippingtime/"+shippingtime;
			    			
			    		}else{
			    			post_file = base_path+"product/addcart/id/"+product_id+"/count/"+count;
			    			
			    		}
				        jQuery.post(post_file, { },
				            function(data)
				                {
				        	        	document.getElementById('error_info').style.display= "none";
										
										if(addressid && addressid!='')
										{
											document.getElementById('success_info').style.display= "block";
											jQuery("#cart_count").html(count);
											jQuery("#success_info").html('"'+ productname+'"'+" was successfully added to your shopping cart.");
											window.location.href =base_path+'user/addresses#success_info';
										}else{
											document.getElementById('success_info').style.display= "none";
											window.location.href = base_path+'product/cart';
										}
				                }
			            	);
			    	}else
			    	{
			    		document.getElementById('error_info').style.display= "block";
						document.getElementById('success_info').style.display= "none";
			    		jQuery("#error_info").html('"'+ productname+'"'+" not available");
			    	}
			    }
			    else
			    {
			    	document.getElementById('error_info').style.display= "block";
					document.getElementById('success_info').style.display= "none";					
		    		jQuery("#error_info").html('Quantity of "'+productname+'"'+" not available");
			    	
			    }    	
	    			
	        }
            }
    ,'json');
	
}

function trim(str)
{
    if(!str || typeof str != 'string')
        return null;

    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}

/*function change_productdetails(value,addressid,count){
	
	var arr = explod(',',value);
	document.getElementById('product_id_'+addressid).value = arr[0];
	document.getElementById('product_name_'+addressid).value = arr[1];
	document.getElementById('remaining_quantity_'+addressid).value = arr[2];
	for(var i=1;i<=count;i++){		
		if(i==arr[3])
			document.getElementById('prods_price_'+i+'_'+addressid).style.display = 'block';
		
		else
			document.getElementById('prods_price_'+i+'_'+addressid).style.display = 'none';
	}	
	//document.getElementById('prods_price_'+addressid).style.display = 'none';
	
	
}*/

function change_productdetails(value,addressid,count,base_path,addproductarr){
	
	var arr = explod(',',value);
	document.getElementById('product_id_'+addressid).value = arr[0];
	document.getElementById('product_name_'+addressid).value = arr[1];
	document.getElementById('remaining_quantity_'+addressid).value = arr[2];
		var addprod = explod(',',addproductarr);	
		var flag;
	
	//var finalvalue  = explod('_',addprod[p]);
	
	post_file = base_path+"product/checkgiftallowed/product_id/"+arr[0];
	jQuery.post(post_file, { },
	        function(data){
		    	
		        if(data.length){
		        	opt = data[0].name;
		        	if(opt==1){
		        			//document.getElementById('address_gift_'+addressid+'_'+arr[0]).style.display = 'block';
		        			cartaddress(addprod,addressid,arr[0],1);
		        	}else {
		        			
			        		//document.getElementById('address_gift_'+addressid+'_'+arr[0]).style.display = 'none';
			        		cartaddress(addprod,addressid,arr[0],0);
			        	}
		        	 		
		        }
		        
		     }			        
	    ,'json');
	
	
	
	for(var i=1;i<=count;i++){	
		
		
		if(i==arr[3])
			document.getElementById('prods_price_'+i+'_'+addressid).style.display = 'block';		
		else
			document.getElementById('prods_price_'+i+'_'+addressid).style.display = 'none';
	}	
	//document.getElementById('prods_price_'+addressid).style.display = 'none';
	
	
}

function cartaddress(addprod,addressid,prodid,flag){
	
for (var p = 0; p < addprod.length; p++) {
		
		if(addprod[p] == addressid+'_'+prodid && flag ==1)
		{
				document.getElementById('address_gift_'+addprod[p]).style.display = 'block';
		}
		else{
			 document.getElementById('address_gift_'+addprod[p]).style.display = 'none';
		}
	}
}
function removecart(product_id,base_path) {
	jQuery("#shoppingbag").html("<div align='center'><img src='"+base_path+"frontend/themes/default/images/ajax-loader.gif'></div>");
    post_file = base_path+"product/addcart/id/"+product_id+"/remove/1";
    jQuery.post(post_file, { },
        function(data)
            {
    			jQuery("#shoppingbag").html(data);
    			document.getElementById('errormsg').style.display= "none";
            }
        );
    
    
}

function addtocartsimple(product_id,base_path,theme,simplecount,quantity) {	
	
	
			var values = document.getElementById('simpleqty['+simplecount+']').value;
			if(values!=0)
			{
				document.getElementById('error_info').style.display= "none";
				document.getElementById('number_info').style.display= "block";
				
				var i;
				for (i = 0; i < values.length; i++)
				 {
					var c = values.charAt(i);
					 if (((c < "0") || (c > "9"))){
						 jQuery("#number_info").html("Please Enter Numbers Only");
						 return false;
					 }
					 else{
						 document.getElementById('number_info').style.display= "none";
					 }				 
				 }		
			}
			
			else {
				
					document.getElementById('error_info').style.display= "block";
					document.getElementById('number_info').style.display= "none";
					jQuery("#error_info").html("Please Enter Product Quantity");
				    post_file = base_path+"product/view/id/"+product_id;
				    jQuery("#simpleqty["+simplecount+"]").focus();
				    return false;
			     }				
		   
	
			count = values;
			
			var status;
		    post_file = base_path+"product/checkqtyavail/id/"+product_id+"/qty/"+quantity;
		    jQuery.post(post_file, { },
		        function(data)
		            {
			    	var opt = "";
			    	var pname = "";
			    	
			        if(data.length)
			        {
			        	
			        	opt = data[0].name;			        	
			        		
					    if(opt!=0)
					    {	
					    	if(quantity >= count)
					    	{
								jQuery("#shoppingbag").html("<div align='center'><img src='"+base_path+"frontend/themes/default/images/ajax-loader.gif'></div>");
							    post_file = base_path+"product/addcart/id/"+product_id+"/count/"+count;
							    jQuery.post(post_file, { },
							        function(data)
							            {
							    			jQuery("#shoppingbag").html(data);
							            }
							        );
					    	}else
					    	{
					    		jQuery("#errormsg").html("Products not available");
					    		document.getElementById('errormsg').style.display= "block";
					    	}
					    }
					    else
					    {
					    	pname = data[0].productname;
					    	jQuery("#errormsg").html("<div class='no_item_found'>Products "+pname+" not available</div>");
					    	document.getElementById('errormsg').style.display= "block";
					    	qtyFlag = 0;
					    }    	
			    			
			        }
		            }
		    ,'json');    	
		
}	

function addtocartwishlist(product_id,base_path,theme,simplecount,quantity) {
	
	jQuery("#errormsg").html('');
	
	//alert(quantity);
	
	var status;
    post_file = base_path+"product/checkqtyavail/id/"+product_id+"/qty/"+quantity;
    jQuery.post(post_file, { },
        function(data)
            {
    	var opt = "";
    	var pname = "";
    	
        if(data.length)
        {
        	
        	opt = data[0].name;       	
        		
		    if(opt!=0)
		    {	
			    	if(quantity >= simplecount)
			    	{    	
				
					    post_file = base_path+"product/view/id/"+product_id;
					    count = simplecount;
						jQuery("#shoppingbag").html("<div align='center'><img src='"+base_path+"frontend/themes/default/images/ajax-loader.gif'></div>");
					    post_file = base_path+"product/addcart/id/"+product_id+"/count/"+count;
					    jQuery.post(post_file, { },
				        function(data)
				            {
				    			jQuery("#shoppingbag").html(data);
				            }
				        );
				    
				    	}
			    	else
				    	{
				    		jQuery("#errormsg").html("<div class='no_item_found'>Products not available</div>");
				    		document.getElementById('errormsg').style.display= "block";
				    	}
			   
			        }
				    else
				    {
				    	pname = data[0].productname;
				    	jQuery("#errormsg").html("<div class='no_item_found'>Products "+pname+" not available</div>");
				    	document.getElementById('errormsg').style.display= "block";
				    	qtyFlag = 0;
				    }
        }
            }

	,'json');

}

function deletewishlist(product_id,base_path)
{
	var post_file = base_path+"product/deletewishlist/id/"+product_id;
	var item_element = '#del_success';
		
	 jQuery.post(post_file, { },
		        function(data)
		            {
		 			 var msg = "";	    	
					 if(data.length)
			         {
						 document.getElementById('del_success').style.display='block';
						 msg = "Wishlist Product Deleted Successfully";
						 
						
						 jQuery(item_element).attr("disabled",false);	                		
	                	 jQuery(item_element).html(msg);
	                	 window.location = base_path+"user/wishlist";
			         }
		 				
		            }		        
	 ,'json');
	 
	 
	 
	 
}

function addtowishlist(base_path,prodId)
{	
	var post_file;
    post_file = base_path+"user/chkuserlogin";    
    item_element = '#error_info';
    zero_note = 'Loading';	
    jQuery.post(post_file, { },
	        function(data)
	            {
	    			var opt = "";	    			
	                if(data.length)
	                {	                	
	                	opt = data[0].name;
	                	if(opt!=0)
	                	{
	                		window.open(base_path+"product/wishlist/id/"+prodId,'_self');
	                		
	                	}else
	                	{
	                		document.getElementById('error_info').style.display='block';
	                		opt = 'Please Login to add wishlist this product';
	                		jQuery(item_element).attr("disabled",false);	                		
	                		jQuery(item_element).html(opt);
	                	}
	                    
	                }
	                else
	                {	                	
	                	opt = zero_note;
	                }
	                
	            }
	        ,'json');		
   
}



//check review fields
function storeReview(post_file,wait_msg,noitem_msg){

	
	var name = document.getElementById('name').value;
	var summary = document.getElementById('summary').value; 
	var review = document.getElementById('review').value;
	var prodId = document.getElementById('productId').value;
	var item_element = '#review_message';

	
	document.getElementById('err_name').style.display = 'none';
	document.getElementById('err_summary').style.display = 'none';
	document.getElementById('err_review').style.display = 'none';
	if(name == '')
	{
		document.getElementById('err_name').innerHTML = "<ul class='valid_errors'><li>Value is required and can't be empty</li></ul>";
		document.getElementById('err_name').style.display = 'block';
		
		
	}

	if(summary == '')
	{
		document.getElementById('err_summary').innerHTML = "<ul class='valid_errors'><li>Value is required and can't be empty</li></ul>";
		document.getElementById('err_summary').style.display = 'block';
		
	}

	if(review == '')
	{
		document.getElementById('err_review').innerHTML = "<ul class='valid_errors'><li>Value is required and can't be empty</li></ul>";
		document.getElementById('err_review').style.display = 'block';
	}

	if(name == '' || summary == '' || review == '')
	{
		return false;
	}
	
	return true;
}

function checkProcudtAvailQty(base_path,product_id,quantity) {	
	
	//jQuery("#errormsg").html("<div align='center'><img src='"+base_path+"frontend/themes/default/images/ajax-loader.gif'></div>");
	var status;
    post_file = base_path+"product/checkqtyavail/id/"+product_id+"/qty/"+quantity;
    jQuery.post(post_file, { },
        function(data)
            {
	    	var opt = "";
	    	
	        if(data.length)
	        {
	        	opt = data[0].name;       	
	        		
			    if(opt)
			    {		    	
			    	status = 1;
			    }
			    else
			    {
			    	
			    	jQuery("#errormsg").html("<div class='no_item_found'>Products not available</div>");
			    	document.getElementById('errormsg').style.display= "block";
			    	status = 0;
			    }    	
	    			
	        }
            }
    ,'json');
    
       return status;
   
}

function show_giftmessage(val,product_id)
{
	
	var arr = explod(',', product_id);
	
	for (var i = 0; i < arr.length; i++) {
		if(arr[i] == val)
		{
			if(document.getElementById('gift_'+arr[i]).style.display == 'none'){
				document.getElementById('gift_'+arr[i]).style.display = 'block';
			}
			else{
				document.getElementById('gift_'+arr[i]).style.display = 'none';
			}
		}
		else{
			document.getElementById('gift_'+arr[i]).style.display = 'none';
		}
	}
	
}

function show_shippingaddress(val,product_id)
{
	
	var arr = explod(',', product_id);
	
	for (var i = 0; i < arr.length; i++) {
		if(arr[i] == val)
		{
			if(document.getElementById('shipping_'+arr[i]).style.display == 'none'){
				document.getElementById('shipping_'+arr[i]).style.display = 'block';
			}
			else{
				document.getElementById('shipping_'+arr[i]).style.display = 'none';
			}
		}
		else{
			document.getElementById('shipping_'+arr[i]).style.display = 'none';
		}
	}
	
}

function copy_giftmessage(val,product_id)
{

	var value = document.getElementById('gift_message_'+val).value;
	var gift_from = document.getElementById('gift_from_'+val).value;
//	if(value == '' || gift_from == ''){
//		alert('Field Is Empty');
//		return false
//	}
	var arr = explod(',', product_id);
	
	for (var i = 0; i < arr.length; i++) {
		if(arr[i] == val)
		{
			if(document.getElementById('gift_message_'+arr[i-1]).value == '' || document.getElementById('gift_from_'+arr[i-1]).value == ''){
				alert('Field Is Empty');
				return false
			}
			document.getElementById('gift_message_'+arr[i]).value = document.getElementById('gift_message_'+arr[i-1]).value;
			document.getElementById('gift_from_'+arr[i]).value = document.getElementById('gift_from_'+arr[i-1]).value;
		}
		else{
			
		}
	}
	
}



function change_button(){
	var address_id1 = explod(',', document.getElementById('address_id').value);
	
	for (var i = 0; i < address_id1.length; i++) {
		
	alert(document.getElementById(address_id1[i]).value);
	if(document.getElementById(address_id1[i]).value == '--select--'){	
		document.getElementById('error_ship').value='N';
		return true;		
	   }
	   if(document.getElementById(address_id1[i]).value == 'no'){
		var flag="Y";
		return flag;
	   }
	}
	
	
}

function button_checkout(base_path,product_id)
{	
	var address_id = explod(',', document.getElementById('address_id').value);
	var productid = explod(',', product_id);	
	var addresscheck ='';
	var shipaddress = 1 ;
	var shipping_check = false;
	var shipping_address_id = '';	
	var flag = 0;var initial_count = 0;
	
	for (var i = 0; i < address_id.length; i++) {
			
			
		shipping_address_id = document.getElementById(address_id[i]).value;
		if(shipping_address_id=="--select--"){
			alert('Please select Address');
			jQuery("#loading").html("");		
			return false;
		}//else if(shipping_address_id=="no"){ alert("HERE");
			//var flag=true;
			//return true;
		//}
		jQuery("#loading").html("<span style='font-weight:bold; color:red;'>Processing...</span>");
		post_file = base_path+"product/shippingaddval/id/"+shipping_address_id;
		jQuery.post(post_file, { },
		        function(data){
			    	var opt = "";
			    	shipping_check = true;
			        if(data.length){
			        	opt = data[0].name;
			        	if(opt==0){
			        		shipaddress = 0;
			        	}else if(opt=="chk"){
			        		shipaddress1 = 1;
			        	}
			        	if(initial_count==(address_id.length-1)){
			        		if(!shipaddress){		
			        			//jQuery("#loading").html("<div align='center'><h1>Loading...</h1></div>");
			        			alert("Please Check your Shipping Address and Update Cart");
			        			jQuery("#loading").html("");			        			
					        	return false;
			        		}else{
			        			
			        			//jQuery("#loading").html("<div align='center'><h1>Loading...</h1></div>");
			        			document.getElementById('checkoutInfo').value =1;
			        			document.checkoutform.submit();
			        			jQuery("#loading").html("");		
			        			return true;
			        		}
			        	}     		
			        }
			        initial_count++;			       
			     }			        
		    ,'json');
		
			/*if(document.getElementById(address_id[i]).value == 'no'){
						
						document.getElementById('checkoutInfo').value =1;
						document.checkoutform.submit();
						return true;
					}*/
	}
	/*for (var i = 0; i < address_id.length; i++) {
		shipping_address_id1 = document.getElementById(address_id[i]).value;
		if(shipping_address_id1 == 'no'){
			
			document.getElementById('checkoutInfo').value =1;
			document.checkoutform.submit();
			return true;
		}
	}*/

	
	
}

function update_shopping()
{
	var address_id = explod(',', document.getElementById('address_id').value);
	
	for (var i = 0; i < address_id.length; i++) {
		if(document.getElementById(address_id[i]).value == '--select--')
		{
			alert('Please select Address');
			return false
		}
	
	}
	return true
}

function explod (delimiter, string, limit) {
	 
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2 ||
        typeof arguments[0] == 'undefined' ||
        typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === '' ||
        delimiter === false ||
        delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function' ||
        typeof delimiter == 'object' ||
        typeof string == 'function' ||
        typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}


var alert_title='Input Restriction';

function limitTextarea(textarea,maxLines,maxChar){
	var lines=textarea.value.replace(/\r/g,'').split('\n'),lines_removed,char_removed,i;
	if(maxLines&&lines.length>maxLines){
		lines=lines.slice(0,maxLines);
		lines_removed=1
	}
	if(maxChar){
		//alert('You can not enter more\nthan '+maxChar+' characters per line')
		i=lines.length;
		while(i-->0)if(lines[i].length>maxChar){
			lines[i]=lines[i].slice(0,maxChar);
			char_removed=1
		}
		if(char_removed){ 
			//alert('You can not enter more\nthan '+maxChar+' characters per line')
		}
	}
	if(char_removed||lines_removed)textarea.value=lines.join('\n')
}

function watchTextarea(textareaid){
	document.getElementById(textareaid).onkeyup()
}

function set_ie_alert(){
window.alert=function(msg_str){
//vb_alert(msg_str)
}
}