/**********************************************************************************
* ps_album_view
**********************************************************************************/
function refreshAlbumThumb(mh_id, postid, anchorid, filename, redirect_url) {
//alert("PHOTO");
	// Ensure the call is in the right place
	var local_postid = document.getElementById('postid'+anchorid).value;
	
	if ( local_mh_id == mh_id && local_postid == postid ) {
		open_minihompy(mh_id, mh_id, redirect_url);
		 //Use the following code if the phototool does not resize the photo after modify
		// ensure the new image is reloaded after upload
		var intRand = Math.floor(Math.random()*1000);
		var newImage = new Image();
		newImage.src = filename + '?' + intRand ;
		document.getElementById('img'+ anchorid).src = newImage.src;
		
		anchorPos ="pict"+anchorid;
		location.hash  = anchorPos;
		
	} else {
		open_minihompy(mh_id, mh_id, redirect_url);
	}
	
}

function refreshProfileThumb(mh_id, filename, redirect_url) {
//alert("PROFILE");
		var intRand = Math.floor(Math.random()*1000);
		var newImage = new Image();
		newImage.src = filename + '?' + intRand ;
		document.getElementById('ps_index_img').src = newImage.src;
	
}

function editTitle(mode, id, postid) {

	switch(mode) {
		case "edit":
			titleTxt  = document.getElementById('title'+id);
			titleSet  = document.getElementById('titleset'+id);
			titleFld  = document.getElementById('titlefld'+id);
			editBtn   = document.getElementById('btnedittitle'+id);
			
			titleSet.style.display = 'inline';
			titleFld.value = strip_htmltag('decode', titleTxt.innerHTML);	
			titleTxt.style.display = 'none';
			editBtn.style.display = 'none';		
			break;
		
		case "cancel":
			titleTxt  = document.getElementById('title'+id);
			titleSet  = document.getElementById('titleset'+id);
			editBtn   = document.getElementById('btnedittitle'+id);
			
			titleSet.style.display = 'none';
			titleTxt.style.display = 'inline';
			editBtn.style.display = 'inline';
			break;
			
		case "save":
			titleFld  = document.getElementById('titlefld'+id);
			titleTxt  = document.getElementById('title'+id);
			
			fn = document.frmEdit;
			fn.postid.value = postid;
			fn.updateFld.value = 'title';
			fn.updateVal.value = titleFld.value;
			
			if ( trim(fn.updateVal.value) == "" ) {
				alert(str_msg_no_title);
				return;
			}
			
			if(wordFilter(titleFld.value, 'T')){
				return;
			}
			
			if(FilterPattern('HAN',titleFld.value)){
				return;
			}

			fn.submit();			
			editTitle('cancel',id, 0);
			titleTxt.innerHTML = strip_htmltag('encode', fn.updateVal.value);
			break;
	}
	
}


