function SubmitOnEnter(e){
	var keycode;
	if (window.event) 
		keycode = window.event.keyCode;
	else if (e) 
		keycode = e.which;
	else 
		return true;

	if (keycode == 13)
	{
		LoginUser_Unified();
		return false;
	}
	else
		return true;
}

function valName(e) {
    var k;
    document.all ? k = e.keyCode : k = e.which;
    //alert(k);
    if (k == 13)
        createSite();
    
    return (k > 47 && k < 58  || k > 64 && k < 91  || k > 96 && k < 123  ||  k == 45 || k == 8);
}


function valLogin(e) {
    var k;
    document.all ? k = e.keyCode : k = e.which;
    //alert(k);
    if (k == 13)
        LoginUser();

    return true;
}

function blankField(obj) {
    obj.style.backgroundImage = "";
}

function createSite() {
    
    var SiteDomain = document.getElementById("SiteDomain").value;
    if (SiteDomain != "") {
        ajaxVerifyDomain(SiteDomain);
    }
    else {
        document.getElementById("SiteNotification").innerHTML = "Please name your mobile site.";
        setTimeout("clearError()", 2000);
    }

}

function DisplayError(ErrorMessage) {

    document.getElementById("NotificationWrapper").style.display = "";

}

function CloseError() {
    document.getElementById("NotificationWrapper").style.display = "none";
}


function ajaxVerifyDomain(SiteDomain) {

    url = "/birddog/LoginXML.aspx?SiteDomain=" + SiteDomain;
    //alert(url);
   
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }
        }
    }

    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    http_request.onreadystatechange = alertVerifyDomain;
    http_request.open('GET', url, true);
    http_request.send(null);


}

function clearError() {
    document.getElementById("SiteNotification").innerHTML = "&nbsp;";
}


function alertVerifyDomain() {
    switch (http_request.readyState) {
        //Loading 
        case 1:
            try {
                
            } catch (e) { }
            break;
        //Loaded 
        case 2:
            break;
        //Interactive                 
        case 3:
            break;
        //Completetion
        case 4:
            if (http_request.status == 200) {
                var xmldoc = http_request.responseXML;
                var root_node = xmldoc.getElementsByTagName('valid').item(0);
                var valid = root_node.firstChild.data;


                if (valid == "false") {

                    document.getElementById("SiteNotification").innerHTML = "A site with this name already exists.";
                    setTimeout("clearError()", 2000);

                }
                else {
                    document.getElementById("CreateSite").submit();
                }

            }
            else {
                //alert(http_request.status);
                //alert('There was a problem with the request.');
                try {
                    // self.WebpagePreviewFrame.document.getElementById("PageContent5122009").innerHTML = 'Could Not Find Address';
                } catch (e) { }
            }
    }

}

function LoginUser() {

    ajaxLogin(document.getElementById("BDUserID").value, document.getElementById("BDPassword").value);
}

function LoginUser_Unified() {

    ajaxLogin_Unified(document.getElementById("login_name").value, document.getElementById("login_password").value);
}

function LoginDemoUser() {

   ajaxLogin_Unified('cfm', 'cfm');
}

function ajaxLogin_Unified(UserName,Password) {

	var params = "user_name=" + UserName + "&password=" + Password;
	
	//COMMENT here when copying to main!
   // url = "http://www.coastalflow.com/birddog/UnifiedLogin.aspx?" + params;
	
	//AND UNCOMMENT THIS when copying to main!
	url = "https://www.coastalflow.com/birddog/UnifiedLogin.aspx?" + params;
   // alert(url);
   // alert(UserName);

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }
        }
    }

    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    http_request.onreadystatechange = alertAjaxLogin;
    http_request.open('GET', url, true);
    http_request.send();


}

function ajaxLogin(UserName,Password) {

    url = "http://www.coastalflow.com/birddog/LoginXML.aspx?user_name=" + UserName + "&password=" + Password;
   // alert(url);
   // alert(UserName);

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }
        }
    }

    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    http_request.onreadystatechange = alertAjaxLogin;
    http_request.open('GET', url, true);
    http_request.send(null);


}



function alertAjaxLogin() {

    switch (http_request.readyState) {
        //Loading  
        case 1:
            try {

            } catch (e) { }
            break;
        //Loaded  
        case 2:
            break;
        //Interactive                  
        case 3:
            break;
        //Completetion
        case 4:
            if (http_request.status == 200) {
                var xmldoc = http_request.responseXML;
                var root_node = xmldoc.getElementsByTagName('valid').item(0);
				var loc_node = xmldoc.getElementsByTagName('location').item(0);
                
             
                
                var valid = root_node.firstChild.data;


                if (valid == "false") {

                    //document.getElementById("SiteNotification").innerHTML = "A site with this name already exists.";
                    alert("Invalid Login");
                   // setTimeout("clearError()", 2000);


                }
                else {
					var loc = loc_node.firstChild.data;
                    window.location = loc;
                    //alert(document.getElementById("CrossVerify").status);
                    //alert(document.CrossVerify.status.lo);
                    //window.location = "http://contentmobi.com/admin/dashboard";
                }

            }
            else {
                //alert(http_request.status);
                //alert('There was a problem with the request.');
                try {
                    // self.WebpagePreviewFrame.document.getElementById("PageContent5122009").innerHTML = 'Could Not Find Address';
                } catch (e) { }
            }
    }

}
