function showForm() {
	scroll(0,0);
	modalEffect('frmForms');
	document.getElementById('frmForms').style.display='';
	document.getElementById('loginName').focus();
}

function showFrmFile(hide) {
	document.getElementById('image_title').value = '';
	document.getElementById('image_filename').value = '';
	hideShow(hide,'frmFile');
	document.getElementById('image_title').focus();
}

function showFrmArt(hide) {
	document.getElementById('image_filename').value = '';
	
	getTitles();
	
	hideShow(hide,'frmArt');
}

function hideShow(hide,show) {
	document.getElementById(hide).style.display='none';
	document.getElementById(show).style.display='';
}

function changeDim(height,width,margleft,hide,show) {
	document.getElementById('frmForms').style.height=height;
	document.getElementById('frmForms').style.width=width;
	document.getElementById('frmForms').style.marginLeft=margleft;
	hideShow(hide, show);
}

function changeClass(id){
	document.getElementById(id).className='';
}

function showGallery() {
	hideShow('imgLoading','galimage');
	document.getElementById('gallery').style.display = '';
	document.getElementById('bnav').style.display = '';
}

function hideGallery(x) {
	hideShow('galimage', 'imgLoading');
	document.getElementById('gallery').style.display = 'none';
	document.getElementById('bnav').style.display = 'none';
	if (x == 'prev') {
		if (ci == 0) {
			ci = a.length-1;
		} else {
			ci--;
		}
	} else {
		if (ci == a.length-1) {
			ci = 0;
		} else {
			ci++;
		}
	}
	document.getElementById('galimage').src = "images/uploaded/"+a[ci];
	setTimeout("showGallery()",2000);
}



function closeForm() {
	try {
		document.body.removeChild(document.getElementById('dbgMsgPanelModalMask'));
	} catch(e) {}
	document.getElementById('frmForms').style.display='none';
	document.getElementById('frmLogin').style.display='';
	document.getElementById('frmFile').style.display='none';
	document.getElementById('image_title').value = '';
	document.getElementById('image_filename').value = '';
	document.getElementById('art_filename').value = '';
	document.body.style.overflow = 'auto';
	document.location.href='/';
}

function checkLogin() {
	if (validateForm(document.getElementById('frmLogin').elements)==true) {
		try {
			xprox.initialize('CheckLogin');
			xprox.addVariable('login_name', document.getElementById('loginName').value);
			xprox.addVariable('password', document.getElementById('pwd').value);
			xprox.invokeTextResponse(checkLoginStatus);
		} catch(e) {
			alert(e.description);
		}
	}
	return false;
}

function checkLoginStatus() {
	try {
		result = xprox.responseText.split('|');
		if (result[0] != '-1') {
			alert('Login failed. ' + result[1])
			hideShow('imgLoading','frmLogin');
		} else {
			hideShow('imgLoading','menu');
		}
	} catch(e) {}
}

function validateForm(el) {
	var bRet = true;
	for (ii=0;ii<el.length;ii++) {
		if (el[ii].getAttribute('required')=='yes') {
			if (el[ii].value.length ==0) {
				bRet = false;
				alert('One or more fields have been left blank. Fill in all fields and try again.');
				hideShow('imgLoading','frmLogin');
				el[ii].focus();
				break;
			}
		}
	}
	return bRet;
}

function modalEffect(form) {
	var msgBox = document.getElementById(form);
	var modalMask = document.createElement('div');
	var nTop = 0;
	var nLeft = 0;
	var nWidth = 0;
	var nHeight = 0;
	
	if (typeof(window.innerWidth) == 'number') {
		nWidth = window.innerWidth;
		nHeight = window.innerHeight;
	} else {
		nWidth = document.documentElement.clientWidth;
		nHeight = document.documentElement.clientHeight;
	}
	
	nLeft = nWidth/2 - 450/2;
	nTop = nHeight/2 - 150/2;

	msgBox.style.zIndex = 500;
	msgBox.style.position = 'absolute';

	modalMask.id = 'dbgMsgPanelModalMask';
	modalMask.style.zIndex = 101;
	modalMask.style.position = 'absolute';
	modalMask.style.left = 0;
	modalMask.style.top = 0;
	modalMask.style.border = '0px none';
	modalMask.style.backgroundColor = '#4B4B4B';
	modalMask.style.width = nWidth + 'px';
	modalMask.style.height = nHeight + 'px';
	modalMask.innerHTML = '&nbsp;';

	document.body.style.overflow = 'hidden';
	document.body.appendChild(modalMask);

	setOpacity(70,'dbgMsgPanelModalMask');
}

