
/***********************************************
* Dynamic Countdown script- © Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

function cdtime(container, targetdate){
if (!document.getElementById || !document.getElementById(container)) return
this.container=document.getElementById(container)
this.currentTime=new Date()
this.targetdate=new Date(targetdate)
this.timesup=false
this.updateTime()
}

cdtime.prototype.updateTime=function(){
var thisobj=this
this.currentTime.setSeconds(this.currentTime.getSeconds()+1)
setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
}

cdtime.prototype.displaycountdown=function(baseunit, functionref){
this.baseunit=baseunit
this.formatresults=functionref
this.showresults()
}

cdtime.prototype.showresults=function(){
var thisobj=this


var timediff=(this.targetdate-this.currentTime)/1000 //difference btw target date and current date, in seconds
if (timediff<0){ //if time is up
this.timesup=true
this.container.innerHTML=this.formatresults()
return
}
var oneMinute=60 //minute unit in seconds
var oneHour=60*60 //hour unit in seconds
var oneDay=60*60*24 //day unit in seconds
var dayfield=Math.floor(timediff/oneDay)
var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour)
var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute)
var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute))
if (this.baseunit=="hours"){ //if base unit is hours, set "hourfield" to be topmost level
hourfield=dayfield*24+hourfield
dayfield="n/a"
}
else if (this.baseunit=="minutes"){ //if base unit is minutes, set "minutefield" to be topmost level
minutefield=dayfield*24*60+hourfield*60+minutefield
dayfield=hourfield="n/a"
}
else if (this.baseunit=="seconds"){ //if base unit is seconds, set "secondfield" to be topmost level
var secondfield=timediff
dayfield=hourfield=minutefield="n/a"
}
this.container.innerHTML=this.formatresults(dayfield, hourfield, minutefield, secondfield)
setTimeout(function(){thisobj.showresults()}, 1000) //update results every second
}

/////CUSTOM FORMAT OUTPUT FUNCTIONS BELOW//////////////////////////////

//Create your own custom format function to pass into cdtime.displaycountdown()
//Use arguments[0] to access "Days" left
//Use arguments[1] to access "Hours" left
//Use arguments[2] to access "Minutes" left
//Use arguments[3] to access "Seconds" left

//The values of these arguments may change depending on the "baseunit" parameter of cdtime.displaycountdown()
//For example, if "baseunit" is set to "hours", arguments[0] becomes meaningless and contains "n/a"
//For example, if "baseunit" is set to "minutes", arguments[0] and arguments[1] become meaningless etc


function formatresults(){
if (this.timesup==false){//if target date/time not yet met
var displaystring=arguments[0]+" days "+arguments[1]+" hours "+arguments[2]+" minutes "+arguments[3]+" seconds left"
}
else{ //else if target date/time met
var displaystring="Sale Extended. Must End Soon!!"
}
return displaystring
}




function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function moveSlideshow(elementID,final_x,final_y,interval) {
    if (!document.getElementById) return false;
	
    // if the element does not exist we have nothing to do
    if (!document.getElementById(elementID)) return false;
    var elem = document.getElementById(elementID);
    
    // the slideshow events stack up and the animation is not smooth anymore
    if (elem.movement) {
        clearTimeout(elem.movement);
    }
    
    // current slideshow position
    var xpos = parseInt(elem.style.left);
    var ypos = parseInt(elem.style.top);
    if (xpos == final_x && ypos == final_y) {
        return true;
    }
    
    // restrict moving to white area
    if (final_x <= -elem.max_x) {
        final_x = -elem.max_x;
    }
    if (final_x > 0) {
	    final_x = 0;
    }
    
    // animation bit (taken from the book DOM Scripting by Jeremy Keith)
    if (xpos < final_x) {
        var dist = Math.ceil((final_x - xpos)/10);
        xpos = xpos + dist;
    }
    if (xpos > final_x) {
        var dist = Math.ceil((xpos - final_x)/10);
        xpos = xpos - dist;
    }
  
    // again, restrict showing white area
    if (xpos <= -elem.max_x) {
	    xpos = -elem.max_x;
    }
    if (xpos > 0) {
	    xpos = 0;
    }
    
    // fix the elements position
    elem.style.left = xpos + "px";
    elem.style.top = ypos + "px";
    
    // and set up the event again after an interval
    var repeat = "moveSlideshow('"+elementID+"',"+final_x+","+final_y+","+interval+")";
    elem.movement = setTimeout(repeat,interval);
}



function createRequestObject() {
var ro;		
	if (window.XMLHttpRequest) { 	
		ro = new XMLHttpRequest();
	}else if (window.ActiveXObject) {
		try {
			ro = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			ro = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	return ro;
}




function prepareSlideshow() {
	// first lets make sure the browser understands the DOM methods we will be using
  	if (!document.getElementsByTagName) return false;
  	if (!document.getElementById) return false;
  	
	// Make sure the elements exist
  	if (!document.getElementById("slideshow")) return false;
  	var slideshow = document.getElementById("slideshow");
  	var wrapper = document.getElementById("slideshow_wrapper");
  	wrapper.style.overflow = "hidden";
  	
  	// prepare the navigation bit we will be using
  	// left
  	var navigation = document.createElement("ul");
  	navigation.setAttribute("id", "navigation");
  	var li = document.createElement("li");
  	var scroll_left = document.createElement("a");
  	scroll_left.setAttribute("id", "scroll_left");
  	scroll_left.href ="#";
  	var text = document.createTextNode("Left");
  	scroll_left.appendChild(text);
  	li.appendChild(scroll_left);
  	navigation.appendChild(li);
  	slideshow.insertBefore(navigation, wrapper);
  	
  	//right
  	var li = document.createElement("li");
  	var scroll_right = document.createElement("a");
  	scroll_right.setAttribute("id", "scroll_right");
  	scroll_right.href ="#";
  	var text = document.createTextNode("Right");
  	scroll_right.appendChild(text);
  	li.appendChild(scroll_right);
  	navigation.appendChild(li);
  	slideshow.insertBefore(navigation, wrapper);
	
	var slideshow_set = document.getElementById("slideshow_set");
	slideshow_set.style.top = 0+"px";
	slideshow_set.style.left = 0+"px";
	
	// to get the max y position of the gallery image track we need to count all
	// the li items and multiply that number with 130
	var li = slideshow_set.getElementsByTagName("li");
	slideshow_set.max_x = (li.length-1) * 151;
	slideshow_set.max_y = li.length * 130;
	
	// need the width of the gallery so that they do not scroll vertical
	var width = li.length * 150;
	slideshow_set.style.width = width + "px";
	
	// Attach onmouseover event for left
  	scroll_left.onclick = function() {
		// get the current position of the gallery element
		var slideshow_set = document.getElementById("slideshow_set");
		var x = parseInt(slideshow_set.style.left);
		if (x % 150 == 0) {
    		moveSlideshow("slideshow_set",x+150,0,10);
		}
		return false;
	}
	
	// Attach onmouseover event for right
  	scroll_right.onclick = function() {
		// get the current position of the gallery element
		var slideshow_set = document.getElementById("slideshow_set");
		var x = parseInt(slideshow_set.style.left);

		//Stop if showing the last three images
		nowshowing = (li.length-1) - (x / -100); 		
    	if((x % 150 == 0) && (nowshowing != 2)) {   
    		moveSlideshow("slideshow_set",x-150,0,10);
		}
		return false;
	}
}
addLoadEvent(prepareSlideshow);






//New Personalised Maps Page 
function showStep(theStep){
	
	var tabImage = document.getElementById("tabImage")
	tabImage.src = "images/personal/"+ theStep +".jpg"
	
	/*var allSteps = new Array("step1on","step2on","step3on")
	for(i=0; i < allSteps.length ; i++){
		if(theStep == allSteps[i]){
			var theLayer = document.getElementById(allSteps[i])
			theLayer.style.display = 'block'
		}else{
			var theLayer = document.getElementById(allSteps[i])
			theLayer.style.display = 'none'
		}
	}*/
	
	//go to step 2
	if(theStep=="step2on"){
		if(document.getElementById("selectedMap").value==""){
			alert("Please select a map before proceeding to step 2")
		}else{
			document.getElementById("step2on").style.display = 'block'
			document.getElementById("step1on").style.display = 'none'
			document.getElementById("step3on").style.display = 'none'
		}
	}else if(theStep=="step1on"){
		document.getElementById("step2on").style.display = 'none'
		document.getElementById("step1on").style.display = 'block'
		document.getElementById("step3on").style.display = 'none'
		document.getElementById("sTitle").style.display = 'block'
	}else if(theStep=="step3on"){
		document.getElementById("step2on").style.display = 'none'
		document.getElementById("step1on").style.display = 'none'
		document.getElementById("step3on").style.display = 'block'	
		document.getElementById("sTitle").style.display = 'none'
	}
}

