// JavaScript Document
//minimum qty of custom cookie per design
var minimumqty, cookiedesign, cookiemaking, curproductid;

$(document).ready(function(){
	
	$(document).scroll(function(){
	var y=$(document).scrollTop();
		
	/*	  
	  if($('#customorderform').length>0){
		if(y>180){
		  var dy=y-180;
		 // $('#kappy').css('margin-top', dy+'px');	
		}
	  }
  */
   
	});	
 
   if($('#newsletterform').length>0){
	$('#newsletterform').submit(function(){
	 if(!validEmail($('#newsletterform input[name="email_address"]'))){
		alert('Please provide your email address');
		$('#newsletterform input[name="email_address"]').focus();
		return false; 
	 }
	 
	 $('#btnsignup').attr('disabled', true);
	 $('#loader').css({display:'block', left:'280px', top:'-35px'});
     return ajaxpost($('#newsletterform'),NewsLetter);
	}); 
	 
	//============================
	/* $('#btngo').click(function(){
		return false;  
	 });
	 */
	   
   }
 

//=========================================================
if($('.qmark').length>0){
	var thetitle;
	
	$('.qmark').mouseover(function (){
	  thetitle=$(this).attr('title');
	  $(this).attr('title','');
	  
	  $('#tooltip').text(thetitle);
	  var pos = $(this).position();
	  var x=pos.left;
	  var y=pos.top+20;
	  $('#tooltip').css({left: x+'px', top:y+'px'});
	  $('#tooltip').fadeIn('slow');		
	});
	
	$('.qmark').mouseout(function(){
	$(this).attr('title',thetitle);
	 $('#tooltip').fadeOut('slow');
	});
}





/*******************************************************************************************    
************************************* CUSTOM ORDER FORM ************************************
*******************************************************************************************/
 if($('#customorderform').length>0){
	 
	$('#customorderform').submit(function(){
	 return validateCustomOrder();
	});
	
   //safeguard in case option selected before jquery download is completed.
   if($('input[name="cookiemaking"]').attr('checked')!='undefined' && 
      $('input[name="cookiemaking"]').attr('checked')=='checked'){
	   $('#qtybox').slideDown(1000);
	}

	$('input[name="cookiemaking"]').change(function(){
	   $('#qtybox').slideDown(1000);
       if(setCookieMaking()){loadSelect(); }
	   
	    $('#packaging').slideUp(1000);
	});
	
	$('#btncancel').click(function(){
	   $('#qtybox').slideUp(1000);
	   $('#packaging').slideUp(1000);
	});
	

	$('select[name="designqty"]').change(function(){
	 if($('select[name="designqty"]').val()!=''){
	     $('#packaging').slideDown(1000); 
	     setMinimumQty(); 
		 setCustomQty();  
	 }

	});
	
	//==========================================
	$('input[name="cookie"]').change(function(){
		
	if($('input[name="cookie"]:checked').val()=='custom'){
		$('#custombox').slideDown(1000);
		$('#bulkbox').slideUp(1000);
	}
	else{ 
		$('#custombox').slideUp(1000); 
		$('#bulkbox').slideDown(1000);
	}
	
	$('#footerbox').slideDown(1000);

	  setMinimumQty();
	  setCustomQty();	
	  validCustomQty();
	});

	//==========================================
	//defines the total quantity of cookie
	$('select[name="bagtype"]').change(function(){
		setMinimumQty();
		setCustomQty();
	});
	
	//shows the quantity of cookie
	$('input[name="customqty"]').focusout(function(){
		setMinimumQty();
	    validCustomQty();
	});

	$('input[name="bulkqty"]').focusout(function(){
	   setMinimumQty();
	   validBulkQty();
	});

	   
	 $('#chooseribbon').click(function(){
		$('#labelpopup').fadeOut();
		$('#fontpopup').fadeOut();
		$('#ribbonpopup').fadeIn(1000);
	 });

	 $('#chooselabel').click(function(){
		 $('#ribbonpopup').fadeOut();
		 $('#fontpopup').fadeOut();
		 $('#labelpopup').fadeIn(1000);
	 });	    
	
	 $('#choosefont').click(function(){
		 $('#labelpopup').fadeOut();
		 $('#ribbonpopup').fadeOut();
		 $('#fontpopup').fadeIn(1000);
	 });	
	
	$('.btnmakeselection').click(function(){
	    if($(this).attr('id')=='selectribbon'){ selectCustomRibbon(); }
		else if($(this).attr('id')=='selectlabel'){ selectCustomLabel(); }
		else if($(this).attr('id')=='selectfont'){ selectCustomFont(); }
		$(this).parent().first().css('display', 'none');
	});
	
	$('input[name="ribbon"]').change(function(){ selectCustomRibbon(); });
	$('input[name="label"]').click(function(){ selectCustomLabel();});
	$('input[name="font"]').change(function(){ selectCustonFont(); });
	

	$('input[name="ribbon"]').change(function(){
		var tg=$('input[name="tribbon"]').val();
		var txt=$('input[name="ribbon"]:checked').val();
		$('#'+tg).text(txt);
		$('input[name="'+tg+'"]').val(txt);	
	});
	
	$('input[name="font"]').change(function(){
		var tg=$('input[name="tfont"]').val();
		var txt=$('input[name="font"]:checked').val();	
		$('#'+tg).text(txt);
		$('input[name="'+tg+'"]').val(txt);		
	});
	
	$('input[name="label"]').click(function(){
	    var tg=$('input[name="tlabel"]').val();
		var txt=$('input[name="label"]:checked').val();	
		$('#'+tg).text(txt);
		$('input[name="'+tg+'"]').val(txt);		
	});
	
	
     $('#ribbonpopup').mouseleave(function(){
		$(this).css('display', 'none'); 
		 
	 });
	 
     $('#labelpopup').mouseleave(function(){
		$(this).css('display', 'none');   
	 });
	 
	 $('#fontpopup').mouseleave(function(){
		$(this).css('display', 'none');  
	 });
	
	
	
	
};//end if customorderform



/* ************************************************************************************
*************************** COLLECTION COOKIE POP-UP **********************************
***************************************************************************************/
	if($('#cartpopup').length>0){
	var product;
	
		 $('.addtocart').click(function(){
		  var position = $(this).position();
		  var parts=$(this).attr('id').split('_');
		  curproductid=parts[1];
		  product=eval('pd_'+curproductid);
		  
		  //update the bin price in the pop-up window
		  $('#bin_price').html(product.binprice);
		  $('#bag_price').html(product.bagprice);
		  
		  if(product.bagavailable=='N'){
		   $('#add_bag').css('display', 'none'); 
		   $('#spbag').css('display', 'none'); 
		  }
		  else{
		  $('#add_bag').css('display', 'inline'); 
		  $('#spbag').css('display', 'inline'); 
		  }
		  
		  if(product.binavailable=='N'){
		   $('#add_bin').css('display', 'none'); 
		   $('#spbin').css('display', 'none');   
		  }
		  else{
		  $('#add_bin').css('display', 'inline'); 
		  $('#spbin').css('display', 'inline');   
		  }
		  
		  
		  
		  $('input[name="productid"]').val(curproductid);
		   showProductPhotos();
		   
		   y=$(document).scrollTop();	
		   $('#cartpopup').css('top', y+'px'); 
	
		/* if(position.left<450){ $('#cartpopup').css('left','420px'); }
		  else{ $('#cartpopup').css('left','0px'); }
		  */
		  
		  $('#cartpopup').css('left','210px');
		  
		  //$('#cartpopup').css('display','block');
		  $('#cartpopup').fadeIn('slow');  
		 });
	
		 $('#add_set').bind('click mouseover',function(){
		   $('input[name="package"]').val('bag');
		   showSetPhoto();  
		   return false;
		 });
		 
		 $('#add_bag').bind('click mouseover',function(){
			$('input[name="package"]').val('bag');
			

			if(product.bagavailable=='Y'){
				$('#btnaddtocart').attr('disabled', false);
				}
			else{
				$('input[name="package"]').val('');
				$('#btnaddtocart').attr('disabled', true);
				
			 }
			 
		   showBagPhoto();   
			 
		   return false;
		 });	
		  
		 $('#add_bin').bind('click mouseover',function(){
		   $('input[name="package"]').val('bin');
		   
            if(product.binavailable=='Y'){ 
			  $('#btnaddtocart').attr('disabled', false);
			}
			else{ 
			  $('input[name="package"]').val('');
			  $('#btnaddtocart').attr('disabled', true);
			 }
		   showBinPhoto();
		     
		   return false;
		 });
		 
		 
		$('#btnaddtocart').click(function(){
		 $('#addtocartform').submit();
		 //return true;
		});
		
		
		$('#btnclosecart').click(function(){
		  $('#cartpopup').fadeOut('slow');
		});
		
		
		$('#cartpopup').mouseleave(function(){
		   $(this).fadeOut('slow');
		});
		
	}//end if cartpopup

/*******************************************************************************************    
************************************* SHOPPING CART FORM ***********************************
*******************************************************************************************/

	if($('#cartform').length>0){
     
	 $('#btncheckout').click(function(){
	  $('#cartform').attr('action', '/cart/checkout.html');
	  if(validateCart()){$('#cartform').submit(); }
	 });
	
	 $('#labelpopup').css( {'top':'0px', 'right':'0px', 'z-index':'5000'});
	 $('#fontpopup').css(  {'top':'0px', 'right':'0px', 'z-index':'5000'});
	 $('#ribbonpopup').css({'top':'0px', 'right':'0px', 'z-index':'5000'});
	 
	  $('.chooseribbon').click(function(){
		  var position = $(this).position();
		  var dy=(position.top-$('#ribbonpopup').height())+10;
		  y=(dy>0)?dy:0;
		   $('#ribbonpopup').css('top',y+'px');
		  //y=$(document).scrollTop();
		  
			$('#labelpopup').fadeOut();
			$('#fontpopup').fadeOut();
			$('#ribbonpopup').fadeIn(1000);
			
			fieldname=$(this).attr("id");
			//$('input[name="ribbon"]').val($(this).attr("id"));
			var txt=$(this).text();
			
			$('input[name="ribbon"]').each(function(){
			  if(txt==$(this).attr("value")){
				$(this).attr('checked', 'checked');
			  }
			});
	  });
	  
	  
	//===================================  
	  $('.chooselabel').click(function(){
		  var position = $(this).position();
		  var position = $(this).position();
		  var dy=(position.top-$('#labelpopup').height())+10;
		  y=(dy>0)?dy:0;
		   $('#labelpopup').css('top',y+'px');
		  
			$('#ribbonpopup').fadeOut();
			$('#fontpopup').fadeOut();
			$('#labelpopup').fadeIn(1000);
			
			var txt=$(this).text();
			if(txt=='choose one'){txt=''; }
			fieldname=$(this).attr("id");
			
			$('input[name="label"]').each(function(){
			  if(txt==$(this).attr("value")){
				$(this).attr('checked', 'checked');
			  }
			});
	  });  
	  
	//===================================
	  $('.choosefont').click(function(){
		var position = $(this).position();
		var dy=(position.top-$('#fontpopup').height())+10;
		y=(dy>0)?dy:0;
		$('#fontpopup').css('top',y+'px');
		
		$('#labelpopup').fadeOut();
		$('#ribbonpopup').fadeOut();
		$('#fontpopup').fadeIn(1000);

		fieldname=$(this).attr("id");
		
		var txt=$(this).text();
		if(txt=='choose one'){txt=''; }
		$('input[name="font"]').each(function(){
		if(txt==$(this).attr("value")){$(this).attr('checked', 'checked'); }
		}); 
	 });
	 
	 
	//===================================  	  
	$('input[name="ribbon"]').change(function(){
	var txtval=$('input[name="ribbon"]:checked').val();
	 if(txtval!=''){$('#'+fieldname).text(txtval); }
	 else {$('#'+fieldname).text('no ribbon'); }
	 $('input[name="'+fieldname+'"]').val(txtval);
    });


	//===================================  	  
	$('input[name="label"]').change(function(){
	var txtval=$('input[name="label"]:checked').val();
	 if(txtval!=''){$('#'+fieldname).text(txtval); }
	 else {
	  $('#'+fieldname).text('no label'); 
	 /* need to update font value to no font */
	 }
	 $('input[name="'+fieldname+'"]').val(txtval);
    });
	   
	//===================================  	  
	$('input[name="font"]').change(function(){
	var txtval=$('input[name="font"]:checked').val();
	 if(txtval!=''){
	  if($('input[name="label"]:checked').val()==''){
		alert('Please select a label first then a font');  
	  }
	  else { $('#'+fieldname).text(txtval); }
	 }
	 else {$('#'+fieldname).text('no font'); }
	 $('input[name="'+fieldname+'"]').val(txtval);
    });
	
	 
	
	//===================================		
	  $('.btnremove').click(function(){
		 var parts=$(this).attr('id').split('_'); 
		 $('#deletecartform input[name="itemid"]').val(parts[1]);
		 if(confirm('The selected item will be permanently removed from your shopping cart. Would you like to proceed anyway?')){
			$('#deletecartform').submit();
		 }
		     
	  });
	  	
	$('.btnmakeselection').click(function(){
		var target=$(this).parent().find('input[type="radio"]:checked');
	   if(target.val()!=null && target.val()!='undefined'){
	   $(this).parent().first().css('display', 'none');
	   }
	   else{
		alert('Please make a selection'); 
	   }
	});
	
	//===================================					
	$('#cartform').submit(function(){
	  return validateCart();
	});
	  
    $('.showlabel').click(function(){
	  var k=$(this).attr('id').replace('show_', '');
	  
	  if($(this).text()=='Show Custom Label Info'){
	  $('#customlabel_'+k).slideDown('slow');
	  $(this).text('Hide Custom Label Info');	
	  }
	  else{
		$(this).text('Show Custom Label Info');  
		$('#customlabel_'+k).slideUp('slow');
	  }
	});


     $('#ribbonpopup').mouseleave(function(){
		$(this).css('display', 'none'); 
		var txtval=$('input[name="ribbon"]:checked').val();
		if(txtval!=''){$('#'+fieldname).text(txtval); }
	    else {$('#'+fieldname).text('no ribbon'); }
		
		$('input[name="'+fieldname+'"]').val(txtval);
	 });
	 
     $('#labelpopup').mouseleave(function(){
		$(this).css('display', 'none');  
		var txtval=$('input[name="label"]:checked').val();
		
		if(txtval!=''){$('#'+fieldname).text(txtval); }
	    else {$('#'+fieldname).text('no label'); }
		
		$('input[name="'+fieldname+'"]').val(txtval); 
	 });
	 
	 $('#fontpopup').mouseleave(function(){
		$(this).css('display', 'none');  
		var txtval=$('input[name="font"]:checked').val();
		
		if(txtval!=''){
			if($('input[name="label"]:checked').val()==''){
			 alert('Please select a label first then a font');
			 $('#'+fieldname).text('no font');
			}
			else { $('#'+fieldname).text(txtval); }
		}
	    else {$('#'+fieldname).text('no font'); }

		$('input[name="'+fieldname+'"]').val(txtval);
	 });

	  
	}//end if cartform
	
	
	if($('#errmsge').html()!=""){
	  $('#errmsge').fadeIn('fast').delay(6000).fadeOut(2000);	
	}
	  
	$('#errmsge').ajaxError(function() {	 
	$(this).html('Ajax error').fadeIn(500).delay(5000).fadeOut(1000);
	});
	
	//================================
	$('#loader').ajaxStart(function(){
	  $(this).html('loading...');
	  $(this).css({display:'block'});
	 loadtimer=setTimeout(hideLoader,15000);
	});
	
	//================================
	$('#loader').ajaxComplete(function() {
	  $(this).css({display:'none'});	
	  clearTimeout(loadtimer);
	});
	
	//================================
	if($('#shippingform').length>0){
     $('#shippingform').submit(function(){
		return validateShipping(); 
	 });
	}
	
	

});//end document ready



