///////////////////////////////////////////////////////////////
//                                                           //
//  Pass all required global variables and fields from page. //
//  (eg- <script language="javascript">                      //
//      var y = document.xxx; </script>)                     //
//   Skip fields that are not required.                      //
//                                                           //
///////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////
// INITIALIZE GLOBAL variables. VALUES passed from page (after fields).
//   o                      = form name.
//   img_loc                = path to image.
//   td_error_num           = number of columns to make.
//   td_error_loc           = td count from left where error message is shown.
//   table_name             = table id.
//   checkbox_name          = checkbox id.
//   class_regular          = css class when there are no errors.
//   class_regular_checkbox = css class for checkboxes when normal.
//   class_error_checkbox   = css class for checkboxes when error.
//   class_error_textf      = css class for text fields when error.
//   class_error_msg        = css class for the error message.
///////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////
// individual ONFOCUS functions because arguments cannot be passed.
///////////////////////////////////////////////////////////////
function fname_focus_call()
{
	delRow(document.getElementById(rowindex_fname).sectionRowIndex,id_fname,'fname_error');
}
function lname_focus_call()
{
	delRow(document.getElementById(rowindex_lname).sectionRowIndex,id_lname,'lname_error');
}
function address_focus_call()
{
	delRow(document.getElementById(rowindex_address).sectionRowIndex,id_address,'address_error');
}
function zip_focus_call()
{
	delRow(document.getElementById(rowindex_zip).sectionRowIndex,id_zip,'zip_error');
}
function email_focus_call()
{
	delRow(document.getElementById(rowindex_email).sectionRowIndex,id_email,'email_error');
}
function wphone_focus_call()
{
	document.getElementById(id_w_ph_ac).className=class_regular;
	document.getElementById(id_w_ph_pre).className=class_regular;
	document.getElementById(id_w_ph_num).className=class_regular;
	delRow(document.getElementById(rowindex_w_phone).sectionRowIndex,id_w_ph_ac,'phone_error');
	delRow(document.getElementById(rowindex_w_phone).sectionRowIndex,id_w_ph_pre,'phone_error');
	delRow(document.getElementById(rowindex_w_phone).sectionRowIndex,id_w_ph_num,'phone_error');
}
function bday_focus_call()
{
	document.getElementById(id_b_mon).className=class_regular;
	document.getElementById(id_b_day).className=class_regular;
	document.getElementById(id_b_year).className=class_regular; delRow(document.getElementById(rowindex_bday).sectionRowIndex,id_b_mon,'bday_error');
	delRow(document.getElementById(rowindex_bday).sectionRowIndex,id_b_day,'bday_error');
	delRow(document.getElementById(rowindex_bday).sectionRowIndex,id_b_year,'bday_error');
}
function gender_focus_call()
{
	if((rowindex_gender == rowindex_marital) && (window.id_marital_status!==undefined))
	{
		document.getElementById(id_marital_status).className=class_regular;
	}
	document.getElementById(id_gender).className=class_regular;
	delRow(document.getElementById(rowindex_gender).sectionRowIndex,id_gender,error_row_gender);
}
function marital_focus_call()
{
	if((rowindex_marital == rowindex_gender) && (window.id_gender!==undefined))
	{
		document.getElementById(id_gender).className=class_regular;
	}
	document.getElementById(id_marital_status).className=class_regular;
	delRow(document.getElementById(rowindex_marital).sectionRowIndex,id_marital_status,error_row_marital);
}
function checkbox_blur_call()
{
	IsItChecked(checkbox_name,'Sorry, you must agree to the terms and conditions.',id_checkbox);
}
function auth_focus_call()
{
	delRow(document.getElementById(rowindex_auth).sectionRowIndex,id_AuthCode,'authcode_error');
}
///////////////////////////////////////////////////////////////
// checks to see if variable EXIST OR NOT. CALLS individual ONFOCUS/ ONBLUR functions if exist.
///////////////////////////////////////////////////////////////
if(window.id_fname!==undefined)
{
	rowindex_fname;
	id_fname;
	o.fname.onfocus = fname_focus_call;
}
if(window.id_AuthCode!=undefined){
	rowindex_auth;
	id_AuthCode;
	o.AuthCodeBox.onfocus = auth_focus_call;
}
if(window.id_lname!==undefined)
{
	rowindex_lname;
	id_lname;
	o.lname.onfocus = lname_focus_call;
}
if(window.id_address!==undefined)
{
	rowindex_address;
	id_address;
	o.home_address.onfocus = address_focus_call;
}
if(window.id_zip!==undefined)
{
	rowindex_zip;
	id_zip;
	o.home_zip.onfocus = zip_focus_call;
}
if(window.id_email!==undefined)
{
	rowindex_email;
	id_email;
	o.home_email.onfocus = email_focus_call;
}
if(window.id_w_ph_ac!==undefined)
{
	rowindex_w_phone;
	id_w_ph_ac;
	id_w_ph_pre;
	id_w_ph_num;
	o.w_ph_ac.onfocus = wphone_focus_call;
	o.w_ph_pre.onfocus = wphone_focus_call;
	o.w_ph_num.onfocus = wphone_focus_call;
}
if(window.id_b_mon!==undefined)
{
	rowindex_bday;
	id_b_mon;
	id_b_day;
	id_b_year;
	o.b_mon.onfocus = bday_focus_call;
	o.b_day.onfocus = bday_focus_call;
	o.b_year.onfocus = bday_focus_call;
}
if(window.id_gender!==undefined)
{
	rowindex_gender;
	id_gender;
	o.Gender.onfocus = gender_focus_call;
}
if(window.id_marital_status!==undefined)
{
	rowindex_marital;
	id_marital_status;
	o.marital_status.onfocus = marital_focus_call;
}
if(window.id_checkbox!==undefined)
{
	id_checkbox;
	o.elements[checkbox_name].onblur = checkbox_blur_call;
}

