var b1Open = false;
var b2Open = false;
var b3Open = false;
var b4Open = false;
var b5Open = false;
var b6Open = false;
var b7Open = false;

function ToggleSwitch(pID)
{
	ToggleSideNav(pID,true)
}

function ToggleSideNav(pID,pAnimate)
{
	switch(pID)
	{
		case "slidedown_1":
			if (b1Open)
			{
				if (pAnimate) Effect.SlideUp(pID,{ duration: 0.5 });
				else Effect.Fade(pID,{ duration: 0 });
				b1Open = false;
				document.getElementById(pID.replace(/slidedown/,"title")).className = '';
			}
			else
			{
				if (pAnimate) Effect.SlideDown(pID,{ duration: 0.5 }); 
				else Effect.Appear(pID,{ duration: 0 });
				b1Open = true;
				document.getElementById(pID.replace(/slidedown/,"title")).className = 'open';
			}
			break;
		
		case "slidedown_2":
			if (b2Open)
			{
				if (pAnimate) Effect.SlideUp(pID,{ duration: 0.5 });
				else Effect.Fade(pID,{ duration: 0 });
				b2Open = false;
				document.getElementById(pID.replace(/slidedown/,"title")).className = '';
			}
			else
			{
				if (pAnimate) Effect.SlideDown(pID,{ duration: 0.5 }); 
				else Effect.Appear(pID,{ duration: 0 });
				b2Open = true;
				document.getElementById(pID.replace(/slidedown/,"title")).className = 'open';
			}
			break;

		case "slidedown_3":
			if (b3Open)
			{
				if (pAnimate) Effect.SlideUp(pID,{ duration: 0.5 });
				else Effect.Fade(pID,{ duration: 0 });
				b3Open = false;
				document.getElementById(pID.replace(/slidedown/,"title")).className = '';
			}
			else
			{
				if (pAnimate) Effect.SlideDown(pID,{ duration: 0.5 }); 
				else Effect.Appear(pID,{ duration: 0 });
				b3Open = true;
				document.getElementById(pID.replace(/slidedown/,"title")).className = 'open';
			}
			break;
		case "slidedown_4":
			if (b4Open)
			{
				if (pAnimate) Effect.SlideUp(pID,{ duration: 0.5 });
				else Effect.Fade(pID,{ duration: 0 });
				b4Open = false;
				document.getElementById(pID.replace(/slidedown/,"title")).className = '';
			}
			else
			{
				if (pAnimate) Effect.SlideDown(pID,{ duration: 0.5 }); 
				else Effect.Appear(pID,{ duration: 0 });
				b4Open = true;
				document.getElementById(pID.replace(/slidedown/,"title")).className = 'open';
			}
			break;
		
		case "slidedown_5":
			if (b5Open)
			{
				if (pAnimate) Effect.SlideUp(pID,{ duration: 0.5 });
				else Effect.Fade(pID,{ duration: 0 });
				b5Open = false;
				document.getElementById(pID.replace(/slidedown/,"title")).className = '';
			}
			else
			{
				if (pAnimate) Effect.SlideDown(pID,{ duration: 0.5 }); 
				else Effect.Appear(pID,{ duration: 0 });
				b5Open = true;
				document.getElementById(pID.replace(/slidedown/,"title")).className = 'open';
			}
			break;

		case "slidedown_6":
			if (b6Open)
			{
				if (pAnimate) Effect.SlideUp(pID,{ duration: 0.5 });
				else Effect.Fade(pID,{ duration: 0 });
				b6Open = false;
				document.getElementById(pID.replace(/slidedown/,"title")).className = '';
			}
			else
			{
				if (pAnimate) Effect.SlideDown(pID,{ duration: 0.5 }); 
				else Effect.Appear(pID,{ duration: 0 });
				b6Open = true;
				document.getElementById(pID.replace(/slidedown/,"title")).className = 'open';
			}
			break;
		case "slidedown_7":
			if (b7Open)
			{
				if (pAnimate) Effect.SlideUp(pID,{ duration: 0.5 });
				else Effect.Fade(pID,{ duration: 0 });
				b7Open = false;
				document.getElementById(pID.replace(/slidedown/,"title")).className = '';
			}
			else
			{
				if (pAnimate) Effect.SlideDown(pID,{ duration: 0.5 }); 
				else Effect.Appear(pID,{ duration: 0 });
				b7Open = true;
				document.getElementById(pID.replace(/slidedown/,"title")).className = 'open';
			}
			break;		
	}
}

function SwapIMG(imageName, image, imagePath)
{
	if (image.src == '')
	{
		image.src = imagePath;
	}
	document.images[imageName].src = image.src;
}

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 winPopFull(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=1,"
    + "menubar=1,"
    + "resizable=" + control + ","
    + "scrollbars="+scroll+", "
    + "status=1,"
    + "titlebar=1,"
    + "toolbar=1,"
    + "hotkeys=1,"
    + "screenx=" + xposition + ","  //NN Only
    + "screeny=" + yposition + ","  //NN Only
    + "left=" + xposition + ","     //IE Only
    + "top=" + yposition;           //IE Only

    window.open(pageToLoad,winName,args );
}

