// Change History
//------------------------------------------------------------------------------------------
// Date: 	                09th Dec 2008
// Who:      	Gerry
// Change: 	Added  ClearQASValidationError() function
// Reason:	                Part of QAS Donation Address validation functionality
//-------------------------------------------------------------------------------------------

window.onload = function init()
{
    //alert('Javascript found!');

    prepFooeter();	
    linkBehaviour();
	enableForms();
	findSwitch();
	//postcodeCorrect();
	//manualAddressFormat();
	jsShowLookup();

                // ColinM 2007.03.07 : Call RR function accordingly as requested
	// Commented out next 2 lines - Gerry 09 Dec 08, to make the same as the live misc.js file
                //if(typeof tryFlash =='function')          
                //    tryFlash();
}

// Does what it says! the second two arguements narrow the search.
AISgetElementsByClass = function (searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// clear the server generated qas validation error
function ClearQASValidationError()
{
  var qasValidationMessage = document.getElementById('DonateQASValidation');
	if (qasValidationMessage != null) qasValidationMessage.style.visibility='hidden'; 
}

// convert postcodes to uppercase 
//###############################
function postcodeCorrect(){

    
    
	var inputs = AISgetElementsByClass('changeCase');

	if(!inputs) return false;

	 for (i = 0; i < inputs.length; i++)
	 {
	
		input = inputs[i];

		input.onkeyup = function changeString(event) { 
		    
		            evt = event || window.event;
        	                    
                    switch (evt.keyCode) 
                    {
		                // left arrow key
		                case 37: 
		                    break;
		                // right arrow key
		                case 39: 
		                    break;
		                // up and down arrow keys
		                case 38: 
		                    break;
		                default: 
		                    this.value = this.value.toUpperCase(); 
		                    break;
		            }

		    
		    
		}

	  }

}




// P R I N T   P A G E 
//####################

    //Instructions: Give the "print this page" link an id of "printMe".

/* #3134 print function changed to write in "print this page" link dynamically for accessibility reasons. printFunc() seperated into own function. Ed.  */

function prepFooeter()
{
    var footer = document.getElementById('footer');
    if(!footer || footer.className == 'footerSmall') return false

    var printLink = document.createElement('a');
    printLink.id = 'printMe';
    printLink.href = '#printThisPage';
    printLink.onclick = printFunc;
    printLink.innerHTML = 'Print this page';

    footer.insertBefore(printLink, footer.firstChild);
    
    var emailBtn = document.getElementById("email");
    
    if (emailBtn) emailBtn.style.display = "inline";
}


function printFunc()        	
{
    if (window.print)
    {
		var xmlhttp = null;
		var strTridionURI  = document.getElementById('strTridionURI');
		var strSiteSection = document.getElementById('strSiteSection');
		
		if (window.XMLHttpRequest && strSiteSection && strTridionURI)/* #3134 extra checks for required HTML elements. Ed. */
		{
		xmlhttp=new XMLHttpRequest()
		}
		else if (window.ActiveXObject)
		{
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		}
		if (xmlhttp!=null)
		{
			xmlhttp.open("GET","http://" + location.hostname + "/printpage/printed.ashx?Tridion_URI=" + strTridionURI.value + "&SiteSection=" + strSiteSection.value,true)
			xmlhttp.send(null)
		}
		window.print();
		return false;
    }
    else alert("Your browser does not support this functionality. Please use your browser's standard print dialogue.");
    return false
}



// L I N K   B E H A V I O U R S
//##############################

    //Instructions: Give links the attribute 'rel="external"' to open a link in a new window, or 'rel="popup"' to open link as popup. If the browser doesn't have javascript or is unsupported then the links function as normal links.

function linkBehaviour() {

	//Link Title setup
	//These strings added to the links titles to alert the user to the link behviour. This is especially important for popups that may be blocked. 	
	var newTitleText = " (Opens in a new window)";
	var popupTitleText = " (Opens in a popup)";

    //Popup setup:
    var target	= "_popup";		//Name of new window; different targets mean different windows.
    var width	= "780px";		//Don't forget units 
    var height	= "550px";		//Don't forget units
    var top		= "10px";		//Position on page (can this take %?)
    var left	= "10px";		//Position on page (can this take %?)
    var scroll	= "yes";		//(boolean) Best to allow scrolling in case users see content differently than you.
    var tools	= "no"; 		//(boolean) Browser toolbar? usually not for a popup.
    var resize	= "yes";		//(boolean) Resizable? why wouldn't you let someone resize the popup? This has serious accessibility issuses if fixed.
    
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");

	 for (var i=0; i<anchors.length; i++) 
	 {
	    var anchor = anchors[i];	   
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") 
	    {
		    anchor.target = "_blank";
			//anchor.title  = anchor.title + newTitleText; 
	    }			
	   if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "popup") 
	    {  
	       anchor.onclick = popup;
	    }	   
	 }
	 
    function popup()
    {
        window.open(''+this.getAttribute("href"),target,'left='+left+',top='+top+',width='+width+',height='+height+',scrollbars='+scroll+',toolbar='+tools+',resizable='+resize+''); 
        return false;
    }
}




	
	
	

