var IE3 = (bName == "Microsoft Internet Explorer" && bVer >= 4);

//Function to set the NoFile Checkbox
function setNoFileCheckBox(flag, formname)
{
 if(document.forms[formname].elements['nofile'])
 { noFile = document.forms[formname].elements['nofile']; }
 else
 { noFile = ''; }
   noFile.checked = flag;
   
   if (flag == 1)
   {
   	document.forms[formname].elements['browsefile'].value = "";
   	//The format for Catalog item with no File is 'nil'
   	ext= 'nil';
   	formatOptions = document.forms[formname].elements['format'];
	   for ( var i=0,n=formatOptions.length; i<n; i++ )
	   {
	       ext = ext.toLowerCase();
	       optionStr = formatOptions[i].value;
	       optionStr = optionStr.toLowerCase();
	       if (optionStr.indexOf(ext) >= 0)
	       {
		   formatOptions[i].selected = true;
		   break;
	       }
	   }
   }
   
}


//Funtion splits the input parameter based on "\" and then on . to get the filename
function upload_getFilename(val)
{
   var split_array = val.split("\\");
   len = split_array.length;
   if (len == 1)
   {
   		split_array = val.split("/");	
   }
   
   len = split_array.length;
   fn = split_array[len-1];
   ext_array = fn.split(".");
   len = ext_array.length;
   //This is required as the filename itself could contain fullstop
   return fn.replace("."+ext_array[len-1], "");
}

//Funtion splits the input filename and gets the extension
function upload_getFileExtension(val)
{
   var split_array = val.split("\\");
   if (len == 1)
   {
     split_array = val.split("/");	
   }
   len = split_array.length;
   fn = split_array[len-1];
   ext_array = fn.split(".");
   len = ext_array.length;
   a_ext = ext_array[len-1];
   actext = a_ext.split("\\");
   getFilename = upload_getFilename(val);
   if(actext.length == 1 && getFilename!==actext[length])
   { return actext[length]; }
   else
   { return ''; }
}

//Funtion sets the Title and File extension based on filename selected by user.
//REQUIREMENT: The input type text for Title must be "title"
//REQUIREMENT: The input type select for Title must be "format"
//REQUIREMENT: You must supply the form name and it must be unique
function updateUploadTextBox(val, formname)
{
   filename = upload_getFilename(val);
   ext = upload_getFileExtension(val);
   if(document.forms[formname].elements['title'])
   {
   	document.forms[formname].elements['title'].value = filename;
   }
   
   if(document.forms[formname].elements['format'])
   {
   	formatOptions = document.forms[formname].elements['format'];
   }
   else
   {
   	formatOptions = '';
   }

   if (formatOptions!= '')
   {
	   for ( var i=0,n=formatOptions.length; i<n; i++ )
	   {
		   ext = ext.toLowerCase();
		   optionStr = formatOptions[i].innerHTML;
		   optionStr = optionStr.toLowerCase();
		   
		   if (optionStr.indexOf(ext) >= 0)
		   {
			   formatOptions[i].selected = true;
			   break;
		   }
	   }
	}
   //Now, set the nofile option to false
   setNoFileCheckBox(0, formname)
}


function updateUploadCusomTextBox(val, formname)
{
   filename = upload_getFilename(val);
   ext = upload_getFileExtension(val);
   if(document.forms[formname].elements['title'])
   {
   	document.forms[formname].elements['title'].value = filename;
   }
   
   if(document.forms[formname].elements['format'])
   {
   	formatOptions = document.forms[formname].elements['format-selection'];
   }
   else
   {
   	formatOptions = '';
   }

   if (formatOptions!= '')
   {
	   for ( var i=0,n=formatOptions.length; i<n; i++ )
	   {
		   ext = ext.toLowerCase();
		   optionStr = formatOptions[i].innerHTML;
		   optionStr = optionStr.toLowerCase();
		   
		   if (optionStr.indexOf(ext) >= 0)
		   {
			   formatOptions[i].selected = true;
			   break;
		   }
		   else
		   {
			   formatOptions[0].selected = true;
		   }
	   }
	}
	document
   //Now, set the nofile option to false
   document.getElementById('format').value= ext;
   setNoFileCheckBox(0, formname)
}

