// JavaScript Document
var sLEHeight = [];
var sLCHeight = [];
var activeSubMenu=-1;
var closerSubMenu=-1;
var resizeBool = false;
var closerResizeBool=false;
var resizeType=false;
var int;
var int2;
var nextBool=true;
var nextSubMenu=-1;
var calDay = 0;
var calMon = 0;
var calYear = 0;
var catHeight = 0;
var catEndHeight = 0;
var catTimer = 0;

function DataHandler(arr){
	for(var i=0;i<arr.length;i++){
		var data = arr[i].split('=');
		this[data[0]] = data[1];
	}
}
function loadAuctions(){
	if(xmlHttp!=null){
		queryId='cat_auctions';
		var url="includes/javascript-data-queries.php";
		url=url+"?id="+queryId;
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange=catalogueChanged;
		xmlHttp.send(null);
	}
}
function catalogueChanged(){
	queryRunning = true;
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		switch(queryId){
			case 'cat_auctions':
				processAuctions(xmlHttp.responseText);
				break;
		}
	}
}
var auctions;
function processAuctions(str){
	var rows = str.split("|");
	var startDate=0;
	var totalRows;
	var tArr;
	var currentDate=0;
	for(var i=0;i<rows.length;i++){
		if(i==0){
			startDate=rows[i];
		} else if(i==1){
			totalRows = rows[i];
			tArr = new Array();
		} else if(i==2){
			currentDate = rows[i];
			tArr = new Array();
		} else {
			tArr[tArr.length] =  new DataHandler(rows[i].split("[spl]"));
		}
	}
	auctions = tArr;
	showAuctionCalender(null,null,startDate,true,currentDate);
}
function calendarFilter(value,type){
	var list = document.getElementById('auctionList');
	var html = '<table cellpadding="0" cellspacing="0" style="width:100%;">';
	switch(type){
		case "date":
			for(var i=0;i<auctions.length;i++){
				if(parseInt(auctions[i].date_value)>=value && parseInt(auctions[i].date_value)<=(value+86400)){
					var Options = "";
					if(parseInt(auctions[i].lot_count)>0){
						Options = '<a href="auctions.php?aucId='+auctions[i].id+'&type=2" title="Click here to view the lots of this auction">View Lots</a> - <a href="auctions.php?aucId='+auctions[i].id+'&type=1" title="Click here to view more information on this sale" style="font-size:14px;">More Information</a>';
					}
					html+= '<tr>';
					html+= '<td class="calendarAuctionTitle">'+auctions[i].name+'</td>';
					html+= '</tr>';
					html+= '<tr>';
					html+= '<td class="calendarAuctionDesc">'+auctions[i].notice+'</td>';
					html+= '</tr>';
					html+= '<tr>';
					html+= '<td class="calendarAuctionOptions">'+Options+'</td>';
					html+= '</tr>';
					html+= '<tr>';
					html+= '<td class="calendarAuctionSpacer">&nbsp;</td>';
					html+= '</tr>';
				}
			}
			break;
		case "future":
			for(var i=0;i<auctions.length;i++){
				if(parseInt(auctions[i].date_value)>=value){
					var Options = "";
					if(parseInt(auctions[i].lot_count)>0){
						Options = '<a href="auctions.php?aucId='+auctions[i].id+'&type=2" style="font-size:14px;" title="Click here to view the lots of this auction">View Lots</a> - <a href="auctions.php?aucId='+auctions[i].id+'&type=1" title="Click here to view more information on this sale" style="font-size:14px;">More Information</a>';
					}
					html+= '<tr>';
					html+= '<td class="calendarAuctionTitle">'+auctions[i].name+'</td>';
					html+= '</tr>';
					html+= '<tr>';
					html+= '<td class="calendarAuctionDesc">'+auctions[i].notice+'</td>';
					html+= '</tr>';
					html+= '<tr>';
					html+= '<td class="calendarAuctionOptions">'+Options+'</td>';
					html+= '</tr>';
					html+= '<tr>';
					html+= '<td class="calendarAuctionSpacer">&nbsp;</td>';
					html+= '</tr>';
				}
			}
			break;
	}
	html += '</table>';
	list.innerHTML = html;
}
function showAuctionCalender(date,change,startDate,init,currentDate){
	startDate = parseInt(startDate);
	var startDay = 0;

	if(date==null){
		date = new Date();
		startDay = date.getDate();
	} else {
		if(change==null){
			calDay = 0;
		}
	}
	var day = date.getDate();
	var month = date.getMonth();
	var year = date.getFullYear();
	if(init){
		var sStart = startDate;
	}
	calMon = month;
	calYear = year;
	var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	var days = new Array('S','M','T','W','T','F','S');
	//var days = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
	var this_month = new Date(year, month, 1);
	var next_month = new Date(year, month + 1, 1);
	var last_month = new Date(year, month - 1, 1);
	var first_week_day = this_month.getDay();
	var days_in_last_month = Math.round((this_month.getTime() - last_month.getTime()) / (1000 * 60 * 60 * 24));
	var lastStartDate = startDate-(days_in_last_month*86400);
	var days_in_this_month = Math.round((next_month.getTime() - this_month.getTime()) / (1000 * 60 * 60 * 24));
	var nextStartDate = startDate+(days_in_this_month*86400);
	var cal_html = '<table class="calender" cellspacing="0"><tr><th colspan="7" class="calenderMonth"><a href="javascript:showAuctionCalender(new Date('+last_month.getFullYear()+','+last_month.getMonth()+',1),null,'+lastStartDate+');" title="Click here to go to the next month"><<</a>   '+months[month]+' '+year+'   <a href="javascript:showAuctionCalender(new Date('+next_month.getFullYear()+','+next_month.getMonth()+',1),null,'+nextStartDate+');" title="Click here to go to the next month">>></a></th></tr><tr>';
	var counter=0;
	var dayCount=1;
	for(var i=0;i<days.length;i++){
		cal_html += '<td class="calenderDayTitle">'+days[i]+'</td>';
	}
	cal_html += '</tr>';
	for(var i=0;i<35;i++){
		var hasAuction=false;
		if(counter==0){
			cal_html += '<tr>';	
		}
		if(i>=first_week_day && dayCount<=days_in_this_month){
			for(var a=0;a<auctions.length;a++){
				if(parseInt(auctions[a].date_value)>=startDate && parseInt(auctions[a].date_value)<=(startDate+86400)){
					hasAuction=true;
					break;
				}
			}
			if(startDay!=dayCount){
				if(calDay!=dayCount){
					if(hasAuction){
						cal_html += '<td class="calenderDay"><a href="javascript:calendarFilter('+startDate+',\'date\');" title="Click here to choose this day">'+dayCount+'</a></td>';
					} else {
						cal_html += '<td class="calenderDay" style="color:#CCCCCC;">'+dayCount+'</td>';
					}
				} else {
					if(hasAuction){
						cal_html += '<td class="calenderDayActive"><a href="javascript:calendarFilter('+startDate+',\'date\');" title="Click here to choose this day">'+dayCount+'</a></td>';
					} else {
						cal_html += '<td class="calenderDay" style="color:#CCCCCC;">'+dayCount+'</td>';
					}
				}
			} else {
				if(calDay!=dayCount){
					if(hasAuction){
						cal_html += '<td class="calenderDayToday"><a href="javascript:calendarFilter('+startDate+',\'date\');" title="Click here to choose this day">'+dayCount+'</a></td>';
					} else {
						cal_html += '<td class="calenderDayToday" style="color:#CCCCCC;">'+dayCount+'</td>';
					}
				} else {
					if(hasAuction){
						cal_html += '<td class="calenderDayActive"><a href="javascript:calendarFilter('+startDate+',\'date\');" title="Click here to choose this day">'+dayCount+'</a></td>';
					} else {
						cal_html += '<td class="calenderDayActive" style="color:#CCCCCC;">'+dayCount+'</td>';
					}
				}
			}
			dayCount++;
			startDate+=86400;
		} else {
			cal_html += '<td class="calenderDay">&nbsp;</td>';	
		}
		counter++;
		if(counter==7){
			cal_html += '</tr>';
			counter=0;
		}
	}
	document.getElementById('calenderContainer').innerHTML = cal_html;
	if(init){
		calendarFilter(currentDate,"future");
	}
}
function showCats(){
	var cat = document.getElementById('catContainer');
	cat.style.height = '100px';
	if(cat.style.visibility=='hidden'){
		cat.style.visibility='visible';
		catEndHeight = catContainerHeight;
		document.getElementById('catLink').style.backgroundColor = '#FFFFFF';
		catTimer = setInterval(catResize,10);
		document.getElementById('catLinkA').style.color = '#336633';
	} else {
		//cat.style.visibility = 'hidden';
		catEndHeight = 1;
		catTimer = setInterval(catResize,10);
	}
}
function catResize(){
	var cat = document.getElementById('catContainer');
	if(catHeight==(catEndHeight+1)){
		cat.style.height = catEndHeight+'px';
		clearInterval(catTimer);
		cat.style.visibility = 'hidden';
		document.getElementById('catLink').style.backgroundColor = '';
		document.getElementById('catLinkA').style.color = '#FFFFFF';
	} else if(catHeight==(catEndHeight-1)){
		cat.style.height = catEndHeight+'px';
		clearInterval(catTimer);
	} else {
		catHeight += Math.ceil((catEndHeight-catHeight)/2);
		cat.style.height = catHeight+'px';
		//cat.innerHTML = Math.ceil((catEndHeight-catHeight)/2);
	}
	
}
function ieSixCheck(){
	if(navigator.appName=="Microsoft Internet Explorer"){
		var ver = navigator.appVersion;
		var arr1 = ver.split("MSIE");
		var arr2 = arr1[1].split(";");
		if(arr2[0]==6){
			var menuBar = document.getElementById('menuContainer');
			menuBar.style.marginLeft=115;
		}
	}
}
function userNameUpdate(){
	var ava = document.getElementById('scuseravail');
	ava.innerHTML = '';
	document.getElementById('useravail').value = '0';
}
function registerCheck(){
	var avail = document.getElementById('useravail');
	var resp = document.getElementById('stringResponder');
	var forename=document.getElementById('scforename');
	var surname=document.getElementById('scsurname');
	var email=document.getElementById('scemail');
	var cEmail = document.getElementById('sccemail');
	var address_street = document.getElementById('scaddress_street');
	var address_2 = document.getElementById('scaddress_2');
	var address_city = document.getElementById('scaddress_city');
	var address_country = document.getElementById('scaddress_country');
	var address_post_code = document.getElementById('scpost_code');
	var username = document.getElementById('scusername_reg');
	var password = document.getElementById('scpassword_reg');
	var cPassword = document.getElementById('sccpassword');
	var question = document.getElementById('scquestion');
	var question_password = document.getElementById('scquestion_password');
	var terms = document.getElementById('scterms');
	
	var forenameT=document.getElementById('scforenameT');
	var surnameT=document.getElementById('scsurnameT');
	var emailT=document.getElementById('scemailT');
	var cEmailT = document.getElementById('sccemailT');
	var usernameT = document.getElementById('scusernameT');
	var passwordT = document.getElementById('scpasswordT');
	var cPasswordT = document.getElementById('sccpasswordT');
	var questionT = document.getElementById('scquestionT');
	var question_passwordT = document.getElementById('scquestion_passwordT');
	
	if(terms.checked){
		if(forename.value!=""){
			forename.style.backgroundColor='#FFFFFF';
			forenameT.style.color='#FFFFFF';
			if(surname.value!=""){
				surname.style.backgroundColor='#FFFFFF';
				surnameT.style.color='#FFFFFF';
				if(email.value!=""){
					email.style.backgroundColor='#FFFFFF';
					emailT.style.color='#FFFFFF';
					if(cEmail.value!=""){
						cEmail.style.backgroundColor='#FFFFFF';
						cEmailT.style.color='#FFFFFF';
						if(cEmail.value==email.value){
							if(username.value!=""){
								username.style.backgroundColor='#FFFFFF';
								usernameT.style.color='#FFFFFF';
								if(password.value!=""){
									password.style.backgroundColor='#FFFFFF';
									passwordT.style.color='#FFFFFF';
									if(cPassword.value!=""){
										cPassword.style.backgroundColor='#FFFFFF';
										cPasswordT.style.color='#FFFFFF';
										if(question.value!=""){
											question.style.backgroundColor='#FFFFFF';
											questionT.style.color='#FFFFFF';
											if(question_password.value!=""){
												question_password.style.backgroundColor='#FFFFFF';
												question_passwordT.style.color='#FFFFFF';
												if(avail.value=='1'){
													return true;
												} else {
													alert('Please click \'Check Availability\' next to the username');
												}
											} else {
												question_password.style.backgroundColor='#FFCCCC';
												question_passwordT.style.color='#FF0000';
											}
										} else {
											question.style.backgroundColor='#FFCCCC';
											questionT.style.color='#FF0000';
										}
									} else {
										cPassword.style.backgroundColor='#FFCCCC';	
										cPasswordT.style.color = '#FF0000';
									}
								} else {
									password.style.backgroundColor='#FFCCCC';
									passwordT.style.color='#FF0000';
								}
							} else {
								username.style.backgroundColor = '#FFCCCC';
								usernameT.style.color='#FF0000';
							}
						} else {
							alert("The email addresses you have entered do not match");	
						}
					} else {
						cEmail.style.backgroundColor='#FFCCCC';
						cEmailT.style.color='#FF0000';
					}
				} else {
					email.style.backgroundColor='#FFCCCC';
					emailT.style.color='#FF0000';
				}
			} else {
				surname.style.backgroundColor='#FFCCCC';
				surnameT.style.color='#FF0000';
			}
		} else {
			forename.style.backgroundColor='#FFCCCC';
			forenameT.style.color='#FF0000';
		}
	} else {
		alert("You must agree to our terms & comditions if you would like to register with us");
	}
	window.scroll(0,0);
	resp.innerHTML = 'Please fill in the details below and click \'Register\' to register for an account with us.<br /><br /><span style="color:#FF0000; font-weight:bold;">The fields marked in red are required</span>';
	return false;
}
function selectDay(cDay){
	calDay = cDay;
	document.getElementById('scdate').value = cDay+'/'+(calMon+1)+'/'+calYear;
	createCalender(new Date(calYear,calMon,1),true);
}
function createCalender(date,change){
	var startDay = 0;
	if(date==null){
		date = new Date();
		startDay = date.getDate();
	} else {
		if(change==null){
			calDay = 0;
		}
	}
	var day = date.getDate();
	var month = date.getMonth();
	var year = date.getFullYear();
	calMon = month;
	calYear = year;
	var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	var days = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
	
	var this_month = new Date(year, month, 1);
	var next_month = new Date(year, month + 1, 1);
	var last_month = new Date(year, month - 1, 1);
	var first_week_day = this_month.getDay();
	var days_in_this_month = Math.round((next_month.getTime() - this_month.getTime()) / (1000 * 60 * 60 * 24));
	var cal_html = '<table class="calender" cellspacing="0"><tr><th colspan="7" class="calenderMonth"><a href="javascript:createCalender(new Date('+last_month.getFullYear()+','+last_month.getMonth()+',1));" title="Click here to go to the next month"><<</a>   '+months[month]+' '+year+'   <a href="javascript:createCalender(new Date('+next_month.getFullYear()+','+next_month.getMonth()+',1));" title="Click here to go to the next month">>></a></th></tr><tr>';
	var counter=0;
	var dayCount=1;
	for(var i=0;i<days.length;i++){
		cal_html += '<td class="calenderDayTitle">'+days[i]+'</td>';
	}
	cal_html += '</tr>';
	for(var i=0;i<35;i++){
		if(counter==0){
			cal_html += '<tr>';	
		}
		if(i>=first_week_day && dayCount<=days_in_this_month){
			if(startDay!=dayCount){
				if(calDay!=dayCount){
					cal_html += '<td class="calenderDay"><a href="javascript:selectDay('+dayCount+');" title="Click here to choose this day">'+dayCount+'</a></td>';
				} else {
					cal_html += '<td class="calenderDayActive"><a href="javascript:selectDay('+dayCount+');" title="Click here to choose this day">'+dayCount+'</a></td>';
				}
			} else {
				if(calDay!=dayCount){
					cal_html += '<td class="calenderDayToday"><a href="javascript:selectDay('+dayCount+');" title="Click here to choose this day">'+dayCount+'</a></td>';
				} else {
					cal_html += '<td class="calenderDayActive"><a href="javascript:selectDay('+dayCount+');" title="Click here to choose this day">'+dayCount+'</a></td>';
				}
			}
			dayCount++;
		} else {
			cal_html += '<td class="calenderDay">&nbsp;</td>';	
		}
		counter++;
		if(counter==7){
			cal_html += '</tr>';
			counter=0;
		}
	}
	document.getElementById('calenderContainer').innerHTML = cal_html;
}
function loadImageBank(){
	window.open('image-bank.php','imageBank','width=600,height=600,location=no,menubar=no,scrollbars=1,status=no,toolbar=no');
}
function retSubLinks(id){
	for(var i=0;i<id;i++){
		var elemId = 'subMenu'+i.toString();
		var subM = document.getElementById(elemId);
		if(subM){
			sLCHeight[i] = 0;
			sLEHeight[i] = Math.ceil(document.getElementById(elemId).clientHeight);
			document.getElementById(elemId).style.height=sLCHeight[i]+'px';
		}
	}
}
function resizeSubMenu(id){
	if(activeSubMenu!=-1){
		document.getElementById('menuLink'+activeSubMenu).style.backgroundImage='url(images/menu-normal.jpg)';
	}
	document.getElementById('menuLink'+id).style.backgroundImage='url(images/menu-hover.jpg)';
	if(!resizeBool && !closerResizeBool){
		if(activeSubMenu==-1){
			
		} else {
			if(activeSubMenu!=id){
				closerSubMenu=activeSubMenu;
				int2 = window.setInterval(closeResizeHandler,10);
			}
		}
		if(activeSubMenu!=id){
			activeSubMenu=id;
			resizeBool=true;
			nextBool=false;
			int = window.setInterval(resizeHandler,10);
		}
	} else {
		/*if(resizeBool){
			clearInterval(int);
			resizeBool=false;
		}
		if(closerResizeBool){
			clearInterval(int2);
			closerResizeBool=false;
			sLCHeight[closerSubMenu]=0;
			document.getElementById('subMenu'+closerSubMenu).style.height = '0px';
		}*/
		nextBool=true;
		nextSubMenu=id;
	}
}
function resizeHandler(){
	if(sLCHeight[activeSubMenu]>sLEHeight[activeSubMenu]){
		clearInterval(int);
		sLCHeight[activeSubMenu]=sLEHeight[activeSubMenu];
		document.getElementById('subMenu'+activeSubMenu).style.height = sLCHeight[activeSubMenu]+'px';
		resizeBool=false;
		if(nextBool){
			resizeSubMenu(nextSubMenu);
		}
	} else {
		sLCHeight[activeSubMenu] += Math.ceil((sLEHeight[activeSubMenu]-(sLCHeight[activeSubMenu]-2))/5);
		document.getElementById('subMenu'+activeSubMenu).style.height = sLCHeight[activeSubMenu]+'px';
	}
}
function closeResizeHandler(){
	if(sLCHeight[closerSubMenu]<=0){
		clearInterval(int2);
		sLCHeight[closerSubMenu]=0;
		document.getElementById('subMenu'+closerSubMenu).style.height = '0px';
		closerResizeBool=false;
		if(nextBool){
			resizeSubMenu(nextSubMenu);
		}
	} else {
		sLCHeight[closerSubMenu] -= Math.ceil((sLCHeight[closerSubMenu]+2)/5);
		document.getElementById('subMenu'+closerSubMenu).style.height = sLCHeight[closerSubMenu]+'px';
	}
}
