//Copyright (c) 2010, Omnicognic LLC.
//All rights reserved.
//Written by Conrad Sollitt.
//Software License Agreement (BSD License)
//See full version of InfoFind Web Tools for comments and full license.

function ByID(ID) {
    var Element = ID;
    if (typeof (ID) == "string") {
        if (document.getElementById) {
            Element = document.getElementById(ID);
        } else if (document.all) {
            Element = document.all[ID];
        }
    }
    return Element;
}

function GetConnection() {
	if (typeof(XMLHttpRequest) != "undefined") {
		return new XMLHttpRequest();
	}
	var progIDs = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
	for (var i = 0; i < progIDs.length; i++)
	{
		try {
			var XmlHttp = new ActiveXObject(progIDs[i]);
			return XmlHttp;
		} catch(e) {
		}
	}
    return null;
}

function IsEmail(Value) {
    if (typeof (Value) == "string" && Value != "") {
        if (IsLike(Value.toLowerCase(), "[a-z0-9]*@[a-z]*.*[a-z]")) {
            var aData = Value.split("@");
            if (aData.length != 2) {
                return false;
            }
            return true;
        }
    }
    return false;
}

function IsLike(Text, Pattern, bHandleNewLines) {
    var reg = new RegExp(GetDbRegExpPattern(Pattern));
    if (bHandleNewLines) Text = Text.replace(/\n/g, "");
    return (reg.test(Text) ? true : false);
}
function GetDbRegExpPattern(Pattern) {
    Pattern = Pattern.replace(/\\/g, "\\\\");
    Pattern = Pattern.replace(/\$/g, "[$]");
    Pattern = Pattern.replace(/\./g, "[.]");
    Pattern = Pattern.replace(/\(/g, "\\(");
    Pattern = Pattern.replace(/\)/g, "\\)");
    Pattern = Pattern.replace(/\+/g, "[+]");
    Pattern = Pattern.replace(/\?/g, ".");
    Pattern = Pattern.replace(/\*/g, "%");
    Pattern = Pattern.replace(/%/g, "(.*)");
    Pattern = Pattern.replace(/#/g, "\\d");
    Pattern = Pattern.replace(/\[!/g, "[^");
    Pattern = "^" + Pattern + "$";
    return Pattern;
}

function SendEmail() {
    var Email = document.getElementById("txtEmail").value;
    if (Email == "") {
        alert("Please enter your email address.");
        return;
    }
    if (!IsEmail(Email)) {
        alert("Please enter a valid email address.");
        return;
    }
    var URL = "SubmitEmail.asp?Email=" + encodeURI(Email);
    var ErrorMsg = "An error has occurred, unable to submit your email. If you would like to contact Omnicognic you can do so through a Contact form from a link at the bottom of this page.";
    var Connection = GetConnection();
    if (!Connection) {
        alert(ErrorMsg + " Error 1");
	} else {
	    Connection.onreadystatechange = function() {
	        if (Connection.readyState == 4 && Connection.status == 200) {
	            if (Connection.responseText == "Success") {
	                alert("Thank you for your interest in InfoFind and Omnicognic! You will now be notified of updates to InfoFind and new products from Omnicognic. Please add the domain [@omnicognic.com] to your safe list.");
	                document.getElementById("txtEmail").value = "";
	            } else {
	                alert(ErrorMsg + " Error 2");
	            }
	        } else if (Connection.readyState == 4) {
    	        alert(ErrorMsg + " Error 3")
	        }
	    }
		Connection.open("GET", URL, true);
		Connection.send(null);
	}
}

function ShowInstallInstructions() {
    ByID("Div_Install").style.display = "";
    ByID("Install_Click_Here").style.display = "none";
}

