var arrNews	= new Array();
var xmlHttp;

function winPop(pageToLoad, winName, width, height, center, scroll, control)
{
    xposition=0; yposition=0;
    if ((parseInt(navigator.appVersion) >= 4 ) && (center))
    {
        xposition = (screen.width - width) / 2;
        yposition = (screen.height - height) / 2;
    }
    args = "width=" + width + ","
    + "height=" + height + ","
    + "location=0,"
    + "menubar=0,"
    + "resizable=" + control + ","
    + "scrollbars="+scroll+", "
    + "status=1,"
    + "titlebar=0,"
    + "toolbar=0,"
    + "hotkeys=0,"
    + "screenx=" + xposition + ","  //NN Only
    + "screeny=" + yposition + ","  //NN Only
    + "left=" + xposition + ","     //IE Only
    + "top=" + yposition;           //IE Only

    window.open(pageToLoad,winName,args );
}

function AJAXConnect(pURL, pFunc)
{ 
	xmlHttp = GetXmlHttpObject();
	var sTemp = "";
	if (xmlHttp == null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	
	switch (pFunc)
	{
		case "validateCaptcha":
			xmlHttp.onreadystatechange = stateChangedCaptcha;
			break;
	}
	
	xmlHttp.open("POST", pURL, true);
	xmlHttp.send(null);
}

function stateChangedCaptcha() 
{ 
	if (xmlHttp.readyState == 4)
	{ 
		alert(xmlHttp.responseText);
	}
}

function GetXmlHttpObject()
{
	var xmlHttp = null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
		catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function ValidateEnquiry(pForm)
{
	var bValid = true;
	pForm.challenge.value = Recaptcha.get_challenge();
	pForm.response.value = Recaptcha.get_response();

	if ((bValid) && (pForm.Name.value == ''))
	{
		alert('You must enter your Full Name.');
		bValid = false;
	}

	if ((bValid) && (pForm.Company.value == ''))
	{
		alert('You must enter your Company Name.');
		bValid = false;
	}	
	
	if ((bValid) && (pForm.Telephone.value == ''))
	{
		alert('You must enter your Telephone Number.');
		bValid = false;
	}	
	
	if ((bValid) && (pForm.Email.value == ''))
	{
		alert('You must enter your Email Address.');
		bValid = false;
	}	
	
	if ((bValid) && (pForm.City.value == ''))
	{
		alert('You must enter your City.');
		bValid = false;
	}	
	
	if ((bValid) && (pForm.County.value == ''))
	{
		alert('You must enter your County.');
		bValid = false;
	}		
	
	if ((bValid) && (pForm.Message.value == ''))
	{
		alert('You must enter your Message.');
		bValid = false;
	}

	return bValid;	
}

function ValidatePageForm(pForm)
{
	var bValid = true;

	if ((bValid) && (pForm.PageTitle.value == ''))
	{
		alert('Page Title cannot be left blank.');
		bValid = false;
	}

	if ((bValid) && (pForm.PageContent.value == ''))
	{
		alert('Page Content cannot be left blank.');
		bValid = false;
	}	
	
	return bValid;	
}

function ValidateCSForm(pForm)
{
	var bValid = true;

	if ((bValid) && (pForm.CasestudyTitle.value == ''))
	{
		alert('Casestudy Title cannot be left blank.');
		bValid = false;
	}

	return bValid;	
}

function ValidateExistingCasestudyForm(pForm)
{
	var bValid = true;

	if ((bValid) && (pForm.CasestudyID.value == ''))
	{
		alert('You must select a casestudy to add.');
		bValid = false;
	}

	return bValid;	
}

function Left(pString, pLength)
{
	if (pLength <= 0)
	{
	    return "";
	}
	else if (pLength > String(pString).length)
	{
	    return pString;
	}
	else
	{
	    return String(pString).substring(0,pLength);
	}
}

function Right(pString, pLength)
{
    if (pLength <= 0)
    {
		return "";
	}
    else if (pLength > String(pString).length)
    {
    	return pString;
    }
    else 
    {
       var iStringLength = String(pString).length;
       return String(pString).substring(iStringLength, iStringLength - pLength);
    }
}

/*********************************************************/
/******************** TypeWriter Text ********************/
/*********************************************************/

var iDelay			= 40;
var iCurrentChar	= 1;
var iCurrentStory	= 1;
var bFlickOn		= true;
var iStoryCount		= 0;

function TypeNews()
{
	if ((document.getElementById) && (arrNews.length > 0))
	{
		var sText		= arrNews[iCurrentStory - 1].toString();

		oDiv = document.getElementById('news-content');
		
		if (bFlickOn)
		{
			oDiv.innerHTML = sText.substr(0, iCurrentChar) + '_';
			
			bFlickOn 		= false;
			iCurrentChar++;
		}
		else
		{
			oDiv.innerHTML = oDiv.innerHTML.substr(0, oDiv.innerHTML.length - 1);
			bFlickOn 		= true;
		}

		// if we come to the end of the text
		if (iCurrentChar > sText.length)
		{
			oDiv.innerHTML = oDiv.innerHTML.substr(0, oDiv.innerHTML.length - 1);
			iCurrentChar 	= 1;			
			if (iCurrentStory == iStoryCount)
			{
				iCurrentStory = 1;	
			}
			else
			{
				iCurrentStory++;
			}
			setTimeout("TypeNews()", 3000);
		}
		else
		{
			setTimeout("TypeNews()", iDelay);
		}
	}
}

function OutputNews(pItemCount)
{
	if (pItemCount > arrNews.length)
		iStoryCount = arrNews.length;	
	else
		iStoryCount = pItemCount;
	TypeNews();
}

/*********************************************************/
/********************* Image Fading **********************/
/*********************************************************/

/** SETTINGS **/

var imageSources = [[
    'images/palaces1.jpg',
    'images/palaces2.jpg',
    'images/palaces3.jpg',
    'images/palaces4.jpg'
],[
    'images/public1.jpg',
    'images/public2.jpg',
    'images/public3.jpg',
    'images/public4.jpg'
],[
    'images/private1.jpg',
    'images/private2.jpg',
    'images/private3.jpg',
    'images/private4.jpg'
],[
    'images/social1.jpg',
    'images/social2.jpg',
    'images/social3.jpg',
    'images/social4.jpg'
],[
    'images/special1.jpg',
    'images/special2.jpg',
    'images/special3.jpg',
    'images/special4.jpg'
],[
    'images/decoration1.jpg',
    'images/decoration2.jpg',
    'images/decoration3.jpg',
    'images/decoration4.jpg'
],[
    'images/maintenance1.jpg',
    'images/maintenance2.jpg',
    'images/maintenance3.jpg',
    'images/maintenance4.jpg'
],[
    'images/mande1.jpg',
    'images/mande2.jpg',
    'images/mande3.jpg',
    'images/mande4.jpg'
],[
    'images/conservation1.jpg',
    'images/conservation2.jpg',
    'images/conservation3.jpg',
    'images/conservation4.jpg'
],[
    'images/staff1.jpg',
    'images/staff2.jpg',
    'images/staff3.jpg',
    'images/staff4.jpg',
    'images/staff5.jpg',
    'images/staff6.jpg',
    'images/staff7.jpg'
]];


var change = 0.07;
var frameRate = 40;
var cycle = 16000;
var cycleVariation = 4000;
var baseId = 'fade-image';
var idOffset = 1;

/** END OF SETTINGS **/

function fadeImages()
{
    var starts = [];
    
    for(var b = 0; b < imageSources.length; b++){
        
        var r;
        
        find: while(true){
            r = Math.round(Math.random() * (imageSources.length - 1));
            for(var s = 0; s < starts.length; s++){
                if(starts[s] == r){
                    continue find;
                }
            }
            break;
        }
        
        starts[b] = r;
        startFader(b);
    }
    
    function startFader(b){
    
        var interval;
        var currentImage = 0;
        var opacity = 1;
        var cache = [];
        var stagger = Math.round(cycleVariation * Math.random());
        var wait = (cycle + stagger) / imageSources[b].length;
        
        // preload
        for(var i = 0; i < imageSources[b].length; i++){
            cache[i] = new Image();
            cache[i].src = imageSources[b][i];
        }
        
        var id = baseId + (b + idOffset);
        var block = document.getElementById(id);
        
        block.parentNode.style.backgroundRepeat = 'no-repeat';
        
        advance();
        setTimeout(fade, (wait / imageSources.length) * starts[b]);

        function fadeDown(){
        
            opacity -= change;
            
            if(opacity <= 0){
                opacity = 1;
                
                block.src = cache[currentImage].src;
                
                advance();
                clearInterval(interval);
                setTimeout(fade, wait);
            }
            
            setOpacity();
        }
        
        function advance(){
            if(++currentImage >= cache.length){
                currentImage = 0
            }
            block.parentNode.style.backgroundImage = 'url(' + cache[currentImage].src + ')';
        }
        
        function fade(){
            
            interval = setInterval(fadeDown, frameRate);
        }
        
        function setOpacity(){
            block.style.opacity = opacity; 
            block.style.MozOpacity = opacity; 
            block.style.KhtmlOpacity = opacity; 
            block.style.filter = "alpha(opacity=" + (opacity * 100) + ")"; 
        }
    
    }
    
}