function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function showHelp(title, msg){
		$("#nGlobalErrorDialog").dialog('open');
		$("#nGlobalErrorDialog").dialog( "option", "title", title );
		$("#nGlobalErrorMsg").html(msg);
		$("#nGlobalErrorDialog").dialog( "option", "width", 530 );
		
		
		//$("#nGlobalErrorDialog").dialog( "option", "title", 'Discount Code / Gift Certificate Help' );
		//$("#nGlobalErrorMsg").html("<p>On a regular basis we like to offer discounts to give you the opportunity to try new products.  Depending on the discount, there may be restrictions in its use.<br /><br />These may include the following:<br /><ul><li style='list-style:inside'>Invalid when used with another discount code.<br /><div style='margin-left:14px;font-size:10px; color:#967149'>(If this is the case, choose the discount code with the greatest savings.)</div></li><li style='list-style:inside'>Valid only during a specific period (expired).<br /><div style='margin-left:14px;font-size:10px; color:#967149'>(Contact customer care and we'll be happy to provide you a new discount code.)</div></li><li style='list-style:inside'>Gift certificate not accepted.<br /><div style='margin-left:14px;font-size:10px; color:#967149'>(Currently, gift certificates cannot be used with discount codes.)</div></li></ul></p>");


}

function globalsignin(){
		$("#nGlobalSigninDialog").dialog('open');
		return (false);
}
function passwordreminder(){
		$("#nGlobalPasswordReminderDialog").dialog('open');
		return (false);
}
function globalsignout(){

$.ajax({
		type: "POST",
		url:  "/products/pc/custlo.asp",
		 data: "",
		timeout: 6000,
		success: function(data, textStatus){
			if (data!="FAIL")
			{
				$("#nGlobalErrorMsg").html("<p>You have signed out.</p>");
				$("#nGlobalErrorDialog").dialog('open');
				$("#nGlobalErrorDialog").dialog( "option", "title", 'Sign Out' );
				$("#globalsigninlink").html("<a href='javascript:void(0)' onClick='globalsignin();'>Sign In</a> •");
				idcustomer=0;
			}
			else
			{
				$("#nGlobalErrorMsg").html("<p>There was a problem signing you out.  However, if you close all instances of this browser, your session will be closed.</p>");
				$("#nGlobalErrorDialog").dialog('open');
				$("#nGlobalErrorDialog").dialog( "option", "title", 'Sign Out' );
			}
		}
	});
}

function nucshowemsg2(txt){
		$("#nGlobalErrorMsg").text(txt);
		$("#nGlobalErrorDialog").dialog('open');
		return (false);
}


	  
	  