/**************************************************************************************************
**************************************************************************************************/
function hideLoader(){
$('#loader').css({'display':'none'});	
clearTimeout(loadtimer); 	
}



var Imgs=new Array();

//===========================
function showProductPhotos(){
var product=eval('pd_'+curproductid);

  if(product!='undefined' && product!=null){
  $('#itemname').html(product.title);	
  $('#itemdescription').html(product.description);
  
  Imgs[0]=Imgs[1]=Imgs[2]=new Image();
  Imgs[0].src='/pics/'+product.set;
  Imgs[1].src='/pics/'+product.bin;
  Imgs[2].src='/pics/'+product.bag;	
  
  showSetPhoto();
  }
}

//===========================
function showSetPhoto(){
var product=eval('pd_'+curproductid);
$('#itemphoto').attr('src', '/pics/'+product.set);	
}

//===========================
function showBagPhoto(){
var product=eval('pd_'+curproductid);
$('#itemphoto').attr('src', '/pics/'+product.bag);	
}

//===========================
function showBinPhoto(){
var product=eval('pd_'+curproductid);
$('#itemphoto').attr('src', '/pics/'+product.bin);	
}








//=================================
function validateCustomOrder(){
var cookiemaking=$('input[name="cookiemaking"]:checked').val();
var designqty=$('select[name="designqty"] option:selected').val();
var customqty=$('input[name="customqty"]').val();
var bulkqty=$('input[name="bulkqty"]').val();
var thecookie=$('input[name="cookie"]:checked').val();
var font=$('input[name="font"]:checked').val();
var label=$('input[name="label"]:checked').val();
var ribbon=$('input[name="ribbon"]:checked').val();
var message1=$('input[name="message1"]').val().replace(/^\s+|\s+$/g, '');
var message2=$('input[name="message2"]').val().replace(/^\s+|\s+$/g, '');
var deliverydate=$('input[name="deliverydate"]').val().replace(/^\s+|\s+$/g, '');


if(!validGroupItem('cookiemaking')){
 alert('What will you be making into a cookie?');
 $('input[name="cookiemaking"]').focus();
 return false;	
}
else if((designqty=='') || isNaN(designqty)){
	alert('How many unique cookie designs would you like?');
	$('select[name="designqty"]').focus();
	return false;
}
else if(!setCookieMaking()){
return false;	
}
else if(!validGroupItem('cookie')){
alert('Please select Custom or Bulk Cookies');
$('input[name="cookie"]').focus();
return false;	
}
else if(!setCustomQty()){
 alert('Please enter the minimum cookie quantity.');
 if(thecookie=='custom'){$('input[name="customqty"]').focus(); }
 else{$('input[name="bulkqty"]').focus();   }
 
 return false;	
}
else if(thecookie=='custom'){
  if(customqty=='' || isNaN(customqty)){
	alert('Please enter the gift bag quantity'); 
	$('input[name="customqty"]').focus();
	return false; 
  }
  else if(!validGroupItem('ribbon')){
	alert('Please choose the ribbon color');  
	$('#chooseribbon').focus(); 
	return false;
  }
  else if(label!='' && label!=null && label!='undefined'){
	  if(font==''){
		alert('Please choose the font type');  
		$('#choosefont').focus();
		return false;
	  }
	  else if(font!='' && !validTextField($('input[name="message1"]'))){
		alert('Please specify the message');
		$('input[name="message1"]').val('');
		$('input[name="message2"]').val(message2);
		$('input[name="message1"]').focus();
		return false;  
	  }
   } 

}
else if(thecookie=='bulk'){
   if(bulkqty=='' || isNaN(bulkqty)){
	alert('Please enter the cookie quantity'); 
	$('input[name="bulkqty"]').focus();
	return false; 
   }
   else if(!validBulkQty()){
	alert('Please enter at least the minimum cookie quantity');
	$('input[name="bulkqty"]').focus();
	 return false; 
   }
}
else if(!validTextField($('input[name="deliverydate"]'))){
 alert('Please specify the desired delivery date');	
 $('input[name="deliverydate"]').focus();
 return false;
}	


return true;	
}


