// Check the given checkbox
function checkInput(input) {
    if (input.type == 'checkbox') {
        input.checked = true;
    }
}
// Uncheck the given checkbox
function uncheckInput(input) {
    if (input.type == 'checkbox') {
        input.checked = false;
    }
}
// Invert the given checkbox
function invertCheckInput(input) {
    if (input.type == 'checkbox') {
        input.checked = !input.checked;
    }
}

// Check all checkboxes in the given form
function selectAll(formName)
{
    forAllInputsDo(document.forms.namedItem(formName), checkInput);
}

// Uncheck all checkboxes in the given form
function selectNone(formName)
{
    forAllInputsDo(document.forms.namedItem(formName), uncheckInput);
}

// Invert all checkboxes in the given form
function selectInverse(formName)
{
    forAllInputsDo(document.forms.namedItem(formName), invertCheckInput);
}

// Execute the given function for all form inputs
function forAllInputsDo(form, executeFunction) {
    var inputs =form .getElementsByTagName('input');
    for (i=0; i<inputs.length; i++) {
        executeFunction(inputs[i]);
    }
}

// Return Object based on the name
function GetObjectByName(name)
{
    var obj; // string which is return referencing object by element id

    for(i=0;i<document.forms.length;i++) {
        if (obj = eval('document.forms[i].elements[\'' + name +'\']') )
        {
            return obj;
            break;
        }
    }
    return null;
}

// Limitation for text area field
function textAreaLimit(field, maxlimit)
{
    if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

// Change radio button accordingly to the other field selection
function ChangeRadioSelection(name, index)
{
	var cntrl = GetObjectByName(name);
	cntrl[index].checked = true;
}

var submitcount=0;

function checkSubmit()
{
    if (submitcount == 0) {
        submitcount++;
        return true;
    } else {
        alert('Halaman ini sedang diproses.');
        return false;
    }
}

function resize(which, maxheight, maxwidth) {

    var elem = document.getElementById(which);
    if (elem == undefined || elem == null) return false;
    if (maxheight == undefined) maxheight = 200;
    if (maxwidth == undefined) maxwidth = 500;
    if (elem.width > maxwidth && elem.height > maxheight) {
        if ((elem.width - maxwidth) > (elem.height - maxheight)){
            elem.width = maxwidth;
        }
        else {
            elem.height = maxheight;
        }
    }
    else {
        if (elem.height > maxheight) {
            elem.height = maxheight;
        }
        if (elem.width > maxwidth) {
            elem.width = maxwidth;
        }
    }
}
