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){
	for(var x = 0; x < oRadio.length; x++){
		if(oRadio[x].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){
	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 += ',width=' + iWidth + ',height=' + iHeight + ',screenX=' + iX + ',screenY=' + iY + ',left=' + iX + ',top=' + iY;
	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 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 provide an answer to the "' + theform.elements[i].getAttribute('title') + '" question.');
   					
   					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 provide an answer in the "' + theform.elements[i].getAttribute('title') + '" field.');
   					
   					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 provide an answer in 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) {
	oForm.action = sAction;

	return checkForm(oForm);
}
function submitFormVerify(oForm) {
	if (verifyForm(oForm))
		oForm.submit();
}
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 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
   	}
   }
}