//window.onload = openDiv;
var blnToggle 	= false;
var strImgName 	= "";
var strLinkId 	= "";

function toggle_info(targetid)
{
	
	// Conditional to test if hidden layer has been activated if so close it
	if (blnToggle)
	{
		// set cookie null here
		deleteDivCookie();
		// Set target object for already open layer
		target = document.getElementById(strLinkId);
		
		// Hide it
		target.style.display="none";
		// Set image to off state
		//document.images[strImgName].src = Rollimage[0].src;
		blnToggle = false;
		if (strLinkId != targetid )
		{
			// set cookie
			setDivCookie(targetid);
		// Now set up first or subsequent hit
			target = document.getElementById(targetid);
			target.style.display="block";
			//document.images[name].src = Rollimage[1].src;
			blnToggle 	= true;
			//strImgName 	= name;
			strLinkId 	= targetid;
			//alert("SubStart!!!!");
		}
		//alert("Start!!!!");
	}
	// Conditional that tests whether or not the same link has been opened and closed
	// Above conditional will close an already open link including the same one clicked 
	// opened then closed. This conditional will open a new link so long as its not the same link

	else
	{
		// and here
		setDivCookie(targetid);
	// Now set up first or subsequent hit
		target = document.getElementById(targetid);
		target.style.display="block";
		//document.images[name].src = Rollimage[1].src;
		//document.getElementById(imgID).style.borderWidth = "0px 0px 0px 0px";

		//images[name].styles.borderWidth = '0px';
		//var imgs;
 		//imgs = document.getElementsByTagName(name);
		//imgs[0].style.border = '0px dashed #ccc';
		blnToggle 	= true;
		strImgName 	= name;
		strLinkId 	= targetid;
		//alert("End!!!!");
	}	
}
//
// Cookie functions to keep divs open on page transitions
//
function setDivCookie(myDiv)
{
	//alert("div="+myDiv);
	var expires = new Date();
	// set it to just today
	document.cookie = "DivId="+ myDiv + "; " //expires=" + expires.toGMTString()+"; path=/";
}
function readDivCookie()
{
	var ctr = 0;
	var thisCookie = document.cookie.split('; ');
	ctr = thisCookie.length;
	
	for (var i = 0; i < ctr; i++)
	{
		var cookieName = thisCookie[i].split("=")[0];
		var cookieValue = thisCookie[i].split("=")[1];
		//alert("read div="+cookieValue);
		// onload event calls this function which returns
		// div identity which will be passed to toggle_info()
		if(cookieName == "DivId") 
			return cookieValue;
	}
	return false;	//not what we wanted
}
function deleteDivCookie()
{
	var ctr = 0;
	var thisCookie = document.cookie.split('; ');
	ctr = thisCookie.length;
		
	var expires = new Date();
	expires.setDate(expires.getDate() - 1);
	
	// set it to just today
	document.cookie = "DivId=; expires=" + expires.toGMTString();	
	
}
function openDiv()
{
	//alert("div=shite");
	if (document.cookie != "")
	{
		// there are cookies, check if div id
		var divCookie = readDivCookie()
		if (divCookie != false)
		{
			toggle_info(divCookie);
			//alert("div="+divCookie);
		}
		else
		{
			//alert("div=false");
			return false;
		}
	}
}
//
// Ad Rotator section declare for CAT front page
//
//var ImageFolder = "http://www.diecast4u.com.au/Site_Images/CAT"; //Folder name containing the images
var ImageFolder = "http://localhost/fd/Site_Images/CAT"; //Folder name containing the images

var ImageFileNames = new Array(	'55030.jpg',
								'55036.jpg',
								'55071.jpg',
								'55149.jpg',
								'55159.jpg',
								'55161.jpg',
								'55165.jpg',
								'55175.jpg'); //List of images to use

 //List of hyperlinks associated with the list of images
var ImageURLs = new Array(	'cat/motorgraders.html',
							'cat/compactconstruct.html',
							'cat/lifttrucks.html',
							'cat/backhoeloaders.html',
							'cat/dozer.html',
							'cat/wheelloaders.html',
							'cat/compactors.html',
							'cat/scrapers.html');

var ImageDesc = new Array(	'CAT Motorgraders',
							'CAT Compact',
							'CAT Lift Trucks',
							'CAT Backhoe Loaders',
							'CAT Dozer',
							'CAT Wheel Loaders',
							'CAT Compactors',
							'CAT Scrapers');


var DefaultURL = 'http://localhost/cat/motorgraders.html'; 		//Default hyperlink for the Banner Ad
var DisplayInterval = 5; 						//Number of seconds to wait before the next image is displayed
var TargetFrame = "_blank"; 							//Name of the frame to open the hyperlink into

//Internal variables (do not change these unless you know what you are doing)
var IsValidBrowser = false;
var BannerAdCode = 0;
var BannerAdImages = new Array(NumberOfImages);
var DisplayInterval = DisplayInterval * 1000;
var NumberOfImages = ImageFileNames.length;

//Add a trailing forward slash to the ImageFolder variable if it does not already have one
if (ImageFolder.substr(ImageFolder.length - 1, ImageFolder.length) != "/" && ImageFolder != "") 
{ 
	ImageFolder += "/";
}

if (TargetFrame == '') 
{
	var FramesObject = null;
} 
else 
{
	var FramesObject = eval('parent.' + TargetFrame);
}


