// David M Rautert
// October 2, 2007
// Form Validation


/*
** Configuration Directives
*/

var effects = false;

window.onload = popoverConfirm;

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}


function validateForm() {
	if (!document.getElementsByTagName){ return; }
	
	// fname
	var fname = document.signup_form.person_firstName.value;
	if(fname == null || fname == "") {
		popUpMessage("Please enter your first name.");
		return false;
	}
	
	// lname
	var lname = document.signup_form.person_lastName.value;
	if(lname == null || lname == "") {
		popUpMessage("Please enter your last name.");
		return false;
	}
	
	// Email validation
	var emailAddress = document.signup_form.person_email.value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(!filter.test(emailAddress)) {
		popUpMessage("Please enter a valid email address.");
		return false;
	}
	
	// street address
	var streetaddr = document.signup_form.person_homeAddress_street1.value;
	if(streetaddr == null || streetaddr == "") {
		popUpMessage("Please enter your street address.");
		return false;
	}
	
	// Zip Code validation
	var zipCode = document.signup_form.person_homeAddress_zipcode.value;
	var re = /^\d{5}([\-]\d{4})?$/;
	if(!re.test(zipCode)) {
		popUpMessage("Please enter a valid zip code.");
		return false;
	}
	
	//selection - birthYear
	var birthYearValue = document.signup_form.person_birthYear.value;
	if (birthYearValue == 1){
		popUpMessage("Please select your year of birth.");	
		return false;
	}
	
	
	// gender buttons 
	var form = $('signup_form');
	var buttons = form.getInputs('radio', 'person_gender');
	for(i=0; i<buttons.length; i++) {
		if(buttons[i].checked == true) {
			var somethingIsChecked = true;	
		}
	}	
	if((somethingIsChecked != true) && (buttons.length > 0)) {
		popUpMessage("Please select your gender.");
		return false;
	}
	
	
	// Checkbox validation
	var checkbox = document.signup_form.agreeTerms;
	if(checkbox.checked != true) {
		popUpMessage("You must agree to the terms of use before proceeding.");
		return false;
	}
	
	
	
	/*if(formIsValid == true) {
		return true;	
	} else {
		popUpMessage("Please make sure to fill in all required fields..");
		return false;	
	}*/
	return true;
	
}