function setOpacity(opacity, id) {
	var el = document.getElementById(id).style;
	el.opacity = (opacity / 100);
	el.MozOpacity = (opacity / 100);
	el.KhtmlOpacity = (opacity / 100);
	el.filter = "alpha(opacity=" + opacity + ")";
}

function validateFileForm(el) {
	var bRet = true;
	for (ii=0;ii<el.length;ii++) {
		if (el[ii].getAttribute('required')=='yes') {
			if (el[ii].value.length ==0) {
				bRet = false;
				alert('One or more fields have been left blank. Fill in all fields and try again.');
				hideShow('imgLoading',el);
				el[ii].focus();
				break;
			}
		}
	}
	return bRet;
}

function validateFileExtension(fld) {
	if(!/(\.png|\.gif|\.jpg|\.jpeg)$/i.test(fld.value)) {
		alert("You can only upload a gif, jpg, or png.");
		fld.value = '';
		fld.focus();
		return false;
	}
	return true;
}

function uploadImage(el,titleId,imgType) {
	if (validateFileForm(document.getElementById(el).elements)==true) {
		hideShow(el, 'imgLoading');
		try {
			xprox.initialize('UploadImage');
			xprox.addVariable('imgTitle', document.getElementById(titleId).value);
			xprox.addVariable('imgType', imgType);
			xprox.addVariable('userId', document.getElementById('user_id').value);
			
			if (imgType == 2) {
				xprox.invokeTextResponse(checkArtUploadStatus);
			} else {
				xprox.invokeTextResponse(checkUploadStatus);
			}
		} catch(e) {
			alert(e.description);
		}
		return false;
	}
}

function checkArtUploadStatus() {
	var h3 = document.getElementById('art_title').value;
	try {
		result = xprox.responseText.split('|');
		if (result[0] != '-1') {
			alert('Error! ' + result);
			hideShow('imgLoading','frmArt');
		} else {
			document.getElementById('frmArt').submit();
		}
	} catch(e) {
		alert(e.description);
	}
	document.getElementById('art_filename').value = '';
	rmvList('titlediv','spar');
	hideShow('imgLoading','frmComplete');
	document.getElementById('h3comp').innerHTML = '<br/><br/>An image has been added to the '+ h3 +' collection.<br/><br/>';
}

function checkUploadStatus() {
	var h3 = document.getElementById('image_title').value;
	try {
		result = xprox.responseText.split('|');
		if (result[0] != '-1') {
			alert('Error! ' + result);
			hideShow('imgLoading','frmFile');
		} else {
			document.getElementById('frmFile').submit();
		}
	} catch(e) {
		alert(e.description);
	}
	hideShow('imgLoading','frmComplete');
	if (document.getElementById('flakcheck').checked) {
		document.getElementById('h3comp').innerHTML = '<br/><br/>' + h3 + ' has been uploaded and is now the flakmag comic.<br/><br/>';
	} else {
		document.getElementById('h3comp').innerHTML = '<br/><br/>' + h3 + ' has been uploaded, and it was not made the flakmag comic.<br/><br/>';
	}
}

function getImages(g) {
	try {
		xprox.initialize('GetImages');
		xprox.addVariable('g', g);
		xprox.invokeTextResponse(checkGetImagesStatus);
	} catch(e) {
		alert(e.description);
	}
	return false;
}
	
function checkGetImagesStatus() {
	try {
		result = xprox.responseText.split('|');
		if (result[0] != '-1') {
			alert('Error! ' + result);
		} else {
			x = result[1].split(',');
			for (j=0; j<x.length; j++) {
				a[a.length] = x[j];
				if (x[j] == c) {
					ci = a.length - 1;
				}
			}
		}
	} catch(e) {
		alert(e.description);
	}
}

function editImages(g) {
	hideShow('menu','imgLoading');
	imgType = g;
	try {
		xprox.initialize('EditImages');
		xprox.addVariable('g', g);
		xprox.invokeTextResponse(checkEditImagesStatus);
	} catch(e) {
		alert(e.description);
	}
	return false;
}
	
