/***************************************************************************************
Validate PID and Birthdate Check Forms
***************************************************************************************/
//Making sure there is both a PID option checked, and a birthdate.
function validateUnsub(theForm) {
	var reason = "";
	reason += validateEmail(theForm.Email);
	
	if (reason != "") {
		alert("Some fields need correction:\n" + reason);
		return false;
	}else{
		var sure = window.confirm("Are you sure you want to unsubscribe this email address?")
		if (sure)
			return true;
		else
			return false;
	}
}

/***************************************************************************************
Validate PID and Birthdate Check Forms
***************************************************************************************/
//Making sure PID is numeric, that there is a PID option checked, and there is birthdate.
function validatePidCheck(theForm) {
	var reason = "";
	reason += validatePid(theForm.pid);
	reason += validatePidStatus(theForm.pid_status);
	reason += validateEmptyBday(theForm.birthMonth,theForm.birthDay,theForm.birthYear);
	
	if (reason != "") {
		alert("Some fields need correction:\n" + reason);
		return false;
	}else{
		return true;
	}
}

function validateReqPidCheck(theForm) {
	var reason = "";
	reason += validateEmpty(theForm.pid);
	reason += validatePid(theForm.pid);
	//reason += validatePidStatus(theForm.pid_status);
	reason += validateEmptyBday(theForm.birthMonth,theForm.birthDay,theForm.birthYear);
	
	if (reason != "") {
		alert("Some fields need correction:\n" + reason);
		return false;
	}else{
		return true;
	}
}

//Checking only birthdate
function validateBday(theForm) {
	var reason = "";
	reason += validateEmptyBday(theForm.birthMonth,theForm.birthDay,theForm.birthYear);
	
	if (reason != "") {
		alert(reason);
		return false;
	}else{
		return true;
	}
}

/*\\ Sub functions //*/
function validatePid(fld) {
    var error = "";
	var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');
	
	if (fld.value.length != 0 && isNaN(stripped)) { 

        error = "The Player ID may only be numbers.\n";
        fld.style.border = 'thin solid #85262f';
    }else {
        fld.style.border = '';
    }
    return error;
}

function validatePidStatus(fld) {
	var error = "";
	
	myOption = -1;
	for (i = fld.length-1; i > -1; i--) {
		if (fld[i].checked) {
			myOption = i;
			i = -1;
		}
	}
	if (myOption == -1) {
		//fld.style.border = 'thin solid #85262f';
		error = "Please enter a player number status.\n";
	}
	return error;
}

function validateEmptyBday(month, day, year) {
    var error = "";
 
    if (month.value.length == 0 || day.value.length == 0 || year.value.length == 0) {
    	
		month.style.border = 'thin solid #85262f';
		day.style.border = 'thin solid #85262f';
		year.style.border = 'thin solid #85262f';
                
		error = "Please enter a complete birthdate.\n"
    } else {
    	if (isNaN(month.value) || isNaN(day.value) || isNaN(year.value)) {
    	
			month.style.border = 'thin solid #85262f';
			day.style.border = 'thin solid #85262f';
			year.style.border = 'thin solid #85262f';
					
			error = "Please enter only numbers for birthdate.\n"
		}else{
			
			//return month.value.length;
			//if (month.value.length < 2){ error .= "Month must be two digits long.\n"; }
			//if (day.value.length < 2){ error .= "Day must be two digits long.\n"; }
			//if (year.value.length < 4){ error .= "Year must be four digits long.\n"; }
			
			month.style.border = '';
			day.style.border = '';
			year.style.border = '';
		}
    }
    return error;  
}


/***************************************************************************************
Validate Update Form
***************************************************************************************/
// Calls a bunch of sub functions to test for empty and incorrectly filled out fields.
function validateFormOnSubmit(theForm) {
	var reason = "";
	reason += validateEmpty(theForm.firstname);
	reason += validateEmpty(theForm.lastname);
	reason += validateEmpty(theForm.birthdate);
	reason += validateEmpty(theForm.address1);
	reason += validateEmpty(theForm.city);
	reason += validateDropdown(theForm.state);
	reason += validateZip(theForm.zip);
	reason += validateEmail(theForm.Email);
	reason += validateTerms(theForm.legal);
	
	if (reason != "") {
		alert("Some fields need correction:\n" + reason);
		return false;
	}
	return true;
}

/*\\ Sub functions. //*/
function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
    	var label = "";
    	label = findLabelTextFor(fld);
    	if (label == null) {
    		label = "required field";
    	}
        fld.style.border = 'thin solid #85262f';
        
        
        if (label.charAt(0).match(/[aeiouAEIOU]/)){
        	error = "You didn't enter an "+label+".\n"
        }else{
        	error = "You didn't enter a "+label+".\n"
        }
    } else {
        fld.style.border = '';
    }
    return error;  
}

function validateDropdown(opt) {
	var error = "";
	if (opt.selectedIndex == 0) {
		error = "You didn't select a State.\n";
	}    
	return error;
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.border = 'thin solid #85262f';
        error = "You didn't enter an Email Address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.border = 'thin solid #85262f';
        error = "Please enter a valid Email Address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.border = 'thin solid #85262f';
        error = "The Email Address contains illegal characters.\n";
    } else {
        fld.style.border = '';
    }
    return error;
}

function validateZip(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a ZIP code.\n";
        fld.style.border = 'thin solid #85262f';
    } else if (isNaN(parseInt(stripped))) {
        error = "The ZIP code contains illegal characters.\n";
        fld.style.border = 'thin solid #85262f';
    }else {
        fld.style.border = '';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.border = 'thin solid #85262f';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.border = 'thin solid #85262f';
    }
    return error;
}

function validateMobile(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

    if (fld.value == "") {
        
    } else if (isNaN(parseInt(stripped))) {
        error = "The mobile number contains illegal characters.\n";
        fld.style.border = 'thin solid #85262f';
    } else if (!(stripped.length == 10)) {
        error = "The mobile number is the wrong length. Make sure you included an area code.\n";
        fld.style.border = 'thin solid #85262f';
    }
    return error;
}

function validateTerms(fld) {
    var error = "";

   if (fld.checked == false) {
        error = "You must be 21 and agree to the Privacy Policy.\n";
   }
    return error;
}

/***************************************************************************************
Utility functions
***************************************************************************************/
function findLabelFor (elOrId) {
  var el = typeof elOrId == 'string' ? document.getElementById(elOrId) : 
elOrId;
  var labels = document.getElementsByTagName('LABEL');
  var found = false;
  for (var l = 0; l < labels.length; l++)
    if (found = el.id == labels[l].htmlFor)
      break;
  if (found)
    return labels[l];
  else
    return null;
}

function findLabelTextFor (elOrId) {
  return findLabelFor(elOrId).firstChild.nodeValue;
}

function combDate(theForm) {
	var month = theForm.bday_mo.value;
	var day = theForm.bday_day.value;
	var year = theForm.bday_year.value;

	theForm.custom_birthday.value = month+"/"+day+"/"+year;
  }

function trim(s){
  return s.replace(/^\s+|\s+$/, '');
}