registerNS("Misc");

// Container, tag type, class name
function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}




Misc.checkAllBoxesWithClass = function(className){
	var checkBoxes = getElementsByClassName(document, "input", className);
	
	for(var i = 0; i < checkBoxes.length; i++){
		checkBoxes[i].checked = "checked";
	}
}

Misc.uncheckAllBoxesWithClass = function(className){
	var checkBoxes = getElementsByClassName(document, "input", className);
	
	for(var i = 0; i < checkBoxes.length; i++){
		checkBoxes[i].checked = "";
	}
}



// If the passed input contains originalText, clears the input
Misc.clearText = function(input, originalText){
	
	if(input.value == originalText)
		input.value = '';
	
}



// Will set the value of a certain element (given by id) to be hint if
// it currently has no value, and then make that hint automatically
// disappear when the element is focussed
Misc.setInlineHint = function(id, hint){
	Misc.setInline(id, hint, true);
}


// Same as above, except the text doesn't disappear when the element
// is focussed
Misc.setInlineStarter = function(id, start){
	Misc.setInline(id, start, false);
}


Misc.setInline = function(id, content, isHint){
	
	var elem = document.getElementById(id);
	
	if(!elem.value || elem.value == "" || elem.value == content){
		
		// Make sure the arrays have been declared
		if(!Misc.inlineHintElems) Misc.inlineHintElems = new Array();
		if(!Misc.inlineHints) Misc.inlineHints = new Array();
	
		Misc.inlineHintElems.push(id);
		Misc.inlineHints.push(content);
		elem.value = content;
		
		if(isHint)
			elem.onfocus = function(){Misc.clearText(elem, content)};
	
	}
	
}


// Called when the form is submitted. Removes all hints from elements
// so that they are not stored in the DB
Misc.clearHints = function(){
	
	if(!Misc.inlineHintElems) return;
	
	for(var i = 0; i < Misc.inlineHintElems.length; i++){
		Misc.clearText(document.getElementById(Misc.inlineHintElems[i]), Misc.inlineHints[i]);
	}
	
}




// Passed the id of a textarea. If the textarea has no content, sets its number of rows
// to be 1 to save space. When the textarea is clicked, the number of rows is changed
// to expandedNumRows
Misc.setExpandingTA = function(id, expandedNumRows){
	
	var elem = document.getElementById(id);
	
	if(!elem.value){
		elem.rows = 1;
		elem.onclick = function(){elem.rows = expandedNumRows};
		elem.onchange = function(){elem.rows = expandedNumRows};
	} else {
		elem.rows = expandedNumRows;
	}
	
}



Misc.is_int = function(sText){
	
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	
	for (var j = 0; j < sText.length && IsNumber == true; j++) { 
	  Char = sText.charAt(j); 
	  if (ValidChars.indexOf(Char) == -1) {
	     IsNumber = false;
	  }
	}
	
	return IsNumber;
}


// Given a date, checks that it is in dd/mm format.
// If so, returns date
// If not but the date can be corrected, returns the corrected date
// Otherwise returns -1
Misc.validDate = function(date){
	
	var comp = date.split('/');
	if(!(comp.length == 2 || comp.length == 3)) return -1;
	
	if(comp[0].length < 1 || !Misc.is_int(comp[0]) || comp[0] < 1 || comp[0] > 31) return -1;
	if(comp[0].length == 1) comp[0] = "0" + comp[0];
	
	if(comp[1].length < 1 || !Misc.is_int(comp[1]) || comp[1] < 1 || comp[1] > 12) return -1;
	if(comp[1].length == 1) comp[1] = "0" + comp[1];
	
	if(comp[2]){
		if(comp[2].length == 4) comp[2] = comp[2][2] + comp[2][3];
		if(comp[2].length != 2 || !Misc.is_int(comp[2])) return -1;
	}
	
	return comp[0] + "/" + comp[1];
	
}





// Given a date, checks that it is in dd/mm/yy format.
// If so, returns date
// If not but the date can be corrected, returns the corrected date
// Otherwise returns -1
Misc.validLongDate = function(date){
	
	var comp = date.split('/');
	if(!(comp.length == 3)) return -1;
	
	if(comp[0].length < 1 || !Misc.is_int(comp[0]) || comp[0] < 1 || comp[0] > 31) return -1;
	if(comp[0].length == 1) comp[0] = "0" + comp[0];
	
	if(comp[1].length < 1 || !Misc.is_int(comp[1]) || comp[1] < 1 || comp[1] > 12) return -1;
	if(comp[1].length == 1) comp[1] = "0" + comp[1];
	
	if(comp[2].length == 4) comp[2] = comp[2][2] + comp[2][3];
	if(comp[2].length != 2 || !Misc.is_int(comp[2])) return -1;
	
	return comp[0] + "/" + comp[1] + "/" + comp[2];
	
}



// Returns the index of the element in the array.
// if the element is not present, returns -1
Misc.positionOf = function(elem, arr){
	
	if(!arr) return -1;
	
	for(var i = 0; i < arr.length; i++){
		if(arr[i] == elem) return i;
	}
	return -1;
}




Misc.checkDateFields = function(){
	
	var dateFields = Misc.getElementsByClassName("datefield", document.body);
	
	if(dateFields){
		for(var i = 0; i < dateFields.length; i++){
			var field = dateFields[i];
			if(field && field.value != 'dd/mm/yy' && field.value != ''){
				if(!Misc.dateFieldOK(field)){ 
					return false;
				}
			}
		}
	}
	
	return true;
	
}



Misc.dateFieldOK = function(dateField){

	var newDate = Misc.validLongDate(dateField.value);

	if(newDate == -1){
		alert("The date '" + dateField.value + "' is not in a valid format. Please enter the date in the format 'dd/mm/yy'");
		return false;
	} else {
		dateField.value = newDate;
		return true;
	}
	
}





Misc.getElementsByClassName = function( strClassName, obj ) {
    var ar = arguments[2] || new Array();
    var re = new RegExp("\\b" + strClassName + "\\b", "g");

    if ( re.test(obj.className) ) {
        ar.push( obj );
    }
    for ( var i = 0; i < obj.childNodes.length; i++ )
        Misc.getElementsByClassName( strClassName, obj.childNodes[i], ar );
    
    return ar;
}




