// JavaScript Document

function minimum()
{
	
	var moq = $('#moq').val();
	if (document.getElementById('quantity').value < moq )
	{
		alert ('The minimum order quantity for this item is '. moq );
		document.getElementById('avalible').value = 'no';
	}
	else
	{
		document.getElementById('avalible').value = 'yes';
	}	
}



function digits(evt) 
{
	evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false
    }
   return true
}

function artwork()
{
	if (document.getElementById('customize2').checked == true)	{
		document.getElementById('imprint').disabled=false;
		document.getElementById('preproduction').disabled=false;
		document.getElementById('id1').disabled=false;
		document.getElementById('id2').disabled=false;
	}
	else
	{
		document.getElementById('imprint').disabled=true;
		document.getElementById('id1').value="";
		document.getElementById('id2').value="";
		document.getElementById('id1').disabled=true;
		document.getElementById('id2').disabled=true;
		document.getElementById('preproduction').disabled=true;
		document.getElementById('digitalproof').disabled=true;
	}		
}

function checkPantone()
{
	if (document.getElementById('colour').value == 'CC')	{
		document.getElementById('pantone').disabled=false;

	}
	else
	{
		document.getElementById('pantone').disabled=true;
		document.getElementById('pantone').value = "";
	}		
}


function checkImprintMoq(){
	
	var quantity = parseInt($('#quantity').val());
		
	// reset names
	$.map(imprintname, function(value, key) {
		if (value != 0) {
			$("#imprint option[value='" + key + "']").text(value);	
		}
	});
	
	
	// check with is avalible
	$.map(imprintmoq, function(value, key) {
		
		// convert to int
		parseInt(value);
		
		// skip the 0 value
		if (value != 0) {
			if (quantity < value) {
				$("#imprint option[value='" + key + "']").attr("disabled","disabled");
				var org = $("#imprint option[value='" + key + "']").text();
				$("#imprint option[value='" + key + "']").text(org + ' (available from ' + value + ' piece)');
			} else {				
				$("#imprint option[value='" + key + "']").removeAttr('disabled');
			} 
		}
	});
}



function checkColourMoq(){
	
	var quantity = parseInt($('#quantity').val());
		
	// reset names
	$.map(colourname, function(value, key) {
		if (value != 0) {
			$("#colour option[value='" + key + "']").text(value);
		}
	});
	
	
	// check with is avalible
	$.map(colourmoq, function(value, key) {
		
		// convert to int
		parseInt(value);
		
		// skip the 0 value
		if (value != 0) {
			if (quantity < value) {
				$("#colour option[value='" + key + "']").attr("disabled","disabled");
				var org = $("#colour option[value='" + key + "']").text();
				$("#colour option[value='" + key + "']").text(org + ' (available from ' + value + ' piece)');
				//$('option:selected', 'select').removeAttr('selected').next('option').attr('selected', 'selected');
			} else {				
				$("#colour option[value='" + key + "']").removeAttr('disabled');
			
			} 
		}
	});
	
	calculatePrice();
	
}



function calculatePrice()
{
	if ($('#quantity').val() != ''){
		
		var moq = $('#moq').val() ;
		
		
			
		if ($('#quantity').val() > (moq-1))
		{
			document.getElementById('avalible').value = 'yes';	
			var url = $("#url").val();
            
            var prestatus = $('#preproduction').attr('checked');
                
            if (prestatus){
                pre = 'yes';
            } else {
                pre = 'no';
            }
            
			$.post(url, {
				colour : $('#colour').val(),
				itemid : $('#item').val(),
                preproduction : pre,                                            
				quantity : $('#quantity').val(),
				memsize: $('#memsize').val(),
				imprint: $('#imprint').val(),
				custom: $('#customize2:checked').val(),	
				rand : Math.random()
				}, function(data) {
					var notavailable = data.toString().search(new RegExp(/The memory size is not available/i));
					if (notavailable > 0) {
						 document.getElementById('avalible').value = 'no';
						 $('#calculatearea').html(data);	
					} else {
						$('#calculatearea').html(data);		
					}
				});
		} else {
			document.getElementById('avalible').value = 'no';
			alert ('The minimum order quantity for this item is '+moq);
		}
	}
}


$(document).ready(function() {
	
	
	$("#butadd").click(function() {
		if (document.getElementById('avalible').value == 'yes') {	
			$("#butadd").hide();
			document.basket.submit();	
		}
	});
	
	
	$("#currency").change(function() {
			
		var url = $("#currencyurl").val();
		
		$.post(url, {
			currency : $('#currency').val(),
			action : 'updateCurrency',
			rand : Math.random()
		}, function(data) {
			if (data == 'yes') {
				window.location.reload();
			}
			if (data == 'no') {
			}
		});
	});
});


$().ready(function() {
  $('#dialog').jqm();
});



