// JavaScript Document
/**
 * gets content from URL and writes it
 * to a specified area.
 *
 * author: Dave Cooke
 * Date: Jan. 25, 2008
 * Notes: see http://www.w3schools.com/dom/dom_http.asp
 */
 function getNewsItem(urlObject)
 {
	var url = urlObject;
	//prompt("",url);
	new Ajax.Request(url,
    {
    method:'get',
    onSuccess: function(transport){
      var response = transport.responseText || "no response text";
      //prompt("Success! \n\n", response);
	  var theContent = $('news');
      //theContent.update(url).setStyle({ background: '#dfd' });
	  theContent.update(response);
    },
    onFailure: function(){ alert('Something went wrong...') }
    });
 }//end function getNewsItem.
 /**
  * callSoapService
  *
  * calls the soap client on the server which will
  * call the function required.
  *
  */
 function callPHPFunction()
 {
	 	var url = "http://umanitoba.ca/ip/dcooke/retrieve-info2.php";
		//prompt("",url);
		new Ajax.Request(url,
		{
		
		method:'get',
		onSuccess: function(transport){
			//alert("hererer");
		  var response = transport.responseText || "no response text";
		  //prompt("Success! \n\n", response);
		  var theContent = $('midText');
		  //theContent.update(url).setStyle({ background: '#dfd' });
		  theContent.update(response);
		},
		onFailure: function(){ alert('Something went wrong...') }
		});	 
 }//end callSoapService
 /**
  * hideContacts
  *
  * Hides all contacts except the contact that was passed as
  * a parameter. <script src="AC_RunActiveContent.js" language="javascript"></script>
  * This function tries to grab all elements from the document
  * that have the class name "fullInfo". It then iterates through
  * the list of elements and hides all those elements ACCEPT the
  * one element that has the contactID value as its element ID.
  * 
  * requires: protocol.js, Effect.js (from scriptaculous http://script.aculo.us/)
  * param: contactID (css element id)
  * author: Dave Cooke
  * date: Feb 20th, 07
  */
function hideContacts(contactID) {
	//var contacts = document.getElementsByName("fullInfo");
	var contacts = document.getElementsByClassName("fullInfo");
	var contactsNum = contacts.length;
	//alert("hideContacts: "+contactID);
	//alert("contactsNum: "+contactsNum);
	//only do the loop if contactID exists.
	if(contactID)
	{
		for (i=0; i<contactsNum; i++)
		{  
			//get the current object that contains the div and contact info
			selectedNode = contacts[i];
			//get the class of the div...used for debugging
			selectedNodeClass = contacts[i].className;
			//get the ID of the div
			ident = selectedNode.identify();
			//determine if the current ID is the same as the contactID
			//passed as a parameter. If it isn't then make sure that the full
			//contact information for this node is not shown.
			if(contactID != ident )
			{
				//alert("selectedNodClass == contactID:  "+selectedNodeClass+" = "+contactID+" = "+contactID.indexOf(selectedNodeClass));
				Effect.BlindUp(selectedNode,{duration:0});
			}//end if.
	
		}//end for loop
	}//end if.
}//end hideContacts
/**
 * expandContact
 *
 * Displays all the contact information for the given
 * contact which is identified by the contactID (css element ID)
 *
 * requires: protocol.js, Effect.js (from scriptaculous http://script.aculo.us/)
 * param: contactID (css element ID)
 * author: Dave Cooke
 * date: Feb 20th, 07
 *
 */
function expandContact(contactID)
{
	
	Effect.BlindDown(contactID,{duration:1});
	hideContacts(contactID);
	
}//end makeContactsWindow
/**
 *
 * showMedia
 *
 * uses window.js and prototype.js libraries from
 * scriptaculous to display a web page.
 *
 * auth: Dave Cooke
 * date: Mar. 14, 2008
 *
 */
