FormHelper = function(){
	//HBI is a class
	return this;
}

FormHelper.toggleCheckUncheck = function(control,idprefix){
	var aInputs = document.getElementsByTagName("INPUT");
	var aCheckboxes = new Array();
	var input ;
	
	for(var i=0; i < aInputs.length; i++){	
		input = aInputs[i];
		if(input.type=="checkbox" && (idprefix.length==0 || (input.id.indexOf(idprefix) == 0 && input.id.length))){
			input.checked = control.checked;
		}
	}
	return;
}

FormHelper.setFuseactionAndSubmit = function(form,fuseaction){
	form.fuseaction.value = fuseaction;
	form.submit();
	return true;
}


FormHelper.addHelpLink = function(helpButtonId,helpMessage,showEvent,hideEvent){
	var helpButton = document.getElementById(helpButtonId);
	
	var helpLink = HBI.createElement('DIV',helpButton);
	
	!showEvent? showEvent="mouseover":null;
	!hideEvent? hideEvent="mouseout":null;
	
	HBI.addClass(helpLink,"helpLink");
	helpLink.innerHTML = helpMessage;	
	
	helpButton.helpLink = helpLink;
	helpButton.helpLink.style.display = 'none';
	
	fnHelpButtonMouseOver=function(ev) {
		helpButton.helpLink.style.display = 'block';
		helpButton.helpLink.style.zIndex=1000;
		FormHelper.hideSelects();
	}
	fnHelpButtonMouseOut=function(ev) {
		helpButton.helpLink.style.display = 'none';
		helpButton.helpLink.style.zIndex=1000;
		FormHelper.showSelects();
	}
	

	HBI.addEvent(helpButton,showEvent,fnHelpButtonMouseOver);
	HBI.addEvent(helpButton,hideEvent,fnHelpButtonMouseOut);

}

FormHelper.hideSelects = function(){
	var aSelects 	= document.getElementsByTagName("SELECT");
	var iSelect;
	for(iSelect=0;iSelect < aSelects.length; iSelect++){
		aSelects[iSelect].style.display = 'none';
	}
}

FormHelper.showSelects = function(){
	var aSelects 	= document.getElementsByTagName("SELECT");
	var iSelect;
	for(iSelect=0;iSelect < aSelects.length; iSelect++){
		aSelects[iSelect].style.display = 'inline';
	}
}

FormHelper.hasValue = function (sText, bTrim) {
	var str					= "";
	
	if (sText.length == 0) {
		return false;
	}
	else {
		if(bTrim) {
		    //trim whitespace - for backward compatability this is optional
		    str				= sText.replace(/^\s+/,'').replace(/\s+$/,'');
		    if (str.length == 0) {
		    	return false;
		    }
		}
		return true;
	}
}

FormHelper.checkMaxChars = function (sText, nMaxChars) {
	return sText.length <= parseInt(nMaxChars);
}

FormHelper.hasFileType = function(sFileName, lstFileTypes, cDelimeter) {
	var cDelimeter 		= cDelimeter || ",";
	var aFileTypes		= lstFileTypes.split(cDelimeter);
	
	for (i = 0; i < aFileTypes.length; i++) {
  		eval("format = /(\." + aFileTypes[i] + ")$/");
  		if (format.test(sFileName)) return true;
	}
	
	return false;
}

/*
 * Find out if a checkbox has been selected
 */
FormHelper.isCheckboxSelected = function(objCheckbox) {
	return FormHelper.numberCheckboxSelected(objCheckbox) > 0;
}

FormHelper.isInteger = function(nNumber) {
	var format = /^-?\d+$/;
  	return format.test(nNumber)
}

