﻿var oHttp = new XMLHttpRequest();

var emailDiv;
var linkDiv;
var infoDiv;
var capchaDiv;
var imgNum;

var anchor;
var email;
var link;
var cat;
var capchaInput;
var toolTip;

var mousePosX;      // mouse position X
var mousePosY;      // mouse position Y
// http://devoracles.com/the-best-method-to-check-for-internet-explorer-in-javascript
var IE = /*@cc_on!@*/false;

function init() {
        document.getElementById("goButton").onclick = Validate;
        document.getElementById("anchorHelp").onclick = AnchorMsg;
        document.getElementById("anchorHelp").onmouseout = HideToolTip;
        document.getElementById("categoryHelp").onclick = CategoryMsg;
        document.getElementById("categoryHelp").onmouseout = HideToolTip;
        document.onmousemove = getMousePosXY;

        emailDiv = document.getElementById("emailErrorMsg");
        linkDiv = document.getElementById("linkErrorMsg");
        infoDiv = document.getElementById("infoMsg");
        capchaDiv = document.getElementById("capchaErrorMsg");
        toolTip = document.getElementById("helpToolTip");

        Captcha();
    
}

function Validate() {
    email = document.getElementById("EmailTextBox");
    link = document.getElementById("LinkTextBox");
    anchor = document.getElementById("anchorDropDownList");
    cat = document.getElementById("categoryDropDownList");
    capchaInput = document.getElementById("capcha");
    var url = "DefaultHandler.ashx?";
    url += "email=" + email.value + "&link=" + link.value + "&anchor=" + anchor.selectedIndex + "&cat=" + cat.selectedIndex
            + "&imgNumber=" + imgNum + "&capcha=" + capchaInput.value;

    ClearDivs();
    
    oHttp = new XMLHttpRequest();
    oHttp.onreadystatechange = SetResp;
    oHttp.open("GET", url, true);
    oHttp.send();

    infoDiv.innerHTML = "אנא המתן...";
}

function Captcha() {
    imgNum = Math.floor(Math.random()*43);
    var captchaImg = document.getElementById("captchaImg");
    var img = "<img width='200px' src='images/" + imgNum + ".jpg' />"
              + "<span><img id='capchaHelp' src='images/question_mark.png' style='width:16px; padding-bottom:10px' /></span>";
    captchaImg.innerHTML = img;

    document.getElementById("capchaHelp").onclick = CapchaMsg;
    document.getElementById("capchaHelp").onmouseout = HideToolTip;
}

function SetResp() {
    var ready = oHttp.readyState;
    if (ready == 4) {
        var data = oHttp.responseText;
        var result = eval("[" + data + "]");

        var infoMsg = "";
        var contact = false;

        if (result[0] == 1) {
            emailDiv.innerHTML = "כתובת דואר אלקטרוני לא חוקית";
        }
        else {
            emailDiv.innerHTML = "";
        }

        if (result[1] == 1) {
            linkDiv.innerHTML = "כתובת הקישור לא חוקית";
            capchaInput.value = "";
        }
        else {
            linkDiv.innerHTML = "";
        }

        if (result[2] == 1) {
            capchaDiv.innerHTML = "הצירוף שהיקשת אינו נכון";
            capchaInput.value = "";
        }
        else {
            capchaDiv.innerHTML = "";
        }

        if (result[3] == 1) {
            infoMsg = "הפייג' ראנק של האתר אינו עומד בקריטריון<br>";
            contact = true;
        }

        if (result[4] == 1) {
            infoMsg += "תוכן האתר אינו ראוי<br>";
            contact = true;
        }
        
        // result[5] is the Msg from DataBase not the other checks
        if (result[4] == 2 || result[5] == 1) {
            infoMsg = "אירעה תקלה. אנא נסה שוב מאוחר יותר";
            contact = false;
        }

        if (contact) {
            infoMsg += "אנא עיין <a href='Takanon.aspx'>בתקנון</a><br>";
            infoMsg += "אם לדעתך חלה טעות, אנא <a href='mailto:contact@tenswebmarketing.com'> צור איתנו קשר</a>";
        }

        infoDiv.innerHTML = infoMsg;

        if (result[0] == 1 || result[1] == 1 || result[2] == 1 || result[3] == 1 || result[4] != 0 || result[5] == 1) {
            if (IE) {
                Captcha();
            }
        }

        if (result[0] == 0 && result[1] == 0 && result[2] == 0 && result[3] == 0 && result[4] == 0) {
            var i = anchor.selectedIndex;
            document.getElementById("ourLink").value = "<a href='http://www.TENSLINK.com'>" + anchor.options[i].text + "</a>";
            document.getElementById("osxBtn").click();

            email.value = "";
            link.value = "http://";
            capchaInput.value = "";
            if (IE) {
                Captcha();
            }

            infoDiv.innerHTML = "הרישום להגרלה עבר בהצלחה";
        }
    }
}

function AnchorMsg() {
    toolTip.innerHTML = "Anchor Text  בו תשתמש באתר שלך כדי לקשר אלינו";
    PositionToolTip();    
}

function CategoryMsg() {
    toolTip.innerHTML = "ציין מאיזו קטגוריה תרצה שנקשר אליך אם תזכה";
    PositionToolTip();
}

function CapchaMsg() {
    toolTip.innerHTML = "הקש את צירוף האותיות ו/או המספרים שבתמונה";
    PositionToolTip();
}

function HideToolTip() {
    toolTip.style.display = "none";
}

function PositionToolTip() {
    toolTip.style.left = mousePosX;
    toolTip.style.top = mousePosY;
    toolTip.style.display = "block";
}

function getMousePosXY( e ) {
    if (!e) var e = window.event;
    // CHROME & FF
    if (e.pageX || e.pageY) {
        mousePosX = e.pageX + document.body.scrollLeft + "px";
        mousePosY = e.pageY - 30 + "px";
    }
    // IE
    else if (e.clientX || e.clientY) {
        mousePosX = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft + 10;
        mousePosY = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop - 25;
    }    
}

function ClearDivs() {
    emailDiv.innerHTML = "";
    linkDiv.innerHTML = "";
    capchaDiv.innerHTML = "";
    infoDiv.innerHTML = "";
}

