﻿function search(theEvent) {
    if (theEvent == null || theEvent.keyCode == 13) {
        var inputs = document.aspnetForm.elements;
        var search = "";
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].id != "" && inputs[i].id.indexOf("SearchText") > 0) {
                search = inputs[i].value;
            }
        }
        var url = "Search.aspx?kw=" + search;
        if (location.href.indexOf("owl=") > 0) {
            url += "&owl=1&by=GPR&dir=down"
        }

        location.href = url;
        return false;
    }
    return true;
}

function searchTop(theEvent) {
    var topSearch = document.getElementById("searchword");
    if (theEvent.keyCode == 13) {
        var url = "Search.aspx?kw=" + topSearch.value;
        location.href = url;
        return false;
    }
    return true;
}
function checkEnter(theEvent, btnId) {
    var btn = eval("getMainForm()." + btnId);
    if (theEvent.keyCode == 13) {
        btn.click();
        return false;
    }
    return true;
}

function getMainForm() {
    var doc = document;

    if (doc.forms.length == 0) {
        return null;
    }

    if (doc.forms.length == 1) {
        return doc.forms[0];
    }

    // find the form with the most elements
    var maxElements = 0;
    var theForm = doc.forms[0];

    for (var i = 0; i < doc.forms.length; i++) {
        if (doc.forms[i].elements.length > maxElements) {
            theForm = doc.forms[i];
        }
    }

    return theForm;
}

var theWindow;

function placeLinkWindow(element) {

    var webSiteGuid = element.getAttribute('ex_siteGuid')
    var mywebSiteGuid = element.getAttribute('ex_mysiteGuid')

    theWindow = new Window({ className: "spread", title: "Place link",
        top: 70, left: 200, width: 480, height: 400,
        url: "PlaceLink.aspx?wsid=" + webSiteGuid + "&mwsid=" + mywebSiteGuid, showEffectOptions: { duration: 1.5}
    })
    theWindow.show();

}

function placeOneWayLinkWindow(element) {

    var webSiteGuid = element.getAttribute('ex_siteGuid')
    var mywebSiteGuid = element.getAttribute('ex_mysiteGuid')

    theWindow = new Window({ className: "spread", title: "Place one way link",
        top: 70, left: 200, width: 480, height: 420,
        url: "PlaceOneWayLink.aspx?wsid=" + webSiteGuid + "&mwsid=" + mywebSiteGuid, showEffectOptions: { duration: 1.5}
    })
    theWindow.show();

}


function checkWindowClose(theEvent) {
    if (theEvent.keyCode == 27) {
        top.theWindow.close();
    }
}

function openExchangeWindow(element) {

    var top = EXBrowserUtil.elementTop(element);

    var webSiteGuid = element.getAttribute('ex_siteGuid')
    var mywebSiteGuid = element.getAttribute('ex_mysiteGuid')

    theWindow = new Window({ className: "spread", title: "Exchange link",
        top: top - 270, left: 200, width: 580, height: 140,
        url: "ExchangeLink.aspx?wsid=" + webSiteGuid + "&mwsid=" + mywebSiteGuid, showEffectOptions: { duration: 1.5}
    })
    theWindow.show();
}

function approveLinkWindow(element) {

    var webSiteGuid = element.getAttribute('ex_siteGuid')
    var mywebSiteGuid = element.getAttribute('ex_mysiteGuid')

    theWindow = new Window({ className: "spread", title: "Approve link exchange",
        top: 170, left: 200, width: 540, height: 140,
        url: "ApproveLink.aspx?wsid=" + webSiteGuid + "&mwsid=" + mywebSiteGuid, showEffectOptions: { duration: 1.5}
    })
    theWindow.show();
}

function removeLinkWindow(element) {

    var webSiteGuid = element.getAttribute('ex_siteGuid')
    var mywebSiteGuid = element.getAttribute('ex_mysiteGuid')

    theWindow = new Window({ className: "spread", title: "Remove link exchange",
        top: 170, left: 200, width: 440, height: 170,
        url: "RemoveLink.aspx?wsid=" + webSiteGuid + "&mwsid=" + mywebSiteGuid, showEffectOptions: { duration: 1.5}
    })
    theWindow.show();
}

function removeOneWayLinkWindow(element) {

    var webSiteGuid = element.getAttribute('ex_siteGuid')
    var mywebSiteGuid = element.getAttribute('ex_mysiteGuid')

    theWindow = new Window({ className: "spread", title: "Remove link exchange",
        top: 170, left: 200, width: 440, height: 170,
        url: "RemoveOneWayLink.aspx?wsid=" + webSiteGuid + "&mwsid=" + mywebSiteGuid, showEffectOptions: { duration: 1.5}
    })
    theWindow.show();
}


function placeAllLinks() {
    theWindow = new Window({ className: "spread", title: "Place All links",
        top: 10, left: 200, width: 440, height: 490,
        url: "PlaceAllLinks.aspx", showEffectOptions: { duration: 1.5}
    })
    theWindow.show();
}

function verifyWebSite(url) {
    theWindow = new Window({ className: "spread", title: "Verify website",
        top: 20, left: 200, width: 440, height: 170,
        url: "VerifyWebSite.aspx?url=" + url, showEffectOptions: { duration: 1.5}
    })
    theWindow.show();
}

function sortBy(column, direction) {
    var url = location.href;

    if (url.indexOf("?") < 0) {
        url = url + "?page=1";
    }
    var url = replaceQueryString(url, "by", column);
    var url = replaceQueryString(url, "dir", direction);

    location.href = url;
}


function replaceQueryString(url, param, value) {
    var re = new RegExp("([?|&])" + param + "=.*?(&|$)", "i");
    if (url.match(re))
        return url.replace(re, '$1' + param + "=" + value + '$2');
    else
        return url + '&' + param + "=" + value;
}

