function openLinkDialog(channelGuid, showPostings, friendlyUrl, readOnly, defaultvalue)
{
	var link = '/veluxcommon/tools/ImageEditALTandHREF.aspx?type=modaldialog';
	if(channelGuid!='')
	{
		link+='&channelGuid='+channelGuid;
	}
	if(showPostings)
		link+='&showPostings=true';
	else
		link+='&showPostings=false';
	if(friendlyUrl)
		link+='&friendlyurl=true';
	else
		link+='&friendlyurl=false';
	if(readOnly)
		link+='&readonly=true';
	if(defaultvalue!=null&&defaultvalue!='')
		link+='&DefaultValue='+defaultvalue;
	return window.showModalDialog(link,'Pick a link','scroll:no; dialogHeight: 450px; dialogWidth: 500px; edge: Raised; center: Yes; help: No; resizable: No; status: No;');
}

function showImagePop(imageGUID)
{
	var lefter = event.clientX+5;
	var topper = event.clientY+5;
	if(window.oPopup)
	{
		oPopup.document.body.style.backgroundColor='silver';
		oPopup.document.body.innerHTML = '<img src="/veluxcommon/tools/GetResourceImage.aspx?image='+imageGUID+'&thumb=large" style="border:solid 1px black;width:114;height:114;">'; 
		oPopup.show(lefter, topper, 116, 116, document.body);
	}
}
function hideImagePop()
{
	if(window.oPopup)
	{
		oPopup.hide();
	}
}

function insertImage(parentContainer,guid,GalleryGUID,width,height)
{
	var op = window.opener;
	if(!op)
	{
		document.location.href='/veluxcommon/tools/GetResourceImage.aspx?image='+guid;
		return;
	}
	var hiddenUrl = op.document.getElementById(parentContainer+'_txtImageUrl');
	var image = op.document.getElementById(parentContainer+'_imgImage');
	if(hiddenUrl && image)
	{
		if(hiddenUrl)hiddenUrl.value='/veluxcommon/tools/GetResourceImage.aspx?image='+guid;
		var description = op.document.getElementById(parentContainer+'_taDescription');
		var dessrc = document.getElementById('txtMetaData');
		if(description&&dessrc)
		{
			description.value=dessrc.value;
		}
		
		var iguid = op.document.getElementById(parentContainer+'_txtImageGUID');
		if(iguid)
		{
			iguid.value = guid;
			//alert('imageGuid:'+guid)
		}
		
		var iGalleryGUID = op.document.getElementById(parentContainer+'_txtImageResourceGalleryGUID');
		if(iGalleryGUID&&GalleryGUID)
		{
			iGalleryGUID.value=GalleryGUID;
			//alert('GalleryGUID:'+GalleryGUID)
		}

		var iwidth = op.document.getElementById(parentContainer+'_txtImageWIDTH');
		if(iwidth && width)
		{
			iwidth.value = width;
			//alert('width:'+width)
		}
		
		var iheight = op.document.getElementById(parentContainer+'_txtImageHEIGHT');
		if(iheight && height)
		{
			iheight.value = height;
			//alert('height:'+height)
		}
		


		
		if(image)image.src='/veluxcommon/tools/GetResourceImage.aspx?image='+guid;
		var imageMap = op.document.getElementById(parentContainer+'_MapHTMLData');
		if(imageMap)
		{
			var mapdata='';
			mapdata +='<img src="'+('/veluxcommon/tools/GetResourceImage.aspx?image='+guid)+'"\r\n\t';
			mapdata +='name="'+(parentContainer+'_image')+'"\r\n\t';
			mapdata +='id="'+(parentContainer+'_image')+'"\r\n\t';
			mapdata +='border="0"\r\n\t';
			mapdata +='usemap="#'+(parentContainer+'_imageMAP')+'">\r\n';
			mapdata +='<map NAME="'+(parentContainer+'_imageMAP')+'">\r\n\r\n';
			mapdata +='\t\t<!-- Insert mapdata here -->\r\n\r\n';
			mapdata +='</map>';
			imageMap.value=mapdata;
		}
		top.close();
	}
	else
	{
		var hiddenUrl = op.document.getElementById(parentContainer);
		if(hiddenUrl)
		{
			if(hiddenUrl)hiddenUrl.value='/veluxcommon/tools/GetResourceImage.aspx?image='+guid;
			top.close();
		}
		else
		{
			var objInstance = eWebEditProUtil.getOpenerInstance();
			if (objInstance && objInstance.editor)
			{
				eWebEditProUtil.getOpenerInstance().editor.pasteHTML('<img src="/veluxcommon/tools/GetResourceImage.aspx?image='+guid+'">');
				//objInstance.asyncCallMethod("pasteHTML", ['<sup><small>TM</small></sup>'], null, new Function());		
				//alert('gogo')
				self.close();
			}
			else
			{
				//write nice error message
				alert('Requested action is not available');
			}
		}
	}
}

