// JavaScript for nomination form

addEvent(window,'load',initTips);


///////////////////////////////////////////////////////////////
// email a friend
function submit_friendly_form() {

	if (document.form1.email1.value || document.form1.email2.value || document.form1.email3.value || document.form1.email4.value || document.form1.email5.value) {
		if (document.form1.email1.value) {
			if(!check_email(document.form1.email1.value,document.form1.email1)) { return; }
		}

		if (document.form1.email2.value) {
			if(!check_email(document.form1.email2.value,document.form1.email2)) { return; }
		}

		if (document.form1.email3.value) {
			if(!check_email(document.form1.email3.value,document.form1.email3)) { return; }
		}

		if (document.form1.email4.value) {
			if(!check_email(document.form1.email2.value,document.form1.email4)) { return; }
		}

		if (document.form1.email5.value) {
			if(!check_email(document.form1.email2.value,document.form1.email5)) { return; }
		}
		document.form1.submit();
	}
	else {
		alert("Please insert an email address into one of the boxes.");
	}
}

/////////////////////////////////////////////////////////////////////
// email address validation
function check_email(which_email,for_focus) {
	var i = 0;
	var the_test = 0;
	var testString = which_email;
	
	msg_alert= "Please use a valid email address.";
	
	if (testString.length < 8) {
		msg_alert = msg_alert + "\nYour email address is too short to be valid."
		the_test = 1;
	}
	if ((i = testString.indexOf ("@",1)) < 1) {
		msg_alert = msg_alert + "\nAll email addresses, must include the '@' sign.";
		the_test = 1;
	}
	if (testString.indexOf (".",i) <= i+2) {
		msg_alert = msg_alert + "\nThere must be at least 2 characters between the '@' sign and the dot '.'";
		the_test = 1;
	}
	
	if (the_test) {
		alert(msg_alert);
		if(for_focus){ 
			for_focus.focus();
		}
		return(false);
	}
	else {
		return(true);
	}
}

// auto-select a radio button by ID if the text field is not empty
// call this when somebody starts typing in a form field that's a followup to the radio button selection
function select_radio(t,objid){
	if(document.getElementById){
		var obj = document.getElementById(objid)
		if(obj){
			if(t.value.length>0){ obj.checked = 'checked'
			}
		}
	}
}


////////////////////////////////////////////////////////////////////	

// pull the nom id and email values from the form on the page
// print the nomination form
function print_page(nom_id,email) {
	var args = "width=740,height=540,scrollbars=yes"
	window.open("the_form.php?what_to_do=print_nomination&nomination="+nom_id+"&my_email="+email,"nomination",args);
}
	
var preview=0;