function updateUploadCusomTextBox(val, formname)
{
   filename = upload_getFilename(val);
   ext = upload_getFileExtension(val);
   if(document.forms[formname].elements['title'])
   {
   	document.forms[formname].elements['title'].value = filename;
   }
   
   if(document.forms[formname].elements['format'])
   {
   	formatOptions = document.forms[formname].elements['format-selection'];
   }
   else
   {
   	formatOptions = '';
   }

   if (formatOptions!= '')
   {
	   for ( var i=0,n=formatOptions.length; i<n; i++ )
	   {
		   ext = ext.toLowerCase();
		   optionStr = formatOptions[i].innerHTML;
		   optionStr = optionStr.toLowerCase();
		   
		   if (optionStr.indexOf(ext) >= 0)
		   {
			   formatOptions[i].selected = true;
			   break;
		   }
		   else
		   {
			   formatOptions[0].selected = true;
		   }
	   }
	}
	document
   //Now, set the nofile option to false
   document.getElementById('format').value= ext;
   setNoFileCheckBox(0, formname)
}

var rowid;
var userfullname;
var userid;

rowid = 0;
userfullname = '';
userid = '';

function deleteCurrentId(deleteidurl,targetblock, msgstr)
{
	if (rowid!==0)
	{
		var row = rowid.split('-')
		itemid = row[0];
		var result = confirm(msgstr);
		if (result == true)
			{
				if (itemid != 0)
				{
					if(row[1]=='ci')
					{
						ajaxpage(deleteidurl + itemid +'/ci/', targetblock);
					}
					else
					{
						ajaxpage(deleteidurl + itemid +'/ui/', targetblock);
					}
				}
				else
				{
					alert("Please select File to Delete.")
				}
			}
			else
			{
				return false;
			}
	}
	else
	{
		alert('Click on Item row/grid and select the Item first');
		return false;
	}
}

function RemoveMeCurrentId(deleteidurl,targetblock, msgstr,favid)
{
	var row = favid.split('-')
	itemid = row[0];
	{
		var result = confirm(msgstr);
		
		if (result == true)
		{
			if (itemid != 0)
			{
				if(row[1]=='ci')
				{
					if(IE3)
					{
						window.location.href = deleteidurl + itemid +'/ci/';
						window.location.reload();
					}
					else
					{
						window.location = deleteidurl + itemid +'/ci/';
					}
				}
				else
				{
					if(IE3)
					{
						window.location.href = deleteidurl + itemid +'/ui/';
						window.location.reload();
					}
					else
					{
						window.location = deleteidurl + itemid +'/ui/';
					}
				}
			}
			else
			{
				alert("Please select File to Delete.");
			}
		}
		else
		{

		}

	}
}

lastfolderselection = 0;

function changecolor(listitemid,foldertype)
{	
	if (foldertype == 'Folders')
		{ color = '#F8F08D'; }
	else if (foldertype == 'Projects')
		{ color = '#FDBBBB';}
	else if (foldertype == 'Lightbox')
		{ color = '#BBBBFD'; }
		
	if (lastfolderselection != 0)
	{
		flag = document.getElementById(lastfolderselection);
		if (flag != null)
		{
			if(document.getElementById(lastfolderselection))
			{ document.getElementById(lastfolderselection).style.backgroundColor = 'transparent'; }
		}
		lastfolderselection = listitemid;
		if(document.getElementById(listitemid))
		{  document.getElementById(listitemid).style.backgroundColor = color ; }
	}
	else
	{
		if (document.getElementById(listitemid))
		{ document.getElementById(listitemid).style.backgroundColor = color; }
		lastfolderselection = listitemid;
	}
	rowid=0;
	setUIOptionsDefault();
}

	function showDetails(ulid)
	{
		document.getElementById(ulid).style.display = 'block';
	}
	function hideDetails(ulid)
	{
		document.getElementById(ulid).style.display = 'none';
	}

	function highlightme(currentrowid)
	{
		if (rowid != 0)
		{
			flag = document.getElementById(rowid);
			if (flag != null)
			{
				document.getElementById(rowid).style.backgroundColor = 'transparent';
			}
			rowid = currentrowid;
			document.getElementById(currentrowid).style.backgroundColor = '#D9D9FF';
		}
		else
		{
			document.getElementById(currentrowid).style.backgroundColor = '#D9D9FF';
			rowid = currentrowid;
		}
		return rowid;
	}


