// 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 = "http://umanitoba.ca/ip/dcooke/html-retiever2.php?var1="+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 = $('midText');



      //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. 



  * 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,wid,hght)



{



var win = new Window({className: "alphacube",  width:wid, height:hght, zIndex: 1000000, resizable: true, title: "News Release", url: html,showEffect:Effect.Appear, showEffectOptions:({ duration: .5 }), hideEffect: Effect.DropOut, draggable:true, wiredDrag: true})

win.showCenter();

//win.show();



}//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



 *



 */



function fetchBlogPosts()



{



		



	 	var url = '/about/media_room/php/GrabPosts.php';	 	



		//prompt("",url);



		new Ajax.Request(url,



		{		



		method:'get',



		onSuccess: function(transport){



		 				  	



		 var response = transport.responseText || "no response text";



		 //prompt("",response);



		 var theContent = $('midText');		  



		 theContent.update(response);

		 fixUnderflow();

		},



		onException: function(transport, exception) {



		var ename = exception.name;



		var emsg = exception.message;



		var theContent = $('midText');



		theContent.update("ERROR: "+ename+", "+emsg);



		}



		});



		//prompt("","After the new Ajax.Request call");







}//end fetchBlogPosts.



/**



 * getFeaturedExpert



 * 



 * Retrieves the current featured expert



 * to be displayed on the media room page.



 *



 * auth: Dave Cooke



 * date: Apr. 15, 08



 */



function getFeaturedExpert()



{



	var url = "/about/media_room/php/getFeaturedExpert.php";



	new Ajax.Request(url,



	{		



		method:'get',



		onSuccess: function(transport){



		 				  	



		 var response = transport.responseText || "no response text";		  		  



		 var theContent = $('feedContent4');		  



		 theContent.update(response);



		}



		



	});



	//prompt("","After the new Ajax.Request call");	



}//end getFeaturedExpert



function ajaxRequest(url,divID)



{



	//prompt("",url + "          " + divID);	



	new Ajax.Request(url,



	{		



		method:'get',



		onSuccess: function(transport){



		 //prompt("","before var response");			  	



		 var response = transport.responseText || "no response text";



		 //prompt("",response);



		 var theContent = $(divID);		  



		 theContent.update(response);

		 

		},



		onFailure: function(){ alert('Something went wrong...'); },



		onException: function(transport, exception) {



		/**



		Of course, this handler will now only restrict its activities to TypeError exceptions. All other errors will be ignored.



		The JavaScript 1.5 specification defines six primary error types, as follows:		



		EvalError - raised when the eval() functions is used in an incorrect manner;		



		RangeError - raised when a numeric variable exceeds its allowed range;		



		ReferenceError - raised when an invalid reference is used;		



		SyntaxError - raised when a syntax error occurs while parsing JavaScript code;		



		TypeError - raised when the type of a variable is not as expected;		



		URIError - raised when the encodeURI() or decodeURI() functions are used in an incorrect manner; 



		excerpt from http://www.alise.lv/ALISE/technolog.nsf/0/53d693d7d1a4f198c2257066003d6153?OpenDocument



		**/



		var ename = exception.name;



		var emsg = exception.message;



		var theContent = $(divID);



		theContent.update("ERROR: "+ename+", "+emsg);



     	//prompt("",exception.message);



   		}



		



	});



	//prompt("","After the new Ajax.Request call");		



	



}//end ajaxRequest



/**



function sendData () {



	var url = 'process.php';



	var pars = Form.serialize('frm');



	var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onLoading: showLoad, onComplete: showResponse} );



}



function showLoad () {



	$('load').style.display = 'block';



}



function showResponse (originalRequest) {



	var newData = originalRequest.responseText;



	$('load').style.display = 'none';



	$('status').innerHTML = newData;



}



**/



function search()



{



	var url = "/about/media_room/php/searchExpert.php";

	//a Prototype.js function...

	var pars = Form.serialize('searchForm');



	//prompt("",url + "        parms: "+ pars);	



	new Ajax.Request(url,



	{		



		method:'get',



		parameters:pars,



		onSuccess: function(transport){



		 //prompt("","before var response");			  	



		 var response = transport.responseText || "no response text";



		 //prompt("",response);



		 var theContent = $('searchResult');		  



		 theContent.update(response);



		},



		onException: function(transport, exception) {



		var ename = exception.name;



		var emsg = exception.message;



		var theContent = $('searchResult');



		theContent.update("ERROR: "+ename+", "+emsg);



		}



	});



	//prompt("","After the new Ajax.Request call");		



	



}//end search

function searchAdmin()



