/* Dialogue JavaScript Document - for Dialogue Box interaction */
/* Created by Jonathan Pratchios - Property of QAS */

/*============================================================*\
Default Dialogue Messages
\*============================================================*/
	
msg_empty     = '&nbsp;'	
			
msg_unknown   = 'alert'   +   ' | ' 									+
				'<h1 class="icon-exclaim">Alert!</h1>'					+
				'<h2>Action Canceled</h2>'								+
				'<p class="p1">We were unable to process your request. '+
				'Please try again or cancel.</p>'									+
				'<ul>'													+
				'<li><a class="button1" href="javascript:killDialogue();">'							+
				'<span class="icon-no">Cancel</span></a></li>'										+
				'<li><a class="button2" id="activeButton" href="javascript:submitDialogue();">'		+
				'<span class="icon-yes">Try Again</span></a></li>'									+
				'</ul>'													+
				'<sub></sub>'			
				
msg_loading   = 'alert'   +   ' | ' 									+
				'<h1 class="icon-loading" style="left:67px;">Loading...</h1>'				+
				'<h2 class="center" style="margin-left:-67px;">Processing Request</h2>'	
				
msg_passed	  = msg_unknown;


/*============================================================*\
================================================================
                       Global Functions
================================================================
\*============================================================*/

function animateTest(){
/***************************************************************
Detail 		:	Tests for animiation eligability 
Arguments	:	none
***************************************************************/
	return false
	switch(_clientInfo.cpu){
		case "slow":
			return false
		case "fast":
			return true
	}
}


function newDialogue(msg,animate,fade){ 
/***************************************************************
Detail 		:	Reveals Dialogue Box 
Arguments	:	msg = message code	
				animate = a boolean declaring whether the fuction should animate or not
				fade = a boolean declaring whether the fuction should fadeout the backdrop or not - defaults to true
***************************************************************/
	if(animate == null){animate = animateTest()} 	// Test for animation (false if slow)	
	if(fade == null){fade = animateTest()} 			// Test for fade animation (false if slow)
	//if(getElement("flash")){getElement("flash").style.visibility = 'hidden';}
	if(getElement("fadeout").className == 'active'){
		killDialogue(animate,false,msg);
		return false;
	}
	
	try {msg = eval(msg)}  					// Assign error code information to variable
	catch (ex){msg = eval("msg_unknown")}	// If not a valid code, default to unknown error	


	msg = msg.split("|") 
	getElement("message").innerHTML = msg[1];
		
											// SUB: Reveal Elements
	var cleanup_1 = function(){
		getElement("dialoguebox").className = msg[0];
		getElement("blockout").className = 'active'
		
		if(animate){
			anim_2.onComplete.subscribe(cleanup_2); 
			anim_2.animate()
		}else{
			cleanup_2()
		}
		anim_1.onComplete.unsubscribe()
	}
	
	var cleanup_2 = function(){
		getElement("fadeout").className = 'active'
		getElement("message").className = 'active'
		if(getElement("activeButton")){  		// removes focus from referring link
			getElement("activeButton").focus(); 
			if (_clientInfo.browser == 'IE'){getElement("activeButton").blur()} 
		} 
		
		anim_2.onComplete.unsubscribe()
	}	
											// SUB: Animate Elements
	var anim_1 = new YAHOO.util.Anim('fadeout', { opacity: { from: .995, to: .05 } }, .33, YAHOO.util.Easing.easeIn);
	var anim_2 = new YAHOO.util.Anim('dialoguebox', { top: { from: 80, to: 127 } }, .25, YAHOO.util.Easing.backBoth);
	
	if(fade){	
		anim_1.onComplete.subscribe(cleanup_1);
		anim_1.animate()
	}else{
		cleanup_1()
	}

}