///////////////////////////////////////////////////////////////
// checks to see if variable EXISTS OR NOT. ASSIGNS DEFAULT values if no.
///////////////////////////////////////////////////////////////
if(window.td_error_loc===undefined)
{
	var td_error_num=2;
	var td_error_loc=1;
}
if(window.class_regular_checkbox===undefined)
{
	var class_regular_checkbox=class_regular;
}

///////////////////////////////////////////////////////////////
// checks to see if PHP error EXISTS OR NOT. DISPLAYS error and STOPS SUBMIT if field passed.
///////////////////////////////////////////////////////////////
if(window.fname_error!==undefined)
{
	var fname_error = fname_error.toString();
	insRow(document.getElementById(rowindex_fname).sectionRowIndex,'',fname_error,1,id_fname,'fname_error');
	var a = false;
	var b = false;
}
else{var a = true; var b = true;}

if(window.addr_error!==undefined)
{
	var addr_error = addr_error.toString();
	insRow(document.getElementById(rowindex_address).sectionRowIndex,'',addr_error,1,id_address,'address_error');
	var c = false;
}
else{var c = true;}

if(window.email_error!==undefined)
{
	var email_error = email_error.toString();
	insRow(document.getElementById(rowindex_email).sectionRowIndex,'',email_error,1,id_email,'email_error');
	var e = false;
}
else{var e = true;}

if(window.ph_error!==undefined)
{
	var ph_error = ph_error.toString();
	insRow(document.getElementById(rowindex_w_phone).sectionRowIndex,'',ph_error,1,id_w_ph_ac,'phone_error');
	document.getElementById(id_w_ph_pre).className=class_error_textf;
	document.getElementById(id_w_ph_num).className=class_error_textf;
	var f = false;
	var g = false;
	var h = false;
}
else{var f = true; var g = true; var h = true;}

if(window.auth_error!==undefined){
	var auth_error = auth_error.toString();
	insRow(document.getElementById(rowindex_auth).sectionRowIndex,'',auth_error,1,id_AuthCode,'authcode_error');
	var c = false;
} else {
	var c = true;
}


