$(function(){
	$('.productnum').live('keyup',function(){
		var v = $(this).val().replace(/[^0-9]/g, '').replace(/^0+/, '');
		$(this).val(v);
	})
	$('input[name=phone]').keyup(function(){
		var p = $(this).val().replace(/[^0-9+-]/g,'').replace(/^[^+0-9]/,'');
		$(this).val(p);
	})
	$('#dragdiv').draggable({handle:'#tt-cart-div'});
	$('#userInfoForm').submit(function(){
		if(!$('input[name=firstName]').val()){
			alert('Please Input Your FirstName!');
			$('input[name=firstName]').focus();
			return false;
		}
		if(!$('input[name=lastName]').val()){
			alert('Please Input Your LastName!');
			$('input[name=lastName]').focus();
			return false;
		}
		if(!$('input[name=email]').val()){
			alert('Please Input Email Address!');
			$('input[name=email]').focus();
			return false;
		}
		if(!$('input[name=confirmemail]').val()){
			alert('Please Confirm Your Email Address!');
			$('input[name=confirmemail]').focus();
			return false;
		}
		if($('input[name=email]').val() != $('input[name=confirmemail]').val()){
			alert('Your Email address is not match!');
			$('input[name=confirmemail]').focus();
			return false;
		}
		if(!$('input[name=phone]').val()){
			alert('Please Input Company Phone No!');
			$('input[name=phone]').focus();
			return false;
		}
	})
})
var random = generateRandomNumbers();
// click Inquity link pop a div 
function threeTeamInquiry(productID){
	var divtop = $(document).scrollTop();
	$.get("/threeteam/threeteam-cart.ajax.php", {action:'pop',productID:productID, productNums:1 ,notes:"",top:divtop},
		function(data){
			if($('#closeDiv').length>0){ $("#closeDiv").click(); }
			$('#dragdiv').append(data);
	});
}
//click add button and add a product to my cart
function addProdcutToCart(productID){
	var productNums  = $('#TTCartProductNums'+productID).attr('value');
	if(!productNums){
		alert('Please Input Quantity!');
	}
	var productNotes = $('#TTCartProductNotes'+productID).attr('value');
	//ajax request
	$.get("/threeteam/threeteam-cart.ajax.php", {action:'add',productID:productID, productNums:productNums ,notes:productNotes,flush:random},
		function(data){
			if(data=='false'){
				alert('the product have been added!');
				//$('#updateDelTr_'+productID).remove();
				return false;
			}
			//page hander*/
	});
	if(!productNums){
		$('#updateDelTr_'+productID).empty().append('<img id="submitProduct"  src="/threeteam/images/add-product.gif" onclick="addProdcutToCart('+productID+')"  style="cursor:pointer" title="add product"/>');
	}else{
		$('#updateDelTr_'+productID).empty().append('<img id="update-divcart" src="/threeteam/images/add_quantity_cart.png" onclick="updateDivCart('+productID+')" style="cursor:hand" title="update quantity"/><br /><img id="del-divcart" src="/threeteam/images/cacle-product.gif" onclick="delDivCart('+productID+')" style="cursor:hand" title="delete"/>');
	}
	if(productNums){
		$('#porductCartSubmit').replaceWith('<div id="divCartSubmit" class="ui-dialog-content ui-widget-content" style="border:3px solid #000;border-top:0;text-align:left;padding:5px;background:#f6a828;"><div style="font-weight:bolder;text-align:center;margin-bottom:-20px;">Browse our large selection of over 4000 products.<br />New products added weekly.<div style="width:100%;position:relative;z-index:100;top:-20px;left:10px;text-align:left;font-weight:normal;"><a style="color:#333;text-decoration:underline;" href="javascript:continueViewProduct();">cancel</a></div></div><div style="height:26px;padding:5px 10px;"><div style="width:100%;height:26px;"><span style="float:left;"><img src="/threeteam/images/continue-select.gif" onclick="continueViewProduct()" style="cursor:hand" title="continue" /></span><span style="float:right;"><img src="/threeteam/images/send-inquiry.gif" onclick="sumitDivCart()" style="cursor:hand" title="submit" /></span></div></div></div>');
	}
}
//click cacle button 
function cacleAddProduct(productID){
	$('#TT_'+productID).remove();
	$('#porductCartSubmit').replaceWith('<div id="divCartSubmit" class="ui-dialog-content ui-widget-content" style="border:3px solid #000;border-top:0;text-align:left;padding:5px;background:#f6a828;"><div style="font-weight:bolder;text-align:center;margin-bottom:-20px;">Browse our large selection of over 4000 products.<br />New products added weekly.<div style="width:100%;position:relative;z-index:100;top:-20px;left:10px;text-align:left;font-weight:normal;"><a style="color:#333;text-decoration:underline;" href="javascript:continueViewProduct();">cancel</a></div></div><div style="height:26px;padding:5px 10px;"><div style="width:100%;height:26px;"><span style="float:left;"><img src="/threeteam/images/continue-select.gif" onclick="continueViewProduct()" style="cursor:hand" title="continue" /></span><span style="float:right;"><img src="/threeteam/images/send-inquiry.gif" onclick="sumitDivCart()" style="cursor:hand" title="submit" /></span></div></div></div>');
}
//update my cart
function updateDivCart(productID){
	var productNums  = $('#TTCartProductNums'+productID).attr('value');
	var productNotes = $('#TTCartProductNotes'+productID).attr('value');
	//ajax
	$.get("/threeteam/threeteam-cart.ajax.php", {action:'update',productID:productID, productNums:productNums ,notes:productNotes,flush:random});
}
//delete product
function delDivCart(productID){
	$('#TT_'+productID).remove();
	$.get("/threeteam/threeteam-cart.ajax.php", {action:'delete',productID:productID});
	//alert($('#box_order2 #del-divcart').length);
	if($('#box_order2 #del-divcart').length == 0){
		$('#divCartSubmit').replaceWith('<div class="ui-dialog-content ui-widget-content" id="porductCartSubmit" style="border:3px solid #000;border-top:0;text-align:left;padding:3px;background:#f6a828;"><div style="float:left;font-weight:bolder;padding-left:48px;height:36px;">Browse our large selection of over 4000 products.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;New products added weekly.</div><div style="float:right;height:36px;padding:5px 20px 5px 0;"><img id="cacle-addProduct" src="/threeteam/images/cacel-add.gif" onclick="continueViewProduct()" style="display:inline;vertical-align:middle;cursor:pointer" title="cacle product"/></div><div style="clear:both;"></div></div>');
		$('#cacle-addProduct').replaceWith('<img id="cacle-addProduct" src="/threeteam/images/cacel-add.gif" onclick="continueViewProduct()" style="display:inline;vertical-align:middle;cursor:pointer" title="cacle product"/>');
	}
}
//continue button
function continueViewProduct(){
	$('#myFloat').remove();
	$('#tt-cart-div').remove();
	$('#dialog').remove();
	$('#divCartSubmit').remove();
	if($('#porductCartSubmit')){ $('#porductCartSubmit').remove(); }
}
//submit button
function sumitDivCart(){
	//test
	window.location.href="/index.php?option=com_orderproduct&page=order.product";
}
function generateRandomNumbers() {
    var randomNumbers = "";
    do {
        var num = Math.round(10*Math.random());
        if(num == 10) num = 0;
        if(randomNumbers.length == 0) randomNumbers += num.toString();
        for(var i=0;i<randomNumbers.length;i++) {
            if(num.toString() == randomNumbers[i]) break;
        }
        if(i == randomNumbers.length) randomNumbers += num.toString();
    } while(randomNumbers.length != 4);
    return randomNumbers;
}
//if cart is empty
function cartEmpty(){
	if($('#box_order2 #del-divcart').length == 0){
		$('#cacle-addProduct').replaceWith('<img id="cacle-addProduct" src="/threeteam/images/cacel-add.gif" onclick="continueViewProduct()" style="display:inline;vertical-align:middle;cursor:pointer" title="cacle product"/>');
	}
}
