// JavaScript Document
var xmlHttp;
var pageId;
var queryId;
var userArray=[];
var queryRunning=false;
var pageLoading=false;
var fPEmail='';
var viewLotChange=false;
function setAlarm(lotId){
	if(xmlHttp!=null){
		var btnAlarm = document.getElementById('alarm'+lotId);
		btnAlarm.disabled=true;
		btnAlarm.value="Setting..";
		queryId="setalarm";
		var url="includes/javascript-data-queries.php";
		url=url+"?id="+queryId+'&lot_id='+lotId;
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.send(null);
	}
}
function loadLiveAuction(aucId){
	document.body.innerHTML = '';
	window.open('lb/liveauction.php?aucId='+aucId,'imageBank','width=875,height=600,location=no,menubar=no,scrollbars=0,status=no,toolbar=no');
}
function loadWhiteLabel(){
	var url = 'wl.php';
	window.open(url,'ibidliveauction','width=740,height=610,location=no,menubar=no,scrollbars=0,status=no,toolbar=no');
}
function viewLotChangeImage(img,wid,hei){
	var bigImage = document.getElementById('largeImageContainer');
	bigImage.innerHTML = '<img src="'+img+'" style="width:'+wid+'px; height:'+hei+'px; border:none;" />';
}
function checkTopSearch(){
	var src = document.getElementById('sctopsrc').value;
	if(src=='Example: LCD TV'){
		return false;
	} else {
		return true;
	}
}
function clearTopSearchText(){
	var src = document.getElementById('sctopsrc');
	src.value = '';
	src.style.color='#000000';
	src.style.fontStyle='normal';
}
function fPSecureQuestion(){
	var pass = document.getElementById('scquestion').value;
	if(xmlHttp!=null){
		queryId=13;
		var url="includes/javascript-data-queries.php";
		url=url+"?id="+queryId+'&pass='+pass+'&email='+fPEmail;
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.send(null);
	}	
}
function fPEmailCheck(){
	fPEmail = document.getElementById('scemail').value;
	if(xmlHttp!=null){
		queryId=12;
		var url="includes/javascript-data-queries.php";
		url=url+"?id="+queryId+'&email='+email;
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.send(null);
	}
}
function checkUserName(){
	var user = document.getElementById('scusername_reg').value;
	if(user!=''){
		if(xmlHttp!=null){
			queryId=11;
			var url="includes/javascript-data-queries.php";
			url=url+"?id="+queryId+'&username='+user;
			xmlHttp.open("GET",url,true);
			xmlHttp.onreadystatechange=stateChanged;
			xmlHttp.send(null);
		}
	} else {
		alert("You need to enter a username");
	}
}
function toggleEditor(id) {
	if (!tinyMCE.get(id)){
		tinyMCE.execCommand('mceAddControl', false, id);
	}else{
		tinyMCE.execCommand('mceRemoveControl', false, id);
	}
}
function createXMLHttpRequest(){
	try {
		//firefox, opera & Safari
		xmlHttp = new XMLHttpRequest();
	} catch(e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e){
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e){
				xmlHttp=null;	
			}
		}
		
	}
}
createXMLHttpRequest();