//===============================
function validGroupItem(fld){
var itemval=$('input[name="'+fld+'"]:checked').val();
var found=false;

$('input[name="'+fld+'"]').each(function(i){
	if(itemval==$(this).attr('value')){found=true; }
});

return found;
}



//===================================================================================================
//===================================================================================================
//Custom Order Form
function setCookieMaking(){
cookiemaking=$('input[name="cookiemaking"]:checked').val();
$('#mincustomqty').html('');
$('#minbulkqty').html('');
return true;
}


//===============================
function loadSelect(){
var k=0;
var designStr='<option value="" selected>Select</option>';
var bagStr='';
var thecookiemaking=eval(cookiemaking);
var n=cookiemaking.length-1;
//display the bulk price label
$('#bulk_price').html(thecookiemaking['bulk'].price);

for (var x in thecookiemaking){
	
  if(thecookiemaking[x].packaging!='bulk'){	  
    designStr+='<option value="'+thecookiemaking[x].qty+'">'+thecookiemaking[x].qty+'</option>'; 
    bagStr+='<option value="'+thecookiemaking[x].id+'">'+thecookiemaking[x].product+'</option>';
  }	
}

$('#designqty').html(designStr);
$('#bagtype').html(bagStr);

//select the last option in the drop-down list
$('#bagtype').children().filter(':first').attr('selected','selected');


return true;
}