function setUserInfo(targetfulltext,targetid)
{
	userfullname = targetfulltext;
	userid = targetid;
}

function assignMe(megstring)
{
	var fullnametarget = window.opener.window.userfullname;
	var useridtarget = window.opener.window.userid;
	 if(rowid!==0)
	 {
		var row = rowid.split('-')
		itemid = row[0];
		getFullname = itemid+'-FullName';
		fullname = document.getElementById(getFullname).innerHTML;

		window.opener.window.document.getElementById(fullnametarget).value = fullname;
		window.opener.window.document.getElementById(useridtarget).value = itemid;
		window.close();
	 }
	 else
	 {
		alert(megstring);
		return false;
	 }
}


function createCopyFolder(crtcopyfld,targetblock)
{
	if(lastfolderselection != 0)
	{
		var fldid = lastfolderselection.split('-')
		folderid = fldid[0];
		if (folderid != 0)
		{
			ajaxpage(crtcopyfld + folderid +'/', targetblock);
		}
	}
	else
	{
		alert('Click on Folder Name and select the Folder First');
	}
}


function copyUserFolderItem(copyfldurl)
{

	var row = rowid.split('-')
	itemid = row[0];
	{
		if (itemid != 0)
		{
			if(row[1]=='ci')
			{
				var link = copyfldurl + itemid +'/ci/';
				window.open(link);							}
			else
			{
				var link = copyfldurl + itemid +'/ui/';
				window.open(link);				
			}
		}
		else
		{
			alert("Please select File to Delete.")
		}
	}
}

//Function gets the current user id selected and makes a GET request
function inviteUser(url, containerid, messagestr)
{
	if (rowid !== 0)
	{
		var page_request = false;
	    var params = "userid="+rowid;
		if (window.XMLHttpRequest) // if Mozilla, Safari etc
			page_request = new XMLHttpRequest()
		else if (window.ActiveXObject)
		{ // if IE
			try 
			{
				page_request = new ActiveXObject("Msxml2.XMLHTTP")
			}
			catch (e)
			{
				try
				{
					page_request = new ActiveXObject("Microsoft.XMLHTTP")
				}
				catch (e)
				{}
			}
		}
		else
			return false
		
		page_request.onreadystatechange=function()
		{
			if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
				document.getElementById(containerid).innerHTML=page_request.responseText
			return;
		}
		
		page_request.open("POST",url,true);
		page_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	    page_request.send(params);
	    rowid = 0;
	}
	else
	{
		alert(messagestr);
		return false;
	}	
	return;
}

// FUNCTION IS USED TO VALIDATE FOLDER NAME
// This function will accept the valid url and the form object 
// for the particular url.
function newfoldervalidation(passurl,formobj)
{
// Checking for Foldername is not Empty and If not empty folder
// name is not Default Folder.
	
	var validation = true;
//	var durval = document.getElementById('duration').value;
	var vdur = /^([0-9])+$/;


	if (document.getElementById('foldername').value != '' && document.getElementById('foldername').value != 'Default Folder'  && document.getElementById('foldername').value != 'Deleted Items')
	{
		// Executing url with form object after validation is true. 
		document.getElementById('foldername-msg').style.display = 'none';
		validation = true;

//		if(!vdur.test(durval))
//		{
//			validation = false;
//		}
//		if (durval != 'No Expiry' && validation != true && durval != ' ')
//		{
//			document.getElementById('duration-msg1').style.display = 'block';
//			validation = false;
//		}
//		else
//		{
//			document.getElementById('duration-msg1').style.display = 'none';
//			validation = true;
//		}
	}
	else
	{
		// Showing Error Message in template which is in Span statement.		
		document.getElementById('foldername-msg').style.display = 'block';
		validation = false;
	}
	
	if (validation == true )
	{
		postajaxpage(passurl, formobj ,'myfolderblock');
	}
}