function checkEditImagesStatus() {
	try {
		result = xprox.responseText.split('#');
		if (result[0] != '-1') {
			alert('Error! ' + result);
		} else {
		
			var d = document.createElement('div');
			d.id = 'ordList';
			document.getElementById('orderlist').appendChild(d);
			
			for (j=0;j<6;j++) {
				var d = document.createElement('div');
				d.id = 'listCol'+j;
				document.getElementById('ordList').appendChild(d);
			}
				
			x = result[1].split('|');
			z=0;
			
			for (j=0; j<x.length; j++) {
				y = x[j].split(',');
				
				var d = document.createElement('div');
				d.id = 'd'+j;
				d.className = 'dOrd';
				
				var i1 = document.createElement('input');
				i1.id = 'io'+j;
				i1.className = 'iOrd';
				i1.setAttribute("type","text");
				i1.setAttribute("size","4");
				i1.setAttribute("maxlength","4");
				i1.value = y[2];
				i1.setAttribute("onchange","changeArray("+j+")");
				
				var i2 = document.createElement('input');
				i2.id = 'it'+j;
				i2.className = 'iTit';
				i2.setAttribute("type","text");
				i2.setAttribute("size","15");
				i2.setAttribute("maxlength","30");
				i2.value = y[1];
				i2.setAttribute("onchange","changeArray("+j+")");
				
				var i3 = document.createElement('input');
				i3.id = 'ih'+j;
				i3.setAttribute("type","hidden");
				i3.value = y[0];
				
				var del = document.createElement('div');
				del.id = 'del'+y[0];
				del.className = 'delBtn';
				if (imgType == 1) {
					del.innerHTML = "x";
					del.setAttribute("onclick","deleteImage("+y[0]+",'"+y[1]+"')");
				} else {
					del.innerHTML = "<br/>";
				}
				
				if (imgType == 2) {
					var taDiv = document.createElement('div');
					taDiv.id = 'ta'+j;
					taDiv.className = 'taDiv';
					var i4 = document.createElement('textarea');
					i4.id = 'ic'+j;
					i4.className = 'taArt iTit';
					i4.setAttribute("rows","3");
					i4.value = y[3].replace(/@/g, ",");
					i4.setAttribute("onchange","changeArray("+j+")");
				}
				
				document.getElementById('listCol'+z).appendChild(d);
				document.getElementById('d'+j).appendChild(i1);
				document.getElementById('d'+j).appendChild(i2);
				document.getElementById('d'+j).appendChild(i3);
				if (imgType == 2) {
					document.getElementById('d'+j).appendChild(taDiv);
					document.getElementById('ta'+j).appendChild(i4);
				}
				document.getElementById('d'+j).appendChild(del);
				
				if (j == 49) {
					z=1;
				}
				if (j == 99) {
					z=2;
				}
				if (j == 149) {
					z=3;
				}
				if (j == 199) {
					z=4;
				}
				if (j == 249) {
					z=5;
				}
			}
		}
	} catch(e) {
		alert(e.description);
	}
	changeDim('600px','1050px','75px','imgLoading','frmOrder');
}

function updateImages() {
	document.getElementById('imgLoading').style.marginLeft = '500px';
	hideShow('frmOrder','imgLoading');
	try {
		if (imgType == 1) {
			xprox.initialize('UpdateImages');
		} else {
			xprox.initialize('UpdateArt');
		}
		xprox.addVariable('imgType', imgType);
		xprox.addVariable('len', a.length);
		for (j=0; j<a.length; j++) {
			xprox.addVariable('io'+j, a[j][0]);
			xprox.addVariable('it'+j,  a[j][1]);
			xprox.addVariable('ih'+j, a[j][2]);
			if (imgType == 2) {
				xprox.addVariable('ic'+j, a[j][3]);
			}
		}
		
		xprox.invokeTextResponse(checkUpdateImagesStatus);
	} catch(e) {
		alert(e.description);
	}
	return false;
}
	
function checkUpdateImagesStatus() {
	try {
		result = xprox.responseText.split('|');
		if (result[0] != '-1') {
			alert('Error! ' + result);
		} else {
			
		}
	} catch(e) {
		alert(e.description);
	}
	
	a=[];
	rmvList('orderlist','ordList');
	editImages(imgType);
	hideShow('imgLoading','frmOrder');
	document.getElementById('imgLoading').style.marginLeft = '230px';	
}

