
sfHover = function(){
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i = 0; i < sfEls.length; i++) {
		sfEls[i].onmouseover = function(){
			this.className += " sfhover";
		}
		sfEls[i].onmouseout = function(){
			this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


//************************************************************************************************************************************************* 
	function setPageSize(pageSize){
		$.get('index.php', { action: "setPageSize", pageSize: $('#pageSizeChanger').val() }, function(){
			window.location.reload();
		});
	}

	$(document).ready(function(){

//		$("ul.nav").superfish();

		$(':text, :password').focus( function(){ $(this).addClass('selectselected'); } );
		$(':text, :password').blur( function(){ $(this).removeClass('selectselected'); } );

//		$(':button').mouseover(function(){ $(this).addClass('selectselected'); });
//		$(':button').mouseout(function(){ $(this).removeClass('selectselected'); });

//		$(':submit').click(function(){ $(this).attr('disabled','true'); });

		$(".stripe tr:nth-child(even)").addClass("alt");
//		$('#rightcolumn').cornerz({ radius:15, corners:"tr br" });
//		$('.browseannounce').cornerz({ radius:15, corners:"tl" });
//		$('.details').cornerz({ borderWidth: 1, radius:15 });

	});
	
//*************************************************************************************************************************************************		
		
//*************************************************************************************************************************************************
function validateNew(){
	var name=$('#uName').val();
		var request=$.post("submitregistration.php",{
			action: 'checkname',
			name: name
		},function(data){
			if(data=='true'){
				$('#alreadyTaken').show();	
			}else{
				$('#frmRegNew').submit();
			}	
		});
}
//*************************************************************************************************************************************************
	
//*************************************************************************************************************************************************	

	function validateExisting(){
		//clear notices
		$('#enterFName').hide();
		$('#enterLName').hide();
		$('#enterUserName').hide();
		$('#passwordLength').hide();
	//	$('#invalidEmail').hide(); 
		$('#alreadyTaken').hide();
		$('#specialChars').hide();
		
		var fName = $('#fName').val();
		var lName = $('#lName').val();
		var name=$('#uName').val();
		var pass=$('#pWord').val();
	//	var email=$('#email').val();
		
		//[/\\,.<>:;'"\[\]{}`~!#$%^&*()-+=\|] regexp
		
		var tfName = fName.replace(/^\s+|\s+$/g, '') ;
		var tlName = lName.replace(/^\s+|\s+$/g, '') ;
		var tname = name.replace(/^\s+|\s+$/g, '') ;
		var tpass = pass.replace(/^\s+|\s+$/g, '') ;
		
		
		var specCharMatch = /~|`|,|\.|<|>|\/|\?|; |\:|\^|&|\*|\(|\)|-|\+|\=|%|\$|#|\!|\{|\}|\[|\]|\||\\|'|"/
		
		if(specCharMatch.test(tname)){
			$('#specialChars').show();
			return;	
		}
				
		
		if( tfName.length < 1 ){
			$('#enterFName').show();
			return;
		}
		
		if( tlName.length < 1 ){
			$('#enterLName').show();
			return;
		}

		if( tname.length < 1 ){
			$('#enterUserName').show();
			return;
		}

		if( tpass.length < 5 ){
			$('#passwordLength').show();
			return;
		}
	/*
		if(!emailCheck(email,true)){
			$('#invalidEmail').show();
			return;
		}
	*/
		var request=$.post("submitregistration.php",{
			action: 'checkname',
			name: name
		},function(data){
			if(data=='true'){
				$('#alreadyTaken').show();	
			}else{
				document.getElementById('frmRegExisting').submit();
			}	
		});
	}
//*************************************************************************************************************************************************	
	function sameAsBilling(){
		if($('#theShipCheck').attr('checked')){
			$('#shipName').val($('#billName').val());
			$('#shipAdd1').val($('#billAdd1').val());
			$('#shipAdd2').val($('#billAdd2').val());
			$('#shipCity').val($('#billCity').val());
			$('#shipState').val($('#billState').val());
			$('#shipZip').val($('#billZip').val());
			$('#shipPhone').val($('#billPhone').val());
			$('#shipFax').val($('#billFax').val());
		}
	}
//*************************************************************************************************************************************************	
	
	function showRegNew(){
	}

//*************************************************************************************************************************************************
	
	function showCartBox(start){
		$(".cart").load("lib/php/cart.php?action=showCartBox&start="+start, function(){
			$(".stripe tr:nth-child(even)").addClass("alt");
		});
	}
	
//*************************************************************************************************************************************************	
	
	function showDetail(partNumber){
		$('#leftcolumn').load("detail.php?action=showDetail&partNumber="+partNumber);
	}
	
//*************************************************************************************************************************************************	
	
	function checkAll(itemList){
		var value = ($('#AllChk').attr("checked")) ? $('#AllChk').attr("checked"): false;
		$("input[id^=chk]").attr({checked:value});
	}
	
//*************************************************************************************************************************************************	
	
	function applyCartChanges(){
		var valid = true;
	
		$("input[id^=qty]").each(function(){					
				if(  parseInt($(this).val()) < 1 ){
					valid = false;
				}
			});
	
		if(valid){
			$('#formAction').val('updateQty');
			document.getElementById('cartForm').submit();
		}
	}
	
//*************************************************************************************************************************************************	
	
	function deleteCartItems(){
		$('#formAction').val('deleteSelected');
		document.getElementById('cartForm').submit();	
	}
	
//*************************************************************************************************************************************************	
	
	function showQuikLists(){
		location.href = "quiklists.php";
	}
	
//*************************************************************************************************************************************************	
	
	function addToList(){
		var form = document.getElementById('cartForm');
		
		form.action = 'cart_parts_2_list.php';
		form.submit();	
	}
	
//*************************************************************************************************************************************************	
	
	function blockZero(item,origQty){
		// escape [special chars if they exist
		item = addslashes(item);
		
		var theQty = $('#'+item).val();
		
		if(parseInt(theQty)<1 || isNaN(parseInt(theQty)) && theQty.length > 0){
			alert('Order quantity cannot be less than 1. Please remove the item.');
			$('#'+item).val(origQty);
		}		
	}
	
//*************************************************************************************************************************************************	
	
	function login(){
		$.post("login.php", {
			username: $('#username').val(),
			password: $('#password').val()
		}, function(returndata){
			$("#loginBlock").html(returndata);
			$.getScript("lib/php/cart.php?action=loggedIn",function(){
				if(loggedIn){
					//showCartBox(0);
					location.href="index.php";
				}
			});
		});
	}
	
//*************************************************************************************************************************************************	

	function addslashes(str){
		str=str.replace(/\\/g,'\\\\');	
		str=str.replace(/\[/,'\\[');
		str=str.replace(/\]/,'\\]');
		str=str.replace(/\'/g,'\\\'');
		str=str.replace(/\"/g,'\\"');	
		return str;
	};	  
	
	function stripslashes(str) {
		str=str.replace(/\\'/g,'\'');
		str=str.replace(/\\"/g,'"');
		str=str.replace(/\\\[/g,'[');
		str=str.replace(/\\\]/g,']');		
		str=str.replace(/\\\\/g,'\\');	
		return str;
	}
	
//*************************************************************************************************************************************************	

/*			$.fn.hoverClass = function(c) {
		return this.each(function(){
			$(this).hover( 
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
		});
	};	  
*/

//*************************************************************************************************************************************************

	function showPage(url){
		$('#leftcolumn').load(url);
	}
	
//*************************************************************************************************************************************************	
	
	function comingSoon(){
		alert('This feature is still under development.')
	}
	
//*************************************************************************************************************************************************	
	
	function showRegister(){
		$('#leftblock').load("register.php");
	}


//*************************************************************************************************************************************************	

	function addToCart(PartNumber){
		$.getScript('lib/php/cart.php?action=loggedIn',function(){
			if( !loggedIn ){
				alert('Your session has expired due to inactivity.\nPlease login again.');
				location.href='index.php';
				return;
			}
			
			qty=$('#qty_'+PartNumber).val();
			
			if(qty < 0){
				qty = qty * -1
				$('#qty_'+PartNumber).val(qty);
			}
			
			$("#addedcheck"+PartNumber).fadeIn('slow');
			
			$(".cart").fadeTo(50,'.1',function(){
				$(".cart").fadeTo(250,'1');
				
				$.post("lib/php/cart.php",{
					action: "add",
					partnumber: PartNumber,
					qty: qty
					}, function(data){
						$(".cart").html(data);
						});
			});
	});
}
//*************************************************************************************************************************************************	

	function loadResults(page_id, category_id){
		start=page_id*12;
		$("#leftblock").load("browse.php?category="+category_id+"&start="+start+"&action=paginate");
	}

//*************************************************************************************************************************************************
	
	// subClass, subClassDescription, classDescription
	function loadBrowse(p,d,c){
		$.getScript("browse.php?action=getTotalResults&category="+p, function(){
			$("#leftcolumn").load("browse.php?category="+p+"&start=0", function(){
				
				var ubc = "<a href='index.php'>Home</a>&nbsp;>&nbsp;<a href=\"#\" onClick=\"loadSubs('" + p.substring(0,1) + "','" + c + "')\">"+ c +"</a> " + '&nbsp;>&nbsp;' + d
				var sbc = '>&nbsp;<a href="index.php">Home</a><br>>&nbsp;'+ "<a href=\"#\" onClick=\"loadSubs('" + p.substring(0,1) + "','" + c + "')\">"+ c +"</a> " + '<br>' + '>&nbsp;' + d

				$('#numresults').html(numResults+' products found');
				$('#upperbreadcrumb').html(ubc);
				$('#sidebreadcrumb').html(sbc);

				$("#categories").html("Soon this will have filters...");

				if(numResults>12){
					$(".pagination").pagination(numResults, {
						category_id:p,
						items_per_page:12,
						callback: loadResults
					});
				}
			});
		});
		
	}

//*************************************************************************************************************************************************

	function loadSubs(p,cat){
		$("#categories").load("lib/php/sidemenu.php?category="+p, function(){  });
		$("#leftcolumn").load("browse.php?action=newParts&class="+p, function() {
			var ubc = "<a href='index.php'>Home</a>&nbsp;>&nbsp;<a href=\"#\" onClick=\"loadSubs('" + p + "','" + cat + "')\">"+cat+"</a> > ";
			var sbc = '>&nbsp;<a href="index.php">Home</a><br>>&nbsp;<a href="#">'+cat+'</a>';
			$("#upperbreadcrumb").html(ubc);
			$("#sidebreadcrumb").html(sbc);
			$("#resultinfo").show();
		});
	}
	
//*************************************************************************************************************************************************	
	
	function loadContents(){
		// IN PROGRESS
		alert('LOADING');
	}
	
//*************************************************************************************************************************************************	

	function roundCorners(){
		$('.cart').cornerz({radius:10, corners:"tr"})
		$('.categories').cornerz({radius:10, corners:"br"})
		$('.resultinfo').cornerz({radius:10, corners:"tl"})
	}
	
//*************************************************************************************************************************************************	

	function validListName(obj){
		var str = addslashes(obj.value);
	
		if(str.length > obj.value.length){
			$('#list_name').val(obj.value.substr(0,obj.value.length-1));
			alert('Special characters are not allowed in the list name.')
		}
	}	
	
//*************************************************************************************************************************************************	
	
	function setQuikAction(str, obj){
		var valid = true;
		var name = '';			// set this string if you don't wanna loose the list name otherwise on invalid the list name is erased
		var index = 0;
		var form = document.getElementById('listForm');		

		// use switch to determine caller
		switch(str){
			case 'make_new':
				// if make_new page thencheck if adding or subtracting rows
				temp_str = obj.value;		// object value string from DOM object that called function
				x = 5;
				rows = parseInt($('#rows').val());
				x = (temp_str.search(/-/) >= 0 ? rows-x : rows+x);
				
				// rows will only be an input object if called from maek_new function
				$('#rows').val(x.toString());
				break;
			case 'save_new':	// make sure it'svalid before saving
				var temp_str = '';
				var list_name = $('#list_name').val();
				
				// check blank list name
				valid = (list_name).length <= 0 ? false : true;
				temp_str = "The list name cannot be blank.";
				
				// check for special characters by comparing length								
				if(valid){
					valid = (addslashes(list_name)).length > list_name.length ? false : true;
					temp_str = "The list name cannot contain special characters.";
				}
				
				// validate part quantities
				if(valid){
					$("input[id^=qty]").each(function(i,e){
						var prt = ($("input[id^=part]"))[i].value;
						
						if($(this).val() == '0' || $(this).val().toString().length == 0 && prt.length > 0){
							valid = false;
							temp_str = "Cannot have parts with quantity < 1, please remove part #"+prt+" from the list.";	
						}
					});
				}

				if(!valid){ 
					alert(temp_str);
					name = $('#list_name').val();
				}
				break;
			case 'copy_2_cart':	
				// set list name to be posted			
				$('#list_name').val(obj.name);
				valid = confirm('Do you want to Copy '+obj.name+' into your Shopping Cart?');
				break;
			case 'remove_list':
				$('#list_name').val(obj.name);				
				valid = confirm('Are you sure you want to delete this list?');
				break;
			case 'add2cart':
				valid = confirm('Add selected parts to you Shopping Cart?');
				break;
			case 'delete':
				var tempStr = '';
			
				if(obj == null || obj == 'undefined'){
					tempStr = 'Are you sure you want to delete these parts?';
				}else{					
					for(var i=0; i<form.length; i++){
						form.elements[i].checked = false;

						if(obj.name == form.elements[i].value){
							index = i;
						}
					}
					
					form.elements[index].checked = true;
					tempStr = 'Are you sure you want to delete part #'+form.elements[index].value+'?';
				}
			
				valid = confirm(tempStr);				
				if(!valid){	// uncheck if not removing part
					form.elements[index].checked = false;
					name = $('#list_name').val();
				}
				
				break;
			case 'update':
				$("input[id^=qty]").each(function(){					
					if($(this).val() == '0'){
						valid = false;
						name = $('#list_name').val();
					}
				});
				break;
			default:
				// the default is a list name when they link to the list parts from the list of all saved lists
		}

		if(valid){
			$('#formAction').val(str);
			document.getElementById('listForm').submit();
		}else{
			$('#list_name').val(name);
		}
	}
	
//*************************************************************************************************************************************************	
function pausecomp(millis)
{
	var date = new Date();
	var curDate = null;
	
	do { curDate = new Date(); }
	while(curDate-date < millis);
} 


function cart2List(){
	var str = '';
	var valid = true;
	
	// IF new list type in then adds, otherwise checks for a radio button to be chacked
	if(($('#list_name').val()).length > 0 ){
		str = $('#list_name').val();
	}else{
		var btns = document.getElementsByName('qlist');
		var index = -1;
		var rad = 'undefined';
		
		for(var i=0; i < btns.length; i++){
			rad = btns[i];
			
			if(rad.checked){
				index = i;
			}
		}
		
		if(index == -1){
			valid = false;
			alert('Please type a NEW list name or select an existing list to add the parts to.');
		}else{
			rad = btns[index];
			str = rad.value;
		}
	}
	
	if(valid){
		$('#formAction').val('copy');
		$('#cartName').val(str);
		document.getElementById('listForm').submit();
	}
}