function setDownloadLink(copyfldurl, itemid)
{
	var row = rowid.split('-')
	itemid = row[0];
	{
		if (itemid != 0)
		{
			if(row[1]=='ci')
			{
				var link = copyfldurl + itemid+'/';
				document.getElementById('downloaditem').href = link;
			}
			else
			{
				var link = copyfldurl + itemid+'/';
				document.getElementById('downloaditem').href = link;
			}
		}
		else
		{
			alert("Please select File to Delete.")
		}
	}
}

function setUploadAddDOLink(copyfldurl, itemid)
{
	var row = rowid.split('-')
	itemid = row[0];
	{
		if (itemid != 0)
		{
			if(row[1]=='ci')
			{
				var link = "JavaScript:showDialog('"+copyfldurl + itemid  +"/ci/','AddDo','600','400','yes')";
				document.getElementById('uploadandaddfile').href = '';
				document.getElementById('uploadandaddfile').href = link;
			}
			else
			{
				var link = "JavaScript:showDialog('"+copyfldurl + itemid +"/ui/','AddDo','600','400','yes')";
				document.getElementById('uploadandaddfile').href = '';
				document.getElementById('uploadandaddfile').href = link;
			}
		}
		else
		{
			alert("Please select Item to Upload and Add File.")
		}
	}
}

function setOverwriteDOLink(copyfldurl, itemid)
{
	var row = rowid.split('-')
	itemid = row[0];
	{
		if (itemid != 0)
		{
			if(row[1]=='ci')
			{
				var link = "JavaScript:showDialog('"+copyfldurl + itemid  +"/ci/','AddDo','600','515','yes')";
				document.getElementById('overwritefile').href = '';
				document.getElementById('overwritefile').href = link;
			}
			else
			{
				var link = "JavaScript:showDialog('"+copyfldurl + itemid +"/ui/','AddDo','600','515','yes')";
				document.getElementById('overwritefile').href = '';
				document.getElementById('overwritefile').href = link;
			}
		}
		else
		{
			alert("Please select Item to Overwrite File.")
		}
	}
}

function setVersionDOLink(copyfldurl, itemid)
{

	if(document.getElementById('editdometadata'))
	{
		document.getElementById('editdometadata').onclick = '';
	}
	if(rowid!==0)
	{
		var row = rowid.split('-')
		itemid = row[0];
		if (itemid != 0)
		{
			if(row[1]=='ci')
			{
				var link = "JavaScript:showDialog('"+copyfldurl + itemid  +"/ci/','AddDo','600','400','yes')";
				document.getElementById('versionfile').href = '';
				document.getElementById('versionfile').href = link;
			}
			else
			{
				var link = "JavaScript:showDialog('"+copyfldurl + itemid +"/ui/','AddDo','600','400','yes')";
				document.getElementById('versionfile').href = '';
				document.getElementById('versionfile').href = link;
			}
		}
		else
		{
			alert("Please select Item to Version File.")
		}
	}
	else
	{
		return false;
	}
}

function setEditDOMetadataLink(copyfldurl, itemid)
{
	if(document.getElementById('editdometadata'))
	{
		document.getElementById('editdometadata').onclick = "";
	}

	var row = rowid.split('-')
	itemid = row[0];
	{
		if (itemid != 0)
		{
			if(row[1]=='ci')
			{
				var link = "JavaScript:showDialog('"+copyfldurl + itemid  +"/ci/','AddDo','670','440','yes')";
				document.getElementById('editdometadata').href = '';
				document.getElementById('editdometadata').href = link;
			}
			else
			{
				var link = "JavaScript:showDialog('"+copyfldurl + itemid +"/ui/','AddDo','670','440','yes')";
				document.getElementById('editdometadata').href = '';
				document.getElementById('editdometadata').href = link;
			}
		}
		else
		{
			alert("Please select Item to Edit Metadata.")
		}
	}
}