/////////////////////////////////////////////////////////////////
function submit_form() {
	// check to make sure mandatory fields are filled in:
	
	if (!document.form1.nom_first_name.value) {
		alert ("Please insert the nominee's first name.");
		document.form1.nom_first_name.focus();
	}
	else if (!document.form1.nom_last_name.value) {
		alert ("Please insert the nominee's last name.");
		document.form1.nom_last_name.focus();
	}
	else if(!document.form1.nom_work.value) {
		alert ("Please insert the name of the company who the nominee works for.");
		document.form1.nom_work.focus();
	}
	else if(!document.form1.nom_title.value) {
		alert ("Please insert the title of the nominee at their work.");
		document.form1.nom_title.focus();
	}
	else if(document.form1.nom_state.options.value == 'none') {
	
		alert ("Please tell us in what state the \nnominee performed the exceptional service, \nsacrifice or innovation.");
		document.form1.nom_state.focus();
	
	}
	else if(!document.form1.description.value) {
	
		alert ("Please fill in a brief description of \nthe nominee's exceptional service, \nsacrifice or innovation.");
		document.form1.description.focus();
	}
	else if(!document.form1.your_first_name.value) {
	
		alert ("Please insert your first name.");
		document.form1.your_first_name.focus();
	}
	else if(!document.form1.your_last_name.value) {
	
		alert ("Please insert your last name.");
		document.form1.your_last_name.focus();
	
	}
	else if(!document.form1.your_email.value) {
	
		alert ("Please insert your email address.");
		document.form1.your_email.focus();
	
	}
	else if(document.form1.your_state.options.value == 'none') {
	
		alert ("Please tell us which state you live in.");
		document.form1.your_state.focus();
	
	}
	else if(document.form1.your_email.value != document.form1.your_email2.value) {
	
		alert ("Please re-type your email address.\nThe confirm email address does not match\nthe original.");
		document.form1.your_email.focus();
	}
	else if(document.form1.heard_about_assoc.checked == true && !document.form1.nursing_association.value) {
	
		alert ("Please tell us which professional association told you about the Cherokee Inspired Comfort Award.");
		document.form1.nursing_association.focus();
	}
	else if(document.form1.related.options.value == 'yes' && !document.form1.related_how.value) {
	
		alert ("Please tell us how you are related to the person you are nominating.");
		document.form1.related_how.focus();
	}
	else if(!document.form1.how_do_you_know.value) {
	
		alert ("Please tell us how you know the person you are nominating.");
		document.form1.how_do_you_know.focus();
	}
	else if(document.form1.work_with.options.value == 'yes' && !document.form1.your_employer.value) {
	
		alert ("Please tell us who your employer is.");
		document.form1.your_employer.focus();
	
	}
	else if(document.form1.work_with.options.value == 'yes' && !document.form1.your_title.value) {
	
		alert ("Please tell us your title at work.");
		document.form1.your_title.focus();
	}
	else if ((document.form1.nom_phone1.value || document.form1.nom_phone2.value || document.form1.nom_area_code.value) && (!document.form1.nom_area_code.value || !document.form1.nom_phone1.value || !document.form1.nom_phone2.value)) {
	
		alert ("Please fill out the entire phone number for the nominee \nOR\nleave it blank.");
		document.form1.nom_area_code.focus();
	
	}
	else if (!document.form1.your_area_code.value || !document.form1.your_phone1.value || !document.form1.your_phone2.value) {
	
		alert ("Please fill out your entire phone number.");
		document.form1.your_area_code.focus();
	
	}
	else if(!document.form1.nurse_type1.checked && !document.form1.nurse_type2.checked && !document.form1.nurse_type3.checked && !document.form1.nurse_type4.checked && !document.form1.nurse_type5.checked) {
	
		alert ("Please tell us which type of nurse your nominee is.");
		document.form1.nurse_type1.focus();
	
	}
	// not required
	/*
	else if(document.form1.credit_for_nomination[0].checked && document.form1.credited_nursing_school.value.length=='') {
		alert ("Please tell us which nursing school should get credit.");
		document.form1.credited_nursing_school.focus();	
	}
	else if(document.form1.credit_for_nomination[1].checked && document.form1.credited_nursing_school.value.length=='') {
		alert ("Please tell us which nursing association should get credit.");
		document.form1.credited_nursing_school.focus();	
	}
	else if(document.form1.credit_for_nomination[2].checked && document.form1.credited_employer.value.length=='' ) {
		alert ("Please tell us who the employer is.");
		document.form1.credited_employer.focus();
	}
	else if(document.form1.credit_for_nomination[2].checked && document.form1.credited_employer_location.value.length=='') {
		alert ("Please tell us where the employer is located.");
		document.form1.credited_employer_location.focus();
	}
*/
	else {
	
		if (document.form1.nom_phone1.value && document.form1.nom_phone2.value && document.form1.nom_area_code.value) {
	
			document.form1.nom_phone.value = "(" + document.form1.nom_area_code.value + ")" + document.form1.nom_phone1.value + "-" + document.form1.nom_phone2.value;
	
		}
	
	
	
		if (document.form1.your_phone1.value && document.form1.your_phone2.value && document.form1.your_area_code.value) {
	
			document.form1.your_phone.value = "(" + document.form1.your_area_code.value + ")" + document.form1.your_phone1.value + "-" + document.form1.your_phone2.value;
	
		}
	
		if(document.form1.how_to_inform[0].checked || document.form1.how_to_inform[1].checked) {
			var the_nom_email = check_email(document.form1.nom_email.value,document.form1.nom_email);
			if (!the_nom_email) { return false; }
		}
		var my_email = check_email(document.form1.your_email.value,document.form1.your_email);
		if (my_email) {
			if (preview==1) {
				document.form1.submit();
				//alert("submit form");
			} else {
				preview_submission();
			}
		} else { return false; 
		}
	}
//	return false
} // end submit_form()