function killDialogue(animate,fade,swap){
/***************************************************************
Detail 		:	Hides dialogue box and resets its contents
Arguments	:	animate = a boolean declaring whether the fuction should animate or not - defaults to true
				fade = a boolean declaring whether the fuction should fadeout the backdrop or not - defaults to true
				swap = NOT YET IMPLIMENTED - will allow for swapping to a new dialogue
***************************************************************/
	if(animate == null){animate = animateTest()} 		// Test for animation (false if slow)	
	if(fade == null){fade = animateTest()} 				// Test for fade animation (false if slow)
	
	x = YAHOO.util.Dom.getRegion("message");
	msg_height = x.bottom - x.top - 24;		// Calculate the height of the message element for IE
	YAHOO.util.Dom.setStyle('message', 'height', msg_height); 
	getElement("message").style.height = msg_height + 'px';
	getElement("message").innerHTML = msg_empty;
	
	//if(_clientInfo.browser == 'Internet Explorer'){getElement("message").innerHTML = msg_empty;}	// Accomodates IE
	
	var anim_1 = new YAHOO.util.Anim('dialoguebox', {top: { from: 127, to: 0 }}, .25, YAHOO.util.Easing.backIn);
	var anim_1_1 = new YAHOO.util.Anim('message', { height: { from: msg_height, to: 50 }}, .25, YAHOO.util.Easing.easeOut);
	var anim_2 = new YAHOO.util.Anim('fadeout', { opacity: { to: .995 }}, .33, YAHOO.util.Easing.easeOut);

	
	var cleanup_1 = function(){
		
		if(fade){
			anim_2.onComplete.subscribe(cleanup_2); 
			anim_2.animate()
		}else{
			anim_1.onComplete.unsubscribe()	
			cleanup_2();	
		}
	}
	
	var cleanup_2 = function(){
		getElement("message").style.height = "auto"
		getElement("message").innerHTML = msg_empty;
		//getElement("message").className = ""		
		getElement("dialoguebox").className = ""
		getElement("blockout").className = ""
		getElement("fadeout").className = ""
		
		anim_2.onComplete.unsubscribe()
		if(swap){ newDialogue(swap,animate,false) 
		};
		
	}
	
	if(animate){	
		anim_1.onComplete.subscribe(function(){anim_1_1.animate()});	
		anim_1_1.onComplete.subscribe(cleanup_1);
		anim_1.animate()
	}else{
		cleanup_1()		
	}
	
}

var submitURL

function submitDialogue(){
/***************************************************************
Detail 		:	Submits the Form within the Dialogue box after first running form verification 
***************************************************************/
	var ajax_form = frames['ajaxFrame'].document.forms[0]
	var dialogue_form = document.forms[ajax_form.id]
	
	if (dialogue_form){ // Original Submit
		error = formVerification(dialogue_form)
		if (error){
			x = getElement("formError")
			x.parentNode.removeChild(x)
			var label = error.field.parentNode
			error.field.parentNode.innerHTML += '<p class="p1" id="formError">' + error.message + '</p>'
			getElement('formError').style.display = "block"
			var field = label.getElementsByTagName("input")[0];
			field.value = error.val;
			field.focus();
			return
		} else {
		// copy values from filled form (in dialogue) into a stored variable
		var form_action = dialogue_form.action
		var submit_vars = "?"
		
		// copy action of filled form (for resubmit)
		for(i=0; i < dialogue_form.length; i++){
			// propogate those values into original form (in ajaxFrame) 
			ajax_form.elements[i].value = dialogue_form.elements[i].value
			submit_vars += dialogue_form.elements[i].name + '=' + escape(dialogue_form.elements[i].value);
			if (i < dialogue_form.length - 1){submit_vars += '&'}
		}
		tempStr = String(dialogue_form.action)
		submit_URL = 'dialogue/' + tempStr.slice(tempStr.lastIndexOf("/")+1,tempStr.length) + submit_vars;
		
		// display loading screen to provide feedback to user
		
		//frames['ajaxFrame'].document.forms['ajax_form'].submit()
		
		
		killDialogue(animateTest(), false, 'msg_loading');
		window.setTimeout("ajaxLoader(submit_URL);", "1250");	// submit original form
		timeout = window.setTimeout("killDialogue(true, false, 'msg_unknown')", "10000");
		}
	}else{ // Re-Submit
		// submit form through a GET ajaxLoader()
		killDialogue(animateTest(), false, 'msg_loading');
		window.setTimeout("ajaxLoader(submit_URL);", "1250");	// submit original form
		timeout = window.setTimeout("killDialogue(true, false, 'msg_unknown')", "15000");
	}
		return
}


/*=============================================*\
SCRAP CODE FOR BUILDING
\*=============================================*/


function test(){



}
