function isFilled(sString){
	var whitespace = ' \t\n\r';
   var i;
   
   if (sString != undefined && sString != null)
   	sString = sString + ''
   
   if((sString == null) || (sString.length == 0))
		return false;
		
   for(i = 0; i < sString.length; i++){
      var c = sString.charAt(i);

      if(whitespace.indexOf(c) == -1)
			return true;
   }
   return false;
}
function isEmail(s){
   if(!isFilled(s))
      return false;
   var i = 1;
   var sLength = s.length;
   while((i < sLength) && (s.charAt(i) != '@')){
      i++
   }
   if((i >= sLength) || (s.charAt(i) != '@'))
      return false;
   else i += 2;
      while((i < sLength) && (s.charAt(i) != '.')){
         i++
      }
   if((i >= sLength - 1) || (s.charAt(i) != '.'))
      return false;
   return true;
}
function isNumeric(s){
	if (!isFilled(s))
      return false;
		
	var checkOK = '0123456789.,';
   for (i = 0; i < s.length; i++){
      ch = s.charAt(i);
      for (j = 0;  j < checkOK.length;  j++)
         if (ch == checkOK.charAt(j))
            break;
      if (j == checkOK.length){
         return false;
      }
   }
   return true;
}
function isValidString(s, checkString){
   for (i = 0; i < s.length;  i++){
      ch = s.charAt(i);
      for (j = 0;  j < checkString.length;  j++)
         if (ch == checkString.charAt(j))
            break;
      if (j == checkString.length){
         return false;
      }
   }
   return true
}
function isInValidString(s, checkString){
   for (i = 0; i < s.length;  i++){
      ch = s.charAt(i);
      for (j = 0;  j < checkString.length;  j++)
         if (ch == checkString.charAt(j))
            return false;
   }
   return true
}
function isDate(sDay, sMonth, sYear){
   if (!isFilled(sDay) || !isFilled(sMonth) || !isFilled(sYear))
      return false;
   var iDay = parseInt(sDay, 10);
   var iMonth = parseInt(sMonth, 10);
   var iYear = parseInt(sYear, 10);
	if (iMonth < 1 || iMonth > 12)
      return false;
   if ((iMonth == 1 || iMonth == 3 || iMonth == 5 || iMonth == 7 || iMonth == 8 || iMonth == 10 || iMonth == 12) && (iDay < 1 || iDay > 31))
      return false;
   if ((iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11) && (iDay < 1 || iDay > 30))
      return false;
   if ((iMonth == 2)){
      if ((((iYear % 4) == 0)) && (iDay < 1 || iDay > 29)){
         return false;
      }
      else if ((((iYear % 4) != 0)) && (iDay < 1 || iDay > 28)){
         return false;
      }
   }
   return true;
}
function isTime(sHour, sMinute){
   if (!isFilled(sHour) || !isFilled(sMinute))
      return false;
   if(sMinute.length != 2)
      return false;
   var iHour = parseInt(sHour, 10);
   var iMinute = parseInt(sMinute, 10);
	if (iHour < 1 || iHour > 12)
      return false;
	if (iMinute < 0 || iMinute > 59)
      return false;
   return true;
}
function isRadioChecked(oRadio){
	if (oRadio.length) {
   	for(var x = 0; x < oRadio.length; x++)
   		if(oRadio[x].checked)
   			return true;
   } else {
   	if(oRadio.checked)
   		return true;
   }
   		
	return false;
}
function textMaxLength(oText, iMax) {
	if(oText.value.length > iMax) {
		alert('This field can be no longer than ' + iMax + ' characters long.');
		oText.value = oText.value.substr(0, iMax);
	}
}
function openWindow(sUrl, sName , sProperties){
	window.open(sUrl, sName, sProperties);
}
function openWindowMove(sUrl, sName, sProperties, iWidth, iHeight, iMoveX, iMoveY, bDialog){
	var iX, iY;
	
	iX = iMoveX;
	if(iMoveX == 'left')
		iX = 0
	if(iMoveX == 'middle')
		iX = (screen.width / 2) - (iWidth / 2)
	if(iMoveX == 'right')
		iX = screen.width - iWidth - 10;
		
	iY = iMoveY;	
	if(iMoveY == 'top')
		iY = 0;
	if(iMoveY == 'middle')
		iY = ((screen.height - 56) / 2) - (iHeight / 2);
	if(iMoveY == 'bottom')
		iY = screen.height - 56 - iHeight;

	sProperties += ',screenX=' + iX + ',screenY=' + iY + ',left=' + iX + ',top=' + iY +
					',width=' + iWidth + ',height=' + iHeight +
					',dialogWidth=' + iWidth + 'px,dialogHeight=' + iHeight + 'px';

	if (bDialog) {
		sProperties = replace(sProperties, ',', ';');
		window.showModalDialog(sUrl, sName, sProperties);
	} else {
		window.open(sUrl, sName, sProperties);	
	}
}
function popupLink(sLink){
	window.opener.location = sLink;
	window.close();
}
function checkAll(oMaster, oGroup){
	if(oGroup && oGroup.length) {
   	for(var i=0; i < oGroup.length; i++) {
   		oGroup[i].checked = oMaster.checked;
   	}
   } else if(oGroup) {
   	oGroup.checked = oMaster.checked;
   }
}
function checkMaster(oMaster, oCheck){
	oGroup = eval('oCheck.form.' + oCheck.name);

	if(oGroup.length) {
   	for(var i=0; i < oGroup.length; i++) {
   		if(!oGroup[i].checked) {
   			checkAll(oGroup[i], oMaster)
   			return true;
   		}
   	}
   	
   	checkAll(oGroup[i - 1], oMaster)
   } else {
   	oMaster.checked = oGroup.checked;
   }
}
function refresh(){
	window.location.reload();
}
function listAdd(oList, oText) {
	var aValues = oText.value.split('\n');
	
	for(i = 0; i <  aValues.length; i++) {
		if(isFilled(aValues[i].replace('\r', '')))
      	oList.options[oList.options.length] = new Option(aValues[i].replace('\r', ''), 'new');
	}
	
	oText.value = ''
	oText.focus();
}
function listJump(oForm, iIndex, iDifference) {
	var oFrom_list = oForm.elements[iIndex];
	var oTo_list = oForm.elements[iIndex + iDifference];
	
	if (oFrom_list.options && oTo_list.options) {
		oTo_list.options[oTo_list.options.length] = new Option(oFrom_list[oFrom_list.selectedIndex].text, oFrom_list[oFrom_list.selectedIndex].value);
		oTo_list.selectedIndex = oTo_list.options.length - 1;
		
		oFrom_list[oFrom_list.selectedIndex] = null;
	}
}
function listDelete(oList, sRecord) {
	if(oList.selectedIndex >= 0) {
		sRecord.value += oList.options[oList.selectedIndex].value + '|||||'

		for(i = oList.selectedIndex; i < oList.options.length -1; i++) {
			oList.options[i].value = oList.options[i + 1].value;
			oList.options[i].text = oList.options[i + 1].text;
		}
		
		oList.options.length -= 1;
	}
}
function listMove(oList, iDifference) {
	var iSelected = oList.selectedIndex;
	
	if(iSelected + iDifference >= 0 && iSelected + iDifference < oList.length && iSelected != -1){
		var sTmpValue1 = oList.options[iSelected].value;
		var sTmpValue2 = oList.options[iSelected].text;
			
		oList.options[iSelected].value = oList.options[iSelected + iDifference].value;
		oList.options[iSelected].text = oList.options[iSelected + iDifference].text
		
		oList.options[iSelected + iDifference].value = sTmpValue1;
		oList.options[iSelected + iDifference].text = sTmpValue2;
		
		oList.selectedIndex = oList.selectedIndex + iDifference
	}
}
function listPost(oList, sValue, sText, sDelete) {
	sValue.value = '';
	sText.value = '';

	for(i = 0; i < oList.options.length; i++) {
		sValue.value += oList.options[i].value + '|||||';
		sText.value += oList.options[i].text + '|||||';
	}
	
	if (sValue.value.length > 5)
		sValue.value = sValue.value.substr(0, sValue.value.length -5);
	if (sText.value.length > 5)
		sText.value = sText.value.substr(0, sText.value.length -5);
	if (sDelete.value.length > 5)
		sDelete.value = sDelete.value.substr(0, sDelete.value.length -5);
}
function listEdit(oList, sLink) {
	if(oList.selectedIndex >= 0 && oList.options[oList.selectedIndex].value != 'new')
		document.location = sLink + oList.options[oList.selectedIndex].value;
}
function radioSelect(oRadio, sValue) {
	for(var i = 0; i < oRadio.length; i++){
		if(oRadio[i].value == sValue)
			oRadio[i].checked = true;
		else
			oRadio[i].checked = false;
	}
}
function checkForm(theform) {
	
	var sQuestion_tag = 'q_id_';
	var sOther_answer_tag = 'o_q_id_';
	var iQuestion_id;
	
	for(var i = 0; i < theform.elements.length; i++) {
		if(theform.elements[i].getAttribute('validate') == 'True') {
			switch(theform.elements[i].type) {
			case 'checkbox':
			case 'radio':
   			if(theform.elements[i].getAttribute('required') == 'True') {
   				if(!isRadioChecked(eval('theform.' + theform.elements[i].name))) {
   					alert('Please select ' + theform.elements[i].getAttribute('title') + '.');
   					
   					return false;
   				}
   			}
				
				break
			case 'select-one':
			case 'select-multiple':
				if(isFilled(theform.elements[i].getAttribute('list'))) {
					var sTag = theform.elements[i].getAttribute('list');
					
					eval('listPost(theform.' + sTag + '_list, theform.' + sTag + '_value, theform.' + sTag + '_text, theform.' + sTag + '_delete)');
				}
				
   			if(theform.elements[i].getAttribute('required') == 'True') {
   				if(theform.elements[i].selectedIndex == -1 || eval('theform.' + theform.elements[i].name + '[' + theform.elements[i].selectedIndex + '].value') == '') {
   					alert('Please select a "' + theform.elements[i].getAttribute('title') + '".');
   					
   					return false;
   				}
   			}
				
				break
			case 'hidden':
   			if(theform.elements[i].getAttribute('group') == 'True') { 
   				aTags = theform.elements[i].value.split(',');
   				
   				aValues = new Array(aTags.length)
   				
   				for(var j = 0; j < aTags.length; j++) {
   					oElement = eval('theform.' + aTags[j])
   					
   					switch(oElement.type) {
   					case undefined:
      					for(var x = 0; x < oElement.length; x++) {
      						if(oElement[x].checked) {
      							aValues[j] = oElement[x].value;
      							
      							break;
      						}
      					}
      					
      					break;
      				case 'select-one':
      				case 'select-multiple':
      					if(oElement[oElement.selectedIndex].value.substring(0, '|||||a_id_other'.length) == '|||||a_id_other') {
                     	iQuestion_id = oElement.name.substring(sQuestion_tag.length, oElement.name.length);
                     	
                     	aValues[j] = eval('theform.' + sOther_answer_tag + iQuestion_id + '.value');
      					} else if(isFilled(oElement[oElement.selectedIndex].value)) {
     							aValues[j] = oElement[oElement.selectedIndex].text;
     						}
      					
      					break;
      				}      					

   					if((aValues[j] == undefined || aValues[j] == '') && theform.elements[i].getAttribute('required') == 'True') {
   						alert('Please provide an answer to all the "' + theform.elements[i].getAttribute('title') + '" fields.');
   							
   						return false;
   					}
   					
   					if(theform.elements[i].getAttribute('exclusive') == 'True') {
      					for(var k = 0; k < j; k++) {
      						if(aValues[k] == aValues[j] && aValues[k] != undefined && aValues[k] != '' && aValues[k] != '|||||a_id_other|||||a_other' && isFilled(aValues[k])) {
      							alert('Please make sure that all the "' + theform.elements[i].getAttribute('title') + '" fields are different.');
      							
      							return false;
      						}
      					}
      				}
   				}
   			}
   			
   			if(theform.elements[i].getAttribute('date') == 'True') {
   				if (theform.elements[i].getAttribute('validate').toLowerCase == 'True'.toLowerCase && !isDate(eval('theform.' + theform.elements[i].name + '_day.value'), eval('theform.' + theform.elements[i].name + '_month.value'), eval('theform.' + theform.elements[i].name + '_year.value'))){
						alert('Please make sure that all the "' + theform.elements[i].getAttribute('title') + '" date is valid.');
						
						return false;   					
   				}
   				theform.elements[i].value = eval('theform.' + theform.elements[i].name + '_year.value') + '-' + eval('theform.' + theform.elements[i].name + '_month.value') + '-' + eval('theform.' + theform.elements[i].name + '_day.value') 
   			}
   						
				break
			case 'text':
			case 'textarea':
				if(!isInValidString(theform.elements[i].value, '|')) {
					alert('The "' + theform.elements[i].getAttribute('title') + '" field cannot contain |.');
					
					return false;
				}				
				
   			//Required
   			if(theform.elements[i].getAttribute('required') == 'True') {
   				if(!isFilled(theform.elements[i].value)) {
   					alert('Please fill out the "' + theform.elements[i].getAttribute('title') + '" field.');
   					
   					return false;
   				}
   			}
   
   			//Email
   			if(theform.elements[i].getAttribute('email') == 'True') {
   				if(!isEmail(theform.elements[i].value)) {
   					alert('Please make sure that the "' + theform.elements[i].getAttribute('title') + '" field is a valid email address.');
   					
   					return false;
   				}
   			}
   			
   			//Numeric
   			if(theform.elements[i].getAttribute('number') == 'True') {
   				if(!(isNumeric(theform.elements[i].value) || (theform.elements[i].getAttribute('required') != 'True' && !isFilled(theform.elements[i].value)))) {
   					alert('Please make sure that the "' + theform.elements[i].getAttribute('title') + '" field is numeric.');
   					
   					return false;
   				}
   			}
   
   			//Length min
   			if(isFilled(theform.elements[i].getAttribute('length_min'))) {
   				if(theform.elements[i].value.length < theform.elements[i].getAttribute('length_min')) {
   					alert('The "' + theform.elements[i].getAttribute('title') + '" field needs to be more than ' + theform.elements[i].getAttribute('length_min') + ' characters.');
   					
   					return false;
   				}
   			}
   			
   			//Length max
   			if(isFilled(theform.elements[i].getAttribute('length_max'))) {
   				if(theform.elements[i].value.length > theform.elements[i].getAttribute('length_max')) {
   					alert('The "' + theform.elements[i].getAttribute('title') + '" field needs to be less than ' + theform.elements[i].getAttribute('length_max') + ' characters.');
   					
   					return false;
   				}
   			}
   				
   			//More than
   			if(isFilled(theform.elements[i].getAttribute('more_than'))) {
   				if(parseFloat(theform.elements[i].value) < theform.elements[i].getAttribute('more_than')) {
   					alert('The "' + theform.elements[i].getAttribute('title') + '" field needs to be more than ' + theform.elements[i].getAttribute('more_than') + '.');
   					
   					return false;
   				}
   			}
   			
   			//Less than
   			if(isFilled(theform.elements[i].getAttribute('less_than'))) {
   				if(parseFloat(theform.elements[i].value) > theform.elements[i].getAttribute('less_than')) {
   					alert('The "' + theform.elements[i].getAttribute('title') + '" field needs to be less than ' + theform.elements[i].getAttribute('less_than') + '.');
   					
   					return false;
   				}
   			}
			}
		}
	}

	//return confirm('Form valid. Continue?');
	return true;
}
function submitForm(oForm, sAction, sTarget, bIgnore_validation) {
	if (bIgnore_validation || checkForm(oForm)) {
		if (isFilled(sAction)) 
			oForm.action = sAction;
		
		if (isFilled(sTarget)) 
			oForm.target = sTarget;
			
		oForm.submit();
		
		oForm.target = '_self';
	} else
		return false;
}
function help(iHelp_page_id) {
	openWindowMove('/oscar/help/page.asp?h_p_id=' + iHelp_page_id, '_help', 'scrollbars=yes,resizable=yes', '300', '400', 'right', 'top');
}
function submitFormOscar(oForm, sAction) {
	oForm.action = sAction;

	return checkForm(oForm);
}
function otherUpdate(oText, sType) {
	if(isFilled(oText.value)) {
   	var sQuestion_tag = 'q_id_';
   	var sOther_answer_tag = 'o_q_id_';
   	var iQuestion_id = oText.name.substring(sOther_answer_tag.length, oText.name.length);
   	var oParent = eval('oText.form.' + sQuestion_tag + iQuestion_id);
   	
   	switch(sType) {
   	case 'radio':
   		for(var i=0; i < oParent.length; i++) {
   			if(oParent[i].value == '|||||a_id_other|||||a_other')
   				oParent[i].checked = true;
   			else
   				oParent[i].checked = false;
   		}
   		break
   	case 'checkbox':
   		for(var i=0; i < oParent.length; i++) {
   			if(oParent[i].value == '|||||a_id_other|||||a_other')
   				oParent[i].checked = true;
   		}
   		break
   	case 'select':
   		oParent.selectedIndex = oParent.length - 1;
   		
   		break
   	}
   }
}
function insertFile(oField, sRoot, sDefault_path, oTarget_image) {
	var sPath;
	
	if (oField.value != '')
		sPath = oField.value;
	else
		sPath = sDefault_path;
			
	var sImage = window.showModalDialog('/editor/file/default.asp?root=' + sRoot + '&path=' + sPath, '_file_manager', 'status=no;scrollbars=auto;resizable=no;dialogWidth=700px;dialogHeight=450px;center=yes');

	if(sImage != null && sImage != "") {
		oField.value = unescape(sImage);
		
		if(oTarget_image != null && oTarget_image != undefined) {
			oTarget_image.src = '/' + sRoot + sImage;
		}
	}
}
function insertColour(oField) {
	var sNon_hex;
	
	if (oField.value.length > 0) {
		sNon_hex = oField.value.substring(1, oField.value.length);
	}

	var sColour = window.showModalDialog('colour_wheel.asp?colour=' + sNon_hex, '_colour_wheel', 'status=no;scrollbars=auto;resizable=no;dialogWidth=276px;dialogHeight=331px;center=yes');

	if(sColour != null && sColour != '') {
		oField.value = unescape(sColour);
	}
}
function removeFile(oField, oTarget_image) {
	oField.value = '';
	
	if(oTarget_image != null && oTarget_image != undefined) {
		oTarget_image.src = '/images/spacer.gif';
	}
}
function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
function clearTextbox(oInput, sText){
	if(oInput.value == sText) {
		oInput.value = '';
	} else if(oInput.value == ''){
		oInput.value = sText;
	}
}
function popupEditor(sName, sFolder) {
	openWindowMove('/admin/editor.asp?field_name=' + sName + '&folder=' + sFolder, '_editor', 'scroll:no,status:no', '775', '504', 'middle', 'middle', false);
}	

function popupReturn(sName, sValue) {
   if (sValue != 'undefined') {
   	document.getElementById(sName).value = escape(sValue);
		document.getElementById(sName + '_preview').innerHTML = sValue;
	}
}
function passwordReminder(oForm) {
	if (oForm == null)
		oForm = document.forms.login;

	if (!isFilled(oForm.username.value)) {
		alert('Please enter you username above.');
	} else {
		oForm.action = '/scripts/password.asp'
		oForm.submit();
	}
}
function applyStyle(oDiv, sStyle) {
	oDiv.className = sStyle;
}
