﻿// JScript File
var liteweb = {
	vroot: "",
	_pageInit: [],
	_SearchMsg: "Search...",
	_Newsletter: "Email address here...",
	AppendInit: function(obj) {
		liteweb._pageInit.push(obj);
	},
	init: function(vroot) {
		liteweb._initSiteSearch();
		liteweb._initNewsletter();
		liteweb.vroot = vroot;
		liteweb._init();
	},
	_init: function() {
		for (var i = 0; i < liteweb._pageInit.length; i++)
			if ("function" == typeof liteweb._pageInit[i])
			liteweb._pageInit[i]();
	},
	CheckSiteSearch: function(f, ac) {

		if ("focus" == ac && f.value.Trim() == liteweb._SearchMsg) {
			f.value = '';
			f.style.color = f.oldColor;
		}
		if ("blur" == ac && f.value.Trim() == '') {
			f.oldColor = f.style.color;
			f.value = liteweb._SearchMsg;
			f.style.color = "#361502";
		}
	},
	CheckNewsletter: function(f, ac) {
		if ("focus" == ac && f.value.Trim() == liteweb._Newsletter) {
			f.value = '';
			f.style.color = f.oldColor;
		}
		if ("blur" == ac && f.value.Trim() == '') {
			f.oldColor = f.style.color;
			f.value = liteweb._Newsletter;
			f.style.color = "#a0a0a0";
		}
	},
	_initSiteSearch: function() {

		var sq = $("input[name=q]");
		if (sq.length > 0) {
			sq.bind("focus", function() {
				liteweb.CheckSiteSearch(this, 'focus');
			});
			sq.bind("blur", function() {
				liteweb.CheckSiteSearch(this, 'blur');
			});
			//sq[0].value = "";
			liteweb.CheckSiteSearch(sq[0], 'blur');
			var sf = document.forms["SearchForm"];
			sf.onsubmit = function() {
				var err = sq[0].value.Trim() == liteweb._SearchMsg ||
				sq[0].value.Trim() == "";
				if (err) {
					alert("Please enter your search querry!");
					sq[0].focus();
					return false;
				}
			}
		}
	},
	_initNewsletter: function() {
		var sq = $("input[id=newsletter-field]");
		if(sq.length == 0)
			return;
		sq.bind("focus", function() {
			liteweb.CheckNewsletter(this, 'focus');
		});
		sq.bind("blur", function() {
			liteweb.CheckNewsletter(this, 'blur');
		});
		sq.bind("keyup", function(e){
			if(e.keyCode === 13)
				liteweb.SubmitNewsletter();
		});
		liteweb.CheckNewsletter(sq[0], 'blur');
		$("#newsletter-submit").click(liteweb.SubmitNewsletter);
	},
	SubmitNewsletter: function(){
		var sq = $("input[id=newsletter-field]");
		var err = sq[0].value.Trim() == liteweb._Newsletter ||
				sq[0].value.Trim() == "";
		if (err) {
			alert("Please enter your email address!");
			sq[0].focus();
			return false;
		}
		err = liteweb_Val.ValEmail(sq[0].value);
		if (!err) {
			alert("Please enter a correct email address!");
			sq[0].focus();
			return false;
		}
		var url = "http://www.sabis.net/newsletter.ashx";
		var data = {
			action: "create",
			"email": sq[0].value.Trim()
		};
		$.ajax({
			url: url,
			data: data,
			type: "GET",
			sucess: function(d){
				if(d.responseText == "success"){
					alert("Your email address was successfully added to the SABIS® Newsletter...");
					sq[0].value = "";
					return;
				}
				alert(d.responseText);
			},
			error: function(d){
				alert(d.responseText);
			},
			complete: function(d){
				if(d.responseText == "success"){
					alert("Your email address was successfully added to the SABIS® Newsletter...");
					sq[0].value = "";
					return;
				}
				alert(d.responseText);
			}
		});
	}
};
var lw = {};
liteweb.AppendInit(function() {
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-6104796-12']);
	_gaq.push(['_trackPageview']);

	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
	})();
});
