//********************************************

function document_onkeydown() {
	
	switch (window.event.keyCode) {
		case 123: //F12
			jsrsDebugInfo();
			break;
	}
}

function keepSessionAlive() { 
    jsrsExecute(Application["ApplicationPath"] + "/ServerScript/Default.aspx", null, "KeepSessionAlive", null); 
} 

function turnArrayToString(obj) {
	var str = "";

	for (var i = 0; i < obj.length; i++) {
		if (i == 0) {
			str = "\"" + escape(obj[i]) + "\"";
		} else {
			str += ",\"" + escape(obj[i]) + "\"";
		}
	}
	return str;
}

function messageBox(title, buttons, width, height, buttonWidth, align, valign, objectType, args) {
	return window.showModalDialog("Common/MessageBox.aspx?Title=" + title + "&Buttons=" + buttons + "&ButtonWidth=" + buttonWidth + "&Align=" + align + "&VAlign=" + valign + "&ObjectType=" + objectType, args, "center:yes;dialogWidth:" + width + "px;dialogHeight:" + height + "px;status:no;");
}

function sentenceCase(value){
    var firstWord = true;
    var returnValue = "";
    var words;

    if (value == null || value.length == 0) {
        return value;
    } else {
		words = value.split(" ");
		for (i in words) {
			var word = words[i];
            if (firstWord) {
                returnValue += word.substr(0, 1).toUpperCase();
                returnValue += word.substr(1, word.length - 1);
            } else {
                returnValue += " ";
                returnValue += word.substr(0, 1).toLowerCase();
                returnValue += word.substr(1, word.length - 1);
            }
            firstWord = false;
        }
        return returnValue;
    }
}

function cboDated_onchange() {
	switch (getSelectValue(document.frmHead.cboDated)) {
        case "Tomorrow":
            document.frmHead.txtStart.value = addDays(getCurrentDate(), 1);
            document.frmHead.txtEnd.value = document.frmHead.txtStart.value;
            break;
        case "Today":
            document.frmHead.txtStart.value = getCurrentDate();
            document.frmHead.txtEnd.value = document.frmHead.txtStart.value;
            break;
        case "Yesterday":
            document.frmHead.txtStart.value = addDays(getCurrentDate(), -1);
            document.frmHead.txtEnd.value = document.frmHead.txtStart.value;
            break;
        case "Next Week":
            document.frmHead.txtStart.value = addDays(getCurrentDate(), 8 - getDatePart(getCurrentDate(), "w"));
            document.frmHead.txtEnd.value = addDays(document.frmHead.txtStart.value, 6);
            break;
        case "This Week":
            document.frmHead.txtStart.value = addDays(getCurrentDate(), 1 + getDatePart(getCurrentDate(), "w") * -1);
            document.frmHead.txtEnd.value = addDays(document.frmHead.txtStart.value, 6);
            break;
        case "Last Week":
            document.frmHead.txtStart.value = addDays(getCurrentDate(), -6 - getDatePart(getCurrentDate(), "w"));
            document.frmHead.txtEnd.value = addDays(document.frmHead.txtStart.value, 6);
            break;
        case "Next Month":
            document.frmHead.txtStart.value = "1 " + getMonthName(getMonth(getCurrentDate()) + 1, false) + " " + getYear(getCurrentDate());
            document.frmHead.txtEnd.value = addMonths(document.frmHead.txtStart.value, 1);
            document.frmHead.txtEnd.value = addDays(document.frmHead.txtEnd.value, -1);
            break;
        case "This Month":
            document.frmHead.txtStart.value = "1 " + getMonthName(getMonth(getCurrentDate()), false) + " " + getYear(getCurrentDate());
            document.frmHead.txtEnd.value = addMonths(document.frmHead.txtStart.value, 1);
            document.frmHead.txtEnd.value = addDays(document.frmHead.txtEnd.value, -1);
            break;
        case "Last Month":
            document.frmHead.txtStart.value = "1 " + getMonthName(getMonth(getCurrentDate()) - 1, false) + " " + getYear(getCurrentDate());
            document.frmHead.txtEnd.value = addMonths(document.frmHead.txtStart.value, 1);
            document.frmHead.txtEnd.value = addDays(document.frmHead.txtEnd.value, -1);
            break;
        case "Next Year":
            document.frmHead.txtStart.value = "1 January " + (getYear(getCurrentDate()) + 1);
            document.frmHead.txtEnd.value = addYears(document.frmHead.txtStart.value, 1);
            document.frmHead.txtEnd.value = addDays(document.frmHead.txtEnd.value, -1);
            break;
        case "This Year":
            document.frmHead.txtStart.value = "1 January " + (getYear(getCurrentDate()));
            document.frmHead.txtEnd.value = addYears(document.frmHead.txtStart.value, 1);
            document.frmHead.txtEnd.value = addDays(document.frmHead.txtEnd.value, -1);
            break;
        case "Last Year":
            document.frmHead.txtStart.value = "1 January " + (getYear(getCurrentDate()) - 1);
            document.frmHead.txtEnd.value = addYears(document.frmHead.txtStart.value, 1);
            document.frmHead.txtEnd.value = addDays(document.frmHead.txtEnd.value, -1);
            break;
    }
    
	enableControl(document.frmHead.txtStart, !frmHead.cboDated.disabled && (getSelectValue(document.frmHead.cboDated) == "On" || getSelectValue(document.frmHead.cboDated) == "On or After" || getSelectValue(document.frmHead.cboDated) == "Between"));
	enableControl(document.frmHead.txtEnd, !frmHead.cboDated.disabled && (getSelectValue(document.frmHead.cboDated) == "On or Before" || getSelectValue(document.frmHead.cboDated) == "Between"));
}

function keepSessionAlive() {
	jsrsExecute("ServerScript/Default.aspx", null, "KeepSessionAlive", null);		
}

function hasObjectPermission(permission) {
	var result = false;
	
	if (Session[frmHead.ObjectType.value + "Permissions"].toLowerCase().indexOf(permission.toLowerCase() + ",") > -1) {
		result = true;
	}
	
	return result
}

function formatURL(url){
    if (url.indexOf("/") == 0){
        return url.substring(1);
    }
}

function MouseHover(x){
	if (typeof(x) == "undefined") {
		x = this;
	}
	x.oldClassName = x.className;
	x.className = x.className + "Hover";
	x.style.cursor = 'pointer'
}

function MouseOffHover(x){
	if (typeof(x) == "undefined") {
		x = this;
	}
	x.className = x.oldClassName;
}

function isDecimal(s, allowDecimal)
{   
	var i;
	var foundDecimal = false;
	if (!allowDecimal) foundDecimal = true;
	
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) {
			if (c == ".") {
				if (foundDecimal) {
					return false;
				} else {
					foundDecimal = true;
				}
			} else {
				return false;
			}
		}
		
    }
    // All characters are numbers.
    return true;
}

function search(){
    if(trim(document.getElementById("txtSearchKeywords").value).length == 0)
    {
        alert("Search criteria is required.");
        return false;
    }
    document.getElementById("frmSearch").action = Application["ApplicationPath"] + "/Search.aspx";
    document.getElementById("frmSearch").submit();
}

function keyPressed(e){
    var keyPressed;
    if(window.event)
        keyPressed = window.event.keyCode; // IE
    else
        keyPressed = e.which; // Firefox

    if (keyPressed == 13) {
       search();
    }
}