function openImagepicker(FieldToUpdate,channelguid,ResourceGalleryGuid,selectedImageGUID,filter)
{
	var ifilter ='';
	if(filter)ifilter = filter;
	var res = 'ResourceGallery['+ResourceGalleryGuid+']ResourceGuid['+selectedImageGUID+']';
	var x = window.showModalDialog('/veluxcommon/AUTH/resourcepicker.aspx?refrenceCount='+(Date.UTC(new Date())),res,'dialogHeight:400px;dialogWidth:600px;help:no;status:no;resizable:yes;scrollbars:no;unadorned:yes;');
	var fu = document.getElementById(FieldToUpdate);
	if(fu)
	{
		fu.value= x;
	}
}

function openUploadWindow(guid)
{
	window.open('/VELUXcommon/tools/ImageUpload.aspx?ResourcePath='+guid,'upload','width=500,height=500;location=no,menubar=no,status=no,titlebar=no,scrollbars=no,resizable=no')
}
function openUploadWindowDummy(guid)
{
	window.open('/VELUXcommon/tools/ImageUpload.aspx?ResourcePath='+guid+'&DUMMYUPLOAD=true','upload','width=500,height=500;location=no,menubar=no,status=no,titlebar=no,scrollbars=no,resizable=no')
}
			

function openEditAH(parentALT,parentHREF,altval,hrefval,parentWindowState,winstatearg)
{
	window.open('/VELUXcommon/Tools/ImageEditALTandHREF.aspx?parentALT='+parentALT+'&parentHREF='+parentHREF+'&altval='+altval+'&hrefval='+hrefval+'&parentOWS='+parentWindowState+'&owsArgs='+winstatearg,'EditImageproperties','width=500,height=220;location=no,menubar=no,status=no,titlebar=no,scrollbars=no,resizable=no')
}