function chooseMap(theSample){
	document.getElementById("sampleIMap").src = 'images/personal/samples/'+theSample+'.png'	
	document.getElementById("sampleLarge").src = 'images/personal/samples/'+theSample+'Large.png'	
	document.getElementById("selectedMap").value = theSample
	document.getElementById("sTitle").style.display = 'none'
		
	//Get the current map size from the database
	http = createRequestObject();
	http.open('get', 'getPMapSize.asp?map='+theSample);
	http.onreadystatechange = handleSizeResponse;
	http.send(null);
	showStep('step2on')
}

function handleSizeResponse(){
	if(http.readyState == 4){
		var stResponse = http.responseText;
		rArray = stResponse.split(" || ")
		document.getElementById("theMapSize").innerHTML = rArray[0];
		document.getElementById("theMapPrice").innerHTML = "£" + rArray[1];
		document.getElementById("finalPrice").value= rArray[1];
		document.getElementById("sampleIMap").src = rArray[2];
	}	
}

function chooseHistoricMap(theSample){
	document.getElementById("sampleIMap").src = 'images/personal/samples/'+theSample+'.jpg'	
	document.getElementById("sampleLarge").src = 'images/personal/samples/'+theSample+'Large.jpg'	
	document.getElementById("selectedMap").value = theSample
	document.getElementById("theYear").value = theSample
	document.getElementById("viewEn").style.display = 'block';
	
	document.getElementById("aViewEn").href = 'images/personal/samples/'+theSample+'Large.jpg';
	document.getElementById("aViewEn").rel = 'lytebox';

	
	//showStep('step2on')
}

