﻿function calculateDimensions(dimension, w, h, windowObject) {
    if (!windowObject)
        windowObject = window;

    var iScreenWidth = screen.width;
    var iScreenHeight = screen.height;
    if (document.all) {
        var iDocumentWidth = windowObject.document.body.offsetWidth;
        var iDocumentHeight = windowObject.document.body.offsetHeight;
        var iDocumentTop = windowObject.screenTop;
        var iDocumentLeft = windowObject.screenLeft;
    }
    else {
        var iDocumentWidth = windowObject.outerWidth;
        var iDocumentHeight = windowObject.outerHeight;
        var iDocumentTop = windowObject.screenY;
        var iDocumentLeft = windowObject.screenX;
    }

    if (dimension == 'left')
        return iDocumentLeft + Math.round((iDocumentWidth - w) / 2);
    else if (dimension == 'top')
        return iDocumentTop + Math.round((iDocumentHeight - h) / 2);
}


function openWindow(url, name, w, h) {
    wleft = calculateDimensions('left', w, h);
    wtop = calculateDimensions('top', w, h);

    var win = window.open(url,
        name,
        'width=' + w + ', height=' + h + ', ' +
        'left=' + wleft + ', top=' + wtop + ', ' +
        'toolbar=no, location=no, status=no, menubar=no, scrollbars=no, resizable=no',
        null);

    win.moveTo(wleft, wtop);
    win.focus();
}

function openModalDialog(url, params, w, h) {
    wleft = calculateDimensions('left', w, h, top);
    wtop = calculateDimensions('top', w, h, top);

    var resObj = window.showModalDialog(url,
        params,
        'dialogWidth=' + w + 'px; dialogHeight=' + h + 'px; ' +
        'dialogLeft=' + wleft + 'px; dialogTop=' + wtop + 'px; ' +
        null);

    return resObj;
}

