/////////////////////////////////////////////////////////////////
// Scripts.js
//
// This file contains customized scripts used by the CPE web site.
//
// Arguments: None
/////////////////////////////////////////////////////////////////



// ------------------------------------------
// Rewrites a page to be "printer-friendly"

function whole_document_printer_compliant_printing()
{ 
	var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
    disp_setting+="scrollbars=yes,width=800, height=600, left=100, top=25";  	 
	
	var currenturl = document.location.href;
	
	var javascript_printing_html_page_title = document.getElementById("upmtb").innerHTML;
	var javascript_printing_html_page_body = document.getElementById("uplbs2").innerHTML;	
	var javascript_printing_html_page_footer = document.getElementById("footer_text").innerHTML;
	
	var docprint=window.open("","",disp_setting); 
   	docprint.document.open();
	docprint.document.write('<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n');
	docprint.document.write('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n');
	docprint.document.write('<head>');
	docprint.document.write('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />\n');
	docprint.document.write('<meta http-equiv="Content-Language" content="en-us" />\n');
	docprint.document.write('<title>DePaul University Continuing and Professional Education</title>\n');
	docprint.document.write('<link rel="stylesheet" href="/standard/css/screen/javascript.css" type="text/css" media="screen" />\n');
	docprint.document.write('<link rel="stylesheet" href="/standard/css/print/javascript.css" type="text/css" media="print" />\n');
	docprint.document.write('<link rel="stylesheet" href="/standard/css/print/layout.css" type="text/css" media="all" />\n');	
	docprint.document.write('<link rel="stylesheet" href="/standard/css/print/default.css" type="text/css" media="all" />\n');	
	docprint.document.write('<link rel="stylesheet" href="/standard/css/print/content.css" type="text/css" media="all" />\n');
	docprint.document.write('<link rel="stylesheet" href="/standard/css/print/images.css" type="text/css" media="all" />\n');
	docprint.document.write('<link rel="stylesheet" href="/standard/css/print/alignment.css" type="text/css" media="all" />\n');
	docprint.document.write('<link rel="stylesheet" href="/standard/css/print/navigation.css" type="text/css" media="all" />\n');
	docprint.document.write('<link rel="stylesheet" href="/standard/css/print/spacing.css" type="text/css" media="all" />\n');
	docprint.document.write('</head>\n');
	docprint.document.write('<body>\n');
	
	/* Begin print left column - for printing and return-to-page options */
	docprint.document.write('<div id="upwl">\n');
	docprint.document.write('<div id="upwlct"><a href="javascript:self.print()" title="Print Page">Print Page</a></div>\n');
	docprint.document.write('<div class="vsb5"></div>\n');
	docprint.document.write('<div id="upwlct"><a href="javascript:window.close()" title="Close Window">Close Window</a></div>\n');
	docprint.document.write('</div>\n');
	
	/* Begin print right column - for remaining site content */
	docprint.document.write('<div id="upwr">\n');
	docprint.document.write('<div id="upmtb">\n');
	docprint.document.write(javascript_printing_html_page_title);
	docprint.document.write('\n</div>\n');
	docprint.document.write('<div class="vsb15"></div>\n');
	docprint.document.write('<div id="uplbs2">\n');
	docprint.document.write(javascript_printing_html_page_body);
	docprint.document.write('\n</div>\n');
	
	/* Manually write in the footer styles to avoid printing extraneous information */	
	docprint.document.write('<div id="uplbs3">\n');
	docprint.document.write('<div id="uplbs3c">\n');
	docprint.document.write('<div id="uplbs3d"></div>\n');
	docprint.document.write('<div class="vsb10"></div>\n');
	docprint.document.write(javascript_printing_html_page_footer);
	docprint.document.write('\n</div>\n');
	docprint.document.write('</div>\n');
	docprint.document.write('</div>\n');
	docprint.document.write('\n</body>\n');
	docprint.document.write('</html>');
	docprint.document.close();
	docprint.focus();
}


// ------------------------------------------
// This function is INCOMPLETE!
// This function allows for course section-only information printing




