var TEXT = 1;
var NUMBER = 2;
var EMAIL = 3;
var DATE = 4;
var DATETIME = 5;
var TIME = 6;
var DIGITS = 7;


function validate_field(field_obj, field_name, field_type)  {
	//field_obj must be a valid form field
  if (field_obj.value.search(/\S/)==-1)   {
    if (field_obj.type == "select-one")
      alert("Please select " + field_name);
    else
      alert("Please fill in " + field_name);
    field_obj.focus();
    return 0;
  }
  else    {
    //checking for email if needed
    if (field_type == EMAIL)    {
      if (field_obj.value.search(/^[^@ ]+@[^@ ]+\.[^@ \.]+$/) == -1)   {
        alert("Please fill in valid " + field_name);
        field_obj.focus();
        return 0;
      }
        return 1;
    }
    else if (field_type == NUMBER)  {
      if (isNaN(field_obj.value)) {
        alert("Please fill in valid " + field_name);
        field_obj.focus();
        return 0;
      }
      else
        return 1;
    }
    else if (field_type == DIGITS)  {
      if (field_obj.value.search(/\D/) == -1)
        return 1;
      else  {
        alert("Please fill in valid " + field_name);
        return 0;  
      } 
    }

		else if (field_type == DATE)    {
			if (field_obj.value.length == 10 &&
				field_obj.value.search(/(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d/) >= 0)    {
				parts = field_obj.value.split("/");
        d = parseInt(parts[0]);
        m = parseInt(parts[1]);
        y = parseInt(parts[2]);

        //get the wrong dates
        if ((d>=30 && m==2) ||
            (d==31 && (m==2 || m==4 || m==6 || m==9 || m==11)) ||
            (d==29 && m==2 && !(((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))))   {
					alert("Please fill in valid " + field_name + "(dd/mm/yyyy)");
          field_obj.focus();
					return 0;
        }
        else
          return 1;
			}
			else    {
			  alert("Please fill in valid " + field_name + "(dd/mm/yyyy)");
        field_obj.focus();
				return 0;
			}
		}
		
		
		else if (field_type == TIME)    {
	    if (field_obj.value.length == 5)    {
	        parts = field_obj.value.split(":");
        if (parts.length == 2)  {
          h = parts[0];
          m = parts[1];
          if (h>=0 && h<=23 && m>=0 && m<=59)
				    return 1;
			    else    {
			      alert("Please fill in valid " + field_name + "(hh:mm)");
    				field_obj.focus();
				    return 0;
			    }
        }
        else    {
          alert("Please fill in valid " + field_name + "(hh:mm)");
					field_obj.focus();
					return 0;
        }
	    }
	    else    {
	      alert("Please fill in valid " + field_name + "(hh:mm)");
			  field_obj.focus();
			  return 0;
	    }
		}


		else if (field_type == DATETIME)    {
			parts = field_obj.value.split(" ");
			if (parts.length == 2)  {
			    dates = parts[0];
			    times = parts[1];
				
				if (dates.length == 10 &&
					dates.search(/(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d/) >= 0)  {
          dateparts = dates.split("/");
          d = parseInt(dateparts[0]);
          m = parseInt(dateparts[1]);
          y = parseInt(dateparts[2]);

          //get the wrong dates
          if ((d>=30 && m==2) ||
              (d==31 && (m==2 || m==4 || m==6 || m==9 || m==11)) ||
              (d==29 && m==2 && !(((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))))   {
						alert("Please fill in valid " + field_name + "(dd/mm/yyyy hh:mm)");
            field_obj.focus();
						return 0;
	        }
	        else    {
	          //check time
						timeparts = times.split(":");
						if (timeparts.length == 2)  {
						  h = parseInt(timeparts[0]);
							m = parseInt(timeparts[1]);
							if (h>=0 && h<=23 && m>=0 && m<=59)
								return 1;
							else    {
							  alert("Please fill in valid " + field_name + "(dd/mm/yyyy hh:mm)");
                field_obj.focus();
								return 0;
							}
						}
						else    {
              alert("Please fill in valid " + field_name + "(dd/mm/yyyy hh:mm)");
              field_obj.focus();
							return 0;
						}
					}
        }
        else    {
          alert("Please fill in valid " + field_name + "(dd/mm/yyyy hh:mm)");
        	field_obj.focus();
					return 0;
        }
			}
			else    {
			  alert("Please fill in valid " + field_name + "(dd/mm/yyyy hh:mm)");
        field_obj.focus();
				return 0;
			}
		}
		else
		    return 1;
	}
}

function validate_sel(obj, fieldname) {
  if (obj.value == 0 || obj.value == "0" || obj.value == "")  {
    alert("Please select " + fieldname);
    obj.focus();
    return false;  
  } 
  else
    return true;
}

function checkValidDate(d, m, y, field_name)    {
	if ((d>=30 && m==2) ||
	    (d==31 && (m==2 || m==4 || m==6 || m==9 || m==11)) ||
	    (d==29 && m==2 && !(((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))))   {
		alert("Please fill in valid Date for " + field_name);
		return 0;
    }
    else
        return 1;
}


function compareTimeString(time1, time2)    {
//return 0 if time1 == time2
//return 1 if time1 > time2
//return -1 if time1 < time2
	if (time1 == time2)
	    return 0;

	//split the time first
	time1part = time1.split(":");
	time2part = time2.split(":");

	time1_h = time1part[0];
	time1_m = time1part[1];
	time2_h = time2part[0];
	time2_m = time2part[1];

	//compare
	if (time1_h > time2_h ||
		(time1_h == time2_h && time1_m > time2_m))
	    return 1;
	else
	    return -1;
}


function compareDateString(date1, date2)    {
//return 0 if time1 == time2
//return 1 if time1 > time2
//return -1 if time1 < time2
	if (date1 == date2)
	    return 0;

	//split the time first
	date1part = date1.split("/");
	date2part = date2.split("/");

	date1_d = date1part[0];
	date1_m = date1part[1];
	date1_y = date1part[2];
	date2_d = date2part[0];
	date2_m = date2part[1];
	date2_y = date2part[2];

	//compare
	if ((date1_y > date2_y) ||
	    (date1_y == date2_y && date1_m > date2_m) ||
	    (date1_y == date2_y && date1_m == date2_m && date1_d > date2_d))
	    return 1;
	else
	    return -1;
}


function winpopup(url, win_width, win_height)  {
	sw = screen.width;
	sh = screen.height;

	x = Math.ceil((sw - win_width) / 2);
	y = Math.ceil((sh - win_height) / 2);
	window.open(url, "_blank", "left=" + x + ",top=" + y + ",menubar=no,scrollbars=yes,status=yes,resizable=yes,width=" + win_width + ",height=" + win_height);
}

function winpopup2(url, win_width, win_height)  {
	sw = screen.width;
	sh = screen.height;

	x = Math.ceil((sw - win_width) / 2);
	y = Math.ceil((sh - win_height) / 2);
	window.open(url, "_blank", "left=" + x + ",top=" + y + ",toolbar=yes,menubar=yes,scrollbars=yes,status=yes,resizable=yes,width=" + win_width + ",height=" + win_height);
}

function winpopup3(url, win_width, win_height)  {
	sw = screen.width;
	sh = screen.height;

	x = Math.ceil((sw - win_width) / 2);
	y = Math.ceil((sh - win_height) / 2);
	window.open(url, "_blank", "left=" + x + ",top=" + y + ",menubar=no,scrollbars=no,status=yes,resizable=no,width=" + win_width + ",height=" + win_height);
}

//restrict typing beyond maximum length. (used by textarea)
function restrict_length(obj, len)  {
  if (obj.value.length > len) {
    alert("Maximum Length Reach");
    obj.value = obj.value.substr(0, len);
  }
}


function validateRadioGroup(theForm, prefix, desc) {
  var count = 0;
  for (i=0; i < theForm.length; i++) {
    if (theForm[i].type == 'radio' && 
    theForm[i].name.substr(0, prefix.length) == prefix &&
    theForm[i].name != prefix + '_all' &&
        theForm[i].checked == true) {
      count++; 
    }
  }
  if (count == 0) {
    alert('Please select an answer for '+desc);
    return false;
  }
  return true;
}

function validate_rd(obj, fieldname){
  myOption = -1;
  for (i=obj.length-1; i > -1; i--) {
    if (obj[i].checked) {
      myOption = i; i = -1;
    }
  }
  
  if (myOption == -1) {
    alert("Please select " + fieldname);
    return false;
  }else{
    return true;
  }
}

function validateCheckboxGroup(theForm, prefix, desc) {
  var count = 0;
  for (i=0; i < theForm.length; i++) {
    if (theForm[i].type == 'checkbox' && 
    theForm[i].name.substr(0, prefix.length) == prefix &&
    theForm[i].name != prefix + '_all' &&
        theForm[i].checked == true) {
      count++; 
    }
  }
  if (count == 0) {
    alert('Please select at least one answer for  '+desc);
    return false;
  }
  return true;
}