//===============================
function setMinimumQty(){
var productid=$('select[name="bagtype"] option:selected').val();

if(productid!='undefined' && productid!=null && productid!=''){	
var pdname='pd_'+productid;
var thecookiemaking=eval(cookiemaking);
var product=thecookiemaking[pdname];
  minimumqty=product.minqty;
  $('input[name="productid"]').val(productid);
 }
 else{
	alert('What will we be making into a cookie?'); 
	$('#packaging').css('display','none');
	$('input[name="cookiemaking"]').focus();
	return false;
 }
  
return true;	
}


//===============================
function setCustomQty(){
var designqty=$('select[name="designqty"] option:selected').val();
var customqty=$('input[name="customqty"]').val();
var bulkqty=$('input[name="bulkqty"]').val();
var minvalue=minimumqty*parseInt(designqty);

if(isNaN(designqty)){
  alert('Please select the number of unique cookie designs');	
  $('select[name="designqty"]').focus();
  return false;
}


if(isNaN(minimumqty)){
  alert('What will we be making into a cookie?');		
  $('input[name="cookiemaking"]').focus();
  return false;
}


if($('input[name="cookie"]:checked').val()=='custom'){
  return validCustomQty();	
}
else if($('input[name="cookie"]:checked').val()=='bulk'){
  return validBulkQty();
}
else{ 

  if(parseInt(customqty)<minvalue){ 
	    $('#mincustomqty').text('The minimum quantity is '+ minvalue);
		$('#mincustomqty').fadeIn(500);
	}
	
	if((cookiedesign=='mpeople')|| (cookiedesign=='both')){
	  var n=150*parseInt(designqty);
	  if(parseInt(bulkqty)<n){ 
	    $('#minbulkqty').text('The minimum quantity is '+ n);
		$('#minbulkqty').fadeIn(500);
	  }
	}
	else{
	  var n=150*parseInt(designqty);
	  if(parseInt(bulkqty)<n){ 
	    $('#minbulkqty').text('The minimum quantity is '+ n);
		$('#minbulkqty').fadeIn(500);
	  }
	}
}

	
  return false;
}