/////////////////////////////////////////////////////////////////
// preview nomination form
function preview_submission() {
	var the_description = document.form1.description.value;
	var split_description = new Array();
	var split_description = the_description.split("\n");
	var html_description = "<p>";
	for(var i=0;i<split_description.length;i++){
		html_description = html_description + split_description[i] + "<br />";
	}
	
	var newwin = window.open('','','scrollbars=yes,width=700,height=500,resizable=yes');
	newwin.document.write('<HTML><HEAD><TITLE>Cherokee Inspired Comfort Award - Preview Submission</TITLE></HEAD>');
	newwin.document.write('<BODY bgcolor=white leftmargin=\"0\" marginwidth=\"0\" topmargin=\"0\" marginheight=\"0\">');
	newwin.document.write('<table width="100%" border=0 cellpadding=0 cellspacing=0><tr><td width=5></td><td align="right"><a href=\"javascript:window.close()\">close window</a></td><td></td></tr></table><br /><br /><CENTER>');
	newwin.document.write('<table width=600 border=0 cellpadding=0 cellspacing=0 bgcolor=white class=body><tr><td><B>Please review your submission.</B>  If all the fields are filled in the way you want them to be, press the \'Submit Nomination\' button at the bottom of this page.  If you need to edit any part of the submission page, close this window, and make your changes to the nomination form.<br /><br /></td></tr></table>');
	newwin.document.write('<table width=600 border=0 cellpadding=3 cellspacing=1 bgcolor=lightgrey class=body>');
	newwin.document.write('<tr><td colspan=2   ><B>Information about the person you want to nominate (the nominee):</B></td></tr>');
	newwin.document.write('<tr><td bgcolor=white width=220 align=right>Nominee\'s Name:</td><td bgcolor=white width=380 align=left>' + document.form1.nom_first_name.value + ' ' + document.form1.nom_last_name.value + '</td></tr>');
	newwin.document.write('<tr><td bgcolor=white width=220 align=right>Name of the nominee\'s employer:</td><td bgcolor=white width=380 align=left>' + document.form1.nom_work.value + '</td></tr>');
	newwin.document.write('<tr><td bgcolor=white width=220 align=right>Nominee\'s title at work:</td><td bgcolor=white width=380 align=left>' + document.form1.nom_title.value + '</td></tr>');
	newwin.document.write('<tr><td bgcolor=white width=220 align=right>Nominee\'s telephone number:</td><td bgcolor=white width=380 align=left>' + document.form1.nom_phone.value + '</td></tr>');
	var i = document.form1.nom_state.selectedIndex
	var state = document.form1.nom_state.options[i].value
	newwin.document.write('<tr><td bgcolor=white colspan=2>Where did the nominee perform the exceptional service, sacrifice or innovation?<br />' + ' City: ' + document.form1.nom_city.value + ' | State: ' + state + ' | Zip code: ' + document.form1.nom_zip.value +'</td></tr>');
	newwin.document.write('</table><table width=600 border=0 cellpadding=3 cellspacing=1 bgcolor=lightgrey class=body>');
	newwin.document.write('<tr><td bgcolor=white colspan=2>Description:<br />' + html_description + '</td></tr>');
	newwin.document.write('</table><table width=600 border=0 cellpadding=3 cellspacing=1 bgcolor=lightgrey class=body>');
	
	var i = document.form1.where.selectedIndex
	var value = document.form1.where.options[i].value
	newwin.document.write('<tr><td bgcolor=white colspan=2>Where did your nominee provide the exceptional service, sacrifice or innovation?<br />' + value + '</td></tr>');
	
	// find which of these checkboxes are checked
	var value = ''; 
	var checkarr = ['nurse_type1','nurse_type2','nurse_type3','nurse_type4','nurse_type5']
	if(document.getElementById){
		for(var i=0;i<checkarr.length;i++){
			var node= document.getElementById(checkarr[i]);
			if(node && node.checked){
				label = findInputLabel(checkarr[i]);
				value += label + "<br />";
			}
		}
	}
	newwin.document.write('<tr><td bgcolor=white colspan=2>What type of healthcare professional is the nominee?<br />' + value + '</td></tr>');
	
	newwin.document.write('</table><br /><br />');
	
	/* ************************************************************************ */
	// second table:
	newwin.document.write('<table width=600 border=0 cellpadding=3 cellspacing=1 bgcolor=lightgrey class=body>');
	newwin.document.write('<tr><td colspan=2><B>Information About You:</B></td></tr>');
	newwin.document.write('<tr><td bgcolor=white width=220 align=right>Your name:</td><td bgcolor=white width=380 align=left>' + document.form1.your_first_name.value + ' ' + document.form1.your_last_name.value + '</td></tr>');
	newwin.document.write('<tr><td bgcolor=white width=220 align=right>Your telephone number:</td><td bgcolor=white width=380 align=left>' + document.form1.your_phone.value + '</td></tr>');
	newwin.document.write('<tr><td bgcolor=white width=220 align=right>Your email address:</td><td bgcolor=white width=380 align=left>' + document.form1.your_email.value + '</td></tr>');
	
	
	newwin.document.write('<tr><td bgcolor=white colspan=2>If you are a current member of any nurse association (state, specialty, or private), please type the full name of the association:<br />' + document.form1.nurse_association.value + '</td></tr>');
	newwin.document.write('<tr><td bgcolor=white colspan=2>If you currently attend a school of nursing, please type the full name of the school:<br />' + document.form1.nursing_school.value + '</td></tr>');
	
	var i = document.form1.your_state.selectedIndex
	var state = document.form1.your_state.options[i].value
	newwin.document.write('<tr><td bgcolor=white colspan=2>Where do you live?<br />' + ' City: ' + document.form1.your_city.value + ' | State: ' + state + ' | Zip code: ' + document.form1.your_zip.value +'</td></tr>');
	
	// find which of these checkboxes are checked
	var value = ''; 
	// array structure: the checkbox,the followup field id (if any)
	var checkarr = [
		'heard_about_assoc',"nursing_association",
		'heard_about1',null,
		'heard_about2',"heard_about_nursing_website",
		'heard_about3',"heard_about_nursing_publication",
		'heard_about4',null,
		'heard_about5',null,
		'heard_about6',null,
		'heard_about7','heard_about_school_of_nursing',
		'heard_about8','heard_about_other'
	]
	if(document.getElementById){
		for(var i=0;i<checkarr.length;i+=2){
			var node= document.getElementById(checkarr[i]);
			if(node && node.checked){
				var label = findInputLabel(checkarr[i]);
				// see if we have an elaboration on this answer
				if(checkarr[i+1]){
					var more = document.getElementById(checkarr[i+1])
					if(more){ label += ': '+more.value 
					}
				}
				value += label + "<br />";
			}
		}
	}
	newwin.document.write('<tr><td bgcolor=white colspan=2>How did you hear about the Cherokee Inspired Comfort Award? <br />' + value + '</td></tr>');
	
	// find the selected radio button and the followup to it
	var value = ''; var found = false
	for (var i=0;i<document.form1.credit_for_nomination.length;i++) {
		if (document.form1.credit_for_nomination[i].checked) {
			var input = document.getElementById(document.form1.credit_for_nomination[i].id);
			if(input){ value += getLabelSurrounding(input) + ': ' 
			}
			found = true
			break;
		}
	}
	if(found){
		switch(i){
			case 0:	
				value += document.form1.credited_nursing_school.value
				break;
			case 1:
				value += document.form1.credited_nursing_association.value
				break;
			case 2:
				value += document.form1.credited_employer.value
				value += ' in '
				value += document.form1.credited_employer_location.value
				break;
		}
		//alert('nom credit index = '+i+'\nid='+document.form1.credit_for_nomination[i].id+'\n'+value); 
		newwin.document.write('<tr><td bgcolor=white colspan=2>Whom would you like to receive credit for this nomination?<br />' + value + '</td></tr>');
	}


	var i = document.form1.related.selectedIndex
	var value = document.form1.related.options[i].value
	newwin.document.write('<tr><td bgcolor=white colspan=2>Are you related to the person you are nominating? ' + value + '</td></tr>');
	newwin.document.write('<tr><td bgcolor=white colspan=2>If so, how are you related?<br />' + document.form1.related_how.value + '</td></tr>');
	newwin.document.write('<tr><td bgcolor=white colspan=2>How do you know the person you are nominating?<br />' + document.form1.how_do_you_know.value + '</td></tr>');
	
	var i = document.form1.work_with.selectedIndex
	var value = document.form1.work_with.options[i].value
	newwin.document.write('<tr><td bgcolor=white colspan=2>Do you work with the person you are nominating? ' + value + '</td></tr>');
	newwin.document.write('<tr><td bgcolor=white colspan=2>If you work with your nominee, who is your employer?<br />' + document.form1.your_employer.value + '</td></tr>');
	newwin.document.write('<tr><td bgcolor=white colspan=2>If you work with your nominee, what is your title?<br />' + document.form1.your_title.value + '</td></tr>');
	
	var i = document.form1.do_you_wear_uniforms.selectedIndex
	var value = document.form1.do_you_wear_uniforms.options[i].value
	newwin.document.write('<tr><td bgcolor=white colspan=2>Do you wear uniforms at work? ' + value + '</td></tr>');
	newwin.document.write('<tr><td bgcolor=white colspan=2>Name of the fine Cherokee retailer where you buy your uniforms (if applicable):<br />' + document.form1.retailer_name.value + '</td></tr>');
	
	
	newwin.document.write('</table>');
	newwin.document.write('<br /><INPUT type=button name=\"final_submit\" value=\"Submit Nomination\" ONCLICK=\"window.opener.submit_form_now()\; window.close()\;\">');
	newwin.document.write('</CENTER><br /><br /></BODY></HTML>');
	newwin.focus();
	preview=0;
}
/* *********************************************************************************** */
// label maker
function findInputLabel(field_name){
	var label = 'Unknown Field'; 
	var objid = null
	if(document.getElementsByTagName){
		var inputs = document.getElementsByTagName('input')
		for(var i=0;i<inputs.length;i++){
			if (inputs[i].name==field_name){
				var id = inputs[i].getAttribute('id')
				var label_temp = getLabelFor(id)
				if( label_temp ){ label = label_temp
				} else {
					label_temp = getLabelSurrounding(inputs[i])
					if( label_temp ){ label = label_temp
					}
				}
			}
		}
	}
	return label
}

