﻿// General JavaScript Functions
var over = false;
function gotoUrl(url) {
    var domain = "http://www.business-file.com/new/";
    domain = window.location.href.substring(0, window.location.href.lastIndexOf("/")) + "/";
    //domain = "http://localhost:49191/WebsiteV3/";
    window.location.href = domain + url;
}
function mm_link(url) {
    var mm_contents_td = document.getElementById("mm_contents_td");
    for (var i = 0; i < mm_contents_td.childNodes.length; i++) {
        if (url == mm_contents_td.childNodes[i].id) mm_contents_td.childNodes[i].className = "matchmakerOverview";
        else mm_contents_td.childNodes[i].className = "matchmakerOverviewhidden";
    }
}
function validateTelephone(source, args) {
//    var expr = /^[0|\+]\d{7,}$/;
    var expr = /^[0-9 ()+]{10,20}$/;
    args.IsValid = args.Value.match(expr) != null;
}
function validateEmail(source, args) {
    //    var expr = /^(\w+\.{0,1}\w+)+@(\w+\.+\w+)+$/;
    var expr = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    args.IsValid = args.Value.match(expr) != null;
}
// JavaScript Document
function Req(source, arguments) {
    arguments.IsValid = arguments.Value != "";
    if (!arguments.IsValid) source.innerHTML = "*";
}
function ReqDate(source, arguments) {
    Req(source, arguments);
    if (arguments.IsValid) {
        arguments.IsValid = arguments.Value.match(/^(([0-2][0-9]|30)\/(04|06|09|11)|([0-2][0-9]|3[01])\/(01|03|05|07|08|10|12)|([0-2][0-9])\/02)\/[12]\d{3}$/);
        if (!arguments.IsValid) {
            source.innerHTML = "?";
            source.setAttribute("title", "This field requires a valid date");
        } else {
            source.innerHTML = " ";
        }
    }
}
function Posspng(source, arguments) {
    if (arguments.Value != "") {
        arguments.IsValid = arguments.Value.match(/png|png|jpeg|JPEG$/);
        if (!arguments.IsValid) source.innerHTML = "Please only upload .png images";
        else source.innerHTML = " ";
    }
}
function PossDoc(source, arguments) {
    if (arguments.Value != "") {
        arguments.IsValid = arguments.Value.match(/doc|DOC$/);
        if (!arguments.IsValid) source.innerHTML = "Please only upload .DOC files";
        else source.innerHTML = " ";
    }
}
function ReqPhoneNumber(source, arguments) {
    Req(source, arguments);
    if (arguments.IsValid) {
        arguments.IsValid = arguments.Value.match(/^[0-9 ()+]{10,20}$/);
        if (!arguments.IsValid) {
            source.innerHTML = "?";
            //ShowValMsg();
            source.setAttribute("title", "This field requires a valid telephone number");
        }
    }
}
function ReqEmail(source, arguments) {
    Req(source, arguments);
    if (arguments.Value == "") arguments.IsValid = false;
    if (arguments.IsValid) {
        arguments.IsValid = arguments.Value.match(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/);
        if (!arguments.IsValid) {
            source.innerHTML = "?";
            //ShowValMsg();
            source.setAttribute("title", "This field requires a valid email address");
        }
    }
}
function ReqPostcode(source, arguments) {
    Req(source, arguments);
    if (arguments.IsValid) {
        arguments.IsValid = arguments.Value.match(/^[a-zA-Z](\d\w{0,1}|[a-zA-Z]\d\w{0,1}) \d\w{2}$/);
        if (!arguments.IsValid) {
            source.innerHTML = "?";
            //ShowValMsg();
            source.setAttribute("title", "This field requires a valid postcode\ne.g: SW14 6NU");
        }
    }
}
function PossDate(source, arguments) {
    if (arguments.Value != "") {
        arguments.IsValid = arguments.Value.match(/^(([0-2][0-9]|30)\/(04|06|09|11)|([0-2][0-9]|3[01])\/(01|03|05|07|08|10|12)|([0-2][0-9])\/02)\/[12]\d{3}$/);
        if (!arguments.IsValid) {
            source.innerHTML = "?";
            source.setAttribute("title", "This field requires a valid date");
        } else {
            source.innerHTML = " ";
        }
    }
}
function maskPhoneNumberInput(e, input) {
    var keynum;
    var keychar;
    var numcheck;

    if (window.event) // IE
    {
        keynum = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }
    keychar = String.fromCharCode(keynum);
    if (keychar == ' ') return keychar;
    else if (keychar == '+' && input.value.length == 0) return keychar;
    var isDigit = keychar.match(/\d/) != null;
    return isDigit;
}