///////////////////////////////////////////////////////////////
// RUN ALL checks on EXISTING fields before submitting.
///////////////////////////////////////////////////////////////
function ValidateAll()
{
	if(window.id_fname!==undefined)
	{
		var a = CheckName(o.fname.value,'Please enter your first name.','The first name is not in a valid format.',document.getElementById(rowindex_fname).sectionRowIndex,id_fname,'fname_error');
	}
	else{var a = true;}

	if(window.id_lname!==undefined)
	{
		var b = CheckName(o.lname.value,'Please enter your last name.','The last name is not in a valid format.',document.getElementById(rowindex_lname).sectionRowIndex,id_lname,'lname_error');
	}
	else{var b = true;}

	if(window.id_address!==undefined)
	{
		var c = CheckEmpty(o.home_address.value,'Please enter your street address.','',document.getElementById(rowindex_address).sectionRowIndex,id_address,'address_error');
	}
	else{var c = true;}

	if(window.id_zip!==undefined)
	{
		var d = CheckNum(o.home_zip.value,'Please enter your zip code.','The zipcode you entered is not valid.',document.getElementById(rowindex_zip).sectionRowIndex,id_zip,5,'zip_error');
	}
	else{var d = true;}

	if(window.id_email!==undefined)
	{
		var e = CheckEmail(o.home_email.value,'Please enter your email.','The email you entered is not valid.',document.getElementById(rowindex_email).sectionRowIndex,id_email,'email_error');
	}
	else{var e = true;}

	if(window.id_w_ph_ac!==undefined)
	{
		var f = CheckNum(o.w_ph_ac.value,'Please enter your day time phone.','The phone number is not in a valid format.',document.getElementById(rowindex_w_phone).sectionRowIndex,id_w_ph_ac,3,'phone_error');
		var g = CheckNum(o.w_ph_pre.value,'Please enter your day time phone.','The phone number is not in a valid format.',document.getElementById(rowindex_w_phone).sectionRowIndex,id_w_ph_pre,3,'phone_error');
		var h = CheckNum(o.w_ph_num.value,'Please enter your day time phone.','The phone number is not in a valid format.',document.getElementById(rowindex_w_phone).sectionRowIndex,id_w_ph_num,4,'phone_error');
	}
	else{var f = true; var g = true; var h = true;}

	if(window.id_b_mon!==undefined)
	{
		var i = CheckEmpty(o.b_mon.value,'Please select your birthdate.','',document.getElementById(rowindex_bday).sectionRowIndex,id_b_mon,'bday_error');
		var j = CheckEmpty(o.b_day.value,'Please select your birthdate.','',document.getElementById(rowindex_bday).sectionRowIndex,id_b_day,'bday_error');
		var k = CheckEmpty(o.b_year.value,'Please select your birthdate.','',document.getElementById(rowindex_bday).sectionRowIndex,id_b_year,'bday_error');
		var p = computeAge(o,document.getElementById(rowindex_bday).sectionRowIndex,'','Sorry, you must be 18 years or older to register.',id_b_year,'bday_error');
	}
	else{var i = true; var j = true; var k = true; var p = true;}

	if(window.id_gender!==undefined)
	{
		var l = CheckEmpty(o.Gender.value,'Please select your gender/marital status.','',document.getElementById(rowindex_gender).sectionRowIndex,id_gender,error_row_gender);
	}
	else{var l = true;}

	if(window.id_marital_status!==undefined)
	{
		var m = CheckEmpty(o.marital_status.value,'Please select your gender/marital status.','',document.getElementById(rowindex_marital).sectionRowIndex,id_marital_status,error_row_marital);
	}
	else{var m = true;}

	if(window.id_checkbox!==undefined)
	{
		var n = IsItChecked(checkbox_name,'Sorry, you must agree to the terms and conditions.',id_checkbox);
	}
	else{var n = true;}


	if(window.id_AuthCode!==undefined)
	{
		var q = CheckEmpty(o.AuthCodeBox.value,'Please enter code you see above.','',document.getElementById(rowindex_auth).sectionRowIndex,id_AuthCode,'authcode_error');
	}
	else{var q = true;}

	if(a && b && c && d && e && f && g && h && i && j && k && l && m && n && p && q)
	{
		o.submit();
	}
	else
	{
		return false;
	}
}

