$(document).ready(function() {
	
	$("#step1Form").validate({
	  rules: {
  	  pass: {minlength:4},
    	pass2: {equalTo: "#pass", minlength:4}
  	},
		submitHandler: function(form) {regFrom()}

	});
	
	$(".fade img").hover(
      function () {
        $(this).fadeTo("fast",0.7);
      }, 
      function () {
        $(this).fadeTo("fast",1);
      }
    );


	$("#lsignin").click(function () {
			$("#lresult").html("<img src='/images/ajax-loader2.gif' alt='' class='img3'");											
			var email = $("#lemail").val();
			var pass = $("#lpass").val();
     $("#lresult").load("/checkuser.php", { 'logindata[]': [email, pass] } ); 
  });
	
	$(".prodlist, .prodlist2,").corner();    


});

function regFrom() {
	$("#rresult").html("<img src='/images/ajax-loader2.gif' alt='' class='img3'");
		var cpname = $("#cpname").val();
		var title = $("#title").val();
		var firstname = $("#firstname").val();
		var lastname = $("#lastname").val();
		var email = $("#email").val();
		var tel = $("#tel").val();
		var fax = $("#fax").val();
		var address1 = $("#address1").val();
		var address2 = $("#address2").val();
		var town = $("#town").val();
		var postcode = $("#postcode").val();
		var county = $("#county").val();
		var country = $("#country").val();
		var pass = $("#pass").val(); 
		var redirect = $("#redirect").val();
		
	$("#rresult").load("/checkuser.php", { formstep1:'1', regcpname: cpname, regtitle: title, regfirstname: firstname, reglastname: lastname, regemail: email, regtel: tel, regfax: fax, regaddress1: address1, regaddress2: address2, regtown: town, regpostcode: postcode, regcounty: county, regcountry: country, regpass: pass, regredirect: redirect  } );
	}

function removediv(thisId) {$("#showstatus,div#"+thisId).slideUp("fast");}

function addtocart(thisId)
	{
		
		$("#showstatus,div#"+thisId).html("<img src='/images/ajax-loader.gif' alt=''> Loading shopping cart...").show("fast");
  	$("#cartitems").text("Loading...").load("/cart.php?action=itemadd&itemid="+thisId,{},function(){
   		$("#showstatus,div#"+thisId).html("<div><img src='/images/green_tick.png' alt=''> Product has been added successfuly.</div>");
			$("p#p"+thisId).replaceWith("<p class='text1'><a href='/showcart.php'><img src='/images/show_cart.png' alt=''></a></p>");
			
			setTimeout("removediv("+thisId+")",5000); 
 			});  
		
	}

function remfromcart(thisId)
	{
		$("#showstatus").html("<img src='/images/ajax-loader.gif' alt=''> Loading shopping cart...").show("fast");
  	$("#cartitems").text("Loading...").load("/cart.php?action=itemrem&itemid="+thisId,{},function(){
			$("#"+thisId).remove();																																																		
   		$("#showstatus").html("<div><img src='/images/green_tick.png' alt=''> Product has been removed successfuly</div>");
			sumcart();
			
			setTimeout("removediv("+thisId+")",5000);
 			});  
		
	}
	
function sumcart(fvalue)
	{
						var pele=new Array();	
						var qele=new Array();
						var cartsum = 0;
						var elm = 0;
						$(".pprice").each(function(i, selected){
							pele[i] = $(selected).text();
						});
						$(".prrp").each(function(i, selected){
							rele[i] = $(selected).text();
						});
						$("input[@name^='pq']").each(function(i, selected){
							qele[i] = $(selected).val();
						});
						
						for ( var n in pele )
							{	
							elm = pele[n] * qele[n];
							cartsum = cartsum+elm;
							}
						cartsum = cartsum;	
						cartvat = cartsum * 0.175;
						ctotal = cartsum + cartvat;
						$("#csubtotal").html(cartsum.toFixed(2));
						$("#cvat").html(cartvat.toFixed(2));
						$("#ctotal").html(ctotal.toFixed(2));
	}
				
function BookmarkLink(burl,btitle) {

 var title = btitle; 
 var url = burl;
 
if ( navigator.appName != 'Microsoft Internet Explorer' )
{ window.sidebar.addPanel(btitle,burl,""); }
else { window.external.AddFavorite(burl,btitle); } }


//Search functions
function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;
}


function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	return row[0] + " (id: " + row[1] + ")";
}

function lookupAjax(){
	var oSuggest = $("#CityAjax")[0].autocompleter;

	oSuggest.findValue();

	return false;
}

function lookupLocal(){
	var oSuggest = $("#CityLocal")[0].autocompleter;

	oSuggest.findValue();

	return false;
}

$(document).ready(function() {
	$("#CityAjax").autocomplete(
		"autocomplete_ajax.cfm",
		{
			delay:10,
			minChars:2,
			matchSubset:1,
			matchContains:1,
			cacheLength:10,
			onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:true
		}
	); 	
});