// ------------------------------------------
// Verifies "email_sent" form for completion

function checkFormEmailSent_Custom1(Frm)
{
	// Validate name.
	if (Frm.YourName.value == "")
	{
		alert("Your name is required.")
		Frm.YourName.focus()
		return false
	}

	// Validate email.
	if  (!validEmail(Frm.YourEmail.value)) 
	{
		alert("Invalid e-mail specified")
		Frm.YourEmail.focus()
		return false
	}
	
	// Validate friend's email
	if (Frm.FriendEmail.value == "")
	{
		alert("Your friend's email is required.")
		Frm.FriendEmail.focus()
		return false
	}

	return true
}



// ------------------------------------------
// Clears out the contents of the CPE Google search engine box

function clearForms()
{
	document.forms[0].reset();
}



// ------------------------------------------
// Opens a new browser window (supersedes the "target=" attribute, which is deprecated)

function NewWindow(page)
{
	window.open(page);
	return false;
}



// ------------------------------------------
// Verfies OLS examination submission

// universal examination type function
function OLSExamVerify(ExamForm) {
	
	// Loop through all the form's elements.
	for (var i=0; i<ExamForm.elements.length; i++) {
		
		// reset validation variable for each element item
		var ExamValidation = false;
		
		// current form item type
		var OLSExamElementType = ExamForm.elements[i].type;
		
		switch (OLSExamElementType) {
			
			// radio button
			case 'radio':
			
			// current form element
			var OLSExamElement = ExamForm.elements[i];
			
			// compare current form element to all other elements
			if (ValidateOLSExamRadio(ExamForm, OLSExamElement)) {
				ExamValidation = true;
			}
			break;
		}
		
		// escape loop if validation fails
		if (!ExamValidation) {
			return false;
		}
	}
	
	return true;
}


// radio button verification
function ValidateOLSExamRadio(Exam, CurrentExamElement) {
	
	// control element name
	var TotalSelected = 0;

		// Loop through all the form's elements.
		for (var i=0; i<Exam.elements.length; i++) {
			
			// current form item verification
			var ExamVerify = Exam.elements[i].checked;
			
			// 1) Compare control element name to current element iteration
			// 2) Determine element marking
			if ((Exam.elements[i].name == CurrentExamElement.name) && (ExamVerify == true)) {
				TotalSelected++;
			}		
		}
		
		// Alert the registrant if no items have been checked.
		if (TotalSelected != 1) {
			alert('A response is required for:\nQuestion #'+CurrentExamElement.name);
			CurrentExamElement.focus();
			return false;
		}
		
		else {
			return true;
		}
}



// ------------------------------------------
// Verify that at least one breakout has been selected - Breakout.asp
function verifyBreakoutSelection(Frm)
{
	
// Instantiate the counter variable
var namedVariable = 0;

// Proceeding the "|" (vertical line) element.  Secondary values are fees.
var FeeMatch = new RegExp("[0-9]*$");

// Preceeding the "|" (vertical line) element.  Primary values are breakout attributes.
var BreakoutMatch = new RegExp("[0-9]*\u007C$");
	
	// Loop through all elements on the form.
	for(var i=0; i<Frm.elements.length; i++)
	{
		
	// Set the variables to breakout item values and fee item values
	var value_string = Frm.elements[i].value;
	var BreakoutFee = FeeMatch.exec(value_string);
	var BreakoutItem = BreakoutMatch.exec(value_string);
	
	// Convert checkbox item into a conditional value
	var BreakoutCheck = Boolean(Frm.elements[i].checked);
	
	// Convert arrays to conditional values
	var TestItem = new Number(parseInt(BreakoutItem[0]));
	var TestFee = Boolean(BreakoutFee[0]);
	
		// Check all items
		if((!TestFee) && (BreakoutCheck) && (TestItem > 0))
		{		
		namedVariable++;
		}
	}
	
	// Determine if any courses have been selected using the increment counter
	switch(true)
	{
	case(namedVariable < 1):
	alert('Please select at least one course session.');
	return false;
	break;
	
	default:
	checkFormBreakout(Frm);
	}
}
