function checkMaxLength(e,obj) {
	var keynum=-9999;
	
	if(window.event) //for IE
		keynum = window.event.keyCode;
	else if(e.which) //for other browsers
		keynum = e.which;

	if(keynum != 8 && obj.value != null && obj.value.length > MC_PHOTO_DESCRIPTION_MAX_LENGTH){
		return false;
	}else{
		return true;
	}
}
function showPhotoUpload(){
	if(!OUR_GALLERY_PHOTO_UPLOAD_VISIBLE){
		if(OUR_GALLERY_PHOTOS_UPLOADED >= OUR_GALLERY_MAX_PHOTOS_LIMIT){
			document.getElementById("divPhotoUpload").innerHTML = '<br><br><br><p><span style="color: #CC0033"><b>' + OUR_GALLERY_MAX_PHOTOS_LIMIT_MSG + '</b></span></p>';
		}
		document.getElementById("divPhotoUpload").style.display="block";
		document.getElementById("aPhoto").innerHTML='<span>' + OUR_GALLERY_HIDE_UPLOAD_PIC_MSG + '</span>';
		OUR_GALLERY_PHOTO_UPLOAD_VISIBLE = true;
		createCookie(OUR_GALLERY_COOKIE_NAME, "1",9999);
	}else{
		document.getElementById("divPhotoUpload").style.display="none";
		document.getElementById("aPhoto").innerHTML='<span>' + OUR_GALLERY_SHOW_UPLOAD_PIC_MSG + '</span>';
		OUR_GALLERY_PHOTO_UPLOAD_VISIBLE = false;	
		eraseCookie(OUR_GALLERY_COOKIE_NAME);
	}
}
function uploadPhoto(){
	var objForm = eval("document." + OUR_GALLERY_FORM_NAME);
	objForm.submit();
}
var cookie = readCookie(OUR_GALLERY_COOKIE_NAME);
if(cookie != null && document.getElementById("divPhotoUpload")){
	showPhotoUpload();
}
function getDocumentHeight(){
	var height = 0;
	var winH = 0;
	if(document.body && document.body.offsetHeight){
		winH = document.body.offsetHeight;
	}else{
		winH = window.innerHeight;
	}
	height = winH - 625;
	if(height < 500){
		height = 500;
	}
	return height;
}
function setEditFormCheckboxValue(sourceObj, destObj) {
	if (destObj != null && sourceObj != null) {
		destObj.value = sourceObj.value;
		if (destObj.value == "true") {
			destObj.checked = false;
		} else {
			destObj.checked = true;
		}
	}
}
function showEditPhotoDiv(index, photoThumbnail){
	
	if(OUR_GALLERY_PHOTO_UPLOAD_VISIBLE){
		showPhotoUpload();
	}
	document.mcPhotoEditForm.photoUid.value=document.getElementById("hidPhotoUid" + index).value;
	document.mcPhotoEditForm.photoTitle.value=document.getElementById("hidPhotoTitle" + index).value;
	document.mcPhotoEditForm.photoDescription.value=document.getElementById("hidPhotoDescription" + index).value;
	document.mcPhotoEditForm.photoOrderNo.value=document.getElementById("hidPhotoOrderNo" + index).value;
	OUR_GALLERY_EDIT_PIC_SLIDER.setValue(document.getElementById("hidPhotoOrderNo" + index).value);
	document.mcPhotoEditForm.photoThumbNailFileNameReadOnly.value=photoThumbnail;
    document.getElementById("imgEditThumbnail").src=photoThumbnail;
	document.getElementById("divPhotoEdit").style.height="" + getDocumentHeight() + "px";    
	document.getElementById("divPhotoEdit").style.display="block";
	document.getElementById("editForm").style.display="block";
	setEditFormCheckboxValue(document.getElementById("hidDisabledFlag" + index), document.mcPhotoEditForm.disabledFlag);
	setEditFormCheckboxValue(document.getElementById("hidDisabledFlag" + index), document.mcPhotoEditForm.disabledFlagReadOnly);
	document.getElementById("fpEditTNShow").style.display = document.getElementById("fpTNShow" + index).style.display;
	document.getElementById("fpEditTNDoNotShow").style.display = document.getElementById("fpTNDoNotShow" + index).style.display;
	document.getElementById("fpEditTNDoNotShow").style.background = document.getElementById("pTNDoNotShow" + index).style.background;
	document.getElementById("ourGalleryModelCenter").focus();
	document.mcPhotoEditForm.photoTitle.focus();
}
function editPhoto(){
	document.getElementById("divPhotoEdit").style.display="none";
	document.getElementById("editForm").style.display="none";
	document.mcPhotoEditForm.submit();
}
function deletePhoto(url){
	var conf = confirm(OUR_GALLERY_DELETE_CONFIRM_MSG);
	if(conf){
		document.location.href = url;
	}
}
function closeEditWindow(){
	document.getElementById("divPhotoEdit").style.display="none";
	document.getElementById("editForm").style.display="none";
	if(!OUR_GALLERY_EDIT_PHOTO_SUCCESS && document.getElementById("edit-form-errors")){
		document.getElementById("edit-form-errors").style.display="none";
	}
}
function ourGalleryLoad(){
	if(OUR_GALLERY_PHOTO_UPLOAD_VISIBLE){
		showPhotoUpload();
	}
	document.getElementById("editForm").style.display="block";
	document.getElementById("divPhotoEdit").style.height= "" + getDocumentHeight() + "px";
	document.getElementById("divPhotoEdit").style.display="block";
	document.getElementById("ourGalleryModelCenter").focus();
	document.mcPhotoEditForm.photoTitle.focus();
	//reset the upload form
	var objUploadForm = eval("document." + OUR_GALLERY_FORM_NAME);
	objUploadForm.photoTitle.value="";
	objUploadForm.photoDescription.value="";
	objUploadForm.photoOrderNo.value=OUR_GALLERY_PHOTOS_UPLOADED + 1;
	OUR_GALLERY_UPLOAD_PIC_SLIDER.setValue(objUploadForm.photoOrderNo.value);
	objUploadForm.disabledFlag.checked=true;
	return;
}
var phhHttp = getPhhHttpObject();
function getPhhHttpObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else {
		if (window.ActiveXObject) {
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
}
var g_disabledFlag = null;
var g_linkShowObj = null;
var g_divShowObj = null;
var g_divDoNotShowObj = null;
function showPicture(url, index) {
	try {
		if (g_linkShowObj != null) {
			return;
		}
		g_disabledFlag = document.getElementById("hidDisabledFlag" + index);
		g_linkShowObj = document.getElementById("linkShow" + index);
		g_divShowObj = document.getElementById("fpTNShow" + index);
		g_divDoNotShowObj = document.getElementById("fpTNDoNotShow" + index);
		if (g_disabledFlag.value == "true") {
			url += "false";
		} else {
			url += "true";
		}
		phhHttp.open("GET", url, true);
		phhHttp.onreadystatechange = handleHttpResponse;
		phhHttp.send(null);
	}
	catch (e) {
		g_linkShowObj = null;
		g_disabledFlag = null;
		g_divShowObj = null;
		g_divDoNotShowObj = null;
		alert("showPicture: javascript error=>" + e.description);
	}
}
function handleHttpResponse() {
	try {
		var objFormError = document.getElementById("form-errors");
		if (phhHttp.readyState == 4) {
			if (phhHttp.status == 200) {
				var respTxt = phhHttp.responseText.replace(/^\s*/, "").replace(/\s*$/, "");
				if (respTxt == OUR_GALLERY_AJAX_SUCCESS || respTxt == OUR_GALLERY_AJAX_FAILURE) {
					if (g_disabledFlag.value == "true") {
						g_linkShowObj.innerHTML = OUR_GALLERY_SHOW_NO_MSG;
						g_disabledFlag.value = "false";
						g_divShowObj.style.display = "block";
						g_divDoNotShowObj.style.display = "none";
					} else {
						g_linkShowObj.innerHTML = OUR_GALLERY_SHOW_YES_MSG;
						g_disabledFlag.value = "true";
						g_divShowObj.style.display = "none";
						g_divDoNotShowObj.style.display = "block";
					}
					document.getElementById("form-ajax-errors").style.display = "none";
				} else {
					if (respTxt == OUR_GALLERY_AJAX_PICTURE_DOES_NOT_EXIST) {
						document.getElementById("form-ajax-errors").style.display = "none";
						alert(OUR_GALLERY_AJAX_PICTURE_DELETED_MSG);
					} else {
						respTxt = "<p><strong>Form Errors:</strong></p>" + "<ul>" + respTxt + "</ul>";
						document.getElementById("form-ajax-errors").innerHTML = respTxt;
						document.getElementById("form-ajax-errors").style.display = "block";
						if(objFormError){
							objFormError.innerHTML="&nbsp;";
						}						
					}
				}
			} else {
				if (phhHttp.status == 500) {
					var respTxt = "<p><strong>Internal Server Error</strong></p>";
					document.getElementById("form-ajax-errors").innerHTML = respTxt;
					document.getElementById("form-ajax-errors").style.display = "block";
					if(objFormError){
						objFormError.innerHTML="&nbsp;";
					}
				}
			}
			g_linkShowObj = null;
			g_disabledFlag = null;
			g_divShowObj = null;
			g_divDoNotShowObj = null;
		}
	}
	catch (e) {
		g_linkShowObj = null;
		g_disabledFlag = null;
		g_divShowObj = null;
		g_divDoNotShowObj = null;
		alert("handleHttpResponse: javascript error=>" + e.description);
	}
}
function initializeSliderControl(sliderControlName, formName, flag){
	var minValue=0;
	var maxValue=1;
	if(flag=="U"){//upload picture
		if(OUR_GALLERY_PHOTOS_UPLOADED == 0){
			minValue=0;
		}else{
			minValue=1;
		}
		maxValue=OUR_GALLERY_PHOTOS_UPLOADED + 1;
	}else if(flag=="E"){//edit picture
		if(OUR_GALLERY_PHOTOS_UPLOADED == 0 || OUR_GALLERY_PHOTOS_UPLOADED == 1){
			minValue=0;
		}else{
			minValue=1;
		}
		maxValue=OUR_GALLERY_PHOTOS_UPLOADED;
	}

	if(document.getElementById(sliderControlName)){
		var uploadPicSlider = new dhtmlxSlider(sliderControlName, {
				size:125,           
				skin: "simplegray",
				vertical:false,
				min: minValue,
				max: maxValue
			});
		uploadPicSlider.init();
		var uploadLinkToObject = eval("document." + formName +".photoOrderNo");
		if(uploadLinkToObject && (uploadLinkToObject.value == null || uploadLinkToObject.value.length == 0)){
			uploadPicSlider.setValue(maxValue);
		}else{
			uploadPicSlider.setValue(uploadLinkToObject.value);
		}
		uploadPicSlider.linkTo(uploadLinkToObject);
		
		return uploadPicSlider;
	}else{
		return null;
	}
}
if(OUR_GALLERY_PICTURE_UPLOAD_AUTHORIZED == true){
	var OUR_GALLERY_UPLOAD_PIC_SLIDER = initializeSliderControl("divSlider", OUR_GALLERY_FORM_NAME, "U");
	var OUR_GALLERY_EDIT_PIC_SLIDER = initializeSliderControl("divEditSlider", "mcPhotoEditForm", "E");
}