function sendLoginDetails(user,pass){
	if(xmlHttp!=null){
		queryId=4;
		var url="includes/javascript-data-queries.php";
		url=url+"?id="+queryId;
		url+="&user="+user+"&pass="+pass;
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.send(null);
	}
}
function sendUserData(val){
	sendCount=val;
	if(xmlHttp!=null){
		queryId=2;
		var url="includes/javascript-data-queries.php";
		url=url+"?id="+queryId;
		if((sendCount+5)>userArray.length){
			var endVal = userArray.length;
		} else {
			var endVal = sendCount+5;	
		}
		for(var i=sendCount; i<endVal;i++){
			url += "&data"+i+"="+userArray[i];
		}
		url+="&startValue="+sendCount+"&endValue="+endVal;
		
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.send(null);
	}
}
function sendUserDetailsData(val){
	sendCount=val;
	if(xmlHttp!=null){
		queryId=3;
		var url="includes/javascript-data-queries.php";
		url=url+"?id="+queryId;
		if((sendCount+1)>userArray.length){
			var endVal = userArray.length;
		} else {
			var endVal = sendCount+1;	
		}
		for(var i=sendCount; i<endVal;i++){
			url += "&data"+i+"="+userArray[i];
		}
		url+="&startValue="+sendCount+"&endValue="+endVal;
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.send(null);
	}
}
function postUserUploadCheck(){
	if(userArray.length>0){
		alert("Uploading");
		sendUserDetailsData(0);
	}
}
function loginUserAccount(){
	if(xmlHttp!=null){
		queryId=4;
		var url="includes/javascript-data-queries.php";
		url=url+"?id=4&user="+user+"&pass="+pass;
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.send(null);
	}
}
function loadPage(id){
	if(!pageLoading){
		pageLoading=true;
		pageId=id;
		document.getElementById("pageContainer").innerHTML = '<span style="font-size:11px; color:#999999;">Loading Page...</span>';
		if(xmlHttp!=null){
			queryId=1;
			var url="includes/javascript-data-queries.php";
			url=url+"?id=1&page_id="+id;
			xmlHttp.open("GET",url,true);
			xmlHttp.onreadystatechange=stateChanged;
			xmlHttp.send(null);
		}
	}
}
function stateChanged(){
	queryRunning = true;
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		switch(queryId){
			case 1://loads the page
				document.getElementById('pageContainer').innerHTML = xmlHttp.responseText;
				pageLoading=false;
				break;
			case 2://mass user accounts loader
				var percent = Math.round((sendCount/userArray.length)*100);
				document.getElementById('pageContainer').innerHTML = document.getElementById('pageContainer').innerHTML+'<br />'+'<span style="font-size:11px; color:#999999;">'+percent+'% Complete.<br />'+xmlHttp.responseText+'</span>';
				if((sendCount+5)>userArray.length){
					sendCount = userArray.length;
				} else {
					sendCount = sendCount+5;	
				}
				if(sendCount<userArray.length){
					sendUserData(sendCount);
				} else {
					document.getElementById('pageContainer').innerHTML = document.getElementById('pageContainer').innerHTML+'<br />'+'<span style="font-size:11px; color:#999999;">100% Complete.</span>';
					alert("Total Uploaded:"+userArray.length);
				}
				break;
			case 3://mass user details loader
				var percent = Math.round((sendCount/userArray.length)*100);
				document.getElementById('pageContainer').innerHTML = '<span style="font-size:11px; color:#999999;">'+percent+'% Complete.</span>';
				document.getElementById('errorContainer').innerHTML = document.getElementById('errorContainer').innerHTML + '<br /><span style="font-size:11px; color:#FF0000;">'+xmlHttp.responseText+'</span>';
				
				if((sendCount+1)>userArray.length){
					sendCount = userArray.length;
				} else {
					sendCount = sendCount+1;	
				}
				if(sendCount<userArray.length){
					sendUserDetailsData(sendCount);
				} else {
					document.getElementById('pageContainer').innerHTML = '<span style="font-size:11px; color:#999999;">100% Complete.</span>';
					alert("Total Uploaded:"+userArray.length);
				}
				break;
			case 4://logs the user in
			if(xmlHttp.responseText=="1"){
					var tpArr = tp.split("[AMP]");
					tp = tpArr[0];
					for(var i=1;i<tpArr.length;i++){
						tp+='&'+tpArr[i];
					}
					window.location = tp;
				} else {
					window.location = 'login.php?err=1&tp='+tp;
				}
				break;
			case 5://switches edit off and on
				var edit = parseInt(xmlHttp.responseText);
				if(edit==1){
					document.getElementById('editModeBtn').innerHTML = 'Edit Off';
					document.getElementById('imageBankLinkContainer').innerHTML = '<a href="javascript:loadImageBank();" title="Click here to open the Image Bank">Image Bank</a>';
				} else {
					document.getElementById('editModeBtn').innerHTML = 'Edit On';
					document.getElementById('imageBankLinkContainer').innerHTML = '';
				}
				loadPage(pageId);
				break;
			case 6://loads the pane creator options	
				if(xmlHttp.responseText=="comp"){
					closeEditor();
				} else {
					document.getElementById('editorFrameContainer').innerHTML = xmlHttp.responseText;
					toggleEditor("sctextarea");
					window.scroll(0,0);
				}
				break;
			case 7://loads the pane creator options	
				if(xmlHttp.responseText=="comp"){
					closeEditor();
				} else {
					document.getElementById('editorFrameContainer').innerHTML = xmlHttp.responseText;
					toggleEditor("sctextarea");
					window.scroll(0,0);
				}
				break;
			case 8://loads the pane creator options	
				if(xmlHttp.responseText=="comp"){
					closeEditor();
					//document.getElementById('editorFrameContainer').innerHTML = xmlHttp.responseText;
				} else {
					document.getElementById('editorFrameContainer').innerHTML = xmlHttp.responseText;
					window.scroll(0,0);
				}
				break;
			case 9://loads the pane creator options	
				var order = document.getElementById('order');
				var str = xmlHttp.responseText;
				var arr = str.split(",");
				var total = parseInt(arr[(arr.length-1)]);
				order.options.length = 0;
				order.options[0] = new Option("<At Top>",0,true,false);
				for(var i=0;i<(arr.length-1);i++){
					var iAdd1 = i+1;
					var arr2 = arr[i].split("|");
					order.options[iAdd1] = 	new Option(arr2[1],arr2[0],false,false);
				}
				break;
			case 10:
				
				var page = [];
				var str = xmlHttp.responseText;
				var arr = str.split("|");
				for(var i=0;i<arr.length;i++){
					var arr2 = arr[i].split("¬");
					page[arr2[0]] = arr2[1];
				}
				document.getElementById('scurl').value = page['page_address'];
				for(var i=0;i<document.getElementById('sec').options.length;i++){
					if(document.getElementById('sec').options[i].value==page['security_level']){
						document.getElementById('sec').selectedIndex = i;
					}
				}
				for(var i=0;i<document.getElementById('scparent').options.length;i++){
					if(document.getElementById('scparent').options[i].value==page['parent_page_id']){
						document.getElementById('scparent').selectedIndex=i;
					}
				}
				if(page['approved']==1){
					document.getElementById('approved').checked=true;	
				} else {
					document.getElementById('approved').checked=false;	
				}
				if(page['record_user_activity']==1){
					document.getElementById('stats').checked=true;	
				} else {
					document.getElementById('stats').checked=false;	
				}
				queryRunning=false;
				changeEditOrder();
				break;
			case 11:
				var ava = document.getElementById('scuseravail');
				if(xmlHttp.responseText=="1"){
					ava.innerHTML = '<img src="images/cross.png" style="border:none;" />';
					document.getElementById('useravail').value = '0';
				} else {
					ava.innerHTML = '<img src="images/tick.png" style="border:none;" />';
					document.getElementById('useravail').value = '1';
				}
				break;
			case 12:
				if(xmlHttp.responseText=="0"){
					var html = '<p>The email address you entered is incorrect, please try again.</p>';
					html += '<table style="width:500px;" cellspacing="0">';
					html += '<tr><td class="tableInfoCell">Email Address:</td><td class="tableInputCell"><input type="text" id="scemail" style="width:95%;" /></td></tr>';
					html += '<tr><td colspan="2" style="text-align:center;"><input type="button" onclick="javascript:fPEmailCheck();" value="Request Password" /></td></tr>'
					html += '</table>';
					document.getElementById('forgottenContainer').innerHTML = html;
				} else {
					var data = xmlHttp.responseText;
					var arr = data.split("|");
					var html = '<p>Please answer your security question, if this is answered correctly your password will be sent to you email address.</p>';
					html += '<table style="width:500px;" cellspacing="0">';
					html += '<tr><td class="tableInfoCell">Security Question: </td><td class="tableInputCell">'+arr[1]+'</td></tr>';
					html += '<tr><td class="tableInfoCell">Security Answer: </td><td class="tableInputCell"><input type="password" id="scquestion" style="width:95%;" /></td></tr>';
					html += '<tr><td colspan="2" style="text-align:center;"><input type="button" onclick="javascript: value="Answer Question" /></td></tr>'
					html += '</table>';
					document.getElementById('forgottenContainer').innerHTML = html;
				}
			case 13:
				if(xmlHttp.responseText=="1"){
					document.getElementById('forgottenContainer').innerHTML = '<p>An email has been sent with your username and password to the email address you have registered.</p>';
				} else {
					var html = '<p>The password you entered was incorrect, please try again.</p>';
					html += '<table style="width:500px;" cellspacing="0">';
					html += '<tr><td class="tableInfoCell">Email Address:</td><td class="tableInputCell"><input type="text" id="scemail" style="width:95%;" /></td></tr>';
					html += '<tr><td colspan="2" style="text-align:center;"><input type="button" onclick="javascript:fPEmailCheck();" value="Request Password" /></td></tr>'
					html += '</table>';
					document.getElementById('forgottenContainer').innerHTML = html;
				}
			case "setalarm":
				var alarmParams = xmlHttp.responseText.split("|");
				var btnAlarm = document.getElementById('alarm'+alarmParams[1]);
				if(alarmParams[0]=="1"){
					btnAlarm.value= 'Alarm Set';
				} else {
					btnAlarm.value = "Error";
				}
				break;
		}
		queryRunning=false;
	}
}