function message(){
	$( "#message" ).dialog({
		show: {effect: 'fade', duration: 1000},
		hide: {effect: 'fade', duration: 1000},
		height: 200,
		autoOpen: true,
		modal: true,
		buttons: {
			Ok: function() {
				$( this ).dialog( "close" );
			}
		}
	});
}

function changeQuantity(sku){
	//this works only on sample_bag for now
	var qty=$("#"+sku).val();
	//alert(sku+"-"+qty);
	$.ajax({
	  url: '/ajax_bag_change_quantity.php?order_type=samples&sku='+sku+'&amount='+qty,
	  success: function(data) {
    	refreshFloatChart();
    	$("#message").html(data);
    	message();
	  }
	});
	
}

function addToSampleBag(ssku,amount){
	amount = typeof(amount) != 'undefined' ? amount : 1;
	//alert(ssku);
	$.ajax({
  url: '/ajax_add_to_bag.php?sku='+ssku+'&order_type=samples&amount='+amount,
  success: function(data) {
    //$('.result').html(data);
    //alert(data);
    refreshFloatChart();
    $("#message").html(data);
    message();
  }
});

}
function refreshBag(btype){
	if (btype=='samples'){
		$.ajax({
		  url: '/ajax_load_account_bag.php?order_type=samples',
		  success: function(data) {
		    //$('.abag').html(data);
		    //$('#abag').html(data);
		    document.getElementById('abag').innerHTML = data;//stupid ie bug
		    //alert('Load was performed.');
		  }
		});
	}
	if (btype=='products'){
		$.ajax({
		  url: '/ajax_load_account_bag.php?order_type=products',
		  success: function(data) {
		    //$('.abag').html(data);
		    
		    //$('#abag').html(data);
		    document.getElementById('abag').innerHTML = data;//stupid ie bug
		    //alert('Load was performed.');
		  }
		});
	}
}
function addToProductBag(sku,order_method,amount){
	amount = typeof(amount) != 'undefined' ? amount : 1;
	$.ajax({
  url: '/ajax_add_to_bag.php?sku='+sku+'&order_type=products&method='+order_method+'&amount='+amount,
  success: function(data) {
    //$('.result').html(data);
    //alert(data);
    refreshFloatChart();
    $("#message").html(data);
    message();
  }
});

}
function removeFromSampleBag(sku,order_method){
	$.ajax({
  url: '/ajax_remove_from_bag.php?sku='+sku+'&order_type=samples&method='+order_method,
  success: function(data) {
    //$('.result').html(data);
    //alert(data);
    refreshFloatChart();
    $("#message").html(data);
		refreshBag('samples');
    message();
  }
  });
}

function removeFromProductBag(sku,order_method){
	$.ajax({
  url: '/ajax_remove_from_bag.php?sku='+sku+'&order_type=products&method='+order_method,
  success: function(data) {
    //$('.result').html(data);
    //alert(data);
    refreshFloatChart();
    $("#message").html(data);
    refreshBag('products');
    message();
  }
  });
}
function clearSampleBag(sku,order_method){
	$.ajax({
  url: '/ajax_remove_from_bag.php?all=yes&order_type=samples&method='+order_method,
  success: function(data) {
    //$('.result').html(data);
    //alert(data);
    refreshFloatChart();
    $("#message").html(data);
		refreshBag('samples');
    message();
  }
  });
}
function clearProductBag(sku,order_method){
	$.ajax({
  url: '/ajax_remove_from_bag.php?all=yes&order_type=products&method='+order_method,
  success: function(data) {
    //$('.result').html(data);
    //alert(data);
    refreshFloatChart();
    $("#message").html(data);
		refreshBag('samples');
    message();
  }
  });
}
function selAllProducts(){
	var frm = document.form_add_multiple;
	var cboxes = document.form_add_multiple.add_single_product;
	for(var i=0;i<cboxes.length;i++){
		if(cboxes[i].checked){
			cboxes[i].checked = false;
		}else if(!cboxes[i].checked){
			cboxes[i].checked = true;
		}
	}
}
function selAllSamples(which_one){
	var frm = document.form_add_multiple;
	var cboxes = document.form_add_multiple.add_single_sample;
	//next two conditions are to sync both dropdowns
	if (which_one.id == 'check_all') {
		document.getElementById('check_all2').selectedIndex = document.getElementById('check_all').selectedIndex;
	}
	if (which_one.id == 'check_all2') {
		document.getElementById('check_all').selectedIndex = document.getElementById('check_all2').selectedIndex;
	}
	
	var selectbox=document.getElementById('check_all').value;
	var selectbox2=document.getElementById('check_all2').value;
	//alert(selectbox);
	
	for(var i=0;i<cboxes.length;i++){
		if(selectbox=='none'){
			cboxes[i].checked = false;
		}else if(selectbox=='all'){
			cboxes[i].checked = true;
		}
	}
}