function updateALTandHREF(parentALT,parentHREF,altval,hrefval,parentWindowState)
{
	var op = window.opener;
	
	if(op)
	{
		var hidalt  = op.document.getElementById(parentALT);
		var hidhref = op.document.getElementById(parentHREF);
		var thisalt  = document.getElementById(altval)
		var thishref = document.getElementById(hrefval)
		var Selopenmode =  document.getElementById('DropDownList1');
		var newWindow=false;
		if(Selopenmode)
		{
			if(Selopenmode.selectedIndex==1)
				newWindow=true;
		}
		if(Selopenmode && hidalt && hidhref && thisalt && thishref)
		{
			var sOpenwin = op.document.getElementById(parentWindowState);
			if(sOpenwin)
			{
				var targetvalue = Selopenmode.options[Selopenmode.selectedIndex].value;
				if(targetvalue)
				{
					var chkPopScroll = document.getElementById('popscroll');
					var chkPopMenu = document.getElementById('popmenu');
					var chkPopStatus = document.getElementById('popstatus');
					var chkPoprz = document.getElementById('poprz');
					var chkPopLocation = document.getElementById('poplocation');
					var stroptions =''
					if(chkPopScroll && chkPopMenu && chkPopStatus && chkPoprz && chkPopLocation)
					{
						var PopLocation = false;
						var PopScroll = false;
						var PopMenu   = false;
						var PopStatus = false;
						var Poprz = false;
						if(chkPopLocation&&chkPopLocation.checked)PopLocation=true;
						if(chkPoprz&&chkPoprz.checked)Poprz=true;
						if(chkPopScroll&&chkPopScroll.checked)PopScroll=true;
						if(chkPopMenu&&chkPopMenu.checked)PopMenu=true;
						if(chkPopStatus&&chkPopStatus.checked)PopStatus=true;
						stroptions = 'menubar='+(PopMenu?'yes':'no')+',status='+(PopStatus?'yes':'no')+',titlebar=no,scrollbars='+(PopScroll?'yes':'no')+',location='+(PopLocation?'yes':'no')+',resizable='+(Poprz?'yes':'no');
					}
					sOpenwin.value=targetvalue+stroptions;
				}
			}			
			hidalt.value=thisalt.value;
			hidhref.value=thishref.value;
			window.close();
		}
		return false;
	}
	return false;
}

function updatethis(parentALT,parentHREF,altval,hrefval)
{
	var op = window.opener;
	if(op)
	{
		var hidalt  = op.document.getElementById(parentALT);
		var hidhref = op.document.getElementById(parentHREF);
		var winopen = op.document.getElementById(parentALT.replace(/_txtImageAlt/g,'_txtWindowOpen'));
		var objwom  = document.getElementById('WOM');
		
		var thisalt  = document.getElementById(altval);
		var thishref = document.getElementById(hrefval);
		if(hidalt && hidhref && thisalt && thishref && winopen)
		{
			thisalt.value=hidalt.value;
			thishref.value=hidhref.value;
			if(objwom)
			{
				objwom.value = winopen.value;
				//alert('Alt value: '+hidalt.value+'\r\nHREF Value: '+hidhref.value+'\r\nOpenmode:'+winopen.value);
			} 
		
		}
		return false;
	}
	return false;
}

function updateSelectedFiles(chkbox,guid,friendlyname)
{
	var divSelectedFiles = document.getElementById('selectedFiles');
	var objtxtSelectedFiles	 = document.getElementById('txtSelectedFiles');
	if(divSelectedFiles&&objtxtSelectedFiles)
	{
		if(chkbox.checked)
		{
			if(objtxtSelectedFiles.value=='')
			{
				objtxtSelectedFiles.value=guid;
			}
			else
			{
				objtxtSelectedFiles.value+='|'+guid;
			}
		}
		else
		{
			var arr = objtxtSelectedFiles.value.split('|');
			var newarr = new Array();
			for(var i=0;i<arr.length;i++)
			{
				if(arr[i]!=guid)
				{
					newarr.push(arr[i]);
				}
			}
			objtxtSelectedFiles.value =newarr.join('|');
		}
		writeSelectedFilesHtml();
	}
}