{



	var url = "http://www.umanitoba.ca/about/media_room/php/admin/searchExpertAdmin.php";

	//a Prototype.js function...

	var pars = Form.serialize('searchForm');



	//prompt("",url + "        parms: "+ pars);	



	new Ajax.Request(url,



	{		



		method:'get',



		parameters:pars,



		onSuccess: function(transport){



		 //prompt("","before var response");			  	



		 var response = transport.responseText || "no response text";



		 //prompt("",response);



		 var theContent = $('searchResult');		  



		 theContent.update(response);



		},



		onException: function(transport, exception) {



		var ename = exception.name;



		var emsg = exception.message;



		var theContent = $('searchResult');



		theContent.update("ERROR: "+ename+", "+emsg);



		}



	});



	//prompt("","After the new Ajax.Request call");		



	



}//end search
function submitForm(divID)
{
	var url = "http://umanitoba.ca/include/form_handler.php";
        var pars = Form.serialize('operatour',true);
	new Ajax.Request(url,
	{
		method:'post',
                parameters:pars,
		onSuccess: function(transport){
		 var response = transport.responseText || url;
		 var theContent = $(divID);
		 theContent.update(response);
                 //$('fpRow').show();
		},
		onException: function(transport, exception) {
		var ename = exception.name;
		var emsg = exception.message;
		var theContent = $(divID);
		theContent.update("ERROR: "+ename+", "+emsg);
		}

	});
	//prompt("","After the new Ajax.Request call");
}//end search
function updateExpert()

{



	var url = 'http://www.umanitoba.ca/about/media_room/php/admin/processUpdateExpert.php';

	// /local/wwwdata/about/media_room/php/processUpdateExpert.php

	// http://www.umanitoba.ca/about/media_room/php/processUpdateExpert.php

	var pars = Form.serialize('expertUpdateForm',true);

	

	//prompt("",url + "        parms: "+ pars);	



	new Ajax.Request(url,



	{		



		method: 'post',



		parameters:pars,



		onSuccess: function(transport){



		 //prompt("","before var response");			  	



		 var response = transport.responseText || "no response text";



		 //prompt("",response);



		 var theContent = $('resultUpdate');		  



		 theContent.update(response);



		},

		onFailure: function(){ alert('Something went wrong...'); },

		onException: function(transport, exception) {



		var ename = exception.name;



		var emsg = exception.message;



		var theContent = $('resultUpdate');



		theContent.update("ERROR: "+ename+", "+emsg);



		}



	});



	

}//end expertUpdate

/**

id=\"firstName\">

	id=\"lastName\">

	id=\"faculty\">

	id=\"dept\">

	id=\"phone\">

	id=\"spec\">

	id=\"meta\">

	id=\"id\"

	**/

function pickExperts(id,first,last,fac,dep,spc,phone,tag)

{



	$('spec').value = spc;

	$('firstName').value = first;

	$('lastName').value = last;

	$('faculty').value = fac;

	$('phone').value = phone;

	$('meta').value = tag;

	$('dept').value = dep;

	$('id').value = id;

	

}//end pickExpert

function clear()

{



	$('spec').value = "";

	$('firstName').value = "";

	$('lastName').value = "";

	$('faculty').value = "";

	$('phone').value = "";

	$('meta').value = "";

	$('dept').value = "";

	$('id').value = "";

	$('delete2').checked = false;

	

	

}//end pickExpert

/**

 * form validation 

 *

 *

 */

function validate_form ( )

