// JavaScript Document
var WebUtil = {};

WebUtil.initJquery = function() {
	$(document).ready(function(event) {
	
		Webzone.initTooltips();
		
		$(".numericfield").keyup(function(event) {
			this.value = this.value.replace(/[^0-9]+/ig, '');
		});
		
		$(".amountfield, .moneyfield").keyup(function(event) {
			this.value = this.value.replace(/[^0-9.]+/ig, '');
		});
		
		$(".showhidediv", $(".sessionhistoryheader")).click(function(event) {
			//$(this).val($(this).val() == ' + ' ? ' - ' : ' + ');
			$(this).attr('src', $(this).attr('src') == '/vwz-files/image/icon/add.png' ? '/vwz-files/image/icon/del.png' : '/vwz-files/image/icon/add.png');
			$(this).closest("h4").next().toggle('normal');
		}).closest("h4").next().show();
		
		/*
		$(".showhidediv", $(".sessionhistoryheader")).click(function(event) {
			//$(this).val($(this).val() == ' + ' ? ' - ' : ' + ');
			$(this).attr('src', $(this).attr('src') == '/vwz-files/image/icon/add.png' ? '/vwz-files/image/icon/del.png' : '/vwz-files/image/icon/add.png');
			$(this).closest("h4").next().toggle('normal');
		}).closest("h4").next().show();
		*/
		
		$(".showhidediv", $(".clientareahomedivheader")).click(function(event) {
			//$(this).val($(this).val() == ' + ' ? ' - ' : ' + ');
			$(this).attr('src', $(this).attr('src') == '/vwz-files/image/icon/add.png' ? '/vwz-files/image/icon/del.png' : '/vwz-files/image/icon/add.png');
			$(this).closest("h3").next().toggle();
		}).closest("h3").next().show();
		
		if($(".datepickerfld").length > 0) {
			$(".datepickerfld").datepicker({
				dateFormat : 'yy-mm-dd'
			});
		}
		
		// Related products in viewcart.tpl
		if($(".cartinterest").length > 0) {
			var date = new Date();
			var url = '/vwz-files/webzone/product.php?ts=' + date.getTime() + '&getrelatedcart=1';
			jQuery.get(url, {}, function(response) {
				//alert(url);
				response = response.replace(/\[READMORE\][\s\S]*\[\/READMORE\]/ig, '');
				$(".cartinterest").html(response);
				Webzone.renderGradientButton(".cartinterest .btnSubmitGreen", "btngreen");
				Webzone.renderGradientButton(".cartinterest .btnSubmitBlue", "btnblue");
			});
		}
		
		if($("#productssidelinkscell").length > 0) {
			$("#productssidelinkscell").html($("#productssidelinksinit").html());
			$("#productssidelinksinit").html('');
		}
		
		if($("select[name='customfield[4]']", "#registerclientform").length > 0) {
			var salesfloor = $("select[name='customfield[4]']", "#registerclientform");
			var getargs = GabeUtil.getArgs();
			$(salesfloor).closest("tr").hide();
			if(getargs.salesfloor) {
				var sfloor = getargs.salesfloor;
				if($("option[value='" + sfloor + "']", $(salesfloor)).length < 1) {
					alert("The sales floor registration link is invalid.");	
				} else {
					$("option[value='" + sfloor + "']", $(salesfloor)).attr('selected', 'selected');
					$("#referredbyfld").html("<strong>" + sfloor + "</strong>");
					$("#referredbyfld").closest("tr").show();
				}
				//alert($(salesfloor).val());
			}
			
		}
		
		if($("select[name='customfield[4]']", "#clientdetailsform").length > 0) {
			var salesfloor = $("select[name='customfield[4]']", "#clientdetailsform");
			$(salesfloor).closest("tr").hide();
		}
		$("li.tnlink").mouseover(function(event) {
			$(".slideDownLinksDiv", $(this)).show('normal');							  
		});
		$("li.tnlink").mouseout(function(event) {
			$(".slideDownLinksDiv", $(this)).hide('normal');					  
		});
		$(".slideDownLinksDiv").hide();
		
		$("#topnavlinks li").mouseover(function(event) {
			//alert($(".hiddensubnav", $(this)).length);
			if($(".hiddensubnav", $(this)).length < 1) {
				$("#subnavlinksmain").html('');
			}
		});
		
		$(".hiddensubnav").parent().mouseover(function(event) {
			//$(".hiddensubnav", $(this)).css('display', 'block');
			//alert($(".hiddensubnav", $(this)).html());
			$("#subnavlinksmain").html('<table border="0" style="margin:0px auto;margin-bottom:5px;"><tr><td>' + $(".hiddensubnav", $(this)).html() + '</td></tr></table>');
			$("#subnavlinksmain").css('display', 'block');
		});
		
		$(".hiddensubnav").parent().mouseout(function(event) {
			
		});
		
		$("#subnavlinksmain").mouseout(function(event) {
			if($(event.relatedTarget).closest("#subnavlinksmain").length < 1) {
				$(this).css('display', 'none');	
			}									
		});
		
		Webzone.renderGradientButton(".btnSubmitGreen", "btngreen");
		Webzone.renderGradientButton(".btnSubmitBlue", "btnblue");
		Webzone.renderGradientButton(".btnSubmitRed", "btnred");
		Webzone.renderGradientButton(".btnSubmitGray", "btngray");
		Webzone.renderGradientButton(".btnSubmitBlack", "btnblack");
		Webzone.renderGradientButton(".bottomcartbutton input[type='button']", "btnblue");
		Webzone.renderGradientButton(".btnSubmitBlueSmall", "btnbluesm");
		
		Webzone.renderPrice(".priceinfo");
		
		jQuery.each($("input[type='text'], input[type='password']"), function(i, val) {
			Webzone.renderGradientField(val);
		});
		
		jQuery.each($("input.webz-domainsearchfield"), function(i, val) {
			$(val).parent().prev().attr('src', '/vwz-files/image/txtinputbig-l.png');
			$(val).parent().next().attr('src', '/vwz-files/image/txtinputbig-r.png');
			$(val).parent().addClass('webz-inputtextbig-c');
			$(val).css('width', '450px');
		});
		
		jQuery.each($("select"), function(i, val) {
			$(val).addClass('webz-inputselect');
		});
		
		$(".priceinfo").show();
		
		jQuery.each($(".proddescription, .cartbox"), function(i, val) {
			$(val).html($(val).html().replace(/\[READMORE\]([\s\S]+?)\[\/READMORE\]/ig, "<p><a href='#' class='pdreadmorelink'>Read More &raquo;</a><div class='pdreadmore'>$1</div></p>"));
		});
		
		$(".pdreadmorelink").click(function(event) {
			$(this).closest("p").next().toggle('normal');
			event.preventDefault();
			
		});
		
		//alert(window.location.pathname + window.location.search);
		$("#productssidelinkscell li a[href='http://www.websitelogin.org" + window.location.pathname + window.location.search + "']").css("color", "#000000").parent().css("background", "url(/vwz-files/image/prodsidelinks-y.png) no-repeat");
		
		$("#productssidelinkscell li a[href='" + window.location.pathname + window.location.search + "']").css("color", "#000000").parent().css("background", "url(/vwz-files/image/prodsidelinks-y.png) no-repeat");
		//alert($("li a[href='" + window.location.pathname + window.location.search + "']").length);
		//alert($("#productssidelinkscell").html());
		
		
		// Domain product configuration 
		$("input[type='radio'][name='domainoption']").click(function(event) {
			var thisval = $(this).val();
			if(thisval != 'register') {
				$("#webz-domainsearchfieldresult").html('');
				$("#webz-domainsearchfield").val('');
				$(".continuehostingorderbutton").show();
				$(".domainavailabilityresults").show();
			} else {
				$(".domainavailabilityresults").hide();
				//$(".continuehostingorderbutton").hide();		
			}
		});
		
		if($("input[type='radio'][name='domainoption'][value='register']").attr('checked') == true) {
			$(".domainavailabilityresults").hide();
			//$(".continuehostingorderbutton").hide();
		}
		
		
		
		// Register domain search field
		if($("#webz-domainsearchfield").length > 0) {
			var domoptions = {};
			// Assigning domain to a hosting product so hide "Click to Continue" button also
			if($("#domainsearchnoform").length > 0) {
				domoptions.noform = 1;	
				domoptions.successCallback = function(domresponse) {
					$(".domainavailabilityresults").hide();
					//$(".continuehostingorderbutton").hide();
					$(".domainsearchresult-table td input[type='image']").click(function(event) {
						event.preventDefault();
						var chosendomain = $("input[name='domains[]']", $(this).closest("td")).val();
						jQuery.each($(".domainsearchresult-table td input[name='domains[]']"), function(i, val) {
							if($(val).val() != chosendomain) {
								$(val).closest("td").html('.');
							}
						});
						var thisform = $(this).closest("form");
						thisform.submit();
					});
				}
			} else {
				// Coming from Register a Domain page
				domoptions.successCallback = function(domresponse) {
					$(".adddomaintocartbutton").show();
				}
			}
			Webzone.registerCheckDomain(document.getElementById('webz-domainsearchfield'), document.getElementById('webz-domainsearchfieldresult'), domoptions);
		}
		
		// GET SHOPPIG CART TOTAL
		var url = '/cart.php?a=view';
		jQuery.get(url, function(response) {
			if(response) {
				var totalcart = response.match(/Total Due Today\:[^$]+(\$[0-9]+\.[0-9]+)/i);
				if(totalcart) {
					totalcart = totalcart[1];
					$(".carttotalamount").html(totalcart);
					$(".carttotalamount").css('z-index', 100);
					if(totalcart == '$0.00' || totalcart == '' || !totalcart) {
						if($("#topCartDiv").length > 0) {
							$("#topCartDiv .cartinfo").html("No items in your cart.");
						}
					}
					Webzone.renderPrice(".carttotalamount.ontheside");
					//alert($(".cartsidetotal").html());
				}
			}
		});
		
	});	
}