// E N A B L E   F O R M S 
//########################

// Enables forms that have been disabled for non-js-enabled user agents.
function enableForms(){

//alert('happyForms!');

    if (!document.getElementsByTagName) return;

    var inputs = document.getElementsByTagName("input");

    for (var i=0; i<inputs.length; i++) 
	 {
	    var input = inputs[i];
                                                                                                                    
		if(input.getAttribute("disabled")){
		
		//alert('disabled');
		input.disabled = false;

		}

	 }			
}


// T O G G L E   V I S I B I L I T Y
//##################################


function findSwitch(){
	var Switch	= document.getElementById('lnkChangePassword');
	var box		= document.getElementById('changepassword');
	
	if(!Switch || !box) return false; // Check the page has the correct elements.

	Switch.onclick = function(){ 
								showHide(box.id,'block'); 
							   }
	
	box.style.display = "none"; // set initial state to closed.
}

function showHide(id,display){
	var box = document.getElementById(id);
	box.style.display = (box.style.display != 'none') ? 'none' : display;
	return false;
}


// SHOW HIDE "ENTER ADDRESS MANUALLY"
//*******************************************

//runs on page load
function manualAddressFormat(){
var form = document.getElementById("manualAddress");

	if(!form) return false;

	var toggle = document.getElementById("manualAddressSwitch");
	
	var test = false;



	toggle.onclick = function()
		{
		
		if(form.style.display == "none")
			form.style.display = "";
		else
			form.style.display = "none";
		
				 return false;
		}

	

	initManualAddress(test);
}

//runs onclick
function initManualAddress(test){


	
	var form = document.getElementById("manualAddress");

	var fields = form.getElementsByTagName("input");

	

	for(var i=0; i<fields.length; i++)
	{
		var field = fields[i];
		if(field.getAttribute("type") == "text" && field.value != "")
		{
			form.style.display = "";
			test = true;
			break;
		}
	}

	if(test == false)
	{

		form.style.display = "none";

	}

}

// 2007.03.08 ColinM / EdwardE : Function to cater for display of lookupAddress functionality
// Notes: Additional stylesheet has been added to accomdate show of lookupaddress panel, this is a workaround
// for inconsistency across browser platforms.
function jsShowLookup()
{
    var cssStr = ".cssHideJSshow{display:block;}";
    var style  = document.createElement("style");
    style.type = "text/css";
    
    if(style.styleSheet) style.styleSheet.cssText = cssStr;
    else style.appendChild(document.createTextNode(cssStr));
    document.getElementsByTagName("head")[0].appendChild(style);
}