function warnOnStepX(element) {

    var step = element.getAttribute('ex_step')
    var cont = true;
    if (step == "3") {
        var message = "Cancel the link exchange will trigger the other website to remove your link. Continue?";
        if (location.href.indexOf("PendingRequests.aspx") >= 0) {
            message = "Cancel the link exchange will create a remove task for you. Continue?";
        }

        var cont = confirm(message);
    }
    if (step == "1" || step == "2") {
        var cont = confirm("Are you sure you want to cancel the link exchange?");
    }
    return cont;
}

function warnOnDeny(element) {

    var cont = confirm("Denying this link exchange will block this website from sending you link exchanges. Continue?");
    return cont;
}

function warnOnDenyOwl(element) {

    var result = prompt("Please specify deny reason", "");
    document.getElementById("ctl00_MainContentTop_ContentPlaceHolder1_DenyReason").value = result;
    return result;
}

function confirmRefreshSiteDetails() {

    var cont = confirm("Refresh web site details will loose any changes made. Continue?");
    return cont;
}


function disableButtonAfterSubmit(btn) {
    window.setTimeout(function () { btn.disabled = true; }, 50);
}

var EXBrowserUtil = new function () {

    /** Check if the client is IE */
    this.isIE = function () { return (navigator.appName.indexOf("Internet Explorer") >= 0); }

    /** Check if the client is IE7 */
    this.isIE7 =
		function () {
		    var verSTR = navigator.appVersion;
		    if (verSTR != null && verSTR.indexOf("MSIE") >= 0) {
		        var index_version_number = verSTR.indexOf("MSIE ") + 5;
		        var verNum = parseInt(verSTR.substring(index_version_number, index_version_number + 1));
		        return (verNum >= 7);
		    }
		    return false;
		}

    /** Check if the client is Mozzila */
    this.isMozzila = function () { return (navigator.appName.indexOf("Netscape") >= 0); }

    this.attachEvent = function (eventName, functionElement, element) {
        if (element == null) {
            element = window;
        }

        if (EXBrowserUtil.isIE()) {
            element.attachEvent("on" + eventName, functionElement);
        }
        else {
            element.addEventListener(eventName, functionElement, false);
        }
    }

    this.elementTop =
		function (element) {
		    var curtop = 0;
		    while (element != null && element.offsetParent != null) {
		        curtop += element.offsetTop
		        element = element.offsetParent;
		    }
		    return curtop;
		}

}

function showLogin() {
    var url = location.href;

    if (url.indexOf("?") < 0) {
        url = url + "?page=1";
    }
    var url = replaceQueryString(url, "login", "1");
    location.href = url;
}

var lastRow = null;
function markRow(element) {

    if (lastRow != null) lastRow.style.backgroundColor = "";

    lastRow = element.parentElement.parentElement;
    lastRow.style.backgroundColor = "red";
}

function handleLinkPlaced(btn) {

    var linkPageElement = document.getElementById("LinksPageTxt");
    if (linkPageElement != null && linkPageElement.value == "") {
        document.getElementById("ErrorMessage").innerHTML = "Links page can't be empty";
        return false;
    }
    disableButtonAfterSubmit(btn);
    return true;
}

function flipArea(areaId) {
    area = document.getElementById(areaId);
    if (area.style.display == "none") {
        area.style.display = "";
    }
    else {
        area.style.display = "none";
    }
}

function warnOnBuyerCancelOwl(element) {

    var daysLeft = element.getAttribute('ex_dl');

    var cont = confirm("Are you sure you wish to CANCEL? You already prepaid this month & there are no proration! You have " + daysLeft + " days left.");
    return cont;
}

function warnOnSellerCancelOwl(element) {

    var daysLeft = element.getAttribute('ex_dl');
    var cont = confirm("Are you sure you wish to CANCEL? The buyer prepaid the whole month, You have " + daysLeft + " days left to get paid for this link. \nNOTE: Remove link task will be added once you confirm.");
    return cont;
}

function getElementInPage(tagId) {
    return document.getElementById('ctl00_MainContentTop_ContentPlaceHolder1_' + tagId);
}

function activatePlaceholders() {
    var detect = navigator.userAgent.toLowerCase();
    if (detect.indexOf("safari") > 0) return false;
    var inputs = document.getElementsByTagName("input");

    for (var i = 0; i < inputs.length; i++) {
        var input = inputs[i];
        if (input.getAttribute("type") == "text") {
            var placeholder = input.getAttribute("placeholder") ;
            if (placeholder != null && placeholder.length > 0) {
                if (input.value.length > 0 && input.value != placeholder) {
                    continue;
                }
                input.value = placeholder;
                input.style.color = "gray";
                input.onclick = function () {
                    if (this.value == this.getAttribute("placeholder")) {
                        this.value = "";
                        this.style.color = "black";
                    }
                    return false;
                }
                input.onblur = function () {
                    if (this.value.length < 1) {
                        this.value = this.getAttribute("placeholder");
                        this.style.color = "gray";
                    }
                }
            }
        }
    }
}

function removePlaceholders() {
    var detect = navigator.userAgent.toLowerCase();
    if (detect.indexOf("safari") > 0) return false;
    var inputs = document.getElementsByTagName("input");

    for (var i = 0; i < inputs.length; i++) {
        var input = inputs[i];
        if (input.getAttribute("type") == "text") {
            if (input.getAttribute("placeholder") && input.getAttribute("placeholder").length > 0) {
                if (input.value == input.getAttribute("placeholder")) {
                    input.value = "";
                }
            }
        }
    }
}

function doOnLoadActions() {
    activatePlaceholders();
}