//
// Javascript
//
var USER_STATUS_ENABLED = -1;

function checkAll(thisObject, items) {
    for(i=0; i<items.length; i++){
        items[i].checked = thisObject.checked;
    }
}

/*
 * Move the cursor to the field.
 */
function focusField(formInput) {
    formInput.focus();
}

function goto(path) {
	document.location.href=path;
}

/*
 * This is for submiting a form when the user change selectbox value.
 * We'll be submitting to a different page according to formAction.
 */
function changeAction(thisObject, formAction) {
    thisObject.form.action = formAction;
    thisObject.form.submit();
}

/*
 * This is for submiting a form when the user change selectbox value.
 * We'll be submitting to what the form action is. -1 would stop the javascript from submitting.
 */
function changeSelectedOption(thisObject) {
    if (thisObject.value!=-1) {
        thisObject.form.submit();
    }
}

/*
 * This function is for disabling a button after the user clicks on the button.
 */
function disableButton(thisButton) {
    thisButton.disabled = true;
	thisButton.form.submit();
}

/*
 * Confirms form submit.
 */
function confirmSubmit(message) {
    if (confirm(message)) {
        return true;
    } else {
        return false;
    }
}

/*
 * This function is for disabling a button after the user clicks on the button.
 */
function disableButton2(thisButton, form) {
    thisButton.disabled = true;
	form.submit();
}

/*
 * This function is used to reset a form field value.
 */
function setFormFieldValue(formFieldName, formFieldValue) {
    formFieldName.value = formFieldValue;
}

/*
 * Get all options in a selectbox given a form field name.
 */
function selectAllOptions(formField) {
    for (i=0; i<formField.options.length; i++) {
        formField.options[i].selected = true;
    }
}

function moveOptions(fromList, toList) {
	for (i=0; i<fromList.options.length; i++) {
		var current = fromList.options[i];
		if (current.selected) {
			toList.options[toList.length] = new Option(current.text, current.value);
			fromList.options[i] = null;
			i--;
		}
	}
}

/*
 * A wrapper to document.getElementById in case i need to make changes to resolve browser compatiblity issues.
 */
function getObjectByID(id) {
	return document.getElementById(id);
}

/*
 * This would turn on a div area.
 */
function showContent(id) {
	thisDiv = getObjectByID(id);
	if (thisDiv) {
		thisDiv.style.display="block";
	}
}

/*
 * This would turn off a div area.
 */
function hideContent(id) {
	thisDiv = getObjectByID(id);
	if (thisDiv) {
		thisDiv.style.display="none";
	}
}

/*
 * Enables one div and at the same time disable another div.
 */
function swapContent(enableDiv, disableDiv) {
    showContent(enableDiv);
    hideContent(disableDiv);
}

/*
 * This would toggle something on/off.
 */
function toggleContent(id) {
	thisDiv = getObjectByID(id);
	if (thisDiv) {
        if(thisDiv.style.display == "" || thisDiv.style.display == "none") {
            thisDiv.style.display="inline";
		} else {
            thisDiv.style.display="none";
		}
	}
}

function updateContent(id, content) {
    getObjectByID(id).innerHTML = content;
}

/*
 * This is used to hide a div area.
 */
function hideDivMenu(thisObject) {
	thisObject.style.visibility='hidden';
}

/*
 * This is used to show a div area.
 */
function showDivMenu(thisObject) {
	thisObject.style.visibility='visible';
}

/*
 * This is used to hide a div area.
 */
function hideElemById(id) {
	thisDiv = getObjectByID(id);
	if (thisDiv) {
        hideDivMenu(thisDiv);
    }
}

/*
 * This is used to show a div area.
 */
function showElemById(id) {
	thisDiv = getObjectByID(id);
	if (thisDiv) {
        showDivMenu(thisDiv);
    }
}

/*
 * Replaces replaceThis with replacedBy in input
 */
