//***********************************************************************************
//
// Onload Event Handler
//
//***********************************************************************************


ViewState = function(guid, section){
	this.guid 			= guid;
	this.section 		= section;
	this.changeUrl  = section + "/" + guid ;
	this.url = "handler.asp?guid=" + guid + "&sectionid=" + section;
}
ViewState.prototype.forward = function(){
		var message = new Object();
		message.guid = this.guid;
		message.section = this.section;
		message.url = this.url; 
		message.type = "loadPage";
		sendMessage(message);
}
ViewState.prototype.back = function(){
		var message = new Object();
		message.guid = this.guid;
		message.section = this.section;
		message.url = this.url; 		
		message.type = "loadPage";
		sendMessage(message);
}


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(init);
addLoadEvent(function() {
  /* more code to run on page load */ 
  /* use this function to add functions to the onload event list */
});

var movie = null;
var output = null;
var jsReady = false;
     


function init(){
  
  jsReady = true;
  
  output = document.getElementById("output");
  
  if(document["talent"]){
    movie = document["talent"];
  }else{
    movie = window["talent"];
  }
  
  if(incorrectPlugin){
    var flDisplay = document.getElementById("noflash");
    var movieContainer = document.getElementById("movie");
    movieContainer.style.display = "none";
    
    flDisplay.style.display = "block";
    
    flDisplay.className = "noflash";

    flDisplay.innerHTML = '<div class="bodytext"><p class="alert">This Site Requires Flash 9</p><p>This site requires Flash version 9,0,28,0. Follow this link to <a href=http://www.macromedia.com/go/getflash/>Get Flash</a></p><h1>ARE YOU TALENT LOOKING FOR OPPORTUNITY?</h1><p>Do you know exactly what you want out of your career?</p><p>More than that - do you know exactly what you want out of life?</p><p>Or are you working too hard and too many long hours to ever stop and think about it - really think about it?</p><h1>ARE YOU OPPORTUNITY LOOKING FOR TALENT?</h1><p>The single most important driver of business success is people. Great, talented people who will help you take your business where you want and need it to go.</p><p>And yet finding the right talent for your business is one of the most difficult tasks you face.</p></div>';
    
  }

  
	// setup back button support
		var state = new ViewState("","");
		dojo.undo.browser.setInitialState(state);

}

function isReady() {
  dojo.debug("isReady called");
   return jsReady;
}



function getPage(pageId){
	dojo.debug("getPage called");
	//alert("getPage:" + pageId);
	if(pageId != ""){
//		pageId.replace('/','');
		//alert("pageid: " + pageId);
		pageId.replace('#','');		
		pageId.replace('?','');		
		var message = new Object();
//		var guid = pageId.substring(message.guid.lastIndexOf("#") + 1);
		if(pageId.indexOf('_') > -1){
			message.section =  pageId.split('_')[0]; 
			message.guid    =  pageId.split('_')[1];				
		}else{
			message.section =  pageId.split('/')[0]; 
			message.guid    =  pageId.split('/')[1];	
		}
		message.type = "loadPage";
//		message.guid = pageId;
		handleGet(message);

	}
}

/**
Tells the movie to load a new xml file.  Currently the main nav list Get
*/
function passNewXML(location){
    var message = new Object();
		message.type = "newXML";
		message.url = location;
		sendMessage(message);
}

/**
Sends message to the movie.
*/
function sendMessage(message){
  //message = null;
  //var message = new Object();
  //message.type = "loadPage";
  for(prop in message){
     dojo.debug(prop + ": " + message[prop]);
   }
  //dojo.debug("sendMessage called");
  movie.sendMovieMessage(message);
  dojo.debug("movie.sendMovieMessage was called");
  //dojo.debug("sendMessage called");
}

/**
Recieves messages from the movie
Called by the movie to send messages to the javascript
*/
function movieMessage (message) {
  /** 
  The default message object:
  message contains the following fields:
  message.type    :String
  message.target  :Object
  */
  dojo.debug("movieMessage: message recieved: " + message.type);
  
  switch(message.type){
   /*case "XMLLoaded":
      handleXMLLoaded(message);
      return(true);
      break;*/
   case "MovieReady":
      handleMovieReady(message);
      return(true);
      break;
   case "PageLoaded":
      handlePageLoaded(message);
      return(true);
      break;
   case "GET":
      /**
      The GET message object:
      message.type  :String // in this case GET
      message.title :String
      message.guid  :String
      message.item  :String
      */
      handleGet (message);
      return(true);
     // break;
		case "POST":
			handlePost(message);
      return(true);
			break;
		case "SendEmail":
//			alert(message.target.guid);
		 // document.location.href="MailTo:" + getEmailAddress(message.target.guid);
 			 getEmailAddress(message.target.guid);
		  break;
    default:
      return(false);
      dojo.debug("movieMessage: A message object of type: " + message.type + " was received, but there is no handler for it.");
  } 
  
}

