/*
########################################################
Version/revision #: 1.0.0
Creation Date: 03/02/2003
Author/Programmer: Sam Nee - eDistrict, Inc

Description of functionality & program flow:
--------------------------------------------------------
Called from javascript function "Validate".  Used to 
compartmentalize each validation type.

The validate_adminimizer_length and validate_adminimizer_blank
functions are needed because the Adminimizer Active X
component changes the way the form field works and therefore, we need
to interact with the field differently...

Value of Adminimizer field =  myFormName.ed.Content
	or, If you have more than one editor on the page then you'll have to change the syntax to this
myFormName.ed(i).Content
	where i is the 0 based index of the editor starting at the top of the page
--------------------------------------------------------
########################################################
*/

function validate_blank(thefield, themsg){
	if(thefield.value==""){
		alert(themsg);
		thefield.focus();
		return false
	}else{
		return true
	}
}

function validate_email(thefield, themsg){
	if(thefield.value.indexOf ('@',0) == -1 || thefield.value.indexOf ('.',0) == -1){
		alert(themsg);
		thefield.select();
		return false
	}else{
		return true
	}
}

function validate_numeric(thefield, themsg){
	if(isNaN(thefield.value)){
		alert(themsg);
		thefield.focus();
		return false
	}else{
		return true
	}
}

function validate_char_length(thefield, maxchars, themsg){
	var field_length = thefield.value.length;
	if(field_length > maxchars){
		alert(themsg + "\n(Currently " + field_length + " characters.)");
		thefield.focus();
		return false
	}else{
		return true
	}
}

function validate_zip_code(thefield, themsg){
	var zipstr = thefield.value;
	var digits="0123456789-"
	if (zipstr.length < 5 || 10 < zipstr.length) {
		alert(themsg);
		thefield.select();
		return false;
	}else{
		for (var i = 0;i < zipstr.length;i++){
			temp = zipstr.substring(i,i+1)
			if (digits.indexOf(temp)==-1){
				alert(themsg);
				thefield.select();
				return false;
			}else{
				return true;
			}
		}	
	}
}

function validate_checkboxes(thefield, themsg){
	var isChecked = false;
	if(thefield.checked){
		isChecked = true;
	}
	for(i=0;i<thefield.length;i++){
		if(thefield[i].checked==true){
			isChecked = true;
		}
	}
	if(isChecked){
		return true;
	}else{
		alert(themsg);
		return false;
	}
	
}

//Used by the phone number fields on the registration form. Prevents the user from needing to 'TAB'
//Triggered via onKeyUp
function moveNextField(thefield,maxchars,nextfield){
	var field_length = thefield.value.length
	if(field_length==maxchars){
		if(nextfield.value==""){
			nextfield.focus();
		}
	}
}


function validate_adminimizer_length(thefield, maxchars, themsg){
	var field_length = thefield.content.length;
	if(field_length > maxchars){
		alert(themsg + "\n(Currently " + field_length + " characters.)");
		thefield.focus();
		return false
	}else{
		return true
	}
}


function validate_adminimizer_blank(thefield, themsg){
	if(thefield.content=="" || thefield.content=="<P>&nbsp;</P>"){
		alert(themsg);
		thefield.focus();
		return false
	}else{
		return true
	}
}