function NumberOfChar(str,char)
{
	var ii, charCount;
	charCount = 0;
	for (ii=0; ii < str.length;ii++)
	{
		var cc = str.charAt(ii);
		if (cc == char)
		{
			charCount = charCount + 1;
		}
	}
	return charCount;
}

function validEmail(str)
{
	var bool;
	bool = true;
	if (str.length < 6)
	{
		bool = false;
		alert("Email address has too few characters.");
	}
	if ((bool) && ((NumberOfChar(str,'@')==0)||(NumberOfChar(str,'@')>1)))
	{
		bool = false;
		alert("Email address contains the wrong number of @'s.");
	}
	if ((bool) && (NumberOfChar(str,'.')==0))
	{
		bool = false;
		alert("Email address must contain at least one '.'.");
	}
	if ((bool) && (NumberOfChar(str,' ')!=0))
	{
		bool = false;
		alert("Email address must not contain any spaces.");
	}
	return bool;
}

function validateRecommendForm(pForm)
{
	var bValid = true;

	if ((bValid) && (pForm.yourname.value == ''))
	{
		alert('You must enter your Name so your friend knows who has recommended us to them!');
		bValid = false;
	}

	if ((bValid) && (pForm.name.value == ''))
	{
		alert('You must enter your Friend`s Name we know who to address the email too!');
		bValid = false;
	}

	if ((bValid) && (pForm.email.value == ''))
	{
		alert('You must enter your Friend`s Email Address so we can email our website link to them!');
		bValid = false;
	}
	else if (bValid)
	{
		bValid = validEmail(pForm.email.value);
	}	
	
	return bValid;		
}

function validateSavingsForm(pForm)
{
	var bValid = true;

	if ((bValid) && (pForm.propertyvalue.value == ''))
	{
		alert('You must enter the value of your property so we can calculate your savings!');
		bValid = false;
	}

	if ((bValid) && (pForm.commission.value == ''))
	{
		alert('You must enter the high street agents commission so we can calculate your savings!');
		bValid = false;
	}
	
	return bValid;		
}

function validateHipsForm(pForm)
{
	var bValid = true;

	if ((bValid) && (pForm.fullname.value == ''))
	{
		alert('You must enter your Full Name so we know who we are speaking to!');
		bValid = false;
	}

	if ((bValid) && ((pForm.email.value == '') && (pForm.phonenumber.value == '') && (pForm.mobilephonenumber.value == '') && (pForm.address.value == '')))
	{
		alert('You must enter at least one method of contact so we can get hold of you.  We require at least one of the following:\n\nEmail Address\nPhoneNumber\nMobilePhoneNumber\nAddress');
		bValid = false;
	}
	
	if ((bValid) && (pForm.email.value != ''))
	{
		bValid = validEmail(pForm.email.value);
	}	
	
	return bValid;		
}

function validateContactForm(pForm)
{
	var bValid = true;

	if ((bValid) && (pForm.fullname.value == ''))
	{
		alert('You must enter your Full Name so we know who we are speaking to!');
		bValid = false;
	}

	if ((bValid) && ((pForm.email.value == '') && (pForm.phonenumber.value == '') && (pForm.mobilephonenumber.value == '')))
	{
		alert('You must enter at least one method of contact so we can answer your enquiry.  We require at least one of the following:\n\nEmail Address\nPhoneNumber\nMobilePhoneNumber');
		bValid = false;
	}
	
	if ((bValid) && (pForm.email.value != ''))
	{
		bValid = validEmail(pForm.email.value);
	}	

	if ((bValid) && (pForm.enquiry.value == ''))
	{
		alert('You need to enter your enquiry/question!');
		bValid = false;
	}	
	
	return bValid;		
}

function validateQuestionnaireForm(pForm)
{
	var bValid = true;

	if ((bValid) && (pForm.Owner1Name.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.Owner1Email.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.Owner1PhoneNumber.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.PropertyAddress.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.PropertyType.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.PropertyLease.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.PropertyAge.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.PropertyBedrooms.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.PropertyEnSuite.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.PropertyBath.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.PropertyWCs.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.PropertyReceptions.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.PropertyConservatory.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.PropertyAttic.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.PropertyGaraging.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.PropertyHeating.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.PropertyServices.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.PropertyOwned.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.MovingPropertyOnMarket.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.MovingPropertyFound.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.MovingRegisteredBuyer.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		bValid = false;
	}
	if ((bValid) && (pForm.MovingDate.value == ''))
	{
		alert('Please enter all required fields (marked *)');
		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);
    }
}

/*********************************************************/
/****************** Savings Calculator *******************/
/*********************************************************/

