function externallink(){
	$("a[rel=external]")
	.attr("target", "_blank")
	.addClass("external"); // Add .external class for IE6 as it doesn't support attribute CSS selectors
}


$(document).ready(function(){

    $("#client_username").attr("class", "normal");
    $("#client_domain").attr("class", "normal");
    $("#client_password").attr("class", "normal");
    $("#clientlogin_radio_hosting").attr("checked", "checked");
    $("input[type=submit]").attr("disabled","");

    /* Navigation Animation
    ===================================================================================================*/

    $(".nav").superfish({
        pathClass : 'current',
        animation : {opacity:'show'},
        delay : 1000
     });

    /* XHTML Strict compliant target="_blank" replacement.
    ===================================================================================================*/
    $("a[rel=external]")
        .attr("target", "_blank")

    /* Javascript hero action - enable the lovely login box :D
    ===================================================================================================*/
    $("#loginbutton_a").click(function(){
        $(this).parent().fadeOut("fast");
        $("#clientlogin").fadeIn("fast");
        $("#clientlogin").fadeTo("fast", 0.90);
        $("#clientlogin_close > a").click(function() {
            $("#clientlogin").fadeOut("fast");
            $("#loginbutton").fadeIn("fast");
        });

        /* Event redirection (Login form)
        ==========================================*/
        $("#clientlogin_form").submit(function () {
            $("#client_username").attr("class", "normal");
            $("#client_domain").attr("class", "normal");
            $("#client_password").attr("class", "normal");
            if(!$("#client_username").attr("value")) {
                $("#client_username").attr("class", "error");
                alert("You didn't enter your username");
                return false;
            }
            if(!$("#client_password").attr("value")) {
                $("#client_password").attr("class", "error");
                alert("You didn't enter your password");
                return false;
            }
            if($("#clientlogin_radio_account").attr("checked")){
                $("#clientlogin_form").attr("action", "http://clients.ifusehosting.com/dologin.php?goto=clientarea");
                $("#client_username").attr("name", "username");
                $("#client_password").attr("name", "password");
            } else {

                /* Next stage of development: Ajax check the domain
                ==================================================*/
                if($("#clientlogin_radio_webmail").attr("checked")) {
                    $("#client_domain").attr("value", $("#client_username").attr("value"));
                }
                if(!$("#client_domain").attr("value")) {
                    $("#client_domain").attr("class", "error");
                    alert("You didn't enter a domain name for your hosting");
                    return false;
                }
                $("input[type=submit]").attr("disabled","disabled");
                var data = $.ajax({
                    url: "/ajax/domain_ours.php",
                    cache: false,
                    async: false,
                    data: "domain="+$("#client_domain").attr("value"),
                    type: "POST"
                }).responseText;
                if(data == 'not_ours') {
                    $("#client_domain").attr("class", "error");
                    $("input[type=submit]").attr("disabled","");
                    alert("Sorry, but it'd appear the domain you've entered is not hosted with us, please check and try again.");
                    return false;
                } else if(data == 'none_entered') {
                    $("#client_domain").attr("class", "error");
                    $("input[type=submit]").attr("disabled","");
                    alert("You didn't enter a domain name for your hosting");
                    return false;
                } else {
                    var aPosition = data.indexOf(":");
                    if(aPosition > 0) {
                        var sDomain = data.split(":", 2);
                        if(sDomain[0] == 'windows' || sDomain[0] == 'linux') {
                            //nothing really
                        } else {
                            $("#client_domain").attr("class", "error");
                            alert("Sorry there was an error processing your request, please try again in a few moments");
                            return false;
                        }
                    } else {
                        $("#client_domain").attr("class", "error");
                        alert("Sorry there was an error processing your request, please try again in a few moments");
                        return false;
                    }
                }
                if($("#clientlogin_radio_hosting").attr("checked")) {
                    if(sDomain[0] == 'windows') {
                        $("#clientlogin_form").attr("action", "https://"+sDomain[1]+":8443/login_up.php3");
                        $("#client_username").attr("name", "login_name");
                        $("#client_password").attr("name", "passwd");
                    } else {
                        $("#clientlogin_form").attr("action", "http://"+sDomain[1]+":2082/login/");
                        $("#client_username").attr("name", "user");
                        $("#client_password").attr("name", "pass");
                    }
                } else if($("#clientlogin_radio_reseller").attr("checked")) {
                    if(sDomain[0] == 'linux') {
                        $("#clientlogin_form").attr("action", "http://"+sDomain[1]+":2086/login/");
                        $("#client_username").attr("name", "user");
                        $("#client_password").attr("name", "pass");
                    } else {
                        $("#client_domain").attr("class", "error");
                        $("input[type=submit]").attr("disabled", "");
                        alert("You seem to have selected the wrong option - although we host this domain it appears to not be a reseller; Perhaps try logging in as a standard hosting account.");
                        return false;
                    }
                } else if($("#clientlogin_radio_webmail").attr("checked")) 
                    if(sDomain[0] == 'windows') {
                        $("#clientlogin_form").attr("action", "http://webmail."+sDomain[1]+"/Login.aspx");
                        $("#client_username").attr("name", 'ctl00\$MPH\$txtUserName');
                        $("#client_password").attr("name", 'ctl00\$MPH\$txtPassword');
                        $("#client_password").attr("name", 'ctl00\$MPH\$LanguageList');
                        $("#client_password").attr("value", 'en');
                    } else {
                        $("#clientlogin_form").attr("action", "http://"+sDomain[1]+":2095/login/");
                        $("#client_username").attr("name", "user");
                        $("#client_password").attr("name", "pass");
                    }{
                }
            }
            return true;
	});

        $("input[name=clientlogin_radio]").click(function() {
            if($(this).attr("value") == 'account' || $(this).attr("value") == 'E-Mail') { 
		$("#tr_domain").hide();
		$("#clientlogin").css("height", "220");
            } else {
		$("#tr_domain").show();
		$("#clientlogin").css("height", "230");
            }
        });
	return false;
    });
});
