// JavaScript Document
window.onerror=null
//Global
var isCSS, isW3C, isIE4, isNN4, isIE6CSS;

function initAPI(){
	if(document.images){
		isCSS = (document.body && document.body.style) ? true : false;
		isW3C = (isCSS && document.getElementById) ? true : false;
		isIE4 = (isCSS && document.all) ? true : false;
		isNN4 = (document.layers) ? true : false
		isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
	}
}
function initPage(){
	initAPI();
}

//set event handler
window.onload = initPage;

// API Functios
function seekLayer(doc,name){
	var theObj;
	for (var i =0; i< doc.layers.length; i++){
	if(doc.layers[i].name == name){
	theObj = doc.layers[i];
	break;
	}
//Dive into nested layers if necessary
if (doc.layers[i].document.layers.length > 0){
		theObj =  seekLayer(document.layers[i].document, name);
		}
	}
	return theObj;
}
	
// Convert object name string or object reference
// into a valid element object reference

function getRawObject(obj){
	var theObj;
	if(typeof obj == "string") {
		if(isW3C){
				theObj = document.getElementById(obj);
			} else if (isIE4) {
				theObj = document.all(obj);
			} else if (isNN4) {
				theObj = seekLayer(document, obj);
			}
		} else {
			//pass through object reference
			theObj = obj;
		}
			return theObj;
	}
	
//Convert object name string or object reference
//into a valid style (orNN4 layer) reference
function getObject(obj){
	var theObj = getRawObject(obj);
	if(theObj && isCSS) {
	theObj = theObj.style;
	}
	return theObj;
}

function writeToLayer(object,content){
	if (document.layers) {
		document.layers[object].document.open();
		document.layers[object].document.write("&nbsp;"+content);
		document.layers[object].document.close();
	}
	else if (document.all){
		 var textArea = getRawObject(object);
		 textArea.innerHTML = "&nbsp;" + content;
	}
}
//Limits the no of selected items in a list
function checkLimits(list, maximum) {

    var options = list.options;
    var size = options.length;
    var numSelected = 0;


    for (var i = 0; i < size; ++i) {
        if (numSelected <= maximum) {
            if (options[i].selected) ++numSelected;
        }
        else {
            options[i].selected = false;
        }
    }
    if (numSelected > maximum) {

        alert('You have tried to select more than ' + maximum + ' options.\n' +
         'Your selection has been limited to the first ' + maximum + ' option(s).');

        return false;
    }
    return true;
} 

function CheckLength(textbox, max) {
    //get the counter
    var counter = getRawObject('Counter');
    maxLen = max; // max number of characters allowed
    if (textbox.value.length >= maxLen) {
        // Alert message if maximum limit is reached.
        // If required Alert can be removed.
        var msg = "You have reached your maximum limit of characters allowed";
        alert(msg);
        // Reached the Maximum length so trim the textarea
        textbox.value = textbox.value.substring(0, maxLen);
        counter.value = 0;
    }
    else { // Maximum length not reached so update the value of my_text counter
        counter.value = maxLen - textbox.value.length;
    }
}