$(document).ready(function()
{
	 $("#btnPrint").click(function() {
		window.print();
		return false;
	}); 
	 
	$.fn.clearForm = function() {
	  return this.each(function() {
	 var type = this.type, tag = this.tagName.toLowerCase();
	 if (tag == 'form')
	   return $(':input',this).clearForm();
	 if (type == 'text' || type == 'password' || tag == 'textarea')
	   this.value = '';
	 else if (type == 'checkbox' || type == 'radio')
	   this.checked = false;
	 else if (tag == 'select')
	   this.selectedIndex = -1;
	  });
	};
	
	$("#nGlobalAutoCloseDialog").dialog({
			bgiframe: true,
			autoOpen: false,
			resizable: false,
			width: 400,
			height: 200,
			modal: false,
			buttons: {}
			,
			close: function() {}
	});	

	$("#nGlobalErrorDialog").dialog({
			bgiframe: true,
			autoOpen: false,
			autoResize:true,
			resizable: false,
			//width: 400,
			//height: 200,
			modal: true,
			buttons: {
				' Close ': function() {						
						$(this).dialog('close');						
				}
			},
			close: function() {}
	});	

	$("#nGlobalConfirmDialog").dialog({
			bgiframe: true,
			autoOpen: false,
			resizable: false,
			width: 400,
			height: 230,
			modal: true,
			buttons: {
				
				' Delete ': function() {
					var dpath=document.getElementById("dpath").value;
						document.location.href=dpath;
				},
				' Cancel ': function() {						
						$(this).dialog('close');						
				}
			},
			close: function() {}
	});
	
	$("#nGlobalSigninDialog").dialog({
			bgiframe: true,
			autoOpen: false,
			resizable: false,
			width: 400,
			height: 230,
			modal: true,
			buttons: {
				' Cancel ': function() {						
						$(this).dialog('close');						
				},
				' Sign In ': function() {	
						doSignin();
				}				
			},
			close: function() {}
	});
	$("#nGlobalPasswordReminderDialog").dialog({
			bgiframe: true,
			autoOpen: false,
			resizable: false,
			width: 400,
			height: 230,
			modal: true,
			buttons: {
				' Cancel ': function() {						
						$(this).dialog('close');						
				},
				' Send Password ': function() {
					if($("#emailreminder").val()!=''){
						var email=$("#emailreminder").val();
						validateEmail(email,1,1)
						if(rtnError=="ok"){
							doSendPassword(email);
						}
						else
						{
							$("#nGlobalErrorDialog").dialog( "option", "title", 'Error' );
							nucshowemsg2(rtnError);
						}
					}
					else
					{
						$("#nGlobalErrorDialog").dialog( "option", "title", 'Error' );
						nucshowemsg2("Please enter your email address.")
					}
				}				
			},
			close: function() {
				document.getElementById("emailreminder").value="";
				document.getElementById("emrtnmsg").innerHTML="";
				document.getElementById("emrtnmsg").style.display="none";
				}
	});	
	$("#nGlobalRegistrationDialog").dialog({
			bgiframe: true,
			autoOpen: false,
			resizable: false,
			width: 400,
			height: 730,
			modal: true,
			buttons: {
				' Cancel ': function() {						
						$(this).dialog('close');						
				},
				' Register ': function() {	
						doRegistration();
						//$(this).dialog('close');						
				}				
			},
			close: function() {}
	});
	$("#nRecipientNoteDialog").dialog({
			bgiframe: true,
			autoOpen: false,
			resizable: false,
			width: 400,
			height: 750,
			modal: true,
			buttons: {
				' Close ': function() {						
						$(this).dialog('close');						
				},
				' Save ': function() {	
						doSaveRecipientNote();
						//$(this).dialog('close');						
				}				
			},
			close: function() {}
	});	
});
function nucUpdateQtys(){
	
	
		$.ajax({
			type: "POST",
			url: "/products/pc/crec.asp",
			 data: $('#smallshoppingcart').formSerialize(),
			timeout: 6000,
			success: function(data, textStatus){
		
				if (data!="FAIL")
				{
					if(data=='ok'){
					updatesmallcartdisplay();
					$("#nGlobalAutoCloseDialog").dialog( "option", "title", 'Add To Cart' );
					$("#nGlobalAutoCloseMsg").html('<p class="autoclosemsg"><br><br>The quantity has been updated.</p>');
					$("#nGlobalAutoCloseDialog").dialog('open');					 
					 var t=setTimeout("$('#nGlobalAutoCloseDialog').dialog('close')",2000);
					}
				}
				else
				{
					$("#nGlobalErrorDialog").dialog( "option", "title", 'Quantity Update Error' );
					$("#nGlobalErrorMsg").html('<p>We apologize, but there was an error when updating your product quantity.  Please try again.  If this error persists, please click the View Cart link at the top of the page and adjust your quantity there.</p>');
					$("#nGlobalErrorDialog").dialog('open');
				}
			}
		});
		
	
	
}
function doSendPassword(email){
	document.getElementById("prajaxworkingmsg").style.display="block";
		$.ajax({
			type: "POST",
			url: "/products/pc/passwordreminder.asp?em="+email,
			 data:"",
			timeout: 6000,
			success: function(data, textStatus){
				document.getElementById("prajaxworkingmsg").style.display="none";
				if (data=="0")
				{
					document.getElementById("emrtnmsg").innerHTML="Your password has been successfully sent.";
					document.getElementById("emrtnmsg").style.display="block";
				}
				else
				{
					document.getElementById("emrtnmsg").innerHTML="There was an issue retrieving your password.  If you feel your email address was entered correctly, please call Customer Care at: 1-800-334-7428.";
					document.getElementById("emrtnmsg").style.display="block";

				}
			}
		});	
	
}