//===========================
function validCustomQty(){
var productid=$('select[name="bagtype"] option:selected').val();
$('input[name="productid"]').val(productid);

var pdname='pd_'+productid;
var thecookiemaking=eval(cookiemaking);
var product=thecookiemaking[pdname];
minimumqty=product.minqty;	

var designqty=$('select[name="designqty"] option:selected').val();
var customqty=$('input[name="customqty"]').val();
var minvalue=minimumqty*parseInt(designqty);

if($('input[name="cookie"]:checked').val()=='custom'){
	
   if(customqty=='' || isNaN(customqty) || (minvalue>parseInt(customqty))){
	  $('#mincustomqty').text('The minimum quantity is '+ minvalue);
	  $('#mincustomqty').fadeIn(500); 
	  return false;
	}
   else{
	$('input[name="customqty"]').val(parseInt(customqty));
    return true; 
   }
}
 	
 return false; 
}

//===========================
function validBulkQty(){
var designqty=$('select[name="designqty"] option:selected').val();
var thecookiemaking=eval(cookiemaking);
var product=thecookiemaking['bulk'];
$('input[name="productid"]').val(product.id);
var minimumqty=product.minqty;

var bulkqty=$('input[name="bulkqty"]').val();
var minvalue=minimumqty*parseInt(designqty);

if($('input[name="cookie"]:checked').val()=='bulk'){
	$('#minbulkqty').text('The minimum quantity is '+ minvalue);
	$('#minbulkqty').fadeIn(500);	
	
  if(isNaN(bulkqty) || parseInt(bulkqty)<minvalue){
	$('input[name="bulkqty"]').focus();  
  }
 else { return true; }
}

return false;
}