function getEmailAddress(mailto){
	var maildata = "";
	dojo.io.bind({
	    url: "post_message.asp?mode=getemail&mailto=" + mailto,
	    load: function(type, data, evt){ 
				dojo.debug(data); 
				maildata = data.email + "?subject=" + data.subject + "&body=" + data.body;
				document.location.href="mailto:" + maildata;
	//			alert(maildata);
			},
			error: function(type, error){ dojo.debug(error); },
			method: "GET",
	    mimetype: "text/json"
	});
	return maildata;
}

function handleMovieReady(message){
	dojo.debug("handleMovieReady: called");
	var destination = window.location.hash;
	if(window.location.search && window.location.search != ""){
		destination = window.location.search
		destination.replace("?","#");
	}
//	alert(window.location.search);
	getPage(destination);
}

function handlePageLoaded(message){
  dojo.debug("###################################");
 	
  for(prop in message.target){
     dojo.debug(prop + ": " + message.target[prop]);
   }

}

/*
function handleXMLLoaded(message){

	dojo.debug("handleXMLLoaded: called");

}
*/
/*
 function handleGet (message){


       The GET message object:
       message.type  :String // in this case GET
       message.title :String
       message.guid  :String
       message.item  :String
       
 
   for(prop in message){
     dojo.debug(prop + ": " + message[prop]);
   }
 
 }*/

function findChild (element, nodeName)
{
	var child;
	
	for (child = element.firstChild; child != null; child = child.nextSibling)
	{
		if (child.nodeName == nodeName)
			return child;
	}
	
	return null;
}

function handleGet(message){
	dojo.debug("handleGet: called " + message.type);

	message.type = "loadPage";
	//dojo.debug("message.type:" + message.type);
//	alert("section:" + message.section + "\nguid:" + message.guid);

	message.section = message.section.replace('#','');
	message.section = message.section.replace('?','');
	//handle the default page without a guid

	if( typeof message.guid == 'undefined'){
		message.guid = message.section;
	}
	message.url = "handler.asp?guid=" + message.guid + "&sectionid=" + message.section;
	var viewState = new ViewState(message.guid,message.section);
	dojo.undo.browser.addToHistory(viewState);
	dojo.debug("pre sent message:"+ message.section + "/" + message.guid);			
	sendMessage(message);
	dojo.debug("sent message:"+ message.section + "/" + message.guid);			
}

function handlePost(message){
	dojo.debug("handlePost: Called");
	var messageBody = "";
	var validationMsg = "";
	var isValid = true;
	
	// always check name, email
	if(message.user_name == ""){
		isValid = false;
		validationMsg += "Please enter your name.\n"				
	}
	if(message.user_email == ""){
		isValid = false;
		validationMsg += "Please enter an email address.\n"
	}
	
	// main validation selector for custom form fields
	switch(message.guid){
		case "haveYouGot":		
			if(message.user_background == ""){
				isValid = false;
				validationMsg += "Please tell us about your background.\n"
			}	
			dojo.byId("messageSubject").value = "Have you Got What it takes?";
			break;

		case "whoDoYouKnow":
			if(message.talent_name == ""){
				isValid = false;
				validationMsg += "Please enter the name of the talent.\n"				
			}
			if(message.talent_email == ""){
				isValid = false;
				validationMsg += "Please enter an email address for the talent.\n"
			}	
			dojo.byId("messageSubject").value = "Who Do You Know";
			break;
	
		default:			
			break;
	}
	
	//dojo.debug("done validation");
	if(!isValid){
		alert(validationMsg);
		return;
	}
	
	
	for(prop in message){

    if(prop == "guid"){
			dojo.byId("messageType").value = message[prop];			
		}else{
			if(prop != "type"){
				messageBody += prop + ":     " + message[prop] + "\n";
			}
		}
		dojo.debug(prop + ": " + message[prop]);
	}
	dojo.byId("messageBody").value = messageBody;
//	dojo.byId("messageType").value = message.guid;
//	dojo.debug(dojo.byId("messageBody").value);
	
	dojo.io.bind({
	    url: "post_message.asp",
	    load: function(type, data, evt){ 
				dojo.debug(data); 
				message.type = "GET";
				message.guid = "THANKS";
				handleGet(message); 
			},
			error: function(type, error){ dojo.debug(error); },
			method: "POST",
			formNode:dojo.byId("messageForm"),
	    mimetype: "text/plain"
	});

	
	
}


//-->