function editAlbumTitle(mode, id, postid, view_type) {

	switch(mode) {
		case "edit":
			if (view_type == 'photo') {
				titleTxt  = document.getElementById('title'+id);
				titleSet  = document.getElementById('titleset'+id);
				titleFld  = document.getElementById('titlefld'+id);
				inputFld  = document.getElementById('inputfld'+id);
				editBtn   = document.getElementById('btnedittitle'+id);
				
				titleTxt.style.display = 'none';
				titleSet.style.display = 'inline';
				inputFld.style.display = 'inline';
				//titleFld.value = strip_htmltag('decode', titleTxt.getElementsByTagName('a')[0].childNodes[0].nodeValue);	
				editBtn.style.display = 'none';	
			} else {
				titleTxt  = document.getElementById('title'+id);
				titleSet  = document.getElementById('titleset'+id);
				titleFld  = document.getElementById('titlefld'+id);
				inputFld  = document.getElementById('inputfld'+id);
				editBtn   = document.getElementById('btnedittitle'+id);
				
				titleTxt.style.display = 'none';
				titleSet.style.display = 'block';
				inputFld.style.display = 'block';
				//titleFld.value = strip_htmltag('decode', titleTxt.getElementsByTagName('a')[0].childNodes[0].nodeValue);	
				editBtn.style.display = 'none';		
			}
			break;
		
		case "cancel":
			if (view_type == 'photo') {
				titleTxt  = document.getElementById('title'+id);
				titleSet  = document.getElementById('titleset'+id);
				inputFld  = document.getElementById('inputfld'+id);
				editBtn   = document.getElementById('btnedittitle'+id);
				
				inputFld.style.display = 'none';
				titleSet.style.display = 'none';
				titleTxt.style.display = 'inline';
				editBtn.style.display = 'inline';
			} else {
				titleTxt  = document.getElementById('title'+id);
				titleSet  = document.getElementById('titleset'+id);
				inputFld  = document.getElementById('inputfld'+id);
				editBtn   = document.getElementById('btnedittitle'+id);
				
				inputFld.style.display = 'none';
				titleSet.style.display = 'none';
				titleTxt.style.display = 'inline';
				editBtn.style.display = 'block';
			}			
			break;
			
		case "save":
			titleFld  = document.getElementById('titlefld'+id);
			titleTxt  = document.getElementById('title'+id);
			hidFolderName = document.getElementById('arr_folder_name'+id);
			
			fn = eval("document.frmEdit"+id+";");
			hidFolderName.value = titleFld.value;
						
			if ( trim(hidFolderName.value) == "" ) {
				alert(str_msg_no_title);
				return;
			}
			
			if(wordFilter(titleFld.value, 'T')){
				return;
			}
			
			if(FilterPattern('HAN',titleFld.value)){
				return;
			}

			fn.submit();	
			
			if (view_type == 'photo') {	
				editAlbumTitle('cancel',id, 0, 'photo');
				titleTxt.innerHTML = strip_htmltag('encode', hidFolderName.value);
				document.getElementById('folderNamePhotos').innerHTML = strip_htmltag('encode', hidFolderName.value);
			} else {
				editAlbumTitle('cancel',id, 0, null);
				titleTxt.getElementsByTagName('a')[0].childNodes[0].nodeValue = strip_htmltag('encode', hidFolderName.value);
			}	
			break;
	}
	
}


function addAlbumTitle(mode) {

	switch(mode) {
		case "add":
			titleSet  = document.getElementById('titleaddset0');
			addBtn   = document.getElementById('btnaddtitle0');
			
			titleSet.style.display = 'inline';
			addBtn.style.display = 'none';		
			break;
		
		case "cancel":
			titleSet  = document.getElementById('titleaddset0');
			addBtn   = document.getElementById('btnaddtitle0');
			
			titleSet.style.display = 'none';
			addBtn.style.display = 'inline';
			break;
			
		case "save":
			titleFld  = document.getElementById('titleaddfld0');
			folder_name  = document.getElementById('arr_folder_name[]');
			
			fn = document.frmAdd;
			folder_name.value = titleFld.value;
			
			if ( trim(folder_name.value) == "" ) {
				alert(str_msg_no_title);
				return;
			}
			
			if(wordFilter(folder_name.value, 'T')){
				return;
			}
			
			if(FilterPattern('HAN',folder_name.value)){
				return;
			}

			fn.submit();			
			addAlbumTitle('cancel');
			break;
	}
	
}