//===========================
function validateCart(){
var n=parseInt($('input[name="nitems"]').val());

for(i=0; i<n; i++){
  minqty=parseInt($('input[name="minqty_'+i+'"]').val());	
  qty=$('input[name="qty_'+i+'"]').val();
  
  if(isNaN(qty) || minqty>parseInt(qty)){
  alert('The minimum quantity is '+minqty);
  $('input[name="qty_'+i+'"]').focus();
  return false;
  }	
  else{
	$('input[name="qty_'+i+'"]').val(Math.abs(parseInt(qty))); 
  }
}


return true;	
}



//===============================
function validateShipping(){
 if($('select[name="shipmethod"] option:selected').val()==''){
  alert('Please select the shipping option');
  select[name="shipmethod"].focus();
  return false;	 
 }
 else if($('input[name="shipdate"]').val()==''){
	alert('Please select th shipping date');
  return false;
 }
 else if($('input[name="deliverydate"]').val()==''){
	alert('Please select the delivery date');
  return false;
 }
	
  return true;
}


//===============================
function ajaxpost(frm,func){
  var thedate=new Date();
  $.post(
  frm.attr('action')+'?'+thedate.getTime(), 
  frm.serialize(), 
  function (result, txtstatus, xhr){ func(result,txtstatus, xhr);},'json');	
  return false; 
}