function replace(input, replaceThis, replacedBy) {
    var strLength = input.length;
	var txtLength = replaceThis.length;

    if ((strLength == 0) || (txtLength == 0)) {
		return input;
	}
    var i = input.indexOf(replaceThis);
    if ((!i) && (replaceThis != input.substring(0,txtLength))) {
		return input;
	}
    if (i == -1) {
		return input;
	}

    var newString = input.substring(0,i) + replacedBy;

    if (i+txtLength < strLength) {
        newString += replace(input.substring(i+txtLength,strLength),replaceThis,replacedBy);
	}
    return newString;
}

//
// Admin module
//
/*
 * This is for creating a new User. We want the display name field to reflect
 * what first name and last name are.
 */
function refreshDisplayName(formFirstName, formLastName, formDisplayName) {
	 formDisplayName.value = formFirstName.value + ' ' + formLastName.value;
}

/*
 * If user account status is enabled, password/confirm password fields are required, and vice versa. This script is
 * used to toggle the required field indicators "*".
 */
function tooglePasswordFields(status, formPassword, formConfirmPassword) {
    if (status == USER_STATUS_ENABLED) {
        showElemById(formPassword);
        showElemById(formConfirmPassword);
    } else {
        hideElemById(formPassword);
        hideElemById(formConfirmPassword);
    }
}

/**
 * Select all buttons for edit Permission
 * @param thisForm
 * @param list
 */
function selectAllAccessItems(thisForm, list) {
    for (var i=0; i<list.length; i++) {
        var formFields = eval('document.' + thisForm.form.name + '.formAccess_' + list[i]);
        for(j=0; j<formFields.length; j++){
            if (formFields[j].value == thisForm.value) {
                formFields[j].checked = true;
                break;
            }
        }
    }
}

//
// Auth module
//
function focusLogin(formUsername, formPassword) {
    // If the email field is empty, move the cursor there.
    // If the user has a saved email address, move the cursor to the password field.
	if (formUsername.value == '' ) {
		formUsername.focus();
	} else {
		formPassword.focus();
	}
}

//
// Blogs module
//
/*
 * Allows HTML preview when composing a new post.
 */
function refreshPostPreview(thisObject, id) {
	 updateContent(id, replace(replace(thisObject.value,'\r\n','<br>'),'\n','<br>'));
}

//
// Hardware module
//
function setWarrantyExpireYear(formName, year) {
    if (year == 0) {
        return;
    }
    warrantyMonth = eval('document.' + formName + '.warrantyMonth');
    warrantyDate = eval('document.' + formName + '.warrantyDate');
    warrantyYear = eval('document.' + formName + '.warrantyYear');
    purchaseMonth = eval('document.' + formName + '.purchaseMonth');
    purchaseDate = eval('document.' + formName + '.purchaseDate');
    purchaseYear = eval('document.' + formName + '.purchaseYear');

    warrantyMonth.value = purchaseMonth.value;
    warrantyDate.value = purchaseDate.value;
    var purchaseYear = purchaseYear.value;

    if (purchaseYear != 0) {
        purchaseYear = parseInt(purchaseYear) + parseInt(year);
    }
    warrantyYear.value = purchaseYear;
}

//
// Issues module
//
function toggleIssueDueDate(formName, thisObject) {
    dueDateMonth = eval('document.' + formName + '.dueDateMonth');
    dueDateDate = eval('document.' + formName + '.dueDateDate');
    dueDateYear = eval('document.' + formName + '.dueDateYear');

    if (thisObject.value==1) {
        dueDateMonth.disabled = false;
        dueDateDate.disabled = false;
        dueDateYear.disabled = false;
    } else {
        dueDateMonth.disabled = true;
        dueDateDate.disabled = true;
        dueDateYear.disabled = true;
    }
}

//
// Portal module
//
/*
 * This would toggle an iFrame on/off.
 */
function toggleIframe(id, href){
	thisIframe = getObjectByID(id);
	thisIframe.src = href;
}

//
// Reports module
//
function checkReportTypeEnabled(reportTypeField) {
    reportTypeField.form.sub.disabled = (reportTypeField.value == '');
}