function doSignin (){
	var email=document.getElementById("dsemailaddress").value;
	var pass=document.getElementById("dspassword").value;
	var errormsg=document.getElementById("dserror");

	if (email.length>0 && pass.length>0){
		 validateEmail(email,1,1)
		 if(rtnError!="ok"){
			 errormsg.innerHTML=rtnError;
			 errormsg.style.display="block";
			 document.getElementById("nGlobalSigninDialog").style.height='180px';
			 return false;
		 }
		 else
		 {
			errormsg.innerHTML="";
			errormsg.style.display="none";
		 	$.ajax({
					type: "POST",
					url:  "/products/pc/ajaxdoSignin.asp?em="+email+"&pw="+pass,
					 data: "",
					timeout: 6000,
					success: function(data, textStatus){
						
						if (data!="FAIL")
						{
							if(data=="suspended"){
								errormsg.innerHTML="Your account has been suspended.  Please contact customer care.";
								errormsg.style.display="block";
								
								document.getElementById("nGlobalSigninDialog").style.height='180px'; 
								return false;								
							}
							var nameArray=data.split("!|!")
							var customerName=nameArray[0]+" "+nameArray[1];
							//$('#nGlobalSigninDialog').dialog('close');
							//$('#quickview').animate({ height: "30px" }, 600 );
							//$("#quickview").effect("highlight", { times: 3 }, 150); 
							document.location="/products/pc/custpref.asp";
						}
						else
						{
							document.getElementById("nGlobalSigninDialog").style.height='180px';
							errormsg.innerHTML="The email address and/or password did not match a current account.";
							errormsg.style.display="block";
						}
					}
				});
		 }
	}else{
		errormsg.innerHTML="Please provide an email address and password.";
		errormsg.style.display="block";
	}
}

  $(function() {
	// Options (defaults):
	 //zIndex:100,
	 //eventType:'click',
	// addOffset:10,
	// animation:'fade',
	// animationSpeed:250
	//$('.one').mouseyDialog();
	
	//$('.two').mouseyDialog({
	//  eventType:'click',
	//  animation:'slide',
	//  animationSpeed:100
	//});
  });


function globalregistration(redir){

	//alert(idcustomer);
	if(idcustomer==0 || idcustomer>9000000){
		$("#nGlobalRegistrationDialog").dialog('open');
		$.ajax({
			type: "POST",
			url: "/products/includes/forms/globalregistration.asp",
			timeout: 6000,
			dataType: "text",
			data:"",
			error: function(request,error) { 
				  if (request.status == "404") { 
				  $("#nGlobalRegistrationMsg").html('<p>We apologize, but there was a problem loading the registration form.  Please close this and try again.</p>');
				  }},
			success: function(data, textStatus){
				if (Right(data,17)=="<!-- complete -->")
				{
					$("#nGlobalRegistrationMsg").html(data);
					document.getElementById("redir").value=redir;
				}
				else
				{
					$("#nGlobalRegistrationDialog").dialog('close');
					$("#nGlobalErrorDialog").dialog( "option", "title", 'Registration Error' );
					$("#nGlobalErrorMsg").html('<p>We apologize, but there was a problem loading the registration form.  Please try again.</p>');
					$("#nGlobalErrorDialog").dialog('open');
				}
			}
		});		
		
	}
	else
	{
		$("#nGlobalErrorMsg").html("<p>You are currently logged in.  If you wish to create a new registration, please sign out first.</p>");
		$("#nGlobalErrorDialog").dialog('open');
		$("#nGlobalErrorDialog").dialog( "option", "title", 'Registration' );
	}
}

function doRegistration(){
	
	//if(regEmail+regpass1+regpass2+regfirstname+reglastname+regaddress+regcity+regstate+regzip+regcountry+regphone==11){
	if(regEmail+regpass1+regpass2==3){
		
		$.ajax({
			type: "POST",
			url: "/products/pc/ajaxdoregistration.asp",
			 data: $('#doRegistration').formSerialize(),
			timeout: 6000,
			success: function(data, textStatus){
				
				if (Left(data,4)!="FAIL")
				{
					if(data=='regsuccess'){
						
						//$("#nGlobalRegistrationDialog" ).dialog( "option", "buttons", { " Close ": function() { $(this).dialog("close"); } } );
						$("#doRegistration").clearForm();
						
						if(document.getElementById("redir").value=='checkout'){
							document.location.href="/products/pc/login.asp";
						}
						else
						{
							document.location.href="/products/pc/custpref.asp";
							$("#nGlobalRegistrationDialog").dialog('close');
							$("#nGlobalErrorDialog").dialog( "option", "title", 'Registration ' );
							$("#nGlobalErrorMsg").html('<h3>Registration Complete</h3><p>Thank you for registering with us.  For your convenience, you have also been signed in.</p>');
							$("#nGlobalErrorDialog").dialog('open');
							idcustomer=99999999999999;
							$("#globalsigninlink").html("<a href='/products/pc/custpref.asp'>Your Account</a> •");
						}
						document.location.href="/products/pc/custpref.asp";
					}
				}
				else
				{
					if(Left(data,14)=="FAIL:DupeEmail"){
						$("#nGlobalErrorDialog").dialog( "option", "title", 'Registration Error' );
						$("#nGlobalErrorMsg").html('<p>The email you\'ve entered is already in our system.  Please use another.</p>');
						$("#nGlobalErrorDialog").dialog('open');						

						return false;
					}
					else
					{
						//alert(data);
						$("#nGlobalErrorDialog").dialog( "option", "title", 'Registration Error' );
						$("#nGlobalErrorMsg").html('<p>We apologize, but there was an error when saving your registration information.  Please try again.  If this error persists, please conatact customer care.</p>');
						$("#nGlobalErrorDialog").dialog('open');
				}
				}
			}
		});
	}
	else
	{
		var errormsg=document.getElementById("dserrorreg");	
		resetErrorBox(1,"The registration form is missing some required information.")
		return false;
	}
	
	
}
	var regEmail=0;
	var regpass1=0;
	var regpass2=0;
	var regfirstname=0;
	var reglastname=0;
	var regaddress=0;
	var regcity=0;
	var regstate=0;
	var regzip=0;
	var regcountry=0;
	var regphone=0;
	