function validateRefer() {
	if (!document.getElementsByTagName){ return; }
	  var form = $('referFriendForm');
	
	var fname = document.referFriendForm.firstName.value;
	var lname = document.referFriendForm.lastName.value;
	var emailAddress = document.referFriendForm.fromEmailAddress.value;
	//var emailAddressb = document.referFriendForm.toAddress.value;
	var emailAddresst = document.getElementById('toAddress').value;
	
	
	
	var currentURL = location.href;
	
	if(fname == "" || lname == "" || emailAddress == "" || emailAddresst == "") {
		popUpMessage("Please make sure to fill out all the fields.");
		return false;
	}
	
	
	var fnameFilter = /^[a-zA-Z\']+$/;
	if(!fnameFilter.test(fname)) {
		popUpMessage("Please enter a valid first name.");
		return false;
	}
	
	
	var lnameFilter = /^[a-zA-Z\'\ ]+$/;
	if(!lnameFilter.test(lname)) {
		popUpMessage("Please enter a valid first name.");
		return false;
	}
	
	// Email validation
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(!filter.test(emailAddress)) {
		popUpMessage("Please make sure your own email address is valid.");
		return false;
	}
	
	// Friend Email validation	
	var filterb  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(!filterb.test(emailAddresst)) {
		popUpMessage("Please enter a valid email address to send to.");
		return false;
	}
	
	
	document.referFriendForm.success.value = currentURL;
	document.referFriendForm.success.value = currentURL;
	
	document.cookie = "sentThisPage=yes";	
	return true;
}

function popoverConfirm(){
	var checkCookie = getCookie('sentThisPage');
	
	if (checkCookie == "yes"){	
		tooltip('headerArea', 'popOverEmailArea', 'show', 0, -300, -250, 'Appear', 0.50);
		document.getElementById('emailThisPageForm').style.display = "none";
		document.getElementById('emailThisPageConfirmation').style.display = "block";
		document.cookie = "sentThisPage=no";
	}
	
	return;
}


function addFriendInput() {
	// Find relative path depth to determine location of images folder
	pathDepth = 0;
	hcpSide = false;
	var strHref = window.location.href;
	var handler = strHref.split('/');
	for(var i=0; i<handler.length; i++) {
		if(handler[i] == "info") {
			pathDepth = 1;
		}
		if(handler[i] == "about_azor" || handler[i] == "managing_hypertension" || handler[i] == "patient_resources" || handler[i] == "utilities" || handler[i] == "professional_resources") {
			pathDepth = 2;
		}
		if(handler[i] == "hcp") {
			hcpSide = true;	
		}
	}
	if(hcpSide == true) {
		pathDepth++;	
	}
	
	// Determine how many friend inputs currently exist
	counter = 1;
	for(var x=0; x<counter; x++) {
		if(document.getElementById('recipient'+counter)) {
			counter++;
		}
	}
	
	if(document.getElementById('toAddress').value == "") {
		alert('You have not filled in the required friend\'s email yet.');
	}
	
	
	// If friend inputs number less than our limit AND first friend email is not blank, construct new input field
	if(counter <= 5 && document.getElementById('toAddress').value != "") {
		if(counter == 5) {
			document.getElementById('addMoreFriendLink').style.display = "none";
		}
		document.getElementById('recipient1').innerHTML = "* Recipient's email address 1";
		myDiv = document.getElementById('extraEmailInputs');
	
		var objFormListArea = document.createElement("div");
		// objFormListArea.setAttribute('class','formlistArea4');
		objFormListArea.style.clear = "both";
		objFormListArea.style.margin = "0px 0px 10px 18px";
		objFormListArea.style.height = "20px";
		objFormListArea.style.width = "430px";
		myDiv.appendChild(objFormListArea);
		
		var objInputLabel = document.createElement("span");
		objInputLabel.setAttribute('id', 'recipient'+counter);
		objInputLabel.innerHTML = "&nbsp;&nbsp;Recipient's email address "+counter;
		objInputLabel.style.float = "left";
		objInputLabel.style.margin = "0px 9px 0px 0px";
		objInputLabel.style.width = "180px";
		objInputLabel.style.display = "inline";
		objInputLabel.style.fontFamily = "Arial,Sans-serif";
		objInputLabel.style.color = "#FFFFFF";
		objInputLabel.style.fontSize = "12px";
		objInputLabel.style.lineHeight = "18px";
		objInputLabel.style.height = "18px";
		objFormListArea.appendChild(objInputLabel);
		
		var objInputArea = document.createElement("input");
		objInputArea.setAttribute('type', 'test');
		objInputArea.setAttribute('id', 'toAddress'+counter);
		//objInputArea.setAttribute('name', 'toAddress'+counter);
		objInputArea.setAttribute('name', 'toAddress');
		// objInputArea.setAttribute('class', 'formTextInputArea2');
		objInputArea.style.float = "left";
		objInputArea.style.display = "inline";
		objInputArea.style.margin = "0px 3px 0px 10px";
		objInputArea.style.padding = "2px 2px 1px 10px";
		objInputArea.style.height = "17px";
		objInputArea.style.width = "170px";
		if(pathDepth == 0) {
			objInputArea.style.background = "url(images/bg_input_form.gif) 0px 0px no-repeat";
		} else if(pathDepth == 1) {
			objInputArea.style.background = "url(../images/bg_input_form.gif) 0px 0px no-repeat";
		} else if(pathDepth == 2) {
			objInputArea.style.background = "url(../../images/bg_input_form.gif) 0px 0px no-repeat";
		}
		objInputArea.style.border = "1px solid #C3C3C3";
		objInputArea.style.fontFamily = "Arial,Sans-serif";
		objInputArea.style.color = "#000000";
		objInputArea.style.fontSize = "11px";
		objInputArea.style.lineHeight = "11px";
		objFormListArea.appendChild(objInputArea);
		
		document.getElementById("extraEmailInputs").style.display = "block";
		var clearBothDiv = document.createElement("div");
		// clearBothDiv.setAttribute('class','clearBoth');
		clearBothDiv.style.clear = "both";
		myDiv.appendChild(clearBothDiv);
	}
}


function clearFriendInput(){
		document.getElementById("extraEmailInputs").innerHTML = "";
		document.getElementById('addMoreFriendLink').style.display = "block";
		tooltip('headerArea', 'popOverEmailArea', 'hide');
		
}


function kickInConfirm(myType) {
	switch(myType) {
		case "email":
		
		
			break;
	}	
	
}


function popUpMessage(myMessage) {
	if(effects == true) {
		new Effect.Appear('d3', { duration: 1.0, queue: 'front' });
		new Effect.Fade('d2', { duration: 4.0, queue: 'end' });
	} else {
		alert(myMessage);	
	}
}


function validateFormB() {
	// if (!document.getElementsByTagName){ return; }
	// alert("test!");
	// first name
	var fname = document.signup_form.person_firstName.value;
	if(fname == null || fname == "") {
		popUpMessage("Please enter a valid first name.");	
		return false;
	}
	
	// last name
	var lname = document.signup_form.person_lastName.value;
	if(lname == null || lname == "") {
		popUpMessage("Please enter a valid last name.");	
		return false;
	}

	//selection - medical degrees
	var medicalDegreeValue = document.signup_form.hcp_professionalTitle.value;
	if (medicalDegreeValue == 1){
		popUpMessage("Please select your professional designation.");	
		return false;
	}
	
	//selection - specialty
	var specialtyValue = document.signup_form.hcp_specialty.value;
	if (specialtyValue == 1){
		popUpMessage("Please select your specialty.");	
		return false;
	} else if ((specialtyValue == "other") && (document.signup_form.specify_other_text.value == "")){
		popUpMessage("Please enter your specialty.");
		return false;
	}
	
	// Email validation
	var emailAddress = document.signup_form.person_email.value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(!filter.test(emailAddress)) {
		popUpMessage("Please enter a valid email address.");
		return false;
	}
	
	// address validaiton
	var physaddress = document.signup_form.person_workAddress_street1.value;
	if(physaddress == null || physaddress == "") {
		popUpMessage("Please enter a valid work street address.");
		return false;
	}
	
	// Zip Code validation
	var zipCode = document.signup_form.person_workAddress_zipcode.value;
	var re = /^\d{5}([\-]\d{4})?$/;
	if(!re.test(zipCode)) {
		popUpMessage("Please enter a valid zip code.");
		return false;
	}
	
	//selection - birthYear
	var birthYearValue = document.signup_form.person_birthYear.value;
	if (birthYearValue == 1){
		popUpMessage("Please select your year of birth.");	
		return false;
	}
	
	// Checkbox validation
	var checkbox = document.signup_form.agreeTerms;
	if(checkbox.checked != true) {
		popUpMessage("You must agree to the terms of use before proceeding.");
		return false;
	}	
}