// *********************
// Taken from E4
// *********************
var timer = 0;
var tempArgs;
// starts timer that shows the News preview after half a second
function startNewsPreviewTimer() {
    hideFloatingDiv();
    tempArgs = arguments;
    clearTimeout(timer);
    timer = setTimeout("showNewsPreview(tempArgs)", 500);
}
// hides all floating objects
function hideAll() {
    hideFloatingDiv();
//    hideLoginPanel();
}
// shows the News preview
function showNewsPreview() {
    var args = arguments[0];
    // gets y position of link clicked and uses that to calculate position of floating div
    var tempY = findPosY(args[0]);
    var floatingDiv = document.getElementById("floatingDiv");
    var h1 = document.documentElement.clientHeight;
    if (tempY + floatingDiv.clientHeight >= h1) tempY -= 200.0;
    floatingDiv.style.top = tempY + "px";
    for (var i = 0; i < args.length - 1; i++) args[i] = args[i + 1];
    // puts date into correct format
    args[6] = formatDate(args[6]);
    // passes News details
    populateNewsPreviewDetails(args);
    // show the News preview object
    showFloatingDiv();
}
// Get Y position of object
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop += obj.offsetTop;
            if (!obj.offsetParent) break;
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}
// shows the floating div
function showFloatingDiv() {
    document.getElementById("floatingDiv").className = "";
}
// hides the floating div
function hideFloatingDiv() {
    var floatingDiv = document.getElementById("floatingDiv");
    if (floatingDiv != null) floatingDiv.className = "hidden";
}

// *********************
// Alternative to above! (Active one)
// *********************
// 
function startMyPreviewTimer(d) {
    hideFloatingDiv();
    tempArgs = d;
    var dd = document.getElementById(d);
    AssignPosition(dd);
    clearTimeout(timer);
    // Commented out to switch off for now!
    //timer = setTimeout("ShowContentDelay(tempArgs)", 5000);
}

var cX = 0; var cY = 0; var rX = 0; var rY = 0;
function UpdateCursorPosition(e) { cX = e.pageX; cY = e.pageY; }
function UpdateCursorPositionDocAll(e) { cX = event.clientX; cY = event.clientY; }
if (document.all) { document.onmousemove = UpdateCursorPositionDocAll; }
else { document.onmousemove = UpdateCursorPosition; }
function AssignPosition(d) {
    if (self.pageYOffset) {
        rX = self.pageXOffset;
        rY = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) {
        rX = document.documentElement.scrollLeft;
        rY = document.documentElement.scrollTop;
    }
    else if (document.body) {
        rX = document.body.scrollLeft;
        rY = document.body.scrollTop;
    }
    if (document.all) {
        cX += rX;
        cY += rY;
    }
    d.style.left = (cX + 10) + "px";
    d.style.top = (cY + 10) + "px";
}
function HideContent(d) {
    if (d.length < 1) { return; }
    document.getElementById(d).style.display = "none";
    clearTimeout(timer);
}
function ShowContent(d) {
    if (d.length < 1) { return; }
    var dd = document.getElementById(d);
    AssignPosition(dd);
    dd.style.display = "block";
}
// Delayed version
function ShowContentDelay(d) {
    if (d.length < 1) { return; }
    var dd = document.getElementById(d);
//    AssignPosition(dd);
    dd.style.display = "block";
}

function ReverseContentDisplay(d) {
    if (d.length < 1) { return; }
    var dd = document.getElementById(d);
    AssignPosition(dd);
    if (dd.style.display == "none") { dd.style.display = "block"; }
    else { dd.style.display = "none"; }
}
