﻿
function resetAllForms(){
    var allForms = document.forms;
    var len = allForms.length;
    
    for (var i=0; i<len; i++)
        allForms[i].reset();
}


function doc(id)
{
	var ie	= (document.all);
	var ns4	= document.layers ? true : false;
	var dom	= document.getElementById && !document.all ? true : false;

	if (dom)
		return document.getElementById(id);
	else if (ie)
		return document.all[id];
	else if(ns4)
		return document.layers[id];
}


function errorHandling(control, message)
{
    doc(control).innerHTML = message;
    doc(control).style.display = 'block';
}


function checkValues(thisForm)
{
	var count = 0;
    var errStr = 'Please make sure all fields have been completed.';
    var valid = false;
    
    // count the number of hidden form items have no value    
	for (var i=0; i<doc(thisForm).elements.length; i++) {
		if (doc(thisForm).elements[i].type == "hidden" && doc(thisForm).elements[i].value == '')
	        count++;
	}
    

    
    if (count>0)
        errorHandling('err_form',errStr);
    else
        valid = true;
        
     
    return valid;
}


//---------------------------------------------------------------------------------------------------------------------------------------------
// Ethical.aspx
//---------------------------------------------------------------------------------------------------------------------------------------------
function ethicalCalc(value, weight, itemId, position)
{
    // apply weighting
    var item = doc('h_' + itemId + '_' + position);
    value = parseInt(value) * parseInt(weight);
    item.value = value;
    
    // send over to overall amount
    var d = doc(item),
    c = doc('count_' + itemId),
    t = doc('total_' + itemId);
    
    var errStr = '';
    // redefine value
    value = 0;
    
    // run thru all textboxes and add up values
    for (var i=1; i<=parseInt(c.value); i++) {
        // if value is empty then display an asterisk
        if (doc('h_' + itemId + '_' + i).value == '' || doc('h_' + itemId + '_' + i).value == null)
           value += 0;
        else
            value += parseInt(doc('h_' + itemId + '_' + i).value);
    }
    
    // output to total textbox
    t.value = value;
}


//---------------------------------------------------------------------------------------------------------------------------------------------
// Attitude.aspx
//---------------------------------------------------------------------------------------------------------------------------------------------
function attitudeCalc(value, weight, position)
{
    // apply weighting
    var item = doc('h_' + position);
    item.value = weight;
    
    var c = doc('count_1'),
    t = doc('total_1');
    
    value = 0;
    for (var i=1; i<=parseInt(c.value); i++) {
        // if value is empty then display an asterisk
        if (doc('h_' + i).value == '' || doc('h_' + i).value == null)
            value += 0;
        else
            value += parseInt(doc('h_' + i).value);
    }
    
    t.value = value;
}