function calculateSavings(pValue,pCommission)
{
	setTimeout('calculateSavingsValue(' + pValue + ',' + pCommission + ')',3000);
}

function calculateSavingsValue(pValue,pCommission)
{
	var dAgentCost = (pValue * (pCommission/100)) * 1.175;
	var dGetSetGoCost = ((pValue * 0.0025) + 199) * 1.175;
	var dSaving = (dAgentCost - dGetSetGoCost).toFixed(0);
	var sHTML = '';

	sHTML += '<div class="pricetxt">&pound;' + dAgentCost.toFixed(2).toString() + '<br/>&pound;' + dGetSetGoCost.toFixed(2).toString() + '</div>';
	sHTML += '<p>Your High Street Agents fees:';
	sHTML += '<br/>Get-Set-Go fees:</p>';	

	if (dSaving > 0)
	{
		sHTML += '<div class="largeorangetxt">&pound;' + dSaving.toString() + '</div>';
		sHTML += '<h2>We can save ';
		sHTML += '<br/>you over....</h2>';		
	}
	else
	{
		sHTML += '<h2>WOW!</h2>';		
		sHTML += '<p>It would appear you have found a High Street Agent who is cheaper than we are.  Please check that they are offering you the same amazing service that we will becuase we think we are the best in the business!</p>';	
	}
	
	sHTML += '<p class="getsetgobtn"><br/><a href="sell_register.aspx" title="I want to Get-Set-Go!" onmouseover="SwapIMG(\'getsetgo3\', getsetgo3On, \'images/btn_getsetgo3_on.gif\')" onmouseout="SwapIMG(\'getsetgo3\', getsetgo3Off, \'images/btn_getsetgo3_off.gif\')"><img id="getsetgo3" src="images/btn_getsetgo3_off.gif" alt="I want to Get-Set-Go!"/></a></p>';
	
	document.getElementById('savings-result').innerHTML = sHTML;
}

/*********************************************************/
/******************** Random Quotes ********************/
/*********************************************************/

function OutputQuote()
{
	var iRandom = Math.floor(Math.random() * arrQuotes.length);
	var shtml;
	
	shtml = '<div id="quote">';
	shtml += '    <img src="images/quote_top.gif" alt="">';
	shtml += '    <div id="quote-content">';
	shtml += '        <p>' + arrQuotes[iRandom][0] + '</p>';
	shtml += '    </div>';
	shtml += '    <img src="images/quote_bottom.gif" alt="">';
	shtml += '    <div id="quotee">' + arrQuotes[iRandom][1] + '<br/>' + arrQuotes[iRandom][2] + '</div>';
	shtml += '</div>';
	
	shtml = '<p class="quotetxt">&quot;' + arrQuotes[iRandom] + '&quot;</p>';
	
	document.write(shtml);

}

/*********************************************************/
/****************** Property Of The Week *****************/
/*********************************************************/

var iArticleIndex = 0;
var iArticleCount = 0;
var arrArticles = new Array();
var sNewsHtml = '';
var oNewsDiv;

function OutputNews()
{
	oNewsDiv = document.getElementById('news');
	iArticleCount = arrArticles.length;	
	SetNewsContent();
}

function DisplayNews()
{
	$('news').fade({ duration: 0.5 }); 
	setTimeout("SetNewsContent()", 500);
}

function DisplayNextArticle()
{
	iArticleIndex++;
	if (iArticleIndex == iArticleCount) iArticleIndex = 0;
	DisplayNews();
}

function DisplayPrevArticle()
{
	iArticleIndex--;	
	if (iArticleIndex < 0) iArticleIndex = (iArticleCount - 1);	
	DisplayNews();
}

function SetNewsContent()
{
	var sArticleImage = (arrArticles[iArticleIndex][0] == '') ? 'images/news/small_blank.gif' : arrArticles[iArticleIndex][0];
	sNewsHtml = '<img src="' + sArticleImage + '" alt="' + arrArticles[iArticleIndex][1] + '"/>';
	sNewsHtml += '<a href="javascript:DisplayNextArticle();" title="Next News Story"><img class="nextprev" src="images/btn_next.gif" alt="Next News Story"/></a>';
	sNewsHtml += '<a href="javascript:DisplayPrevArticle();" title="Previous News Story"><img class="nextprev" src="images/btn_previous.gif" alt="Previous News Story"/></a>';
	
	sNewsHtml += '<h1 class="news">latest news</h1>';
	sNewsHtml += '<h2><a href="' + arrArticles[iArticleIndex][4] + '" title="' + arrArticles[iArticleIndex][1] + '">' + arrArticles[iArticleIndex][1] + '</a></h2>';
	sNewsHtml += '<h2>' + arrArticles[iArticleIndex][2] + '</h2>';
	sNewsHtml += '<p>' + arrArticles[iArticleIndex][3] + '</p>';

	oNewsDiv.innerHTML = sNewsHtml;
	$('news').appear({ duration: 0.5 }); 
}