function showMedia(html)
{
var win = new Window({className: "dialog",  width:800, height:600, zIndex: 1000000, resizable: true, title: "Manitoba Rowing Association", url: html,showEffect:Effect.BlindDown, hideEffect: Effect.SwitchOff, draggable:true, wiredDrag: true})
//win.getContent().innerHTML= ;
//win.getContent().update(response);
win.setStatusBar("MRA");
win.showCenter();
}//end function
/**
 *
 * fetchBlogPosts
 *
 * calls a PHP page on the web server and displays the results.<b> 
 * This function is using the scriptaculous prototype.js library
 * which is essentially using HTTPRequest.
 * auth: Dave Cooke
 * date: Mar. 14, 2008
 *new Ajax.Request('/some_url',   {     method:'get',     onSuccess: function(transport){ 
 */
function fetchBlogPosts()
{
		/**
	 	var url = "http://umanitoba.ca/ip/dcooke/Media_Room/GrabPosts.php";	 	
		prompt("",url);
		new Ajax.Request(url,
		{		
		method:'get',
		onSuccess: function(transport){
		 prompt("asdfhaskdfhl");				  	
		 var response = transport.responseText || "no response text";		  		  
		 var theContent = $('midText');		  
		 theContent.update(response);
		}
		
		});
		prompt("","After the new Ajax.Request call");
		**/
		var url = "http://umanitoba.ca/ip/dcooke/Media_Room/GrabPosts.php";
		new Ajax.Request(url,
		{
		     method:'get',
		     onSuccess: function(transport){
		     var response = transport.responseText || "no response text";
		     alert("Success! \n\n" + response);     },
		     onFailure: function(){ alert('Something went wrong...') }
		}); 	 
}//end fetchBlogPosts.
/**
 * form validation 
 *
 *
 */
function validate_form ( )
{
    valid = true;
	alertText = "";
    if ( document.contactForm.Name.value == "" )
    {
		document.contactForm.Name.style.background = 'Red';
        alertText = alertText + "Please fill in the 'Name' box."+"\n";
        valid = false;
    }//end if
	if ( document.contactForm.Date_of_birth.value == "" )
    {
		document.contactForm.Date_of_birth.style.background = 'Red';
        alertText = alertText + "Please fill in the 'Date of Birth' box."+"\n";
        valid = false;
    }//end if
	if ( document.contactForm.School.value == "" )
    {
		document.contactForm.School.style.background = 'Red';
        alertText = alertText +  "Please fill in the 'School' box."+"\n";
        valid = false;
    }//end if
	if ( document.contactForm.Email.value == "" )
    {
		document.contactForm.Email.style.background = 'Red';
        alertText = alertText + "Please fill in the 'Email' box."+"\n";
        valid = false;
		
    }//end if
	else
	{
		var result = validateEmail(document.contactForm.Email);
		if(result != "")
		{
			alertText = alertText + result;
			valid = false;
		}
		
	}//end else.
	if ( document.contactForm.Phone.value == "" )
    {
		document.contactForm.Phone.style.background = 'Red';
        alertText = alertText + "Please fill in the 'Phone' box."+"\n";
        valid = false;
    }//end if
	else
	{
		var result =  validatePhone(document.contactForm.Phone);
		if(result != "")
		{
			alertText = alertText + result;
			valid = false;
		}//end if.
		
	}//end else.
	if(valid == false)
	{
		alert(alertText);
	}//end if
    return valid;
}//end validate_form
/**
 * trim
 *
 * function courtesy of http://www.webcheatsheet.com/javascript/form_validation.php
 *
 * Aug 05, 2008
 */
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
/**
 * validateEmail
 *
 * function courtesy of http://www.webcheatsheet.com/javascript/form_validation.php
 *
 * Aug 05, 2008
 */
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.background = 'Red';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Red';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Red';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}//end validateEmail
/**
 * validatePhone
 *
 * function courtesy of http://www.webcheatsheet.com/javascript/form_validation.php
 *
 * Aug 05, 2008
 */
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Red';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Red';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Red';
    } 
    return error;
}//end validatePhone