FormHelper.isMoney = function(nAmount) {
	// \u00A3 == £
	var format = /^(\u00A3|\$)?([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{2})?$/;
  	return format.test(nAmount);
}
FormHelper.isPercent = function(nAmount) {
	var format = /^\d+(\.[0-9]{1,3})?(%)?$/;
  	return format.test(nAmount);
}
FormHelper.isNumeric = function(nAmount) {
	var format = /^\d+(\.\d+)?$/;
  	return format.test(nAmount);
}

/*
 * Check if an option is selected in a group of radio buttons
 */
FormHelper.isRadioSelected = function(objRadioButton) {
	
	// more than one radio button in group
	if (objRadioButton.length) {
		for (iCount=0; iCount < objRadioButton.length; iCount++) {
			if (objRadioButton[iCount].checked) return true;
		}
	
	// check for only one radio button in group	
	} else {
		if (objRadioButton.checked) return true;
	}
	
	return false;
}

/*
 * Check if is a valid date (dd/mm/yyyy)
 */
FormHelper.daysInMonth = function(monthNumber, yearNumber) {
	if (monthNumber == 4 || monthNumber == 6 || monthNumber == 9 || monthNumber == 11) {
		return 30;
	} else if (monthNumber == 2) {
		if ((yearNumber % 4) == 0) {
			return 29;
		} else {
			return 28;
		}
	} else {
		return 31;
	}
}
FormHelper.isValidDate = function(strDate) {
	// get delimeter
	var getFirstChar	= /[^\d]/;
	chrDel 				= getFirstChar.exec(strDate);
	
	// check that date is in a valid format (ie dd/mm/yyyy)
	eval("var dateFormat = /^[0-3][0-9]\\" + chrDel + "[0-1][0-9]\\" + chrDel + "[0-9]{4}$/");
	if (!dateFormat.test(strDate)) return false;
  	
  	tmpDateArray	= strDate.split(chrDel);
  	day				= parseInt(tmpDateArray[0],10);
  	month			= parseInt(tmpDateArray[1],10);
  	year			= parseInt(tmpDateArray[2],10);
  	// valid month
  	if (month < 1 || month > 12) {
  		return false;	
  	
  	// check that have correct number of days
	} else if (day < 1 || day > FormHelper.daysInMonth(month, year)) {
  		return false;
  	}
  	
  	return true;
}

/*
 * Find out number of checkboxes selected
 */
FormHelper.numberCheckboxSelected = function(objCheckbox) {
	var numberChecked		= 0;
	
	// more than one Checkbox in group
	if (objCheckbox.length) {
		for (iCount=0; iCount < objCheckbox.length; iCount++) {
			if (objCheckbox[iCount].checked) numberChecked++;
		}
	
	// check for only one checkbox in group	
	} else {
		if (objCheckbox.checked) numberChecked++;
	}
	return numberChecked;
}

/*
 * Get the body content of an iFrame - used for the Rich Text editor validation
 */
FormHelper.getBodyFromIFrameById = function(sTagID) {
	var eIFrameCollection					= document.getElementsByTagName("iFrame");
	var eIFrame							= null;
	var doc								= null;
	var root							= null;
	var sBodyValue						= "";
	
	// get the content of the BODY 
	for (i=0; i < eIFrameCollection.length; i++) {
		if (eIFrameCollection[i].id == sTagID) {
			eIFrame						= eIFrameCollection[i];
			
			doc = eIFrame.contentWindow ? eIFrame.contentWindow.document : eIFrame.contentDocument;
        
			if (doc) {
				root = doc.body ? doc.body : doc.documentElement;
				sBodyValue = root.innerHTML;
			}
		}
	}
    
    return sBodyValue;
}

/*
 * Check if a strign is a valid URL
 */
FormHelper.isValidURL = function(sText, bRequirePrefix) {
	if (bRequirePrefix) {
		var format = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	} else {
		var format = /((ftp|http|https):\/\/)?(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	}
  	return format.test(sText);
}

/*
 * Get the label for a particular form element 
 */
FormHelper.getLabelForId = function(id){
	var aLabels			= document.getElementsByTagName('label');
	for(var i=0; i < aLabels.length; i++){
		if(aLabels[i].htmlFor == id){
			var eLabel =  aLabels[i];
		}
	}
	return eLabel;
}