///////////////////////////////////////////////////////////////
// STRIP extra spaces from end of string.
//   s            = value of field.
///////////////////////////////////////////////////////////////
function StripSpacesFromEnds(s)
{
	while((s.indexOf(' ',0) == 0) && (s.length > 1))
	{
		s = s.substring(1,s.length);
	}
	while((s.lastIndexOf(' ') == (s.length - 1) && (s.length > 1)))
	{
		s = s.substring(0,(s.length - 1));
	}
	if((s.indexOf(' ',0) == 0) && (s.length == 1)) s = '';
	return s;
}

///////////////////////////////////////////////////////////////
// checks if CHECKED or not.
//   checkname    = id of checkbox.
//   errormsg     = error message to display when unchecked.
//   tdname       = input id to add css class.   
///////////////////////////////////////////////////////////////
function IsItChecked(checkname,errormsg,tdname)
{
	if(!document.getElementById(checkname).checked)
	{
		document.getElementById(tdname).className=class_error_checkbox;
		alert(errormsg);
	}
	else
	{
		document.getElementById(tdname).className=class_regular_checkbox;
		return true;
	}
}

///////////////////////////////////////////////////////////////
// checks if EMPTY.
//   value        = value of field.
///////////////////////////////////////////////////////////////
function IsItPresent(value)
{
	value = StripSpacesFromEnds(value);
	if(value.length != 0)
	{
		return value;
	}
	else
	{
		return false;
	}
}

///////////////////////////////////////////////////////////////
// SHOW errors in new row.
//   where        = index of row.
//   error_empty  = error message to display when blank.
//   error_invalid= error message to display when invalid.
//   invalid      = equals 1 if value is invalid.
//   tdname       = input id to add css class.
//   error_field  = id of error row.
///////////////////////////////////////////////////////////////
function insRow(where,error_empty,error_invalid,invalid,tdname,error_field)
{
	document.getElementById(tdname).className=class_error_textf;
	if(!(document.getElementById(error_field)) && (invalid!==1))
	{
		where=where+1;
		var x=document.getElementById(table_name).insertRow(where);
		var y = new Array();
		for(var i=0; i<td_error_num; i++)
		{
			y[i]=x.insertCell(i);
		}
		var z=document.getElementById(table_name).rows[where].cells;
		//z[0].colSpan="2";
		z[td_error_loc].className="main";
		z[td_error_loc].id=error_field;
		y[td_error_loc].innerHTML="<span class=\""+ class_error_msg +"\" style=\"display:block;\"><img src=\""+ img_loc +"\" border=\"0\">&nbsp;"+ error_empty +"&nbsp;</span>";
	}
	else if(!(document.getElementById(error_field)) && (invalid==1))
	{
		where=where+1;
		var x=document.getElementById(table_name).insertRow(where);
		var y = new Array();
		for(var i=0; i<td_error_num; i++)
		{
			y[i]=x.insertCell(i);
		}
		var z=document.getElementById(table_name).rows[where].cells;
		//z[0].colSpan="2";
		z[td_error_loc].className="main";
		z[td_error_loc].id=error_field;
		y[td_error_loc].innerHTML="<span class=\""+ class_error_msg +"\" style=\"display:block;\"><img src=\""+ img_loc +"\" border=\"0\">&nbsp;"+ error_invalid +"&nbsp;</span>";
	}
}

///////////////////////////////////////////////////////////////
// DELETE error row.
//   where        = index of row.
//   tdname       = input id to add css class.
//   error_field  = id of error row.
///////////////////////////////////////////////////////////////
function delRow(where,tdname,error_field)
{
	where=where+1;
	if(document.getElementById(error_field))
	{
		document.getElementById(table_name).deleteRow(where);
		document.getElementById(tdname).className=class_regular;
	}
	else
	{
		return false;
	}
}

