var selectedArr = new Array();
var selectionArr = new Array();
var currentForm = "";
var imageOn = "/image/icon_21x21_selectboxon.gif";
var imageOff = "/image/icon_21x21_selectboxoff.gif";
var curType;
function startTimer() {
    window.setTimeout("timeout()", 7200000);
}

function resetTimer() {
    window.setTimeout("timeout()", 7200000);
}

function timeout() {
    window.setTimeout("timeout()", 7200000);
    location.href="/timeout.do";
}

var selectedPhoto = 0;
function selectPhoto(lnk) {
    if (!selectedArr[lnk]) {
        selectedArr[lnk] = "selected";
        document.images["rad" + lnk].src = imageOn;
        selectedPhoto++;
    } else {
        selectedArr[lnk] = null;
        document.images["rad" + lnk].src = imageOff;
        selectedPhoto--;
    }
    updatePhotoCount();
    if (document.images["selectAll"]) {
        document.images["selectAll"].src = imageOff;
    }
}

function updatePhotoCount() {
    if (document.all["selectedPhotoCount"]) {
        if (selectedPhoto > 0) {
            document.all["selectedPhotoCount"].innerText = selectedPhoto + "张照片被选中";
        } else {
            document.all["selectedPhotoCount"].innerText = "无照片被选中";
        }
    }
}

function selectSinglePhoto(lnk) {
    if (!selectedArr[lnk]) {
        selectedArr[lnk] = "selected";
        document.images["rad" + lnk].src=imageOn;
        for(var key in selectedArr) {
            if (key != lnk && selectedArr[key]) {
                selectedArr[key] = null;
                document.images["rad" + key].src = imageOff;
            }
        }
    }
}

function selectAlbum(lnk) {
    if (!selectedArr[lnk]) {
        selectedArr[lnk] = "selected";
        document.images["rad" + lnk].src=imageOn;
        for(var key in selectedArr) {
            if (key != lnk && selectedArr[key]) {
                selectedArr[key] = null;
                document.images["rad" + key].src = imageOff;
            }
        }
    }
}

function saveData(saveArr) {
    var data = "";
    var fieldSeperator = "||||";
    for(var key in saveArr) {
        var val = saveArr[key];
        if (val instanceof Array) {
            for(j=0; j<val.length; j++) {
                data += key + "##" + escape(val[j]) + fieldSeperator;
            }
        } else {
            data += key + "##" + escape(val) + fieldSeperator;
        }
    }
    data = data.substring(0, data.length - fieldSeperator.length);
    //alert(data);
    document.cookie = "dotcolor=" + data;
}

function abs(v) {
	if (v > 0) {
		return v;
	} else {
		return -v;
	}
}

function sign(v) {
    if (v < 0) {
        return -1;
    } else {
        return 1;
    }
}

function clickOn(frm, opt) {
    var bool = true;
    if (document.forms[frm].elements[opt].value=="false") {
        bool = true;
    } else {
        bool = false;
    }
    clickSwitch(frm, opt, bool);
}

function clickSwitch(frm, opt, val) {
    if (val == false) {
        document.images["rad_" + opt].src = imageOff;
        document.forms[frm].elements[opt].value="false";
    } else {
        document.images["rad_" + opt].src = imageOn;
        document.forms[frm].elements[opt].value="true";
    }
}

function getForm() {
    if (currentForm != "") {
        return document.forms[currentForm];
    } else {
        //Return the first form in the document
        if (document.forms.length > 0) {
            return document.forms[0];
        } else {
            alert("Cannot find any form in the document");
        }
    }
}
function selectSingleRadio(opt) {
    var frm = getForm();
    if (frm.elements[opt].value == "true") {
        document.images["rad_" + opt].src = imageOff;
        frm.elements[opt].value = "false";
    } else {
        document.images["rad_" + opt].src = imageOn;
        frm.elements[opt].value = "true";
    }
}

function selectMultiRadio(section, opt) {
    var sectionArr = selectionArr[section];
    if (!sectionArr) {
        var frm = getForm();
        sectionArr = new Array();
        var count = 0;
        for(var i=0; i<document.images.length; i++) {
            var objImg = document.images[i];
            if (objImg.name) {
                splitStr = objImg.name.split("_");
                if (splitStr.length == 3) {
                    if (splitStr[1] == section) {
                        sectionArr[count] = splitStr[2];
                        count++;
                    }
                }
            }
        }
        selectionArr[section] = sectionArr;
    }
    for(var i=0; i<sectionArr.length; i++) {
        var tmpopt = sectionArr[i];
        var img = "rad_" + section + "_" + tmpopt;
        if (opt == tmpopt) {
            if (getForm().getElementsByTagName(section).value == opt) {
                document.images[img].src = imageOff;
                setValue(section, "");
            } else {
                document.images[img].src = imageOn;
                setValue(section, opt);
            }
        } else {
            document.images[img].src = imageOff;
        }
    }
}

function selectCheckbox(section, opt) {
    var frm = getForm();
    var sectionArr = selectionArr[section];
    if (!sectionArr) {
        var frm = getForm();
        sectionArr = new Array();
        var count = 0;
        for(var i=0; i<document.images.length; i++) {
            var objImg = document.images[i];
            if (objImg.name) {
                splitStr = objImg.name.split("_");
                if (splitStr.length == 3) {
                    if (splitStr[1] == section) {
                        sectionArr[count] = splitStr[2];
                        count++;
                    }
                }
            }
        }
        selectionArr[section] = sectionArr;
    }
    var totalVal = "";
    var img = "rad_" + section + "_" + opt;
    if (document.images[img].src.indexOf(imageOn) >=0) {
        document.images[img].src = imageOff;
    } else {
        document.images[img].src = imageOn;
    }
    for(var i=0; i<sectionArr.length; i++) {
        var tmpopt = sectionArr[i];
        var img = "rad_" + section + "_" + tmpopt;
        if (document.images[img].src.indexOf(imageOn)>=0) {
            totalVal = totalVal + tmpopt + ";";
        }
    }
    setValue(section, totalVal);
}

function setValue(opt, val) {
    var frm = getForm();
    if (frm.elements[opt]) {
        frm.elements[opt].value = val;
    } else {
        alert("Cannot find input " + opt);
    }
}

function changeAlbumSortOrder(sel) {
    var saveArr = new Array();
    saveArr["newSortOrder"] = sel.options[sel.selectedIndex].value;
    saveData(saveArr);
    if (curType) {
        location.href="/albumSelect.do?type=" + curType;
    } else {
        location.href="/albumSelect.do";
    }
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}