//===============================
function NewsLetter(data,txtstatus, xhr){
if(data.error==''){ $('#newsletterform').html(data.msge); }
else{ alert(data.error); }	
$('#btnsignup').attr('disabled', false); 
}


//===========================
function validTextField(fld){
  if(fld.val().replace(/^\s+|\s+$/g, '')!=''){
	return true;  
  }
  
return false;	
}

//===============================
function validEmail(fld){
   if(!validTextField(fld)){ return false; }
	 var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	 if(!reg.test(fld.val())) {return false; }
  return true;
}


//===============================
function selectCustomRibbon(){
var txt=$('input[name="ribbon"]:checked').val();
if($('input[name="ribbon"]:checked').length>0){ 
$('#chooseribbon').text(txt); 
}
else{ 
$('#chooseribbon').text('no ribbon'); 
$('input[name="label"][val=""]').attr('checked','checked');
selectCustomLabel();	
}

}


//===============================
function selectCustomLabel(){
var txt=$('input[name="label"]:checked').val();
if(txt!=''){ 
  if($('input[name="ribbon"]:checked').length==0){
	alert('Please select a ribbon in order to have a label');
  }
  else {$('#chooselabel').text(txt); }
}
else {
  $('#chooselabel').html('no label');
  $('input[name="font"][val=""]').attr('checked','checked');
  selectCustomFont();
}
}


//===============================
function selectCustomFont(){
var txt=$('input[name="font"]:checked').val();
if(txt!=''){ 
  if($('input[name="label"]:checked').val()==''){
   alert('Please select a label before choosing a font');   
  }
  else{ $('#choosefont').text(txt); }
}
else {	
  $('#choosefont').html('no font');
  $('input[name="message1"]').val('');
  $('input[name="message2"]').val('');
 }	
}
