// JavaScript Document
function submitForm(form){
  document.getElementById(form).submit();
}

function data_change(input){
  var field = document.getElementById(input);
  var check = true;
  var value = field.value;
	
	// correct Not A Number values to numbers
  	for(var i=0;i < field.value.length; ++i){
        var new_key = value.charAt(i); 
        if(((new_key < "0") || (new_key > "9")) && 
        !(new_key == "")){
        check = false;
        break;
        }
  	}
	
	if(!check){
	  var chars = field.value.length-1;
	  var new_value = field.value;
	  field.value = new_value.substr(0,chars);
	}
	
	if(input == "day"){
		if(value > 31 || value < 1){
			field.value = "0";
		}
	} else if (input == "month"){
		if(value > 12 || value < 1){
			field.value = "0";
		}
	} else if (input == "year"){
		if(value > 2100 || value < 1){
			field.value = "";
		}
	}
}
function MM_openBrWindow(theURL,winName,features) { //v2.0 URL, window name, 'resizable=yes,scrolling=auto,width=450,height=400'
  window.open(theURL,winName,features);
}

function addTagsToSelection(start,end) {
	var startTag 	= start;
	var endTag		= end;
	
	theSelection = document.selection.createRange().text;
   	if (!theSelection)
    	return false;
	if (document.selection.createRange().parentElement().tagName != 'TEXTAREA')
		return false;
	document.selection.createRange().text = startTag + theSelection + endTag;
}

function insertValueToArea(value) { // needs a form in which the an element is called main 
    var myQuery = document.forms[0].main;
	var myLink = value;

    if(myLink != "") {
        var chaineAj = "";
        chaineAj += myLink;

        //IE support
        if (document.selection) {
            myQuery.focus();
            sel = document.selection.createRange();
            sel.text = chaineAj;
            myQuery.focus();
        }
        //MOZILLA/NETSCAPE support
        else if (myQuery.selectionStart || myQuery.selectionStart == "0") {
            var startPos = myQuery.selectionStart;
            var endPos = myQuery.selectionEnd;
            var chaineSql = myQuery.value;

            myQuery.value = chaineSql.substring(0, startPos) + chaineAj + chaineSql.substring(endPos, chaineSql.length);
        } else {
            myQuery.value += chaineAj;
        }
    }
}

function selectChunk() { // Needs a form with three radio buttons named 'chunk' and values: character, word, sentence
    var chunk, range
    var x = window.event.clientX
    var y = window.event.clientY

    for (var i = 0; i < document.forms[0].chunk.length; i++) {
        if (document.forms[0].chunk[i].checked) {
            chunk = document.forms[0].chunk[i].value
            break    
        }
    }
    if (window.event.srcElement.parentTextEdit) {
        range = window.event.srcElement.parentTextEdit.createTextRange()
        range.collapse()
        range.moveToPoint(x, y)
        range.expand(chunk)
        range.select()
    }
}
function divBoxOpen(divBoxID) {
document.getElementById('divOpen_' + divBoxID).style.display = "block";
document.getElementById('divClose_' + divBoxID).innerHTML="<a href=\"javascript:divBoxClose('" + divBoxID + "');\">Close -</a>";
}
function divBoxClose(divBoxID) {
document.getElementById('divOpen_' + divBoxID).style.display = "none";
document.getElementById('divClose_' + divBoxID).innerHTML="<a href=\"javascript:divBoxOpen('" + divBoxID + "');\">Open +</a>";
} 