function editContent(mode, id, postid) {

	switch(mode) {
		case "edit":
			contentTxt  = document.getElementById('content'+id);
			contentSet  = document.getElementById('contentset'+id);
			contentFld  = document.getElementById('contentfld'+id);
			editBtn     = document.getElementById('btneditcontent'+id);
			editorPost  = document.getElementById('editorPost'+id).value;
			
			contenttext = contentTxt.innerHTML;
			
			if ( editorPost == 1 ) {
				contenttext = strip_editortag(contenttext);
			}
			
			contentSet.style.display = 'inline';
			contentFld.value = strip_htmltag('decode', contenttext);
			editBtn.style.display = 'none';	
			tmp = contentTxt.style.color;
			if ( tmp.length > 0) {
				contentFld.style.color = tmp;
			} else {
				contentFld.style.color = '';
			}
			contentTxt.style.display = 'none';
			break;
		
		case "cancel":
			contentTxt  = document.getElementById('content'+id);
			contentSet  = document.getElementById('contentset'+id);
			editBtn     = document.getElementById('btneditcontent'+id);
			colorPicker = document.getElementById('ColorPicker'+id);
			
			contentSet.style.display = 'none';
			contentTxt.style.display = 'inline';
			editBtn.style.display = 'inline';
			
			if ( colorPicker.style.display == 'block') {
				colorPicker.style.display = 'none';
			}
			
			break;
			
		case "sketchcancel":
			contentTxt  = document.getElementById('content'+id);
			contentSet  = document.getElementById('contentset'+id);
			editBtn     = document.getElementById('btneditcontent'+id);
			
			contentSet.style.display = 'none';
			contentTxt.style.display = 'inline';
			editBtn.style.display = 'inline';			
			
			break;
			
		case "save":
			contentFld  = document.getElementById('contentfld'+id);
			contentTxt  = document.getElementById('content'+id);
			contentColor = document.getElementById('content_fontcolor'+id);
			editorPost  = document.getElementById('editorPost'+id);
			
			fn = document.frmEdit;
			fn.postid.value = postid;
			fn.updateFld.value = 'content';
			fn.updateVal.value = contentFld.value;

			if ( trim(fn.updateVal.value) == "" ) {
				alert(str_msg_no_content);
				return;
			}

			if(wordFilter(contentFld.value, 'B')){
				return;
			}
			
			if(FilterPattern('HAN',contentFld.value)){
				return;
			}

			if ( contentColor.value != "" ) {
				fn.updateVal2.value = contentColor.value;
				contentTxt.style.color = contentFld.style.color;
			} else {
				fn.updateVal2.value = '';
			}

			fn.submit();			
			editContent('cancel',id, 0);
			editorPost.value = 0;
			contentTxt.innerHTML = strip_htmltag('encode', fn.updateVal.value);
			break;
			
		case "sketchsave":
			contentFld  = document.getElementById('contentfld'+id);
			contentTxt  = document.getElementById('content'+id);
			
			fn = document.frmEdit;
			fn.postid.value = postid;
			fn.updateFld.value = 'content';
			fn.updateVal.value = contentFld.value;
			
			if ( trim(fn.updateVal.value) == "" ) {
				alert(str_msg_no_title);
				return;
			}
			
			if(wordFilter(contentFld.value, 'B')){
				return;
			}
			
			if(FilterPattern('HAN',contentFld.value)){
				return;
			}

			fn.submit();			
			editContent('sketchcancel',id, 0);
			contentTxt.innerHTML = strip_htmltag('encode', fn.updateVal.value);
			break;
			
			
		case "colorPicker":
			colorPicker = document.getElementById('ColorPicker'+id);

			if ( colorPicker.style.display == 'block') {
				colorPicker.style.display = 'none';
			} else {
				colorPicker.style.display = 'block';
			}
			break;	
		
		case "colorPickerExtend":
			window.open('/common/include/editor_colorpick.php?type=6&id='+id, 'Editcolorpicker', 'width=330,height=270,resizable=no,menubar=no');
			break;
			
		case "setcolor":
			colorPicker = document.getElementById('ColorPicker'+id);
			contentColor = document.getElementById('content_fontcolor'+id);
			content_fontcolor = postid;
			
			ffcolorcode = '#' + content_fontcolor;
			document.getElementById('contentfld'+id).style.color = ffcolorcode;

			contentColor.value = content_fontcolor;
			colorPicker.style.display="none";
			break;
	}
	return;
}


function editScrap(id, postid) {
	
	scrap  = document.getElementById('scrap_yn'+id);
	
	if ( scrap.value == "Y" ) {
		scrap.value = "N";
	} else {
		scrap.value = "Y";
	}
	
	fn = document.frmEdit;
	fn.postid.value = postid;
	fn.updateFld.value = 'scrap';
	fn.updateVal.value = scrap.value;
			
	fn.submit();			

	
}


function editPostOne(fs, cyid, fid, pid, id) {
	//refresh full size image
	var intRand = Math.floor(Math.random()*1000);
	var newImage = new Image();
	newImage.src = document.getElementById('fullImage' + id).value + '?' + intRand;
	
	
	var str_domain = document.location.href;		// 2005-05-14 : by KHS
	var arr_domain = str_domain.split("/");			// 2005-05-14 : by KHS
	var strUrl = "http://" + fs + ".us.cyworld.com/common/phototool/phototool.php?mh_id=" + cyid + "&folderid=" + fid + "&postid=" + pid + "&anchorid=" + id + "&base_domain="+arr_domain[2];
	//imagetool/?kind=MHPhoto
	var strTargetName = "phototoolEdit";
	var ScrollBar = "no";
	var Resize = "no";
	var WinGubun = "";
	var strProperty = "Center";
	WinWidth = 800;
	WinHeight = 665;
	WinOpen(strUrl,strTargetName,WinWidth,WinHeight,ScrollBar,Resize,WinGubun,strProperty);

}



