﻿var SearchBox= {
	
		DoSearch: function (){
		
			
			var SearchTextValue = $("[id*='QryInput']").val();
			if (SearchTextValue != "Search this site or for a domain")
			{
			if (SearchBox.DomainNameChecker(SearchTextValue))
			{
				var MainDomain = "";
				var sTLD = "";
				var Mainregex = new RegExp(/([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}/);
				var mainresults = Mainregex.exec(SearchTextValue)		
				var regex = new RegExp(/\.[a-zA-Z]{2,6}(\.[a-zA-Z]{2,6})*/);
				var results = regex.exec(SearchTextValue)

				if (mainresults.length > 0)
				{
					MainDomain = mainresults[0];
					MainDomain = MainDomain.replace(results[0], "");
					sTLD = results[0].substring(1);
				}
			
				window.location = '/home/domains/?Action=CheckDomain&Domain=' + MainDomain + '&TLD=' + sTLD;
			}
			else
			{
				window.location = '/search-results/?Qry=' + SearchTextValue;
			}
			}
			
		},	
		
		
		DomainNameChecker: function(domainvalue) {

        var filter = /([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}/;

        if (filter.test(domainvalue)) {
			return true;
        }
        else {
            return false;
        }		

    }
	
};

var Range = {
    ///<summary>
    /// Does all home range functions
    ///</summary> 
    Home: {
        Redirect: function (sAppPath) {
            Range.SetCookie("ProductRange", "page", "home", 7);

            if (sAppPath.indexOf('adsl') != -1){
                Range.SetSubCookie("ProductSubRange", "page", "ADSL", 7);
            }
            if (sAppPath != "") {
                document.location = sAppPath;
            } else {
                document.location = "/";
            }
        }
    },

    ///<summary>
    /// Does all the business range functions
    ///</summary>
    Business: {
        Redirect: function (sAppPath) {
            Range.SetCookie("ProductRange", "page", "business", 7);
            document.location = "/business";

        }
    },

    ///<summary>
    /// Does all the business range functions
    ///</summary>
    Reseller: {
        Redirect: function (sAppPath) {
            Range.SetCookie("ProductRange", "page", "reseller", 7);
            document.location = "/reseller";
            
        }
    },

    SetCookie: function (cookieName, cookieKey, cookieValue, nDays) {
        var today = new Date();
        var expire = new Date();
        if (nDays == null || nDays == 0) {
            nDays = 1;
        }
        expire.setTime(today.getTime() + 3600000 * 24 * nDays);
        document.cookie = cookieName + "=" + escape(cookieKey) + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString() + ";path=/";
    },

    SetSubCookie: function (cookieName, cookieKey, cookieValue, nDays) {
        var today = new Date();
        var expire = new Date();
        if (nDays == null || nDays == 0) {
            nDays = 1;
        }
        expire.setTime(today.getTime() + 3600000 * 24 * nDays);
        document.cookie = cookieName + "=" + escape(cookieKey) + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString() + ";path=/";
    }
}

var SubRange = {
    ///<summary>
    /// Does all home range functions
    ///</summary>
    SetSubProductRangeCookie: function(subProductName)
    {
        SubRange.SetCookie("ProductSubRange", "page", subProductName, 1);
    },
    ADSL: {
        Redirect: function (sAppPath) {
            if (sAppPath.indexOf('ADSL') != -1){
                SubRange.SetCookie("ProductSubRange", "page", "ADSL", 1);
            }
            /*if (sAppPath != "") {
                document.location = sAppPath;
            } else {
                document.location = "/";
            }*/
        }
    },

    ///<summary>
    /// Does all the business range functions
    ///</summary>

    SetCookie: function (cookieName, cookieKey, cookieValue, nDays) {
        var today = new Date();
        var expire = new Date();
        if (nDays == null || nDays == 0) {
            nDays = 1;
        }
        expire.setTime(today.getTime() + 3600000 * 24 * nDays);
        document.cookie = cookieName + "=" + escape(cookieKey) + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString() + ";path=/";
    }   
}

$(document).ready(function () {

    
    //$("[id*='QryInput']")
    // Define what happens when the textbox comes under focus
    // Remove the watermark class and clear the box
    $("[id*='QryInput']").focus(function () {

        $(this).filter(function () {

            // We only want this to apply if there's not
            // something actually entered
            return $(this).val() == "" || $(this).val() == "Search this site or for a domain"

        }).val("");

    });

    // Define what happens when the textbox loses focus
    // Add the watermark class and default text
    $("[id*='QryInput']").blur(function () {

        $(this).filter(function () {

            // We only want this to apply if there's not
            // something actually entered
            return $(this).val() == ""

        }).addClass("Search_Textbox").val("Search this site or for a domain");

    });

    $(function () {
        var loginCode = $('#Username');
        var loginPassword = $('#Password');
        var DomainCheck = $('#txtHostName');
        var SearchBox_Domain = $("[id*='QryInput']");
        var LoginPageUsername = $("[id*='txtUsername']");
        var LoginPagePassword = $("[id*='txtPassword']");
        var code = null;

        loginCode.keypress(function (e) {
            code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) {
                e.preventDefault();
                LoginManager.Submit_Click(e)
            }

        });

        loginPassword.keypress(function (e) {
            code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) {
                e.preventDefault();
                LoginManager.Submit_Click(e)
            }

        });

        DomainCheck.keypress(function (e) {
            code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) {
                e.preventDefault();
                CheckDomainAvailability();
            }

        });
		
		
	   SearchBox_Domain.keypress(function (e) {
            code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) {
                e.preventDefault();
                SearchBox.DoSearch();
            }

        });

        LoginPageUsername.keypress(function (e)
        {
            code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13)
            {
                e.preventDefault();

                if (CSALoginPage.ValidateLoginForm()) //UserControl/WALoginControl/scripts - Make sure that the form is validated before a postback is made
                {
                    $("[id*='btnSubmitLogin']").click();
                };
            }
        });

		LoginPagePassword.keypress(function (e) 
        {
            code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) 
            {
                e.preventDefault();

				if (CSALoginPage.ValidateLoginForm()) //UserControl/WALoginControl/scripts - Make sure that the form is validated before a postback is made
				{
					$("[id*='btnSubmitLogin']").click();
				};
            }
        });
});