function WriteLayer(ID,parentID,sText) 
{
	 if (document.layers) 
	 {
		var oLayer;
	 	if(parentID)
		{
			oLayer = eval('document.' + parentID + '.document.' + ID + '.document');
   		}
		else
		{
			oLayer = document.layers[ID].document;
		}
	oLayer.open();
	oLayer.write(sText);
	oLayer.close();
 	}
 	else if (parseInt(navigator.appVersion)>=5&&navigator.appName=="Netscape") 
 	{
		document.getElementById(ID).innerHTML = sText;
 	}
	else if (document.all) document.all[ID].innerHTML = sText
} 
//Function runs when this page has been loaded and does the following:
//1. Determine the browser name and version  (since the script will only work on Netscape 3+ and Internet Explorer 4+).
//2. Start the timer object that will periodically change the image displayed by the Banner Ad.
//3. Preload the images used by the Banner Ad rotator script
function InitialiseBannerAdRotator() 
{
	var sPath = window.location.href;
	//alert(sPath);
	//alert((sPath.length - 1) + "=" + sPath.lastIndexOf('/'));
	if ((sPath.indexOf('index.php') == -1) && ( sPath.length - 1 != sPath.lastIndexOf('/'))) return;
	//Determine the browser name and version
	//The script will only work on Netscape 3+ and Internet Explorer 4+
	var BrowserType = navigator.appName;
	var BrowserVersion = parseInt(navigator.appVersion);
	
	if (BrowserType == "Netscape" && (BrowserVersion >= 3)) 
	{
		IsValidBrowser = true;
	}
	
	if (BrowserType == "Microsoft Internet Explorer" && (BrowserVersion >= 4)) 
	{
		IsValidBrowser = true;
	}
	
	if (IsValidBrowser) 
	{
		TimerObject = setTimeout("ChangeImage()", DisplayInterval);
		BannerAdCode = 0;
	
		for (i = 0; i < NumberOfImages; i++) 
		{
			BannerAdImages[i] = new Image();
			BannerAdImages[i].src = ' ' + ImageFolder + ImageFileNames[i];
		}
	
	}

}

//Function to change the src of the Banner Ad image
function ChangeImage() {

	if (IsValidBrowser) 
	{
		BannerAdCode = BannerAdCode + 1;
	
		if (BannerAdCode == NumberOfImages) 
		{
			BannerAdCode = 0;
		}
	
		window.document.bannerad.src = BannerAdImages[BannerAdCode].src;
		WriteLayer('prod', null, ImageDesc[BannerAdCode]);
		TimerObject = setTimeout("ChangeImage()", DisplayInterval);
	}
}

//Function to redirect the browser window/frame to a new location,
//depending on which image is currently being displayed by the Banner Ad.
//If Banner Ad is being displayed on an old browser then the DefaultURL is displayed
function ChangePage() 
{
	
	if (IsValidBrowser) {
	
		if (TargetFrame != '' && (FramesObject)) 
		{
			FramesObject.location.href = ImageURLs[BannerAdCode];
		} 
		else 
		{
			document.location = ImageURLs[BannerAdCode];
			
		}
		
	} 
	else if (!IsValidBrowser) 
	{
		document.location = DefaultURL;
	}
}

function openFormPop(divId)
{
	// for pages suchas contact. don't need this function
	//if (document.getElementById('noSubjectOverride') == null)
		//return;
	// toggle display
	togglePop(divId);
	
	var objPopDiv = document.getElementById(divId);
	document.getElementById("subject").value = "Info Request from " + getTitle() + " page";
	// set email hidden field to page email was sent from
	document.getElementById("sendingUrl").value = "URL : " + document.location.href;

}
function getTitle()
{
	var t = document.getElementsByTagName('title')[0];
	if ( !!t.childNodes.length ) 
	{
		return t.firstChild.data;
		//alert( t.firstChild.data );
	} else if ( t.innerHTML ) 
	{
		return 	t.innerHTML;
		//alert( t.innerHTML );
	}
}
function togglePop(divId)
{
	var objPopDiv = document.getElementById(divId);
	if (objPopDiv.style.display == "block")
		objPopDiv.style.display = "none";
	else
		objPopDiv.style.display = "block";
}



function checkForm()
{
	if (trim(document.getElementById("txtInfoName").value) == "")
	{
		alert("Please enter a name");
		document.getElementById("txtInfoName").focus();
		document.getElementById("txtInfoName").select();	
		return false
	}
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(document.getElementById("txtInfoEmail").value))
	{
		alert("Please enter a valid email address");
		document.getElementById("txtInfoEmail").focus();
		document.getElementById("txtInfoEmail").select();	
		return false;
	}
	if (trim(document.getElementById("txaInfoQuestion").value) == "")
	{
		alert("Please let us know what you require");
		document.getElementById("txaInfoQuestion").focus();
		document.getElementById("txaInfoQuestion").select();	
		return false;
	}
	if (trim(document.getElementById("txtCode").value) == "")
	{
		alert("Just enter the the swirly text in box provided.\r\nThis hinders automated spamming");
		document.getElementById("txtCode").focus();
		document.getElementById("txtCode").select();	
		return false;
	}
	
	//return true;
}

function trim(str) 
{
	return str.replace(/^\s+|\s+$/g,"");
}