﻿var advertOK = false;
var bnAdTitle = false;
var bnAdText = false;
var descriptionOK = false;
var handlerDomain;

$(document).ready(function() {
    if (document.getElementById('hfPreValidate').value == "validate") {
        checkTitleText();
        checkAdText();
        checkDescText();
    }
    handlerDomain = "http://" + window.location.host + "/Handlers";
});

function checkTitleText() {
    var text = document.getElementById('titleText').value;
    if (text != "Advert Title" && text != "") {

        document.getElementById('mainSampleLink').innerHTML = text;
        document.getElementById('adTitleCount').innerHTML = (16 - text.length).toString() + " remaining";
        if (text.length > 4) {
            bnAdTitle = true;
            document.getElementById('adTitleCount').style.color = "#32a912";
        }
        else {
            bnAdTitle = false;
            document.getElementById('adTitleCount').style.color = "#d41d4b";
            document.getElementById('adApproved').style.backgroundImage = "url(Images/red_cross_50.png)"
        }
    }
    else {
        bnAdTitle = false;
        document.getElementById('adTitleCount').innerHTML = "16 remaining";
        document.getElementById('adTitleCount').style.color = "#d41d4b";
        document.getElementById('adApproved').style.backgroundImage = "url(Images/red_cross_50.png)"
    }
    if (bnAdTitle && bnAdText) {
        document.getElementById('adApproved').style.backgroundImage = "url(Images/green_tick_50.png)"
    }
    checkValidationButton();
}

function checkAdText() {
    var text = document.getElementById('adTextText').value;
    if (text != "Please enter a brief summary of your services to advertise here." && text != "") {
        document.getElementById('adTextCount').innerHTML = (65 - text.length).toString() + " remaining";
        if (text.length > 20) {
            if (text.length > 65) {
                var newText = text.substring(0, 64);
                document.getElementById('adTextText').value = newText;
                document.getElementById('adSampleText').innerHTML = newText;
            }
            else {
                bnAdText = true;
                document.getElementById('adSampleText').innerHTML = text;
                document.getElementById('adTextCount').innerHTML = (65 - text.length).toString() + " remaining";
                document.getElementById('adTextCount').style.color = "#32a912";
            }
        }
        else {
            bnAdText = false;
            document.getElementById('adTextCount').style.color = "#d41d4b";
            document.getElementById('adApproved').style.backgroundImage = "url(Images/red_cross_50.png)"
        }
    }
    else {
        bnAdText = false;
        document.getElementById('adTextCount').innerHTML = "65 remaining";
        document.getElementById('adTextCount').style.color = "#d41d4b";
        document.getElementById('adApproved').style.backgroundImage = "url(Images/red_cross_50.png)"
    }
    if (bnAdTitle && bnAdText) {
        document.getElementById('adApproved').style.backgroundImage = "url(Images/green_tick_50.png)"
    }
    checkValidationButton();
}

function checkDescText() {
    var text = document.getElementById('txtDescription').value;
    if (text.indexOf("[Your company name] provide a professional [service]") == -1 && text != "") {
        var wordCount = 50;
        var words = text.split(" ");
        for (var i = 0; i < words.length; i++) {
            if (words[i].length > 0) {
                wordCount--;
            }
        }
        if (wordCount < 0) {
            wordCount = 0;
        }
        document.getElementById('descCount').innerHTML = wordCount.toString() + " more words required";
        if (words.length > 49) {
            document.getElementById('descrApproved').style.backgroundImage = "url(Images/green_tick_50.png)";
            descriptionOK = true;
            document.getElementById('descCount').style.color = "#32a912";
            document.getElementById('descCount').style.display = "none";
        }
        else {
            descriptionOK = false;
            document.getElementById('descCount').style.color = "#d41d4b";
            document.getElementById('descrApproved').style.backgroundImage = "url(Images/red_cross_50.png)";
            document.getElementById('descCount').style.display = "inline";
        }
    }
    else {
        descriptionOK = false;
        document.getElementById('descCount').style.color = "#d41d4b";
        document.getElementById('descrApproved').style.backgroundImage = "url(Images/red_cross_50.png)";
        document.getElementById('descCount').innerHTML = "50 more words required";
        document.getElementById('descCount').style.display = "inline";
        checkValidationButton();
    }
    checkValidationButton();
}

function setTitleText() {
    if (document.getElementById('titleText').value == "Advert Title") {
        document.getElementById('titleText').value = "";
        document.getElementById('adTitleCount').style.color = "#d41d4b";
        document.getElementById('adTitleCount').innerHTML = "16 remaining";
    }
}

function setAdText() {
    if (document.getElementById('adTextText').value == "Please enter a brief summary of your services to advertise here.") {
        document.getElementById('adTextText').value = "";
        document.getElementById('adTextCount').style.color = "#d41d4b";
        document.getElementById('adTextCount').innerHTML = "65 remaining";
    }
}

function setDescText() {
    if (document.getElementById('txtDescription').value.indexOf("[Your company name] provide a professional [service]") > -1) {
        document.getElementById('txtDescription').value = "";
        document.getElementById('descCount').style.color = "#d41d4b";
        document.getElementById('descCount').innerHTML = "50 remaining";
    }
}