function getLabelFor(id){
	var label = null
	if(document.getElementsByTagName){
		var labels = document.getElementsByTagName('label')
		var f = null
		for(var i=0;i<labels.length;i++){
			f = labels[i].htmlFor
			if(f==id){
				label = __getLabelValue(labels[i])
				break;	
			}
		}
	}
	return label
}

function getLabelSurrounding(obj){
	var label = null
	var parent = obj.parentNode
	var node = obj
	for(var i=0;i<9999;i++){
		if(parent.tagName=='LABEL'){
			label = __getLabelValue(parent)
			break;	
		}
		parent = node.parentNode
		node = parent
	}
	return label
}

function __getLabelValue(labelnode){
	label = ''
	for(var j=0;j<labelnode.childNodes.length;j++){
		var node = labelnode.childNodes[j]
		if(node.nodeType==3){ label += node.data
		}
	}
	return label;
}
// end label maker
/* *********************************************************************************** */

/////////////////////////////////////////////////////////////////
function submit_form_now() {
	window.focus();
	document.form1.submit();
}

function initTips(){
	if(document.getElementById){
		var x = document.getElementById('nomtips_container');
		var ln = document.getElementById('nomtips_link');
		if(x && ln){
			ln.onclick = function(){
				toggleDisplay('nomtips_container');
				return false;
			}
		}
	}
}


// see http://www.scottandrew.com/weblog/articles/cbs-events
// fails quietly on IE5/MAC
function addEvent(obj, evType, fn, useCapture){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
//	} else {alert("Handler could not be attached");
	}
}


// toggle visibility of an element directly
function toggleDisplay(id) {
  if (document.getElementById) {
    var elem = document.getElementById(id);
    if (elem) {
      if (elem.style.display == 'none') {
        elem.style.display = 'block';
      } else {
        elem.style.display = 'none';
      }
    }
  }
} 