var BrowserDetect = {
    init: function() {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function(data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function(dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
		{
		    string: navigator.userAgent,
		    subString: "Chrome",
		    identity: "Chrome"
		},
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
		},
		{
		    prop: window.opera,
		    identity: "Opera"
		},
		{
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
		},
		{
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
		},
		{
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
		},
		{
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
		},
		{		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
		},
		{
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
		},
		{
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
		}
	],
    dataOS: [
		{
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
		},
		{
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
		},
		{
		    string: navigator.userAgent,
		    subString: "iPhone",
		    identity: "iPhone/iPod"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]

};
BrowserDetect.init();

//------------------------------------------------

function formatDate(date, format) {
    format = format + "";
    var result = "";
    var i_format = 0;
    var c = "";
    var token = "";
    var y = date.getYear() + "";
    var M = date.getMonth() + 1;
    var d = date.getDate();
    var E = date.getDay();
    var H = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    var yyyy, yy, MMM, MM, dd, hh, h, mm, ss, ampm, HH, H, KK, K, kk, k;
    // Convert real date parts into formatted versions
    var value = new Object();
    if (y.length < 4) { y = "" + (y - 0 + 1900); }
    value["y"] = "" + y;
    value["yyyy"] = y;
    value["yy"] = y.substring(2, 4);
    value["M"] = M;
    value["MM"] = LZ(M);
    value["MMM"] = MONTH_NAMES[M - 1];
    value["NNN"] = MONTH_NAMES[M + 11];
    value["d"] = d;
    value["dd"] = LZ(d);
    value["E"] = DAY_NAMES[E + 7];
    value["EE"] = DAY_NAMES[E];
    value["H"] = H;
    value["HH"] = LZ(H);
    if (H == 0) { value["h"] = 12; }
    else if (H > 12) { value["h"] = H - 12; }
    else { value["h"] = H; }
    value["hh"] = LZ(value["h"]);
    if (H > 11) { value["K"] = H - 12; } else { value["K"] = H; }
    value["k"] = H + 1;
    value["KK"] = LZ(value["K"]);
    value["kk"] = LZ(value["k"]);
    if (H > 11) { value["a"] = "PM"; }
    else { value["a"] = "AM"; }
    value["m"] = m;
    value["mm"] = LZ(m);
    value["s"] = s;
    value["ss"] = LZ(s);
    while (i_format < format.length) {
        c = format.charAt(i_format);
        token = "";
        while ((format.charAt(i_format) == c) && (i_format < format.length)) {
            token += format.charAt(i_format++);
        }
        if (value[token] != null) { result = result + value[token]; }
        else { result = result + token; }
    }
    return result;
}

function errMsgBox(msg, title) {
    var div = document.createElement("div");
    div.id = "error_message_window";
    div.title = title;
    div.style.top = "30px";
    div.style.left = "0px";
    div.style.overflow = "auto";
    document.body.appendChild(div);
    div.innerHTML = msg;

    $(div).dialog({
        width: 700,
        height: 500,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
            }
        }
    });

    $(div).scrollTop(0);
    $(div).scrollLeft(0);
}

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split(/[&?#]{1}[\w\d]+=/);
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
   }

function embedSWF(swfUrlStr, replaceElemIdStr, widthStr, heightStr, swfVersionStr, xiSwfUrlStr, flashvarsObj, parObj, attObj, callbackFn) {
	if (swfobject.getFlashPlayerVersion().major == 0) {
		// no flash	
		var protocol = location.href.match(/^https/i) ? 'https://' : 'http://';
		var html = '<p style="line-height:100px;">';
		html += '<img src="http://wwwimages.adobe.com/www.adobe.com/shockwave/download/images/flashplayer_100x100.jpg" width="100" height="100" alt="Adobe Flash Player Logo" align="left" />';
		html += 'This content requires the Adobe Flash Player plugin</p>';
		html += '<object style="display: none;" type="application/x-shockwave-flash" codebase="' + protocol + 'fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" />';
		$(function() {
			document.getElementById(replaceElemIdStr).innerHTML = html;
		});
	}
	else {
		// has flash
		swfobject.embedSWF(swfUrlStr, replaceElemIdStr, widthStr, heightStr, swfVersionStr, xiSwfUrlStr, flashvarsObj, parObj, attObj, callbackFn);
	}
}

function openTutorial(adviceItemID,caller) {
    window.open('AdviceItems.aspx?ItemID=' + adviceItemID+'&caller='+caller, '_blank');
}

// phone 0-9,-,+,(,),End,Home,Delete,Tab,BSpace validator
function validatePhoneNumber(event) {    
    var arr = new Array(8, 9, 32, 35, 36, 37, 39, 40, 41, 43, 45, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57);
    if ($.browser.mozilla && event.keyCode > 0)
        return;
    if ($.inArray(event.which, arr) < 0)
        event.preventDefault();
}

function openDialog(url) {
	var width = Math.min($(window).width(), $('.site_page').width() + 50);
	var height = $(window).height();
	window.open(url, '', 'width=' + width + 'px,height=' + height + 'px,scrollbars=1,resizable=0');
}

var Programe = {
    UpdateNextButton: function() {
        var $next = $('table.prgmContent input[value="Next"]');
        var title = $('title').text();
        setTimeout(function() {            
            if ($('input[ctrlID=hdnPollStatus]').val() == '0') {                
                Programe.SetDisabled($next);
            } /*else if (title.indexOf('Job Feeds') >= 0) {
                var jobtypecount = $('#rssFeedsSettings div.jobtype_item').length;
                var locationscount = $('#rssFeedsSettings div.location_item').length;
                if (jobtypecount > 0 && locationscount > 0)
                    Programe.SetEnabled($next);
                else
                    Programe.SetDisabled($next);
            } else if (title.indexOf('Your Emails') >= 0 || title.indexOf('Edit Email') >= 0) {
                var sent = parseInt($('#hdnSentApplicationsCount').val());
                if (sent > 0)
                    Programe.SetEnabled($next);
                else
                    Programe.SetDisabled($next);
            } else if (title.indexOf('Edit UserSettings') >= 0) {
                var saved = parseInt($('#hdnAccountCredentialsSaved').val());
                if (saved > 0) {
                    Programe.SetEnabled($next);
                    window.location.href = "YourApplications.aspx";
                }
                else
                    Programe.SetDisabled($next);
            }*/
            else {
                Programe.SetEnabled($next);
            }
        }, 250);
    },
    SetDisabled: function(obj) {
        obj.attr('disabled', 'disabled');
        obj.addClass('nextbuttondisabled');
    },
    SetEnabled: function(obj) {
        obj.attr('disabled', '');
        obj.removeClass('nextbuttondisabled');
    }
};