function setMoveFileLink(copyfldurl, itemid)
{

	if(document.getElementById('moveitem'))
	{
		document.getElementById('moveitem').onclick = '';
	}

	var row = rowid.split('-')
	itemid = row[0];
	{
		if (itemid != 0)
		{
			if(row[1]=='ci')
			{
				var link = "JavaScript:ajaxpage('"+copyfldurl + itemid  +"/ci/','item')";
				document.getElementById('moveitem').href = '';
				document.getElementById('moveitem').href = link;
			}
			else
			{
				var link = "JavaScript:ajaxpage('"+copyfldurl + itemid +"/ui/','item')";
				document.getElementById('moveitem').href = '';
				document.getElementById('moveitem').href = link;
			}
		}
		else
		{
			alert("Please select File to Move.")
		}
	}
}

function setMakeCILink(fldurl, itemid)
{
	if(document.getElementById('makeci'))
	{
		document.getElementById('makeci').onclick = '';
	}
	var row = rowid.split('-')
	itemid = row[0];
	{
		if (itemid != 0)
		{
			if(row[1]=='ui')
			{
				var link = "JavaScript:showDialog('"+fldurl + itemid +"/','uitociUpload','690','630','yes')";
				if(document.getElementById('makeci'))
				{
					document.getElementById('makeci').href = '';
					document.getElementById('makeci').href = link;
				}
			}
		}
		else
		{
			alert("Please select File to Move.")
		}
	}
}

function setMessageLink(messagefldurl, itemid)
{

	if(document.getElementById('sendmessage'))
	{
		document.getElementById('sendmessage').onclick = '';
	}

	var row = rowid.split('-')
	itemid = row[0];
	{
		if (itemid != 0)
		{
			if(row[1]=='ci')
			{
				var link = "JavaScript:showDialog('"+messagefldurl + itemid  +"/ci/','showSendMessage','600','370','yes');";
				document.getElementById('sendmessage').href = link;
			}
			else
			{
				var link = "JavaScript:showDialog('"+messagefldurl + itemid +"/ui/','showSendMessage','600','370','yes');" ;
				document.getElementById('sendmessage').href = link;
			}
		}
		else
		{
			alert("Please select File to Delete.")
		}
	}
}




var win= null;

function showDialog(mypage,myname,w,h,scroll){
var winl = (screen.width-w)/2;
var wint = (screen.height-h)/2;
var settings ='height='+h+',';
settings +='width='+w+',';
settings +='top='+wint+',';
settings +='left='+winl+',';
settings +='scrollbars='+scroll+',';
settings +='resizable=yes';
win=window.open(mypage,myname,settings);
if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}

function MM_openBrWindow(theURL,winName,features) {
window.open(theURL,winName,features);
}

var win1= null;

function printReport(mypage,myname,w,h,scroll){
var winl = (screen.width-w)/2;
var wint = (screen.height-h)/2;
var settings ='height='+h+',';
settings +='width='+w+',';
settings +='top='+wint+',';
settings +='left='+winl+',';
settings +='scrollbars='+scroll+',';
settings +='resizable=yes,';
settings +='toolbar=yes,';
settings +='menubar=yes';
win1=window.open(mypage,myname,settings);
if(parseInt(navigator.appVersion) >= 4){win1.window.focus();}
}

function MM_openBrWindow(theURL,winName,features) {
window.open(theURL,winName,features);
}

var win2= null;

