
function is_numeric(mixed_var) {
    return !isNaN(mixed_var);
}

function addCommas(s){
	s += '';
	x = s.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function itemHover() {
	$('.image-holder').mouseover(function() {
		$(this).addClass('image-holder-hover');
	});
			 
	$('.image-holder').mouseout(function() {
		$(this).removeClass('image-holder-hover');
	});
}

function keyUpEvent(i) {
	var item = '#qty_'+i;
	
	$(item).focus(function() {
   		this.select();
   	});
   			
   	$(item).keyup(function(e) {
   		if(e.keyCode != 13) {
   			var regex = /^[0-9]+$/;
   			var val = $(item).val();
   			if(val == '') val = '0';
   			if(val.match(regex)){
   				var id = $(this).attr('id').split('_');
   				var qty = $('.qty_'+id[1]).val();
   				var volume = $('.volume_'+id[1]).text();
				var new_total = (parseInt(val) * parseInt(volume));
				$('.total_'+id[1]).html(addCommas(new_total.toFixed(2)));
				$('#qty_total_'+id[1]).val(addCommas(new_total.toFixed(2)));
				$(item).attr({style:''});
				cubictotal();				
   				return false;
   			} else {
   				this.select();
   				$(item).attr({style:'border:1px solid #FF0000'});
   				return false;
   			}
   		}
   	});
}

function cubictotal() {
	var cbm = 0,cft = 0;
	$('.total').each(function() {
		var each = parseInt($(this).text().replace(',',''));
		cft = cft + each;
	});
	
	cbm = cft / 35.32;
	$('#cft').html(addCommas(cft.toFixed(2)));
	$('#txtcft').val(addCommas(cft.toFixed(2)));
	$('#cbm').html(addCommas(cbm.toFixed(2)));
	$('#txtcbm').val(addCommas(cbm.toFixed(2)));	
}

function itemClick() {
   	$('.image-holder').click(function() {
   		var tr = $('#inventory tr:last').attr('rel');
   		if(typeof tr == 'undefined') tr = 2;
   		var trclass = tr == 2 ? 'content-list-r1 invdata' : 'content-list-r2 invdata';
   		var rel = tr == 1 ? '2' : '1';
		var itemid = $(this).attr('id');
		var checker = $('.qty_'+itemid).length;   		
		var info = $(this).attr('rel').split('_');
		var volume = info[0];
		var price = info[1];
		var itemname = $('#itemname_'+itemid).text();
		var qty = is_numeric($('.qty_'+itemid).val()) ? $('.qty_'+itemid).val() : '0';
		var total = $('.total_'+itemid).text();
		
   		if(checker == 0) {
			var row = '<tr class="'+trclass+'" rel="'+rel+'" id="row_'+itemid+'">'+
					  '<td width="300">'+itemname+'<input type="hidden" name="item[id][]" value="'+itemid+'" class="itemid"><input type="hidden" name="item[volume][]" value="'+$.trim(volume)+'" id="item_volume_'+itemid+'" class="itemvolume"><input type="hidden" id="qty_total_'+itemid+'" value="'+$.trim(volume)+'" class="qty_total"></td>'+
					  '<td width="90" align="center"><input name="item[quantity][]" type="text" class="image-textfield qty_'+itemid+' quantity" value="1" id="qty_'+itemid+'"/></td>'+
					  '<td width="120" align="center" class="volume_'+itemid+'">'+$.trim(volume)+'</td>'+
					  '<td width="120" align="center" class="total_'+itemid+' total">'+$.trim(volume)+'</td>'+
					  '<td width="90" align="center"><img src="images/delete.png" alt="" width="16" height="16" id="del_'+itemid+'"/></td>'+
					  '</tr>';
					  
			$('#inventory').append(row);
			deleteBtn(itemid);			
   		} else {
   			var new_qty = parseInt(qty)+1;
   			var new_total = (parseInt(new_qty) * parseInt(volume));
   			$('.qty_'+itemid).val(new_qty);
   			$('.total_'+itemid).html(addCommas(new_total.toFixed(2)));
   			$('#qty_total_'+itemid).val(addCommas(new_total.toFixed(2)));		
   		}
		
		cubictotal();
		keyUpEvent(itemid);	
   	});	
}

function deleteBtn(id) {
	$('#del_'+id).attr({style:'cursor:pointer',title:'Delete'});
	$('#del_'+id).click(function() {
		var itemid = $(this).attr('id').split('_');
		var rowid = itemid[1];
		var sure = confirm('Are you sure you want to remove this item?');
		
		if(sure) {
			$('#row_'+rowid).fadeOut('fast',function() {
				$(this).remove();
				cubictotal();
				$("#inventory tr:even").removeClass().addClass("content-list-r1 invdata").attr({rel:'1'});
				$("#inventory tr:odd").removeClass().addClass("content-list-r2 invdata").attr({rel:'2'});
			});
		} else {
			return false;
		}
		    			
		return false;
	});	
}

function itemtoolTip() {
	$(".image-holder").tooltip({
		delay: 1,
		extraClass: "radius",
		bodyHandler: function() {
			return $('#tooltiptext').val();
		}
	});	
}

function attachClick() {
	$('#quotebtn, #processbtn').click(function() {		
	
		$('#formdata').attr({action:'process.php'});
		checkSubmit('bongga');
		var emukuKukunan = $('#emukuKukunan').val();		
		
		if(emukuKukunan == 'false') {
			alert('Please fill out the highlighted fields.');
			$.scrollTo(0,0,{duration:1300});
			return false;
		} else {
			// replace the image and remove 'click' event so people can't clicky
			$(this).attr({src:'images/calculating_quote_btn.gif',style:'cursor:"inherit"'}).unbind('click');			
			$('#formdata').trigger('submit');	
		}
		return false;
	});
}

function captblur() {
	$('#captcha').unbind('blur');
	$('#captcha').blur(function() {
		var thisval = $.trim($(this).val());
		
		if(thisval != '') {
			$.post('captcha/captcha2.php',{c:thisval},function(r) {
				if($.trim(r) == 'bad') {
					$('#capt').val(r);
					$('#captcha').removeClass('content-q-inputbox').attr({style:'border: 1px solid #FF8F8F;width: 50px !important'});
				} else {
					$('#capt').val(r);
					$('#captcha').removeClass('content-q-inputbox').addClass('content-q-inputbox').attr({style:'width: 50px !important'});
				}
			});
		} else {
			$(this).removeClass('content-q-inputbox').attr({style:'border: 1px solid #FF8F8F;width: 50px !important'});
		}
	});	
	$('#quotebtn, #processbtn').unbind('mouseover');
	$('#quotebtn, #processbtn').mouseover(function() {
		$.post('captcha/captcha2.php',{c: $('#captcha').val()},function(r) {
				if($.trim(r) == 'bad') {
					$('#capt').val(r);
					//$('#captcha').removeClass('content-q-inputbox').attr({style:'border: 1px solid #FF8F8F;width: 100px !important'});
				} else {
					$('#capt').val(r);
					//$('#captcha').removeClass('content-q-inputbox').addClass('content-q-inputbox').attr({style:'width: 100px !important'});
				}
		});
	});
}

$(document).ready(function() {
	itemClick();
	itemtoolTip();
	$("#captcontainer").load('capt2.php',function() {
		captblur();
	});	
	
	$('#top').click(function() {
		$.scrollTo(0,0,{duration:1000});
	});
	
	$('#inventory tr:even').addClass('content-list-r1 invdata').attr({rel:'1'});
	$('#inventory tr:odd').addClass('content-list-r2 invdata').attr({rel:'2'});
	$('#top, #clear').attr({style:'cursor:pointer'});
	
	$('#clear').click(function() {
		$('.invdata').remove();
		cubictotal();
	});

	$('#txtkeyword').focus(function() {
		$(this).val('');
	});
	
	$('#txtkeyword').keypress(function(e) {
		if(e.keyCode == 13) {
			$('#searchbtn').trigger('click');
			$(this).blur(); 
			return false;
		}
	});

	$('#txtkeyword').autocomplete('suggest.php',{
		width: 260,
		selectFirst: false,
		minChars: 2
	});

	$('#quotebtn, #processbtn').click(function() {		
	
		$('#formdata').attr({action:'process.php'});
		checkSubmit('bongga');
		var emukuKukunan = $('#emukuKukunan').val();		
		var capt = $.trim($('#capt').val());		
		
		if(emukuKukunan == 'false') {
			alert('Please fill out the highlighted fields.');
			$.scrollTo(0,0,{duration:1300});
			return false;
		} 
		else 
		{
			/*capt = 'good';
			if(capt == 'bad')
			{
				alert('Security text is incorrect.');
				$('#captcha').focus();
				return false;
			}
			rzl commented it 090310S*/			
			// replace the image and remove 'click' event so people can't clicky
			$(this).attr({src:'images/calculating_quote_btn.gif',style:'cursor:inherit'}).unbind('click');			
			$('#formdata').trigger('submit');	
		}
		return false;
	});

	$('#formdata').submit(function() {
		var use_inventory = $('.quotationitems').is(':visible');
		var er = false;
		var lang = $('#lang').val();	
		
		if(use_inventory) {
			$('.quantity').each(function() {
				if(!is_numeric(this.value)) {
					$('.'+this.id).attr({style:'border:1px solid #FF0000'});
					er = true;
				} else {
					$('.'+this.id).attr({style:''});
				}
			});
		}
		
		if(er) {
			alert('Quantity must be a number.');			
			return false;
		} else {	
			var langcode = $('#txtlcode').val();
			$('#unload').val('yes');
			$(this).attr({action:'process.php?langcode='+langcode});
			$(this).ajaxSubmit(function(response) {	
				if (($.trim(response) != 'unrated') && ($.trim(response) != 'success')) {
					alert('There are some error occurred test: ' + response);
				}
				else {
					if ($.trim(response) == 'unrated') {
						
						window.location.href = 'thankyou_process.php?m=2&lang=' + lang;
					}
					else {
						window.location.href = 'thankyou_process.php?m=1&lang=' + lang;
					}
				}
			});
		}
		
		return false;
	});
	   	
   	$('#searchbtn').click(function() {
   		var keyword = $.trim($('#txtkeyword').val());
   		
   		//if(keyword == '') {
   		//	alert('Please enter a search keyword.');
   		//	return false;
   		//}
   		var lcode = $('#txtlcode').val();
   		$('#itemcontainer').html('<div style="clear:both;padding-top:100px;text-align:center"><img src="images/loader.gif" border="0"></div>');
   		$.post('itemfilter.php',{txtkeyword:keyword,langcode: lcode},function(data) {
   			if(data) {
   				$('#itemcontainer').html(data);
				itemHover();								
				itemClick();
				itemtoolTip();				
   			}
   		});
   	});	
});