function add_multi_to_bag(order_type,order_method){
	var cfrm=document.form_add_multiple;
	if (order_type=='samples'){
		var samples_to_add= {};
		//alert(cfrm.add_single_sample.value);
		if (cfrm.add_single_sample.value==undefined){
			for (i = 0; i < cfrm.add_single_sample.length; i++){
		  	if (cfrm.add_single_sample[i].checked){
					samples_to_add[cfrm.add_single_sample[i].value] = document.getElementById('add_multiple_samples['+cfrm.add_single_sample[i].value+']').value;
		  	}
			}
   	}else{
   		//this is workaround if only one item is displayed in results, otherwise previous condition is executed
	  	if (cfrm.add_single_sample.checked){
				samples_to_add[cfrm.add_single_sample.value] = document.getElementById('add_multiple_samples['+cfrm.add_single_sample.value+']').value;
	  	}
   	}
   
	}else if(order_type=='products'){
		var products_to_add={};
   for (i = 0; i < cfrm.add_single_product.length; i++){
      if (cfrm.add_single_product[i].checked){
				products_to_add[cfrm.add_single_product[i].value] = document.getElementById('add_multiple_products['+cfrm.add_single_product[i].value+']').value;
      }
   }
	}else{
		alert('Error - no order type defined.');
		return false;	
	}
	//alert(JSON.stringify(samples_to_add));
	//alert(JSON.stringify(products_to_add));
	$.ajax({
	  url: '/ajax_add_to_bag.php?order_type='+order_type+'&multiple=1&method='+order_method,
	  type : 'POST',
	  data : {"samples" : samples_to_add, "products" : products_to_add, "list" : "1"},
	  success: function(ret) {
	    //$('.result').html(data);
	    //alert(data);
	    refreshFloatChart();
	    $("#message").html(ret);
	    message();
	  }
	});
}


function refreshFloatChart(){
	typeo = typeof(typeo) != 'undefined' ? typeo : 1;
	$.ajax({
		url: '/ajax_refresh_bag.php',
		success: function(data) {
		$('#floatc').html(data);
		//alert('Load was performed.');
		}
	});
	if ($('#small_samples_bag_count_top')){
		$.ajax({
			url: '/ajax_refresh_small_samples_bag.php',
			success: function(data) {
			var s=data;
			$('#small_samples_bag_count_bottom').html(s);
			$('#small_samples_bag_count_top').html(s);
			//alert('Load was performed.');
			}
		});
	}
	if ($('#small_products_bag_count_top')){
		$.ajax({
			url: '/ajax_refresh_small_products_bag.php',
			success: function(data) {
			var s=data;
			$('#small_products_bag_count_bottom').html(s);
			$('#small_products_bag_count_top').html(s);
			//alert('Load was performed.');
			}
		});
	}
}



function isValidEmailAddress(emailAddress) {
	//alert(emailAddress);
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	//alert(pattern.test(emailAddress));
	return pattern.test(emailAddress);
}
function trOver (_tr,p) {
   if (p == 'home') {
      _tr.style.borderColor = '#522700';
      _tr.style.backgroundColor = '#FFF8F2';
   }
}
function trOut (_tr,p) {
   if (p == 'home') {
      _tr.style.borderColor = '#CDB4A3';
      _tr.style.backgroundColor = '#FFFFFF';
   }
}

function verifyContact(ev) {
   var good = true;
   if($('#name').val() == '') {
      good = false;
      $('#name').css("background-color","#E48B8F");
   }
   if ($('#phone').val() == '') {
      good = false;
      $('#phone').css("background-color","#E48B8F");
   }
   if ($('#emailcf').val() == '' || !isValidEmailAddress($('#emailcf').val())) {
      good = false;
      $('#emailcf').css("background-color","#E48B8F");
   }
   if ($('#messagecf').val() == '' ) {
      good = false;
      $('#messagecf').css("background-color","#E48B8F");
   }
   if ($('#recaptcha_response_field').val() == '' ) {
      good = false;
      $('#recaptcha_response_field').css("background-color","#E48B8F");
   }

   if (good == false)
   		$("#messagetbody").css("display", "table-row-group");

   return good;
}


