//Main Helper Function Set //
//********************************************************************************************************************************
//DYNAMIC VARIABLES ARE LOADED BY THE _HTMLLAYOUT.XSL PRIOR TO THIS FILE BEING INCLUDED
//********************************************************************************************************************************
//********************************************************************************************************************************


var vFormID, vPos = 0;
var vLoading = '<div class="preloader"><img src="' + gImagesRoot + 'ajax-loader-2.gif" alt="' + gLoading + '" /><br />' + gLoading + '</div>';

//********************************************************************************************************************************
//SCRIPTACULOUS AJAX HELPERS
//********************************************************************************************************************************
function Request(vPage, vDiv, vFade){
	try{
		if(vPage != ''){
			$(vDiv).innerHTML = vLoading;
			if(vFade!=false){
				new Effect.Opacity(vDiv, {duration:2, from:0.2, to:1});
			}
			vOpt = {
				method:'post',
				asynchronous:true, 
				evalScripts:true
			}
			vReq = new Ajax.Updater(vDiv, vPage, vOpt);
		}
	}catch(e){
		//keep going!!
	}
}

function FormPost(vPage, vDiv, vForm){
	try{
		new Ajax.Updater(vDiv, vPage, {asynchronous:true, evalScripts:true,parameters:Form.serialize(vForm)}); 
		//window.setTimeout('Effect.BlindUp(\''+vDiv+'\', {duration:.7});',5000);
		//window.setTimeout('$(\''+vDiv+'\').innerHTML = \'\';',5700);
		//window.setTimeout('Effect.Appear(\''+vDiv+'\', {duration:.7});',5800);
	}catch(e){
		alert("Form post error: " + e);
	}
	return false;
}


function ajaxError(vDiv){
	$(vDiv).innerHTML = "An error occurred";
}


function openWindow(vWindowID, vHTML, vClass, vTitle, vWidth, vHeight){
	win = new Window(vWindowID, {className: vClass, title: vTitle, width:vWidth, height:vWidth}); 
	win.getContent().innerHTML = vHTML; 
	win.setDestroyOnClose(); 
	win.showCenter(); 
	return(false);
}




//********************************************************************************************************************************
//YAHOO YUI HELPER FUNCTIONS
//********************************************************************************************************************************
function getElement(vDiv){
	 return YAHOO.util.Dom.get(vDiv);
};

function logError(vError,vType){
	YAHOO.log(vError,vType);	
};

function viewLog(){
	return new YAHOO.widget.LogReader();
};

function XMLHTTP(vURL, vMode, vDiv){
	div = getElement(vDiv);
	switch(vMode){
		case 1 : 	vMode = "GET";
					break;
		case 2 :	vMode = "POST";
					break;
		default :	vMode = "GET";
					break;
	} 
	return YAHOO.util.Connect.asyncRequest(vMode, vURL, callback); 
};

var handleSuccess = function(o){ 
	if(o.responseText !== undefined){ 
	if(div.innerHTML == vLoading){
		div.innerHTML = o.responseText; 
	}else{
		div.innerHTML += o.responseText;
	} }
}; 
 
var handleFailure = function(o){ 
	div.innerHTML += "<li>Error loading page: " + o.responseText + "</li>"
	logError("Error " + o.status + ": " + o.statusText,"warn");
}; 
	 
var callback = { 
	success: handleSuccess, 
	failure: handleFailure, 
	timeout: 10000 
}; 



//********************************************************************************************************************************
//APPLICATION HELPERS AND FUNCTIONS
//********************************************************************************************************************************
function init(){
	var vDiv = $('noscript');
	vDiv.style.display = "none";
}
function checkIE7(){
	if(navigator.appVersion.match(/\bMSIE 7\b/)){
		$('ie7flash').innerHTML='<a href="http://www.getfirefox.com" title="Get Firefox" target="_blank"><img src="'+gImagesRoot+'ie7.png" alt="Internet Explorer 7.x Version Coming Soon. Get Firefox 2.x for a better, safer browsing experience" /></a>';
	}
}


function openWindow(vURL){
	try{
		window.open("leavingSite.asp?url="+vURL,null,"height=500,width=764,status=yes,toolbar=yes,menubar=yes,location=yes,resizable=yes");
	}catch(e){
		alert(e);
	}
}

function AJAXPost(vDiv, vURL){
	try{
		new Ajax.Updater(container, url, options);	
	}catch(e){
		
	}
		
}