function validate_date(date_field, desc) {
        if (!date_field.value)  
                return true;
        var in_date = stripCharString(date_field.value," ");
        in_date = in_date.toUpperCase();
        var date_is_bad = 0;  
        if (!allowInString(in_date,"/0123456789T+-"))
                date_is_bad = 1; // invalid characters in date
        if (!date_is_bad) { 
                var has_rdi = 0;
                if (in_date.indexOf("T") >= 0){ 
                        has_rdi = 1;
                }
                if (!date_is_bad && has_rdi && (in_date.indexOf("T") != 0)) { 
                        date_is_bad = 2; // relative date index character is not in first position
                }
                if (!date_is_bad && has_rdi && (in_date.length == 1)) { 
                        var d = new Date();
						var return_month = parseInt(d.getMonth() + 1).toString();
						return_month = (return_month.length==1 ? "0" : "") + return_month; 
						var return_date =  parseInt(d.getDate()).toString();
						return_date = (return_date.length==1 ? "0" : "") + return_date; 
				        in_date = return_month + "/" + return_date + "/" + get_full_year(d);		
                        has_rdi = 0; // date doesn't have rdi char anymore (will also cause failure of add'l rdi checks, which is a good thing)
                }
                if (!date_is_bad && has_rdi && (in_date.length > 1) && !(in_date.charAt(1) == "+" || in_date.charAt(1) == "-")) {
                        date_is_bad = 3; // length of rdi string is greater than 1 but second char is not "+" or "-"
                }
                if (!date_is_bad && has_rdi && isNaN(parseInt(in_date.substring(2,in_date.length),10))) {
                        date_is_bad = 4; // rdi value is not a number
                }
                if (!date_is_bad && has_rdi && (parseInt(in_date.substring(2,in_date.length),10) < 0)) {
                        date_is_bad = 5; // rdi value is not a positive integer
                }
                if (!date_is_bad && has_rdi) {
                        var d = new Date();
                        ms = d.getTime();
                        offset = parseInt(in_date.substring(2,in_date.length),10);
                        if(in_date.charAt(1) == "+") {
                                ms += (86400000 * offset);
                        } else {
                                ms -= (86400000 * offset);
                        }
                        d.setTime(ms);
						var return_month = parseInt(d.getMonth() + 1).toString();
						return_month = (return_month.length==1 ? "0" : "") + return_month; 
						var return_date =  parseInt(d.getDate()).toString();
						return_date = (return_date.length==1 ? "0" : "") + return_date; 
				        in_date = return_month + "/" + return_date + "/" + get_full_year(d);	
                        has_rdi = 0;
                }
        } 
        if (!date_is_bad) {
                var date_pieces = new Array();
                date_pieces = in_date.split("/");
                if (date_pieces.length == 2) {
                        var d = new Date();
                        in_date = in_date + "/" + get_full_year(d);
                        date_pieces = in_date.split("/");
                }
                if (date_pieces.length != 3 || parseInt(date_pieces[0],10) < 1 || parseInt(date_pieces[0],10) > 12 
                                || parseInt(date_pieces[1],10) < 1 || parseInt(date_pieces[1],10) > 31 
                                || (date_pieces[2].length != 2 && date_pieces[2].length != 4)) {
                        date_is_bad = 6;  // date is not in format of m[m]/d[d]/yy[yy]
                }
        }
        if (date_is_bad) {
                alert(desc + " must be in the format of mm/dd/yy or mm/dd/yyyy.");
                date_field.focus();
                return (false);
        }
        
        var ms = Date.parse(in_date);
        var d = new Date();
        d.setTime(ms);
		var return_date = d.toLocaleString();
		var return_month = parseInt(d.getMonth() + 1).toString();
		return_month = (return_month.length==1 ? "0" : "") + return_month; 
		var return_date =  parseInt(d.getDate()).toString();
		return_date = (return_date.length==1 ? "0" : "") + return_date; 
        return_date = return_month + "/" + return_date + "/" + get_full_year(d);
        date_field.value = return_date;
        return true;
}       // normalize the year to yyyy
function get_full_year(d) {
		var y = ""
		if (d.getFullYear() != null)
		{
			y = d.getFullYear();
			if (y < 1970) y+= 100;		
		} else
		{	
	        y = d.getYear();
	        if (y > 69  && y < 100) y += 1900;
	        if (y < 1000) y += 2000;
		}
        return y;
}
// The following functions were written by Gordon McComb
// More information can be found here: http://www.javaworld.com/javaworld/jw-02-1997/jw-02-javascript.html
function stripCharString (InString, CharString)  {
        var OutString="";
   for (var Count=0; Count < InString.length; Count++)  {
        var TempChar=InString.substring (Count, Count+1);
      var Strip = false;
      for (var Countx = 0; Countx < CharString.length; Countx++) {
        var StripThis = CharString.substring(Countx, Countx+1)
         if (TempChar == StripThis) {
                Strip = true;
            break;
         }
      }
      if (!Strip)
        OutString=OutString+TempChar;
   }
        return (OutString);
}
function allowInString (InString, RefString)  {
        if(InString.length==0) return (false);
        for (var Count=0; Count < InString.length; Count++)  {
        var TempChar= InString.substring (Count, Count+1);
      if (RefString.indexOf (TempChar, 0)==-1)  
        return (false);
   }
   return (true);
}