{

    valid = true;

	alertText = "";

    if ( document.expertUpdateForm.firstName.value == "" )

    {

		document.expertUpdateForm.firstName.style.background = 'Red';

        alertText = alertText + "Please fill in the 'First Name' box."+"\n";

        valid = false;

    }//end if

    if ( document.expertUpdateForm.lastName.value == "" )

    {

		document.expertUpdateForm.lastName.style.background = 'Red';

        alertText = alertText + "Please fill in the 'Last Name' box."+"\n";

        valid = false;

    }//end if

    if ( document.expertUpdateForm.faculty.value == "" )

    {

		document.expertUpdateForm.faculty.style.background = 'Red';

        alertText = alertText + "Please fill in the 'Faculty' box."+"\n";

        valid = false;

    }//end if

    if ( document.expertUpdateForm.dept.value == "" )

    {

		document.expertUpdateForm.dept.style.background = 'Red';

        alertText = alertText + "Please fill in the 'Dept' 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.expertUpdateForm.phone.value == "" )

    {

		document.expertUpdateForm.phone.style.background = 'Red';

        alertText = alertText + "Please fill in the 'Phone' box."+"\n";

        valid = false;

    }//end if

	else

	{

		var result =  validatePhone(document.expertUpdateForm.phone);

		if(result != "")

		{

			alertText = alertText + result;

			valid = false;

		}//end if.

		

	}//end else.

	if(valid == false)

	{

		//alert(alertText);

		$('resultUpdate').update(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 == 7)) {

        error = "The phone number is the wrong length. Make sure you included an area code.\n";

        fld.style.background = 'Red';

    } 

    return error;

}//end validatePhone



function showMenu(id,subid)

{

	//showMenu('test1','intro')

	if(document.getElementById(id).innerHTML == " - ")

	{

		//prompt(""," if before id: "+id+",display: "+document.getElementById('test2').style.display);

		document.getElementById(subid).style.display = "none";

		//prompt(""," if after id: "+id+",display: "+document.getElementById('test2').style.display);

		document.getElementById(id).innerHTML = " + ";

	}//end if

	else

	{

		//prompt(""," else before id: "+id+",display: "+document.getElementById('test2').style.display);

		document.getElementById(subid).style.display = "list-item";

		//prompt(""," else after id: "+id+",display: "+document.getElementById('test2').style.display);

		document.getElementById(id).innerHTML = " - ";

	}//end else.

}//end function showMenu
function rotateCentreImage(bannerPath)
{
	
	var url = "http://www.umanitoba.ca/ip/dcooke/getShowImages.php?var1="+bannerPath+"&var2='splashImg'";
	
	ajaxRequest(url,'splashImg');



}
function buildImageDIVs(imagePath,divID,firstImage)
{
	var url = "http://www.umanitoba.ca/js/buildImageDIVs.php?var1="+imagePath+"&var2="+divID+"&var3="+firstImage;
	
	new Ajax.Request(url,
	{		
		method:'get',
		onSuccess: function(transport){
		 var response = transport.responseText || "no response text";
		 var theContent = $(divID);
		 theContent.update(response);	 
		
		},

		onFailure: function(){ alert('Something went wrong...'); },
		onException: function(transport, exception) {

		/**
		Of course, this handler will now only restrict its activities to TypeError exceptions. All other errors will be ignored.
		The JavaScript 1.5 specification defines six primary error types, as follows:	
		EvalError - raised when the eval() functions is used in an incorrect manner;
		RangeError - raised when a numeric variable exceeds its allowed range;	
		ReferenceError - raised when an invalid reference is used;		
		SyntaxError - raised when a syntax error occurs while parsing JavaScript code;
		TypeError - raised when the type of a variable is not as expected;	
		URIError - raised when the encodeURI() or decodeURI() functions are used in an incorrect manner;
		excerpt from http://www.alise.lv/ALISE/technolog.nsf/0/53d693d7d1a4f198c2257066003d6153?OpenDocument
		**/

		var ename = exception.name;
		var emsg = exception.message;
		var theContent = $(divID);
		theContent.update("ERROR: "+ename+", "+emsg);
     	
   		}

	});
			
}//end buildImageDIVs
function rotateCentreImage2(id,randomImageFlag){
	var picName = '';
	if (id == 'splashImg')
	{
		picName = 'centre';
	}//end if
	else if (id == 'rightImage')
	{
			picName = 'rightt';
	}//end else if
	else
	{
		//empty.
	}//end else
	//only execute the slideshow if the picName is defined.
	randomImageFlag = 0;
	if (picName != '') {
	
		var list = $(id).getElementsByClassName('slide');
		var size = list.length;
		//prompt("","listSize:"+size);
		if (randomImageFlag == 1) {
			var randomnumber=Math.floor(Math.random()*size);
			var test = picName+randomnumber;
			//prompt("","file:"+test+", listSize:"+size);
			Effect.Appear(picName+randomnumber);
		}//end if
		else {
			start_slideshow(0, size - 1, 5000, picName);
		}//end else.
	}//end if

}
function start_slideshow(start_frame, end_frame, delay,picName)
{
	//prompt("","in startslideshow");
	//prompt("","start_frame:"+start_frame+", start_frame: "+start_frame+", end_frame: "+end_frame+", delay: "+delay);
    setTimeout(switch_slides(start_frame,start_frame,end_frame, delay,picName), delay);
}
function switch_slides(frame,start_frame,end_frame,delay,picName)
{
    return (function()
    {
        Effect.Fade(picName + frame,{duration:2.0,from:1.0,to:0});
        if (frame == end_frame) { frame = start_frame; } else { frame = frame + 1; }
        //setTimeout("Effect.Appear('banner" + frame + "');", 1200);
		//setTimeout("Effect.Appear('banner" + frame + "');", 100);
		Effect.Appear(picName + frame,{duration:2.0,from:0,to:1.0});
        setTimeout(switch_slides(frame, start_frame, end_frame, delay,picName), delay + 5000);    
    })
}
function randUofMoreButton()
{
    //get a number between 0 and 10
    var randNum = Math.floor(11*Math.random());
    if(randNum > 5)
    {
        document.getElementById("uofmoref").style.visibility = "visible";
		document.getElementById("uofmoref").style.display = "";
        document.getElementById("uofmorem").style.display = "none";
    }//end if.
    else
    {
        document.getElementById("uofmorem").style.visibility = "visible";
		document.getElementById("uofmorem").style.display = "";
        document.getElementById("uofmoref").style.display = "none";
    }//end else.
}//end randUofMoreButton