function writeSelectedFilesHtml()
{
	var divSelectedFiles = document.getElementById('selectedFiles');
	var objtxtSelectedFiles	 = document.getElementById('txtSelectedFiles');
	if(divSelectedFiles&&objtxtSelectedFiles)
	{
		var selectedArr = objtxtSelectedFiles.value.split('|');
		var insHTML = new Array();
		insHTML.push('<table>');
		var cntr=0;
			insHTML.push('<tr>');
			insHTML.push('<td>');
			insHTML.push('<a href="#" onclick="if(confirm(\'Are you sure you want to move these files?\'))moveFiles();return false;" class="VeluxButton" title="Moves files in the clipboard to the selected folder">Move</a>');
			insHTML.push('</td>');
			insHTML.push('<td>');
			insHTML.push('<a href="#" onclick="if(confirm(\'Are you sure you want to delete these files?\'))deleteFiles();return false;" class="VeluxButton" title="Deletes all selected files permanently">Delete</a>');
			insHTML.push('</td>');
			insHTML.push('</tr>');

		for(var i=0;i<selectedArr.length;i++)
		{
			insHTML.push('<tr>');
			insHTML.push('<td>');
			insHTML.push('<img src="/veluxcommon/tools/GetResourceImage.aspx?image='+selectedArr[i]+'&thumb=true">');
			insHTML.push('</td>');
			insHTML.push('<td>');
			insHTML.push('<a href="#" onclick="removefromselected(\''+selectedArr[i]+'\');return false;" class="VeluxButton" title="Remove this item from clipboard">remove</a>');
			insHTML.push('</td>');
			insHTML.push('</tr>');
			cntr++
		}
		insHTML.push('</table>');
		if(selectedArr.join('')!='')
			divSelectedFiles.innerHTML = insHTML.join('');
		else
			divSelectedFiles.innerHTML = '';
		
		if(window.SelectedFilesHeight||window.SelectedFilesHeight==0)
		{
			if(selectedArr.join('')!='')
			{
				window.SelectedFilesHeight=200;
				divSelectedFiles.style.display='block';
			}
			else
			{
				window.SelectedFilesHeight=0;
				divSelectedFiles.style.display='none';
			}
		}
	}
}

function selectAllFiles(chk)
{
	var chks = document.getElementsByTagName('INPUT');
	for(var i=0;i<chks.length;i++)
	{
		if(chks[i].id.indexOf('chkbx')==0)
		{
			//chks[i].checked = chk.checked;
			chks[i].click();
		}
	}
}

function removefromselected(guid)
{
	var divSelectedFiles = document.getElementById('selectedFiles');
	var objtxtSelectedFiles	 = document.getElementById('txtSelectedFiles');
	if(divSelectedFiles&&objtxtSelectedFiles)
	{
		var arr = objtxtSelectedFiles.value.split('|');
		var newarr = new Array();
		for(var i=0;i<arr.length;i++)
		{
			if(arr[i]!=guid)
			{
				newarr.push(arr[i]);
			}
		}
		//var chk	 = document.getElementById('chkbx' + guid.replace(/{/g,'').replace(/}/g,'') );
		str ='chkbx' + guid.replace(/{/g,'').replace(/}/g,'');
		var items = document.getElementsByTagName('INPUT');
		for(var i=0;i<items.length;i++)
		{
			if(items[i].id==str)
			{
				items[i].checked = false;
				break;
			}
		}
		objtxtSelectedFiles.value =newarr.join('|');
		writeSelectedFilesHtml();
	}
}

function moveFiles()
{
	var act = document.getElementById('txtAction');
	if(act)
	{
		act.value ='MOVEFILES';
		document.forms[0].submit();
		
	}
	
}

function deleteFiles()
{
	var act = document.getElementById('txtAction');
	if(act)
	{
		act.value ='DELETEFILES';
		document.forms[0].submit();
		
	}
	
}

function importRespurces()
{
	var chks = document.getElementsByTagName('INPUT');
	var HTTPDoc = new ActiveXObject("Microsoft.XMLHTTP");
	//document.write(HTTPDoc.responseText);
	var galid = document.getElementById('resourceGalleryId');
	if(galid && chks)
	{
		for(var i=0;i<chks.length;i++)
		{
			if(chks[i].id.indexOf('chkbox')==0 && chks[i].checked)
			{
				//alert('Demo mode: '+chks[i].value);
				//HTTPDoc.open('get','/veluxcommon/dhitest.aspx?ResourceId='+chks[i].value+'&GalleryGuid='+galid.value,false);
				//HTTPDoc.send();
				window.open('/veluxcommon/dhitest.aspx?ResourceId='+chks[i].value+'&GalleryGuid='+galid.value);
				//if(HTTPDoc.responseText!='success')
				//{
				//	alert(HTTPDoc.responseText);
				//}
				//else
				//{
				//	alert('moved');
				//}
			}
		}
	}
}