function submitToFriend(ev) {
   var good = true;
   if($('#f_name').value == '' || $('#f_name').value == 'Your name') {
      good = false;
      $('#f_name').css("background-color","#E48B8F");
   }
   if ($('#f_email').value == '' || !isValidEmailAddress($('#f_email').val())) {
      good = false;
      $('#f_email').css("background-color","#E48B8F");
   }
   if ($('#t_email').value == '' || !isValidEmailAddress($('#t_email').val())) {
      good = false;
      $('#t_email').css("background-color","#E48B8F");
   }

   if (good == false) {
   		$("#message").html('Error! Please be sure to fill out all of the fields!');
    	message();
   } 
   else {
      $('#sendToFriendStatus').html('<img src="images/main/ajax-email-loading.gif" style="vertical-align: middle;" />&nbsp;E-mail is sending...');
      params = $('#sendToFriend').serialize();
      $('#sendToFriend').hide();
			$.ajax({
				type:'GET',
				data: params,
			  url: '/ajax_send_to_friend.php',
			  success: function(data) {
			    $('#sendToFriendStatus').html(data);
			  }
			});
   }
}

function submitImageToFriend(ev) {
   var good = true;
   if($('#f_name').value == '' || $('#f_name').value == 'Your name') {
      good = false;
      $('#f_name').css("background-color","#E48B8F");
   }
   if ($('#f_email').value == '' || !isValidEmailAddress($('#f_email').val())) {
      good = false;
      $('#f_email').css("background-color","#E48B8F");
   }
   if ($('#t_email').value == '' || !isValidEmailAddress($('#t_email').val())) {
      good = false;
      $('#t_email').css("background-color","#E48B8F");
   }

   if (good == false) {
   		$("#message").html('Error! Please be sure to fill out all of the fields!');
    	message();
   } 
   else {
		$('#sendToFriendStatus').html('<img src="../../images/main/ajax-email-loading.gif" style="vertical-align: middle;" />&nbsp;E-mail is sending...');
		
		params = $('#sendToFriend').serialize();
		$.ajax({
				type:'GET',
				data: params,
				url: '/ajax_send_image_to_friend.php',
				success: function(data) {
					$('#sendToFriendStatus').html(data);
				}
		});
   }
}

function check_inventory(itemNo, company, email){ 

	var yards = $('#yards').val();
	
	$.get(
        "/ajax_check_inventory.php?productID=" + itemNo + "&yards=" + yards + "&company=" + company + "&email=" + email,
        '',
        function(data) {
            if(data.error){         
                alert(data.error);
			    return;    
            }
			else
				$("#inventory").html(data);
		}
    );  
}

function showHide(div){
	if($('#'+div).css("display")=='none')
		$('#'+div).fadeIn('slow',function(){});
	else
		$('#'+div).fadeOut('slow',function(){});
}

function updatePressDetails (img,limg) {
   wimg = $('#press-large');
   //wimgl = $('press-hidden');
   wimg.attr('src','images/main/press-loading.gif');
   //wimgl.attr('src',limg);
   wimg.attr('src',img);
   $('#press-large-link').href = limg;
}

/* hover effect on collections pages what's new, collections, category, books & binders */
$(document).ready(function() {			
	$('.coll-small-box').hover(function() {
			$(this).css('border', '1px solid #CDB4A3');
			$('.imgCaptionBottom', this).css({'background': '#CDB4A3', 'color': '#000'});
		}, function() {
			$(this).css('border', '1px solid #572500');
			$('.imgCaptionBottom', this).css({'background': '#2F1A09', 'color': '#fff'});
	});
	
	var linkNum;
	$('.collLink').hover(function() {
		var linkID = $(this).attr('id');
		linkNum = linkID.substr(9);				
		$('#coll_'+linkNum).css('border', '1px solid #CDB4A3');
		$('#collSpan_'+linkNum).css({'background': '#CDB4A3', 'color': '#000'});						
		
		}, function() {
			$('#coll_'+linkNum).css('border', '1px solid #572500');
			$('#collSpan_'+linkNum).css({'background': '#2F1A09', 'color': '#fff'});
		}				
	);	
});