///////////////////////////////////////////////////////////////
// checks if EMPTY and allows only ALPHA and ' and . and - keys.
//   value        = value of field.
//   error_empty  = error message to display when blank.
//   error_invalid= error message to display when invalid.
//   where        = index of row.
//   tdname       = input id to add css class.
//   error_field  = id of error row.
///////////////////////////////////////////////////////////////
function CheckEmpty(value,error_empty,error_invalid,where,tdname,error_field)
{
	value = IsItPresent(value);
	if(! value)
	{
		insRow(where,error_empty,error_invalid,0,tdname,error_field);
		return false;
	}
	else
	{
		if(tdname==id_b_mon || tdname==id_b_day || tdname==id_b_year)
		{
			if((document.getElementById(id_b_mon).className==class_regular) && (document.getElementById(id_b_day).className==class_regular) && (document.getElementById(id_b_year).className==class_regular))
			{
				delRow(where,tdname,error_field);
				return true;
			}
			else
			{
				return false;
			}
		}
		if(tdname==id_address)
		{
			if(document.getElementById(id_address).className==class_regular)
			{
				delRow(where,tdname,error_field);
				return true;
			}
			else
			{
				return false;
			}
		}
		return true;
	}
}

///////////////////////////////////////////////////////////////
// checks if EMPTY and allows only ALPHA and ' and . and - keys.
//   value        = value of field.
//   error_empty  = error message to display when blank.
//   error_invalid= error message to display when invalid.
//   where        = index of row.
//   tdname       = input id to add css class.
//   error_field  = id of error row.
///////////////////////////////////////////////////////////////
function CheckName(value,error_empty,error_invalid,where,tdname,error_field)
{
	value = IsItPresent(value);
	if(! value)
	{
		insRow(where,error_empty,error_invalid,0,tdname,error_field);
		return false;
	}
	
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.- ";
	var allValid = true;
	for (i = 0;  i < value.length;  i++)
	{
		ch = value.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
	}
	if (!allValid)
	{
		insRow(where,error_empty,error_invalid,1,tdname,error_field);
		return false;
	}
	else
	{
		delRow(where,tdname,error_field);
		return true;
	}
}

///////////////////////////////////////////////////////////////
// checks if EMPTY and allows only NUMERIC and . and - keys.
//   value        = value of field.
//   error_empty  = error message to display when blank.
//   error_invalid= error message to display when invalid.
//   where        = index of row.
//   tdname       = input id to add css class.
//   length       = checks for minimum character length.
//   error_field  = id of error row.
///////////////////////////////////////////////////////////////
function CheckNum(value,error_empty,error_invalid,where,tdname,length,error_field)
{
	value = IsItPresent(value);
	if(! value)
	{
		insRow(where,error_empty,error_invalid,0,tdname,error_field);
		return false;
	}

	var checkOK = "0123456789.- ";
	var allValid = true;
	for (i = 0;  i < value.length;  i++)
	{
		ch = value.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
	}
	if (!allValid)
	{
		insRow(where,error_empty,error_invalid,1,tdname,error_field);
		return false;
	}
	else
	{
		if(length)
		{
			if(value.length<length)
			{
				insRow(where,error_empty,error_invalid,1,tdname,error_field);
				return false;
			}
			else
			{
				if(tdname==id_w_ph_ac || tdname==id_w_ph_pre || tdname==id_w_ph_num)
				{
					if((document.getElementById(id_w_ph_ac).className==class_regular) && (document.getElementById(id_w_ph_pre).className==class_regular) && (document.getElementById(id_w_ph_num).className==class_regular))
					{
						delRow(where,tdname,error_field);
						return true;
					}
					else
					{
						return false;
					}
				}
			}
		}
		return true;
	}
}

