var popupWin;

var currentSelections;
var currentOption;
var currentOptionSelected;

function keepSelections(selectBox, option) {
    currentSelections = new Array();
    var j = 0;
    for (var i = 0; i < selectBox.options.length; i++) {
        if (selectBox[i] != option && selectBox.options[i].selected) {
            currentSelections[j++] = selectBox.options[i];
        }
    }
    currentOption = option;
    currentOptionSelected = option.selected;
}

function reselect() {
    for (var i = 0; i < currentSelections.length; i++) {
        currentSelections[i].selected = true;
    }
    currentOption.selected = !(currentOptionSelected);
}

function gotoTransentric() {
    window.open("http://www.transentric.com");
}

function gotoCopyGuard() {
    window.open("http://www.copyguard.com");
}

function openCFReports(url) {
    window.open(url, "reportWin");
}

function openReports() {
    window.open("/worp/", "reportWin");
}

// centers the popup window
function openWindowPercent(url, name, w, h, perc) {
   var winX = (screen.availWidth - w)*perc*.01;
   var winY = (screen.availHeight - h)*perc*.01;
   popupWin = window.open(url, name, 'width=' + w + ',height=' + h + ',left=' + winX + ',top=' + winY + ',resizable=yes,scrollbars=no');
   popupWin.focus();
}

function checkboxSelectAll(inputName, checked) {
    var elements = document.getElementsByName(inputName);
    if( elements ) {
        for (i = 0; i < elements.length; i++) {
            elements[i].checked = checked;
        }
    }
}

function multiselectAll(selectInput, selected) {
    for (i = 0; i < selectInput.options.length; i++) {
        selectInput.options[i].selected = selected;
    }
}

function getCurrentDate() {
    var today_date = new Date()
    var year = today_date.getFullYear()
    var month = today_date.getMonth()+1
    var day = today_date.getDate()
    if (month < 10) {
        month = "0" + month;
    }
    if (day < 10) {
        day = "0" + day;
    }
    return month + "/" + day + "/" + year;
}

function getCurrentTime() {
    var today_date = new Date()
    var hours = today_date.getHours();
    var minutes = today_date.getMinutes();
    var ampm = "am"
    if (hours > 12) {
        ampm = "pm";
        hours = hours - 12;
    } else if (hours == 12) {
        ampm = "pm";
    } else if (hours == 0) {
        hours = 12;
    }
    if( hours < 10 ) {
        hours = "0" + hours;
    }
    if( minutes < 10 ) {
        minutes = "0" + minutes;
    }
    return hours + ":" + minutes + " " + ampm;
}

function checkExtension() {
    var fileName = document.uploadForm.userfile.value;
    var ext = fileName.substring(fileName.lastIndexOf("."), fileName.length).toUpperCase();
    if(ext != ".CSV" && ext != ".XLS") {
        alert("The user file must be of csv or xls type.");
        return false;
    } else {
        return true;
    }
}

// this gets the cookie when called
    function GetCookie (name) {
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen) {
            var j = i + alen;
            if (document.cookie.substring(i, j) == arg)
                return getCookieVal (j);
            i = document.cookie.indexOf(" ", i) + 1;
            if (i == 0) break;
        }
        return null;
    }

    function getCookieVal(offset) {
        var endstr = document.cookie.indexOf (";", offset);
        if (endstr == -1)
            endstr = document.cookie.length;
        return unescape(document.cookie.substring(offset, endstr));
    }
    
// this deletes the cookie when called
   function DeleteCookie (name,path,domain) {
	        var exp = new Date();
	        exp.setTime (exp.getTime() - 1);
	        document.cookie = name + '=; expires=' + exp.toGMTString() + '; path=' + path + '; domain=' + domain;
   }
    
   function showPsynch(){
        DeleteCookie('SMSESSION','/','.transentric.com');
        DeleteCookie('JSESSIONID','/','.transentric.com');
        window.location.href="/ors";
        var cval = GetCookie (name);
    	var path = 'https://psy.www.transentric.com';
        var vWinCal = window.open(path,
                                  "help",
                                  "width=750,height=500,resizable=1,scrollbars=1,status=0,toolbar=0,location=0,menubar=0,screenX=5,screenY=5,left=20,top=20" );
    	vWinCal.focus();
    }