function selectColor(theColor){
	if(theColor=="FFFFFF" || theColor=="F6E9C6"){
		document.getElementById("sampleTitle").style.color = '#000000'
		document.getElementById("sampleSubTitle").style.color = '#000000'
		
		document.getElementById("finalTitle").style.color = '#000000'
		document.getElementById("finalSubTitle").style.color = '#000000'
		
		document.getElementById("tBlack").checked=true
		document.getElementById("tWhite").checked=false
		document.getElementById("fontColor").value = '000000'
	}else{
		document.getElementById("sampleTitle").style.color = '#FFFFFF'
		document.getElementById("sampleSubTitle").style.color = '#FFFFFF'
		
		document.getElementById("finalTitle").style.color = '#FFFFFF'
		document.getElementById("finalSubTitle").style.color = '#FFFFFF'
		
		document.getElementById("tBlack").checked=false
		document.getElementById("tWhite").checked=true
		document.getElementById("fontColor").value = 'FFFFFF'
	}
	document.getElementById("sampleImage").style.backgroundColor = '#'+theColor
	document.getElementById("largeSample").style.backgroundColor = '#'+theColor
	
	document.getElementById("finalColor").value = theColor
	
}

function updateTitle()	{
	document.getElementById("sampleTitle").innerHTML = document.getElementById("txtTitle").value.toUpperCase()
}	

function updateSubTitle()	{
	document.getElementById("sampleSubTitle").innerHTML = document.getElementById("txtSubTitle").value.toUpperCase()
}	

function populatePreview(){
	document.getElementById("previewType").innerHTML = "Personalised Map: " + document.getElementById("selectedMap").value
	document.getElementById("previewTitle").innerHTML = document.getElementById("txtTitle").value
	document.getElementById("previewSubTitle").innerHTML = document.getElementById("txtSubTitle").value
	document.getElementById("finalTitle").innerHTML = document.getElementById("txtTitle").value.toUpperCase()
	document.getElementById("finalSubTitle").innerHTML = document.getElementById("txtSubTitle").value.toUpperCase()
	buildItem()
}

function populateHistoricPreview(){
	var theYear = document.getElementById("theYear").value;
	document.getElementById("previewType").innerHTML = "The World " + theYear
	
	
	
	document.getElementById("previewTitle").innerHTML = document.getElementById("txtTitle").value
	document.getElementById("previewSubTitle").innerHTML = document.getElementById("txtSubTitle").value
	document.getElementById("finalTitle").innerHTML = document.getElementById("txtTitle").value.toUpperCase()
	document.getElementById("finalSubTitle").innerHTML = document.getElementById("txtSubTitle").value.toUpperCase()
	buildHistoricItem()
}

function buildHistoricItem(){
	var theMap = document.getElementById("selectedMap").value
	var mapTitle = document.getElementById("txtTitle").value
	var mapSubTitle = document.getElementById("txtSubTitle").value
	var mapColor = document.getElementById("finalColor").value
	var mapPrice = document.getElementById("finalPrice").value
	var theYear = document.getElementById("theYear").value;
	//alert(theYear)
	
	if(mapTitle==""){
		alert("Please enter a title for your map before proceeding")
	}else{
		var qs = "?mapType="+theMap+"&mapTitle="+mapTitle+"&mapSubTitle="+mapSubTitle+"&mapColor="+mapColor+"&mapPrice="+mapPrice+"&theYear="+theYear
		document.frmColor.action = "addHistoric.asp"+qs
		document.frmColor.submit()
	}
	
}

function buildItem(){
	var theMap = document.getElementById("selectedMap").value
	var mapTitle = document.getElementById("txtTitle").value
	mapTitle = mapTitle.replace("&","%26");
	var mapSubTitle = document.getElementById("txtSubTitle").value
	mapSubTitle = mapSubTitle.replace("&","%26");
	var mapColor = document.getElementById("finalColor").value
	var mapPrice = document.getElementById("finalPrice").value
	
	var qs = "?mapType="+theMap+"&mapTitle="+mapTitle+"&mapSubTitle="+mapSubTitle+"&mapColor="+mapColor+"&mapPrice="+mapPrice
	document.frmColor.action = "addPersonalised.asp"+qs
	
	
	if(mapTitle==""){
		alert("Please enter a title for your map before proceeding")
	}else{
		//Clear the checkbox
		document.getElementById("useCustomColor").checked = false
		document.frmColor.submit()
	}
	
}
