//comments removed to improve DL time, see old version for comments
function gID(ElToGet){
	return document.getElementById(ElToGet);
}
function ValidateAll(){
	var CF = document.CFForm_1;
	var a = CheckName(CF.fname.value,'Please enter your first name.','The first name is not in a valid format.',gID('tr1').sectionRowIndex,'td1','fname_error');
	var b = CheckName(CF.lname.value,'Please enter your last name.','The last name is not in a valid format.',gID('tr2').sectionRowIndex,'td2','lname_error');
	var c = CheckEmpty(CF.home_address.value,'Please enter your street address.','',gID('tr3').sectionRowIndex,'td3','address_error');
	var d = CheckNum(CF.home_zip.value,'Please enter your zip code.','The zipcode you entered is not valid.',gID('tr4').sectionRowIndex,'td4',5,'zip_error');
	var e = CheckEmail(CF.home_email.value,'Please enter your email.','The email you entered is not valid.',gID('tr5').sectionRowIndex,'td5','email_error');
	var f = CheckNum(CF.w_ph_ac.value,'Please enter your day time phone.','The phone number is not in a valid format.',gID('tr6').sectionRowIndex,'td6-1',3,'phone_error');
	var g = CheckNum(CF.w_ph_pre.value,'Please enter your day time phone.','The phone number is not in a valid format.',gID('tr6').sectionRowIndex,'td6-2',3,'phone_error');
	var h = CheckNum(CF.w_ph_num.value,'Please enter your day time phone.','The phone number is not in a valid format.',gID('tr6').sectionRowIndex,'td6-3',4,'phone_error');
	var i = CheckEmpty(CF.b_mon.value,'Please select your birthdate.','',gID('tr7').sectionRowIndex,'td7-1','bday_error');
	var j = CheckEmpty(CF.b_day.value,'Please select your birthdate.','',gID('tr7').sectionRowIndex,'td7-2','bday_error');
	var k = CheckEmpty(CF.b_year.value,'Please select your birthdate.','',gID('tr7').sectionRowIndex,'td7-3','bday_error');
	var l = CheckEmpty(CF.Gender.value,'Select your gender/marital status.','',gID('tr8').sectionRowIndex,'td8','genm_error');
	var m = CheckEmpty(CF.marital_status.value,'Select your gender/marital status.','',gID('tr8').sectionRowIndex,'td8-2','genm_error');
	var n = IsItChecked('gsg_optout','Sorry, you must agree to the terms and conditions.','tr9');
	var o = computeAge(CF,gID('tr7').sectionRowIndex,'','Sorry, you must be 18 years or older to register.','td7-3','bday_error');
	//var p = CheckEmpty(CF.home_city.value,'Please enter a city.','The city is invalid',gID('tr10').sectionRowIndex,'td10','city_error');
	//var q = CheckEmpty(CF.home_state.value,'Please enter a state.','The state is invalid',gID('tr11').sectionRowIndex,'td11','state_error');

	if(a && b && c && d && e && f && g && h && i && j && k && l && m && n && o) ; else return false;
}
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;
}
function IsItChecked(checkname,errormsg,tdname){
	if(!gID(checkname).checked){
		gID(tdname).className="check-error";
		alert(errormsg);
	} else {
		gID(tdname).className="itext";
		return true;
	}
}
function IsItPresent(value){
	value = StripSpacesFromEnds(value);
	if(value.length != 0) return value; else return false;
}
function insRow(where,error_empty,error_invalid,invalid,tdname,error_field){
	gID(tdname).className="font-error";
	if(!(gID(error_field)) && (invalid!==1)){
		where=where+1;
		var x=gID('thetable').insertRow(where);
		var y=x.insertCell(0);
		var y2=x.insertCell(1);
		var z=gID('thetable').rows[where].cells;
		//z[0].colSpan="2";
		z[1].className="main";
		z[1].id=error_field;
		y2.innerHTML="<span class=\"error\" style=\"display:block;;background:url('img/gsg_error_arrow.gif') no-repeat 1px left;padding-left:10px\">"+ error_empty +"&nbsp;</span>";
	} else if(!(gID(error_field)) && (invalid==1)) {
		where=where+1;
		var x=gID('thetable').insertRow(where);
		var y=x.insertCell(0);
		var y2=x.insertCell(1);
		var z=gID('thetable').rows[where].cells;
		//z[0].colSpan="2";
		z[1].className="main";
		z[1].id=error_field;
		y2.innerHTML="<span class=\"error\" style=\"display:block;\"><img src=\"img/gsg_error_arrow.gif\" border=\"0\">&nbsp;"+ error_invalid +"&nbsp;</span>";
	}
}
function delRow(where,tdname,error_field){
	where=where+1;
	if(gID(error_field)){
		gID('thetable').deleteRow(where);
		gID(tdname).className="itext";
	} else {
		return false;
	}
}
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=='td7-1' || tdname=='td7-2' || tdname=='td7-3'){
			if((gID('td7-1').className=='itext') && (gID('td7-2').className=='itext') && (gID('td7-3').className=='itext')){
				delRow(where,tdname,error_field);
				return true;
			} else {
				return false;
			}
		}
		return true;
	}
}
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;
	}
}
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=='td6-1' || tdname=='td6-2' || tdname=='td6-3') {
					if((gID('td6-1').className=='itext') && (gID('td6-2').className=='itext') && (gID('td6-3').className=='itext')) {
						delRow(where,tdname,error_field);
						return true;
					} else {
						return false;
					}
				}
			}
		}
		return true;
	}
}
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 {
		delRow(where,tdname,error_field);
		return true;
	}
}
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;    
}
function computeAge(form,where,error_empty,error_invalid,tdname,error_field){
	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,10);
		var mb = parseInt(form.b_mon.value,10);
		var db = parseInt(form.b_day.value,10);
		
		// 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,10); 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,10); else ma=ma+12;
		}
		
		ya = yd - yb;

		if(ya<18){
			gID('td7-1').className="font-error";
			gID('td7-2').className="font-error";
			insRow(where,error_empty,error_invalid,1,tdname,error_field);
			return false;
		} else {
			delRow(where,tdname,error_field);
			return true;
		}
	}
}