function rmvList(pdiv,cdiv) {
	pdiv = document.getElementById(pdiv);
	cdiv = document.getElementById(cdiv);
	pdiv.removeChild(cdiv);
}

function changeArray(j) {
	io = document.getElementById('io'+j).value;
	it = document.getElementById('it'+j).value;
	ih = document.getElementById('ih'+j).value;
	if (imgType == 2) {
		ic = document.getElementById('ic'+j).value;
		ic = ic.replace(/,/g, "@");
	}
	
	ind='';
	if (imgType == 2) {
		if (a.length > 0) {
			for (k=0;k<a.length;k++) {
				if (a[k][2] == ih) {
					ind = k;
				}
			}
			if (ind !== '') {
				a[ind] = [io,it,ih,ic];
			} else {
				a[a.length] = [io,it,ih,ic];
			}
		} else {
			a[a.length] = [io,it,ih,ic];
		}
	} else {
		if (a.length > 0) {
			for (k=0;k<a.length;k++) {
				if (a[k][2] == ih) {
					ind = k;
				}
			}
			if (ind !== '') {
				a[ind] = [io,it,ih];
			} else {
				a[a.length] = [io,it,ih];
			}
		} else {
			a[a.length] = [io,it,ih];
		}
	}
}

function addNewTitle(itm) {

	var newTitle = prompt('Enter new title', '[Title]');

	if (newTitle != null) {
		
		var newCaption = prompt('Enter new description', '[Description]');
		
		if (newCaption != null) {
			insertNewTitle(newTitle,newCaption);
		}
	}

}

function insertNewTitle(newTitle,newCaption) {

	try {
		xprox.initialize('AddTitle');
		xprox.addVariable('newTitle', newTitle);
		xprox.addVariable('newCaption', newCaption);
		xprox.invokeTextResponse(checkNewTitleStatus);
	} catch(e) {
		alert(e.description);
	}

	return false;
}

function checkNewTitleStatus() {
	try {
		result = xprox.responseText.split('|');
		if (result[0] != '-1') {
			alert('Failed to create new title. ' + result[1]);
		} else {
		
			var s = document.getElementById('art_title');
			
			var opt = document.createElement('option');
			opt.text = result[1];
			opt.value = result[1];

			s.appendChild(opt);

			s.selectedIndex = opt.index;
			
		}

	} catch(e) {}
}

function getTitles() {
	try {
		xprox.initialize('GetTitleList');
		xprox.invokeTextResponse(checkGetTitlesStatus);
	} catch(e) {
		alert(e.description);
	}
	return false;
}

function checkGetTitlesStatus() {
	try {
		result = xprox.responseText.split('|');
		if (result[0] != '-1') {
			alert('Error! ' + result);
		} else {
			
			var d = document.createElement('div');
			d.id = 'spar';
			document.getElementById('titlediv').appendChild(d);
			
			var s = document.createElement('select');
			s.id = 'art_title';
			s.required = "yes";
			d.appendChild(s);
			
			x = result[1].split(',');
			for (j=0; j<x.length; j++) {
				
				var opt = document.createElement('option');
				
				opt.text = x[j];
				opt.value = x[j];
				
				s.appendChild(opt);
	
			}
		}
	} catch(e) {
		alert(e.description);
	}
}

function deleteImage(id,title) {
	message = 'Click OK to delete '  + title + '. This will remove the image as well as the related information from THE DATABASE.';
	
	if (confirm(message)) {
		document.getElementById('imgLoading').style.marginLeft = '500px';
		hideShow('frmOrder','imgLoading');
		try {
			xprox.initialize('DeleteImage');
			xprox.addVariable('imgType', imgType);
			xprox.addVariable('imageId', id);
			xprox.invokeTextResponse(checkDeleteImage);
		} catch(e) {
			alert(e.description);
		}
		return false;
	}
}

function checkDeleteImage() {
	try {
		result = xprox.responseText.split('|');
		if (result[0] != '-1') {
			alert('Failed to delete image: ' + result);
			hideShow('imgLoading','frmOrder');
		}
		else {
			updateImages();
		}
	} catch(e) {}
}