///////////////////////////////////////////////////////////////
// checks if EMPTY and checks for X@X.XX format.
//   email        = value of field.
//   error_empty  = error message to display when blank.
//   error_invalid= error message to display when invalid.
//   where        = index of row.
//   tdname       = input id to add css class.
//   error_field  = id of error row.
///////////////////////////////////////////////////////////////
function CheckEmail(email,error_empty,error_invalid,where,tdname,error_field)
{
	email = IsItPresent(email);

	if(! email)
	{
		insRow(where,error_empty,error_invalid,0,tdname,error_field);
		return false;
	}

	var i = email.indexOf(' ',0);
	while(i > -1)
	{
		email = email.substring(0,i) + 
			email.substring((i + 1),email.length);
		i = email.indexOf(' ',0);
	}
	if((email.length < 6) ||
	   (email.indexOf('@',0) < 1) ||
	   (email.lastIndexOf('@') != email.indexOf('@',0)) ||
	   (email.lastIndexOf('@') > (email.length - 5)) ||
	   (email.indexOf('..',0) > -1) ||
	   (email.indexOf('@.',0) > -1) ||
	   (email.indexOf('.@',0) > -1) ||
	   (email.indexOf(',',0) > -1))
	{
		insRow(where,error_empty,error_invalid,1,tdname,error_field);
		return false;
	}
	else
	{
		if(document.getElementById(id_email).className==class_regular)
		{
			delRow(where,tdname,error_field);
			return true;
		}
		else
		{
			return false;
		}
	}
}

///////////////////////////////////////////////////////////////
// gets DAYS for each MONTH.
//   month        = today's month.
//   year         = today's year.
//   julianFlag   = for leap years. not used.
///////////////////////////////////////////////////////////////
function getMonthLength(month,year,julianFlag)
{
   var ml;
   if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
      {ml = 31;}
   else {
       if(month==2) {
          ml = 28;
          if(!(year%4) && (julianFlag==1 || year%100 || !(year%400)))
             ml++;
       }
       else
          {ml = 30;}
   }
   return ml;    
}

///////////////////////////////////////////////////////////////
// calculate AGE from BIRTHDAY.
// FILTER of 18 yrs old and above only.
//   form         = form name.
//   where        = index of row.
//   error_empty  = error message to display when blank.
//   error_invalid= error message to display when invalid.
//   tdname       = input id to add css class.
//   error_field  = id of error row.
///////////////////////////////////////////////////////////////
function computeAge(form,where,error_empty,error_invalid,tdname,error_field)
{
//   var ml={31,28,31,30,31,30,31,31,30,31,30,31};

if((form.b_year.value !='') && (form.b_mon.value !='') && (form.b_day.value !=''))
{
   MNames=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
      "Oct","Nov","Dec");

   Today=new Date();   
   var yd = Today.getFullYear()+0;
   var md = Today.getMonth()+1;
   var dd = Today.getDate();

   var yb = parseInt(form.b_year.value);
   var mb = parseInt(form.b_mon.value);
   var db = parseInt(form.b_day.value);

   // Month length 0->use calendar length
   var mLength = 0;
   // 0 if Gregorian, 1 is Julian

   var ma=0;
   var ya=0;

   var da = dd-db;
   // This is the all-important day borrowing code.
   if(da<0)
   {
      md--;
      // Borrow months from the year if necesssary.
      if(md<1)
      {
	 yd--;
	 // Determine no. of months in year
	 if(mLength)
	    {md=md+parseInt(365/mLength);}
	 else
	    {md=md+12;}
      }
      if(mLength==0) // Use real month length if no fixed
      {              // length is indicated - note that we add a leap day if necessary.
         ml=getMonthLength(md,yd);
	 da=da+ml;
      } 
      // For this case, everything works like it did in elementary school.
      else
	 {da+=mLength;} // Use fixed month length
   }

   ma = md - mb;
   // Month borrowing code - borrows months from years.
   if(ma<0)
   {
      yd--;
      if(mLength!=0)
	 {ma=ma+parseInt(365/mLength);}
      else
	 {ma=ma+12;}
   }

   ya = yd - yb;

   if(ya<18)
   {
   	//alert(where +" || "+ error_empty +" || "+error_invalid +" || "+tdname+" || "+ error_field);
   	document.getElementById(id_b_mon).className=class_error_textf;
   	document.getElementById(id_b_day).className=class_error_textf;
	insRow(where,error_empty,error_invalid,1,tdname,error_field);
	return false;
   }
   else
   {
      	delRow(where,tdname,error_field);
	return true;
   }
}
}