function checkValidationButton() {
    document.getElementById('stillReqTextContainer').innerHTML = "";
    if (!bnAdTitle) {
        document.getElementById('stillRequired').style.display = "inline";
        document.getElementById('validateButton').style.display = "none";
        document.getElementById('validateButton').style.cursor = "default";
        var newDiv = document.createElement('div');
        newDiv.className = "reqText";
        newDiv.innerHTML = "Advert Title";
        document.getElementById('stillReqTextContainer').appendChild(newDiv);
    }
    if (!bnAdText) {
        document.getElementById('stillRequired').style.display = "inline";
        document.getElementById('validateButton').style.display = "none";
        document.getElementById('validateButton').style.cursor = "default";
        var newDiv = document.createElement('div');
        newDiv.className = "reqText";
        newDiv.innerHTML = "Advert Text";
        document.getElementById('stillReqTextContainer').appendChild(newDiv);
    }
    if (!descriptionOK) {
        document.getElementById('stillRequired').style.display = "inline";
        document.getElementById('validateButton').style.display = "none";
        document.getElementById('validateButton').style.cursor = "default";
        var newDiv = document.createElement('div');
        newDiv.className = "reqText";
        newDiv.innerHTML = "Company Description";
        document.getElementById('stillReqTextContainer').appendChild(newDiv);
    }
    if (descriptionOK && bnAdText && bnAdTitle) {
        document.getElementById('stillRequired').style.display = "none";
        document.getElementById('validateButton').style.display = "inline";
        document.getElementById('validateButton').style.cursor = "pointer";
    }
}

function validate() {
    var d = new Date();
    var time = d.getTime();
    var strURL = handlerDomain + "/plinkActivationHandler.ashx?d=" + time;
    $.post(strURL, {
        validate: "true",
        compID: document.getElementById('hfCompanyID').value,
        plinkID: document.getElementById('hfPlinkID').value,
        adTitle: document.getElementById('titleText').value,
        adText: document.getElementById('adTextText').value,
        desc: document.getElementById('txtDescription').value,
        userID: document.getElementById('hfUserID').value,
        typeIP: document.getElementById('hfTypeIP').value
    }, function(data) {
        if (data == "success") {
            self.location.href = "console.aspx";
        }
    });
}

function saveForLater() {
    var d = new Date();
    var time = d.getTime();
    var strURL = handlerDomain + "/plinkActivationHandler.ashx?d=" + time;
    $.post(strURL, {
        validate: "false",
        compID: document.getElementById('hfCompanyID').value,
        plinkID: document.getElementById('hfPlinkID').value,
        adTitle: document.getElementById('titleText').value,
        adText: document.getElementById('adTextText').value,
        desc: document.getElementById('txtDescription').value,
        userID: document.getElementById('hfUserID').value,
        typeIP: document.getElementById('hfTypeIP').value
    }, function(data) {
        if (data == "success") {
            showMessage();
        }
    });
}

function openDiv(id1, id2) {
    document.getElementById(id1).style.display = "inline";
    var element = document.getElementById(id2);
    var timer = 0;
    if (element.offsetTop == -299 || element.offsetTop == -300 || element.offsetTop == -301) {
        $(element).each(function(i) {
            timer = (timer * .8 + 800);
            $(this).animate({ top: "0px" }, timer);
        });
    }
}

var currDiv = "";

function closeDiv(id1, id2) {
    currDiv = id1;
    var element = document.getElementById(id2);
    var timer = 0;
    if (element.offsetTop > -290) {
        $(element).each(function(i) {
            timer = (timer * .8 + 800);
            $(this).animate({ top: "-300px" }, timer);
        });
    }
    setTimeout("hideDiv();", 1000);
}

function hideDiv() {
    document.getElementById(currDiv).style.display = "none";
}

function showMessage() {
    var messageDiv = document.getElementById('saved');
    var newPos = ((GetHeight() - messageDiv.offsetHeight) / 2).toString() + "px";
    var bounce1 = (((GetHeight() - messageDiv.offsetHeight) / 2) - 20).toString() + "px";
    var bounce2 = (((GetHeight() - messageDiv.offsetHeight) / 2) - 10).toString() + "px";
    var timer = 0;
    $("#saved").each(function(i) {
        timer = (timer * .8 + 800);
        $(this).animate({ top: newPos }, timer);
    });
    timer = 0;
    $("#saved").each(function(i) {
        timer = (timer * .8 + 400);
        $(this).animate({ top: bounce1 }, timer);
        $(this).animate({ top: newPos }, timer);
    });
    timer = 0;
    $("#saved").each(function(i) {
        timer = (timer * .8 + 400);
        $(this).animate({ top: bounce2 }, timer);
        $(this).animate({ top: newPos }, timer);
    });
    setTimeout("hideMessageDiv()", 2000);
}

function setMessageStartPosition() {
    var messageDiv = document.getElementById('saved');
    document.getElementById('saved').style.left = ((GetWidth() - 450) / 2).toString() + "px";
}

function hideMessageDiv() {
    var timer = 0;
    $("#saved").each(function(i) {
        timer = (timer * .8 + 1200);
        $(this).animate({ top: "-400px" }, timer);
    });
    setTimeout("redirect()", 2000);
}

function redirect() {
    self.location.href = "http://www.locallinkup.com";
}

function GetHeight() {
    var y = 0;
    if (self.innerHeight) {
        y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        y = document.documentElement.clientHeight;
    }
    else if (document.body) {
        y = document.body.clientHeight;
    }
    return y;
}

function GetWidth() {
    var x = 0;
    if (self.clientWidth) {
        x = self.clientWidth;
    }
    else if (document.documentElement && document.documentElement.clientWidth) {
        x = document.documentElement.clientWidth;
    }
    else if (document.body) {
        x = document.body.clientWidth;
    }
    return x;
}