function checkit(fld){
	
	var errormsg=document.getElementById("dserrorreg");
	var curfld=document.forms['doRegistration'].elements[fld];
	var rok="/products/pc/images/sample/pc_icon_requiredok.gif";
	var rnot="/products/pc/images/sample/pc_icon_required.gif";
	
	if(fld=='pcCustomerEmail'){
		email=document.forms['doRegistration'].elements['pcCustomerEmail'].value;
		validateEmail(email,1,1)
		if(rtnError!='ok'){
			resetErrorBox(1,rtnError)
			document.getElementById("reqemail").src=rnot;
			regEmail=0;
		}
		else
		{
			document.getElementById("reqemail").src=rok;
			regEmail=1;
			resetErrorBox(0);
		}
	}
	else
	{
		switch (fld)
		{
		case "pcCustomerPassword":
			if(curfld.value==""){
				resetErrorBox(1,"Please provide a password.");
				document.getElementById("reqpassword1").src=rnot;
				regpass1=0;
			}else{
				if(curfld.value!=document.forms['doRegistration'].elements['pcCustomerConfirmPassword'].value && document.forms['doRegistration'].elements['pcCustomerConfirmPassword'].value!=""){
					resetErrorBox(1,"The two passwords you entered do not match.");
					document.getElementById("reqpassword1").src=rnot;
					document.getElementById("reqpassword2").src=rnot;
					regpass1=0;
					regpass2=0;
				}else{
					document.getElementById("reqpassword1").src=rok;
					regpass1=1;
					resetErrorBox(0);
					if(curfld.value==document.forms['doRegistration'].elements['pcCustomerConfirmPassword'].value && document.forms['doRegistration'].elements['pcCustomerConfirmPassword'].value!=""){
						document.getElementById("reqpassword1").src=rok;
						document.getElementById("reqpassword2").src=rok;
						regpass1=1;
						regpass2=1;
					}					
				}				
			}
		  break;
		case "pcCustomerConfirmPassword":
			if(curfld.value==""){
				resetErrorBox(1,"Please provide a confirmation password.");
				document.getElementById("reqpassword2").src=rnot;
				regpass2=0;
			}else{
				resetErrorBox(0);
				if(curfld.value!=document.forms['doRegistration'].elements['pcCustomerPassword'].value){
					resetErrorBox(1,"The two passwords you entered do not match.");
					document.getElementById("reqpassword1").src=rnot;
					document.getElementById("reqpassword2").src=rnot;
					regpass1=0;
					regpass2=0;
				}else{
					document.getElementById("reqpassword2").src=rok;
					resetErrorBox(0);
					if(curfld.value==document.forms['doRegistration'].elements['pcCustomerPassword'].value && document.forms['doRegistration'].elements['pcCustomerPassword'].value!=""){
						document.getElementById("reqpassword1").src=rok;
						document.getElementById("reqpassword2").src=rok;
						regpass1=1;
						regpass2=1;
					}
				}
			}		
		  break;
		case "pcBillingFirstName":
			if(curfld.value==""){
				resetErrorBox(1,"Please provide your first name.");
				document.getElementById("reqfirstname").src=rnot;
				regfirstname=0;
			}else{
				resetErrorBox(0);
				document.getElementById("reqfirstname").src=rok;
				regfirstname=1;
			}
		  break; 
		case "pcBillingLastName":
			if(curfld.value==""){
				resetErrorBox(1,"Please provide your last name.");
				document.getElementById("reqlastname").src=rnot;
				reglastname=0;
			}else{
				resetErrorBox(0);
				document.getElementById("reqlastname").src=rok;
				reglastname=1;
			}
		  break; 
		case "pcBillingAddress":
			if(curfld.value==""){
				resetErrorBox(1,"Please provide your billing address.");
				document.getElementById("reqaddress1").src=rnot;
				regaddress=0;
			}else{
				resetErrorBox(0);
				document.getElementById("reqaddress1").src=rok;
				regaddress=1;
			}
		  break;
		case "pcBillingCity":
			if(curfld.value==""){
				resetErrorBox(1,"Please provide your billing city.");
				document.getElementById("reqcity").src=rnot;
				regcity=0;
			}else{
				resetErrorBox(0);
				document.getElementById("reqcity").src=rok;
				regcity=1;
			}
		  break;		  
		case "pcBillingStateCode":
			if(curfld.value==""){
				resetErrorBox(1,"Please provide your billing state.");
				document.getElementById("reqstate").src=rnot;
				regstate=0;
			}else{
				resetErrorBox(0);
				document.getElementById("reqstate").src=rok;
				regstate=1;
			}
		  break;
		case "pcBillingPostalCode":
			if(curfld.value==""){
				resetErrorBox(1,"Please provide your billing postal/zip code.");
				document.getElementById("reqzip").src=rnot;
				regzip=0;
			}else{
				resetErrorBox(0);
				document.getElementById("reqzip").src=rok;
				regzip=1;
			}
		  break;		  
		case "pcBillingCountryCode":
			if(curfld.value==""){
				resetErrorBox(1,"Please provide your billing country.");
				document.getElementById("reqcountry").src=rnot;
				regcountry=0;
			}else{
				resetErrorBox(0);
				document.getElementById("reqcountry").src=rok;
				regcountry=1;
			}
		  break;		  
		case "pcBillingPhone":
			if(curfld.value=="" || IsNumeric(curfld.value)==false){
				resetErrorBox(1,"Please provide your billing phone.");
				document.getElementById("reqphone").src=rnot;
				regphone=0;
			}else{
				resetErrorBox(0);
				document.getElementById("reqphone").src=rok;
				regphone=1;
			}
		  break;		  
		}  
	}
}

