//form validation
function eCheckAlpha(sn){
    s= sn.value;
    for (var i = 0; i < s.length; i++) {
        ch = s.substring(i, i + 1)
        if (!((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z") || (ch == " ")))
             return false;
        }
   return true;
}

function CheckAlpha(theForm, Fields){
	for(var i=0; i<Fields.length; i++){
		resetColor(theForm.elements[Fields[i]]);
		if (!eCheckAlpha(theForm.elements[Fields[i]])){
            alert("Field entry is not valid: "+Fields[i]);
            focusField(theForm.elements[Fields[i]]);
            return false;
        }
	}
   return true;
}

function eCheckAlphaNum(sn){
    s= sn.value;
    for (var i = 0; i < s.length; i++) {
        ch = s.substring(i, i + 1)
        if (!((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
           || (ch >= "0" && ch <= "9") || (ch = ".")))
             return false;
        }
   return true;
}
function CheckAlphaNum(theForm, Fields){
   for(var i=0; i<Fields.length; i++){
   		resetColor(theForm.elements[Fields[i]]);
		if (!eCheckAlphaNum(theForm.elements[Fields[i]])){
            alert("Field entry is not valid: "+Fields[i]);
            focusField(theForm.elements[Fields[i]]);
            return false;
        }
	}
   return true;
}

function eCheckEMail(sn){
    s= sn.value;
    if (s.indexOf("@") == -1) return false;
    if (s.indexOf(".") == -1) return false;
    at=false;
    dot=false;
    for (var i = 0; i < s.length; i++) {
        ch = s.substring(i, i + 1)
        if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
                || (ch == "@") || (ch == ".") || (ch == "_")
                || (ch == "-") || (ch >= "0" && ch <= "9")) {
                if (ch == "@"){
                  if (at) return false;
                  else at=true;
                }
                if ((ch==".") && at)
                   dot=true;
        }
        else return false;
    }
   return dot;
}

function CheckEMail(theForm, Fields){
   for(var i=0; i<Fields.length; i++){
		resetColor(theForm.elements[Fields[i]]);
		if (!eCheckEMail(theForm.elements[Fields[i]])){
            alert("Field entry is not valid: "+Fields[i]);
            focusField(theForm.elements[Fields[i]]);
            return false;
        }
	}
   return true;
}

function eCheckNum(sn){
    s= sn.value;
    for (var i = 0; i < s.length; i++) {
        ch = s.substring(i, i + 1)
        if (!(ch >= "0" && ch <= "9"))
             return false;
        }
   return true;
}

function CheckNum(theForm, Fields){
	for(var i=0; i<Fields.length; i++){
		resetColor(theForm.elements[Fields[i]]);
         if (!eCheckNum(theForm.elements[Fields[i]])){
            alert("Invalid number: "+Fields[i]);
            focusField(theForm.elements[Fields[i]]);
            return false;
        }
	}
	return true;
}

function CheckCheckboxmulti(theForm, Fields){
  var unchecked = 0;
	for(var i=0; i<Fields.length; i++){
		resetColor(theForm.elements[Fields[i]]);
    if (!theForm.elements[Fields[i]].checked){
        unchecked++;
    }
	}
  if(unchecked == Fields.length && Fields.length != 0) {
    alert("Select one Choice at least");
    for(var i=0; i<Fields.length; i++) focusField(theForm.elements[Fields[i]]);
    focusField(theForm.elements[Fields[0]])
    return false;
  } else {  
	  return true;
  }
}

function CheckDate(theForm, Fields){
	for(var i=0; i<Fields.length; i++){
    dayField = Fields[i]+'_Day';
    monthField = Fields[i]+'_Month';
    yearField = Fields[i]+'_Year';

    resetColor(theForm.elements[dayField]);
		resetColor(theForm.elements[monthField]);
		resetColor(theForm.elements[yearField]);
    
    if (theForm.elements[dayField].value == '00' || theForm.elements[dayField].value == '') {
      alert("Invalid Date");
      focusField(theForm.elements[dayField]);
      return false;
    }
    if (theForm.elements[monthField].value == '00' || theForm.elements[monthField].value == '') {
      alert("Invalid Date");
      focusField(theForm.elements[monthField]);
      return false;
    }
    if (theForm.elements[yearField].value == '0000' || theForm.elements[yearField].value == '') {
      alert("Invalid Date");
      focusField(theForm.elements[yearField]);
      return false;
    }
	}
	return true;
}

function CheckDateTime(theForm, Fields){
	for(var i=0; i<Fields.length; i++){
    dayField = Fields[i]+'_Day';
    monthField = Fields[i]+'_Month';
    yearField = Fields[i]+'_Year';
    timeField = Fields[i]+'_Time';

    resetColor(theForm.elements[dayField]);
		resetColor(theForm.elements[monthField]);
		resetColor(theForm.elements[yearField]);
		resetColor(theForm.elements[timeField]);
    
    if (theForm.elements[dayField].value == '00' || theForm.elements[dayField].value == '') {
      alert("Invalid Date");
      focusField(theForm.elements[dayField]);
      return false;
    }
    if (theForm.elements[monthField].value == '00' || theForm.elements[monthField].value == '') {
      alert("Invalid Date");
      focusField(theForm.elements[monthField]);
      return false;
    }
    if (theForm.elements[yearField].value == '0000' || theForm.elements[yearField].value == '') {
      alert("Invalid Date");
      focusField(theForm.elements[yearField]);
      return false;
    }
	}
	return true;
}

function CheckRequiredFields(theForm, Fields)
{
   for(i=0; i<Fields.length; i++){
   		resetColor(theForm.elements[Fields[i]]);
        if(theForm.elements[Fields[i]].value == "" || theForm.elements[Fields[i]].value == "NULL"){
            alert("This field is required.");//+Fields[i]);
            focusField(theForm.elements[Fields[i]]);
            return false;
        }
	}
	return true;
}
function focusField(theField){
	theField.focus();
	theField.style.backgroundColor="#abbc93";
}

function resetColor(theField){
	if(theField.type=="text" || theField.type=="password" || theField.type=="select-one"){
		theField.style.backgroundColor="";
	}
}

var RequiredFields = new Array();
var EMail = new Array();
var AlphaNum = new Array();
var Alpha = new Array();
var Num = new Array();
var Checkboxmulti = new Array();
var Date =  new Array();
var DateTime =  new Array();

function validateForm(theForm){
	if(CheckRequiredFields(theForm, RequiredFields))
	if(CheckEMail(theForm, EMail))
	if(CheckAlphaNum(theForm, AlphaNum))
	if(CheckAlpha(theForm, Alpha))
	if(CheckNum(theForm, Num))
	if(CheckDate(theForm, Date))
	if(CheckDateTime(theForm, DateTime))
	if(CheckCheckboxmulti(theForm, Checkboxmulti))
		return true;
	return false;
}
/////

//Special Controls
function SimpleDateChooser(theForm, targetField){
  document.write('<select name="'+targetField+'_Day" size="1"                    \
        onchange="refreshDateField('+theForm.id+', \''+targetField+'\');">       \
				  <option selected="selected" value="00">Day</option>');

	for(var i=1; i<=31; i++) {
    if(i<10) num = '0'+i;
      else num = i;
    document.writeln('<option value="'+num+'">'+num+'</option>');
  }

	document.write('</select><select name="'+targetField+'_Month" size="1"         \
        onchange="refreshDateField('+theForm.id+', \''+targetField+'\');">       \
				  <option selected="selected" value="00">Month</option>');

 	for(var i=0; i<12; i++) {
    if(i<9) num = '0'+(i+1);
      else num = i+1;
    document.writeln('<option value="'+num+'">'+months[i]+'</option>');
  }

  document.write('</select><select name="'+targetField+'_Year" size="1"          \
        onchange="refreshDateField('+theForm.id+', \''+targetField+'\');">       \
				  <option selected="selected" value="0000">Year</option>');

 	for(var i=1900; i<2000; i++) document.writeln('<option value="'+i+'">'+i+'</option>');

	document.write('</select>');

  var date = theForm.elements[targetField].value;
  dateArray = date.split('-',3);

  dayField = targetField+'_Day';
  monthField = targetField+'_Month';
  yearField = targetField+'_Year';
  
  if(dateArray[0] != '') theForm.elements[yearField].value = dateArray[0];
  if(dateArray[1] != '' && dateArray[1] != undefined) theForm.elements[monthField].value = dateArray[1];
  if(dateArray[2] != '' && dateArray[2] != undefined) theForm.elements[dayField].value = dateArray[2];
  
  
}

function refreshDateField(theForm, targetField){
  dayField = targetField+'_Day';
  monthField = targetField+'_Month';
  yearField = targetField+'_Year';
  theForm.elements[targetField].value = theForm.elements[yearField].value + '-' + theForm.elements[monthField].value + '-' + theForm.elements[dayField].value;
}

function SimpleDateTimeChooser(theForm, targetField){
  document.write('<select name="'+targetField+'_Day" size="1"                    \
        onchange="refreshDateTimeField('+theForm.id+', \''+targetField+'\');">       \
				  <option selected="selected" value="00">Day</option>');

	for(var i=1; i<=31; i++) {
    if(i<10) num = '0'+i;
      else num = i;
    document.writeln('<option value="'+num+'">'+num+'</option>');
  }

	document.write('</select><select name="'+targetField+'_Month" size="1"         \
        onchange="refreshDateTimeField('+theForm.id+', \''+targetField+'\');">       \
				  <option selected="selected" value="00">Month</option>');

 	for(var i=0; i<12; i++) {
    if(i<9) num = '0'+(i+1);
      else num = i+1;
    document.writeln('<option value="'+num+'">'+months[i]+'</option>');
  }

  document.write('</select><select name="'+targetField+'_Year" size="1"          \
        onchange="refreshDateTimeField('+theForm.id+', \''+targetField+'\');">       \
				  <option selected="selected" value="0000">Year</option>');

 	for(var i=2005; i<2020; i++) document.writeln('<option value="'+i+'">'+i+'</option>');

	document.write('</select>');
  
  document.write('  <input onchange="refreshDateTimeField('+theForm.id+', \''+targetField+'\');" type="text" name="'+targetField+'_Time" value="00:00" size="5" maxlength="5" />');

  var date = theForm.elements[targetField].value;
  dateTimeArray = date.split(' ',2);
  if(dateTimeArray.length == 2) {
    dateArray = dateTimeArray[0].split('-',3);
    timeArray = dateTimeArray[1].split(':',3);

    dayField = targetField+'_Day';
    monthField = targetField+'_Month';
    yearField = targetField+'_Year';
    timeField = targetField+'_Time';

    if(dateArray[0] != '') theForm.elements[yearField].value = dateArray[0];
    if(dateArray[1] != '' && dateArray[1] != undefined) theForm.elements[monthField].value = dateArray[1];
    if(dateArray[2] != '' && dateArray[2] != undefined) theForm.elements[dayField].value = dateArray[2];
    if(timeArray[0] != '' && timeArray[0] != undefined && timeArray[1] != '' && timeArray[1] != undefined)
        theForm.elements[timeField].value = timeArray[0]+':'+timeArray[1];
  }
}

function refreshDateTimeField(theForm, targetField){
  dayField = targetField+'_Day';
  monthField = targetField+'_Month';
  yearField = targetField+'_Year';
  timeField = targetField+'_Time';

  if(theForm.elements[timeField].value.split(':',2).length != 2)
    theForm.elements[timeField].value = theForm.elements[timeField].value.substr(0,2) + ':' + theForm.elements[timeField].value.substr(2,2);

  if(!(theForm.elements[timeField].value.substr(0,1) >= 0 && 
     theForm.elements[timeField].value.substr(0,1) <= 9 &&
     theForm.elements[timeField].value.substr(1,1) >= 0 &&
     theForm.elements[timeField].value.substr(1,1) <= 9 &&
     theForm.elements[timeField].value.substr(2,1) == ':' &&
     theForm.elements[timeField].value.substr(3,1) >= 0 &&
     theForm.elements[timeField].value.substr(3,1) <= 9 &&
     theForm.elements[timeField].value.substr(4,1) >= 0 &&
     theForm.elements[timeField].value.substr(4,1) <= 9)) theForm.elements[timeField].value = '00:00';
  
  theForm.elements[targetField].value = theForm.elements[yearField].value + '-' + theForm.elements[monthField].value + '-' + theForm.elements[dayField].value + ' ' + theForm.elements[timeField].value + ':00';
}