///<summary>
/// Updates all external links with target _blank
$("a[href^='http:']").not("[href*='webafrica.co.za']").attr('target', '_blank');
///</summary>


}); 

var ObjectStore = {
    register: function (object) {
        if (!ObjectStore.__items) ObjectStore.__items = [];
        var result = ObjectStore.__items.length;
        ObjectStore.__items[result] = object;
        return result;
    },
    find: function (index) { return ObjectStore.__items[index]; }
}

var TrackableObject = $.Class.create({
    initialize: function () {
        this.__index = ObjectStore.register(this);
    }
});

var BrowserFunctions = {
    getInternetExplorerVersion: function () {
        // Returns the version of Internet Explorer or a -1
        // (indicating the use of another browser).
        var rv = -1; // Return value assumes failure.
        if (navigator.appName == 'Microsoft Internet Explorer') {
            var ua = navigator.userAgent;
            var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.exec(ua) != null)
                rv = parseFloat(RegExp.$1);
        }
        return rv;
    }
};


var FadeRotater = $.Class.create(TrackableObject, {
    initialize: function (element, interval) {
        this.parent(arguments.callee, arguments);
        //$super();
        this.__element = $("#" + element)[0];
        this.__started = false;
        this.__interval = interval;
        this.__isIE6 = (BrowserFunctions.getInternetExplorerVersion() < 7.0 && BrowserFunctions.getInternetExplorerVersion() != -1);
    },
    __doSetItem: function () {
        this.setItem();
        this.__endFade();
    },
    start: function () {
        if (!this.__started) {

            this.__started = true;
            this.__startFade();
        }
    },
    stop: function () { if (this.__started) this.__started = false; },
    __startFade: function () {
        if (this.__isIE6) {
            this.setItem();
            this.__loop();
        }
        else {
            var currentscope = this;
            $(currentscope.__element).fadeOut(500, function () { currentscope.__doSetItem(); });
            //Effect.toggle(this.__element, "appear", { afterFinish: this.__doSetItem.bind(this) });
        }
    },
    __endFade: function () {
        var currentscope = this;
        $(currentscope.__element).fadeIn(500, function () { currentscope.__loop(); });
        //Effect.toggle(this.__element, "appear", { afterFinish: this.__loop.bind(this) });
    },
    __loop: function () { setTimeout("ObjectStore.find(" + this.__index + ").__startFade()", this.__interval); }
});

var Net = {
	GetID: function (controlID) {
		return $("[id*=" + controlID + "]");
	}, GetName: function (controlName) {
		return $("[name*=" + controlName + "]");
	}, ValidationOn: function (validationControlID, enable) {
		ValidatorEnable(Net.GetID(validationControlID)[0], enable);
	}
};