function toggle_form_and_submit(vDiv,vPage,vErrorMsg){
	try{
		if(vPos == 0){
			Effect.toggle(vDiv,'blind'); 
			vPos = 1
		}else{
			if($F(vDiv+'_text') != ''){
				FormPost(vPage, vDiv+'_response', vDiv+'_form')
				window.setTimeout('Effect.toggle(\''+vDiv+'\',\'blind\');$(\''+vDiv+'_text\').value = \'\';',2000); 
				vPos = 0
			}else{
				alert(vErrorMsg);
			}
		}
		return false;	
	}catch(e){
		//alert(e);	
	}
	
}

function showMessage(vDiv,vMsg,vThis,vField,vValue){
	try{
		
		mydiv = $(vDiv);
		mydiv.innerHTML = "&quot;" + vMsg + "&quot;";
			
		on_elements = document.getElementsByClassName("selected_message");
		for(i in on_elements){
			on_elements[i].className="";
		}
		
		vThis.className = "selected_message";
		$(vField).value = vValue;
		//alert($F(vField));
	}catch(e){
		alert(e);
	}
}

function opacity(vDiv,vAmount){
	try{
		new Effect.Opacity(vDiv, {duration:0.2, from:0, to:vAmount});
	}catch(e){
		//error
	}
}

function submitform(vFld){
	if(vFld.value!=''){
		vFld.form.submit();
	}
}

function changeWish(vFld,vFld2,vID){
	vWishSuggestionValue = $F(vFld);
	vWishFldValue = $F(vFld2);
	
	try{
		if(vWishFldValue == ''){
			$(vFld2).value = vWishSuggestionValue
		}else{
			//something already in the box - prompt to overwrite
			vAnswer = confirm('You\'ve already started entering a wish - do you want to overwrite it?')
			if(vAnswer==true){
				$(vFld2).value = vWishSuggestionValue
			}
		}
	}catch(e){
		alert(e);	
	}
}

function ShowPopUpText(vDiv,vPopupID,vClass){
	try{
		vRequest = Request(gWidgetRoot + 'Popup.asp?popupid='+vPopupID+'&class='+vClass, vDiv,false);
		$(vDiv).className = 'roundbox_'+vClass;
	}catch(e){
		//error occurred
	}
}

function getUnitFlds(vFld, vDiv){
	try{
		var vSelected = vFld.options[vFld.selectedIndex].value;
		var vDiv = $(vDiv);
		vDiv.innerHTML = vLoading;
		var vURL = gWidgetRoot + 'QuestionnaireGetUnitsForm.asp?units=' + vSelected + '&fldID=' + vFld.name
		var result = Request(vURL,vDiv,false);
	}catch(e){
		//alert('Specified object does not exist');
	}
}

function ShowHideDiv(vDiv, vType, vDisplay){
	try{
		if($(vDiv)){
			vDiv = $(vDiv)
			
			switch(vType){
				case 1:		
						switch(vDisplay){
						case 1:	vDiv.style.display = "none";
								break;
						case 0:	vDiv.style.display = "";
								break;
						};
				default:
						if(vDiv.style.display == "none"){
							vDiv.style.display  = "";
						}else{
							vDiv.style.display  = "none";
						};
			}
		}else{
			//alert("specified object does not exist" + vDiv);
		}
	}catch(e){
		//do nothing
	}
}

function getSelectedIndex(vFld){
	if(document.getElementById(vFld)){
		vFld = document.getElementById(vFld);
		vValue = vFld.options[vFld.selectedIndex].value;
		return(vValue);
	}else{
		alert("Specified field does not exist - " + vFld);
	}
}

function ControlWishType(){
	vType = getSelectedIndex("WishTypeID");
	switch(vType){
		case "1":	ShowHideDiv("sh_01",1,0);
					break;
		case "2":	ShowHideDiv("sh_01",1,1);
					break;
	}
}


function ffForm(){
	// Hide forms
	$(vFormID).hide().end();
	
	// Processing
	$(vFormID).find('li/label').not('.nocmx').each(function(i){
	var labelContent = this.innerHTML;
	var labelWidth = document.defaultView.getComputedStyle(this,'').getPropertyValue('width');
	var labelSpan = document.createElement('span');
	labelSpan.style.display = 'block';
	labelSpan.style.width = labelWidth;
	labelSpan.innerHTML = labelContent;
	this.style.display = '-moz-inline-box';
	this.innerHTML = null;
	this.appendChild( labelSpan );
	}).end();
	
	// Show forms
	$(vFormID).show().end();
}

function fixFormLayout(vForm){
	vFormID = vForm;
	if(document.addEventListener){document.addEventListener('DOMContentLoaded', ffForm, false);}
}