function resetErrorBox(show,msg){
	var errormsg=document.getElementById("dserrorreg");
	if(show==0){
		errormsg.innerHTML="";
		errormsg.style.display="none";
	}
	else
	{
		errormsg.innerHTML=msg;
		errormsg.style.display="block";	
	}
}



function IsNumeric(sText)
{
   var ValidChars = "0123456789()-. ";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}





function validateEmail(addr,man,db) {
	rtnError='ok'
	if (addr == ''){rtnError='Please provide your email address.'; return false;}
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		  if (db) rtnError='Your email address contains invalid characters.';
		  return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
		  if (db) rtnError='Your email address contains non ascii characters.';
		  return false;
	   }
	}
	
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   if (db) rtnError='Your email address must contain an @.';
	   return false;
	}
	if (atPos == 0) {
	   if (db) rtnError='Your email address must not start with @.';
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   if (db) rtnError='Your email address must contain only one @.';
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   if (db) rtnError='Your email address must contain a period in the domain name.';
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   if (db) rtnError='A period must not immediately follow @ in your email address.';
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   if (db) rtnError='A period must not immediately precede @ in your email address.';
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	   if (db) rtnError='Two periods must not be adjacent in your email address.';
	   return false;
	}

	return rtnError;
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function (obj, fromIndex) {
		if (fromIndex == null) {
			fromIndex = 0;
		} else if (fromIndex < 0) {
			fromIndex = Math.max(0, this.length + fromIndex);
		}
		for (var i = fromIndex; i < this.length; i++) {
			if (this[i] === obj)
				return i;
		}
		return -1;
	};
}
Array.prototype.search = function(s,q){
  var len = this.length;
  for(var i=0; i<len; i++){
    if(this[i].constructor == Array){
      if(this[i].search(s,q)){
        return i;
		
        break;
      }
     } else {
       if(q){
         if(this[i].indexOf(s) != -1){
           return i;
		  
           break;
         }
      } else {
        if(this[i]==s){
		
          return i;
          break;
        }
      }
    }
  }
  return false;
}
	function checkSearch(){
		if(document.getElementById("mmsmallsearchbox").value.toLowerCase()=="product search"){
			document.getElementById("mmsmallsearchbox").value="";
			document.getElementById("mmsmallsearchbox").style.color="#000";
		}
	
	}
	function exitSearch(){
		if(document.getElementById("mmsmallsearchbox").value==""){
			document.getElementById("mmsmallsearchbox").value="Product Search";
			document.getElementById("mmsmallsearchbox").style.color="#ccc";
		}
	}
	function doSearch(){
		if(document.getElementById("mmsmallsearchbox").value.toLowerCase()=="product search"){
			return false;
		}
		else
		{
			return true;
		}
	}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