function printMessage(mypage,myname,w,h,scroll){
var winl = (screen.width-w)/2;
var wint = (screen.height-h)/2;
var settings ='height='+h+',';
settings +='width='+w+',';
settings +='top='+wint+',';
settings +='left='+winl+',';
settings +='scrollbars='+scroll+',';
settings +='resizable=yes,';
settings +='toolbar=yes,';
settings +='menubar=yes';
win2=window.open(mypage,myname,settings);
if(parseInt(navigator.appVersion) >= 4){win2.window.focus();}
}

function MM_openBrWindow(theURL,winName,features) {
window.open(theURL,winName,features);
}

function runoption(action,option)
{
	var acont = action.substring(4,0);
	if(action!='none')
	{
		if (acont=='http')
		{
			window.open(action);
		}
		else
		{
			eval(action)
		}
	}
	else
	{
		return false;
	}
}


function dcmsnewfoldervalidation(passurl,formobj)
{
// Checking for Foldername is not Empty and If not empty folder
// name is not Default Folder.
	
	var validation = true;
//	var durval = document.getElementById('duration').value;
	var vdur = /^([0-9])+$/;


	if (document.getElementById('foldername').value != '' && document.getElementById('foldername').value != 'Default Folder'  && document.getElementById('foldername').value != 'Deleted Items')
	{
		// Executing url with form object after validation is true. 
		document.getElementById('foldername-msg').style.display = 'none';
		validation = true;

//		if(!vdur.test(durval))
//		{
//			validation = false;
//		}
//		if (durval != 'No Expiry' && validation != true && durval != ' ')
//		{
//			document.getElementById('duration-msg1').style.display = 'block';
//			validation = false;
//		}
//		else
//		{
//			document.getElementById('duration-msg1').style.display = 'none';
//			validation = true;
//		}
	}
	else
	{
		// Showing Error Message in template which is in Span statement.		
		document.getElementById('foldername-msg').style.display = 'block';
		validation = false;
	}
	
	if (validation == true )
	{
		postajaxpage(passurl, formobj ,'selector-user-folders');
	}
}

function seturl(target,surl)
{
	document.getElementById(target).value = surl;
}

function ClearValue(target)
{
	document.getElementById(target).value='';
}

function clearme(target)
{
	document.getElementById(target).innerHTML='';
}

function showme(target)
{
	document.getElementById(target).style.display='block';
}

function hideme(target)
{
	document.getElementById(target).style.display='none';
}

function makebold(listitemid,foldertype)
{   
  if (foldertype == 'Folders')
    { color = '#F8F08D'; }
  else if (foldertype == 'Projects')
    { color = '#FDBBBB';}
  else if (foldertype == 'Lightbox')
    { color = '#BBBBFD'; }
  if (lastfolderselection != 0)
    {
      flag = document.getElementById(lastfolderselection);
      if (flag != null)
      {
      if(document.getElementById(lastfolderselection))
        { document.getElementById(lastfolderselection).style.fontWeight = 'normal'; }
      }
      lastfolderselection = listitemid;
      if(document.getElementById(listitemid))
        {  document.getElementById(listitemid).style.fontWeight = 'bold' ; }
    }
  else
    {
    if (document.getElementById(listitemid))
      { document.getElementById(listitemid).style.fontWeight = 'bold'; }
        lastfolderselection = listitemid;
      }
}

function setUIOptionsDefault()
{
	if(document.getElementById('editdometadata'))
		{document.getElementById('editdometadata').href="javascript:alert('Click on Item row/grid and select the Item first');";
		 }
	
	if(document.getElementById('downloaditem'))
		{document.getElementById('downloaditem').href="javascript:alert('Click on Item row/grid and select the Item first');";}
	
	if(document.getElementById('moveitem'))
		{document.getElementById('moveitem').href="javascript:alert('Click on Item row/grid and select the Item first');";}
	
	if(document.getElementById('makeci'))
		{document.getElementById('makeci').href="javascript:alert('Click on Item row/grid and select the Item first');";}
	
	if(document.getElementById('sendmessage'))
		{document.getElementById('sendmessage').href="javascript:alert('Click on Item row/grid and select the Item first');";}
}