function showPictureEscapeBlocker(id) {
	var intRand = Math.floor(Math.random()*1000);
	showPicture(document.getElementById('fullImage' + id).value+'?'+intRand);
}


function strip_htmltag(mode, val) {
	
	var tmp = val;
	
	switch(mode) {
		
		case "encode":
			// Be careful! Replace order is matter.
			tmp = tmp.replace(/>/g, "&gt;");
			tmp = tmp.replace(/</g, "&lt;");
			tmp = tmp.replace(/\n/g, "<br>");
			tmp = tmp.replace(/\s\s/g, " &nbsp;");
			break;
			
		case "decode":
			tmp = tmp.replace(/\s&nbsp;/g, "  ");
			tmp = tmp.replace(/<br>\n|<br>|<BR>/g, "\n");
			tmp = tmp.replace(/&gt;/g, ">");
			tmp = tmp.replace(/&lt;/g, "<");
			break;
		
	}

	
	return tmp;
	
}


function strip_editortag(val) {
	
	var tmp = val;
	
	tmp = tmp.replace(/<br \/>|<br>|<BR>/g, "\n");
	tmp = tmp.replace(/<\S[^><]*>/g, "");
	tmp = tmp.replace(/&amp;/g, "&");
	tmp = tmp.replace(/&quot;/g, '"');
	tmp = tmp.replace(/&lt;/g, "<");
	tmp = tmp.replace(/&gt;/g, ">");
	tmp = tmp.replace(/&nbsp;/g, " ");
	
	return tmp;
}


/**********************************************************************************
* ps_album_Wview
**********************************************************************************/
function changeYN(me) {
	if ( me.value == "Y" )
		me.value = "N";
	else
		me.value = "Y";
}

function addAnotherPhoto() {
	
	fn = document.frmWrite;
	
	num = fn.img_count.value;
	
	if ( num < 6 ) { 
	
		document.getElementById('box'+num).style.display = 'block';
	
		num++;
		fn.img_count.value = num;
	
		if ( num == 6 ) {
			document.getElementById('addbtnp').style.display = 'none';
		}
		
	}
	
}

function validateAlbumWrite()
{
	//alert("inside validateAlbumWrite");
	var frm = document.frmWrite;
	//alert(frm.folderid.selectedIndex);
	if ( frm.folderid.selectedIndex == 0 ) {
		alert("Please select the folder");
		frm.folderid.focus();
		return;
	}
			
	try
	{
		for (var i=0; i < frm.dstfile_info_count.value; i++)
		{
			if (eval("frm.title_"+i+".value") == "") {
				alert("Please enter a title.");
				eval("frm.title_"+i+".focus()");
				return false;
			}
			if (eval("frm.content_"+i+".value") == "") {
				alert("Please enter a description.");
				eval("frm.content_"+i+".focus()");
				return false;
			}
			if (wordFilter(eval("frm.title_"+i+".value"), 'T')) {
				eval("frm.title_"+i+".focus()");
				return false;
			}
			if (wordFilter(eval("frm.content_"+i+".value"), 'B')) {
				eval("frm.content_"+i+".focus()");
				return false;
			}
			if (FilterPattern('HAN',eval("frm.title_"+i+".value"))) {
				return false;
			}
			if (FilterPattern('HAN',eval("frm.content_"+i+".value"))) {
				return false;
			}
			
		} // end of for(i)
		
		//document.getElementById('statContent').style.display = 'block';
		//document.getElementById('dspContent').style.display = 'none';
		//alert("all fine");
		frm.submit();
		
	} catch(e) {}
		
}


/**********************************************************************************
* phototool
**********************************************************************************/

	function save(status, message)
	{
		if(status=="true")
		{
			frmRedirect.submit();
		}
		else
		{
			alert(str_msg_photo_save_failed);
			window.close();	
		}
	}
	
	function cancel()
	{	
		window.close();
	}
