// merrazquin 3/10/2008 TODO: Change for production
//var rootDir = "http://www.digitecdev.net/digitecinteractive/"


var rootDir = "http://kdcms.knowledgedirectweb.com/"
function assignedcontent(i)  {
	contWin = window.open('assignedcontent.cfm?cid='+i,'','scrollbars=yes,top=0,left=0,width=490,height=450');
	window.contWin.focus();
}

function channelDesc(m)  {
	descWin = window.open('channeldesc.cfm?cid='+m,'','scrollbars=yes,top=0,left=0,width=495,height=500');
	window.descWin.focus();
}

function charAlert(n,field)  {
	var textField = field;
	
	if(textField.value.length > n)  {
		textField.value= textField.value.substring(0,n)
		
		textField.blur()
		
		alert("No more than " + n + " characters can be entered")
	}
}

function checkzeroORDERDEPT(n)  {
	var c;
	c = document.admin.deptidCNT.length - 1;
	for (i = 0; i <= c ; i++)  {
		if (document.admin.deptidCNT[i].value == document.admin.deptidCNT[n].value && i != n && document.admin.deptidCNT[n].value != 0)  {
			alert('This order number is already being used.  Please pick another order number.');
			document.admin.deptidCNT[n].value = 0;
			break;
		}
	}
}

function contenttype(contentid, contenttype)  {
	if(contentid != "")  {
		switch(contenttype)  {
			case "A":
			document.getElementById('loading').style.display = "none";
			document.getElementById('filechoice').style.display = "none";
			document.getElementById('documentchoice').style.display = "none";
			document.getElementById('articlechoice').style.display = "block";
			break;
			case"F":
			document.getElementById('loading').style.display = "none";
			document.getElementById('articlechoice').style.display = "none";
			document.getElementById('documentchoice').style.display = "none";
			document.getElementById('filechoice').style.display = "block";
			break;
			default:
			document.getElementById('loading').style.display = "none";
			document.getElementById('articlechoice').style.display = "none";
			document.getElementById('filechoice').style.display = "none";
			document.getElementById('documentchoice').style.display = "block";
			break;
		}
		
		document.getElementById('doneBtn').style.display = "";
		document.getElementById('deleteBtn').style.display = "";
		
	}
}

function copyClip(theText)  {
	if (window.clipboardData)  {
		window.clipboardData.setData("Text", theText);
	}
	else alert("This feature not available in the current browser");
}

function deleteAdmin(fuid)  {
	if ( confirm('Are you sure you want to delete this administrator?') )  {
		document.location.href='2070.cfm?uid='+fuid+'&act=D';
	}
}

function deleteChannel(channelid)  {
	if ( confirm('Are you sure you want to delete this channel?') )  {
		document.location.href='3100.cfm?cid='+channelid+'&act=D';
	}
}

function deleteCompany(cmpid)  {
	if ( confirm('Are you sure you want to delete this company?') )  {
		document.location.href='2100.cfm?cid='+cmpid+'&act=D';
	}
}

function deleteContent(contenttype, contentid)  {
	var confirmed = true;
	if(contenttype == "A")
	confirmed = confirm('Are you sure you want to delete this article?');
	else if (contenttype == "F")
	confirmed = confirm('Are you sure you want to delete this file?');
	
	if(confirmed)  {
		document.location.href='4250.cfm?cid='+contentid+'&ctype='+contenttype;
		
	}
}

function deleteContentManager(uid)  {
	if ( confirm('Are you sure you want to delete this content manager?') )  {
		document.location.href='4150.cfm?uid='+uid+'&act=D';
	}
}

function deleteDepartment(deptid)  {
	if ( confirm('Are you sure you want to delete this Department?') )  {
		document.location.href='2560.cfm?deptid='+deptid+'&act=D';
	}
}

function deptDesc(m)  {
	descWin = window.open('index.cfm?deptid='+m,'','resizable=yes, scrollbars=yes,top=0,left=0,width=800,height=600');
	window.descWin.focus();
}

function disableUser(uid)  {
	if ( confirm('Are you sure you want to disable this user?') )  {
		document.location.href='6030.cfm?uid='+uid+'&act=D';
	}
}

function enableUser(uid)  {
	document.location.href='6030.cfm?uid='+uid+'&act=E';
}

function ezineSignUp(deptid)  {
	if($('#ezineSignup').is(":hidden"))
		$('#ezineSignup').slideDown('slow');
	else
		$('#ezineSignup').slideUp('slow');
	//window.open(rootDir+'ezinesignup.cfm?deptid='+(deptid != undefined ? deptid: ''),'','scrollbars=1,resizable=1,width=650,height=350,menubar=0,toolbar=0,location=0');
}

function fileContent(i,s,c,e)  {
	var urllink;
	if (s != '')  {
		urllink = 'company' + i + '/content/' + c + '/' + s;
	}
	else  {
		urllink = e;
	}
	window.open(urllink,'','scrollbars=1,resizable=1,menubar=0,toolbar=0,location=0');
}

function forgotLogin()  {
	window.open(rootDir+'forgotLogin.cfm','','scrollbars=1,resizable=1,width=800,height=350,menubar=0,toolbar=0,location=0');
}

function getAdmin()  {
	var uid = document.admin.userid.options[document.admin.userid.selectedIndex].value;
	document.location.href= '2070.cfm?uid=' + uid
}

function getChannel()  {
	var cid = document.admin.channelid.options[document.admin.channelid.selectedIndex].value;
	document.location.href= '3100.cfm?cid=' + cid
}

function getCompany()  {
	var cid = document.admin.companyid.options[document.admin.companyid.selectedIndex].value;
	document.location.href= '2100.cfm?cid=' + cid
	
}

function getContentManager()  {
	var uid = document.admin.userid.options[document.admin.userid.selectedIndex].value;
	document.location.href= '4150.cfm?uid=' + uid
}

function getDepartment()  {
	var deptid = document.admin.deptid.options[document.admin.deptid.selectedIndex].value;
	document.location.href= '2560.cfm?deptid=' + deptid
	
}

function getDepartmentEdit()  {
	var deptid;
	deptid = document.admin.departmentid.options[document.admin.departmentid.selectedIndex].value;
	document.location.href= '3150.cfm?deptid=' + deptid
}

function switchDeptBanner()  {
	var deptid = document.admin.deptid.options[document.admin.deptid.selectedIndex].value;
	document.location.href= '4385.cfm?deptid=' + deptid
}



function getMember()  {
	var uid;
	uid = document.admin.userid.options[document.admin.userid.selectedIndex].value;
	document.location.href= '6030.cfm?uid=' + uid
}

function preview(deptid)  {
	previewWin = window.open('index.cfm?deptid='+deptid,'','scrollbars=yes,top=0,left=0,width=1024,height=768');
	window.descWin.focus();
}

function setContentType(contenttype)  {
	switch(contenttype)  {
		case "F":
		document.getElementById('loading').style.display = "none";
		document.getElementById('articlechoice').style.display = "none";
		document.getElementById('documentchoice').style.display = "none";
		document.getElementById('filechoice').style.display = "block";
		document.getElementById('doneButton').style.display = "";
		document.getElementById('deleteBtn').style.display = "";
		break;
		
		case "A":
		document.getElementById('loading').style.display = "none";
		document.getElementById('filechoice').style.display = "none";
		document.getElementById('documentchoice').style.display = "none";
		document.getElementById('articlechoice').style.display = "block";
		document.getElementById('doneButton').style.display = "";
		document.getElementById('deleteBtn').style.display = "";
		break;
		
		case "D":
		document.getElementById('loading').style.display = "none";
		document.getElementById('filechoice').style.display = "none";
		document.getElementById('articlechoice').style.display = "none";
		document.getElementById('documentchoice').style.display = "block";
		document.getElementById('doneButton').style.display = "";
		document.getElementById('deleteBtn').style.display = "";
		break;
	}
}

function switchChannel()  {
	i = document.admin.channelid.options[document.admin.channelid.selectedIndex].value;
	document.location.href='4350.cfm?cid='+i
}

function switchChannelVideo()  {
	i = document.admin.channelid.options[document.admin.channelid.selectedIndex].value;
	document.location.href='4355.cfm?cid='+i
}

function switchChannelGraphic()  {
	i = document.admin.channelid.options[document.admin.channelid.selectedIndex].value;
	document.location.href='4365.cfm?cid='+i
}

function switchChannelBanner()  {
	i = document.admin.channelid.options[document.admin.channelid.selectedIndex].value;
	document.location.href='4375.cfm?cid='+i
}

function switchChannelLink()  {
	i = document.admin.channelid.options[document.admin.channelid.selectedIndex].value;
	document.location.href='4400.cfm?cid='+i;
}

function switchContentLink()  {
	i = document.admin.contentid.options[document.admin.contentid.selectedIndex].value;
	document.location.href='4500.cfm?cid='+i
}


function switchContent()  {
	i = document.admin.contentid.options[document.admin.contentid.selectedIndex].value;
	document.location.href='4250.cfm?cid='+i
}

function switchContentAssign()  {
	i = document.admin.contentid.options[document.admin.contentid.selectedIndex].value;
	document.location.href='4360.cfm?cid='+i
}

function switchDisplay()  {
	var ch = document.admin.contenttype.options[document.admin.contenttype.selectedIndex].value;
	switch(ch)  {
		case "A":
		document.getElementById('loading').style.display = "none";
		document.getElementById('filechoice').style.display = "none";
		document.getElementById('documentchoice').style.display = "none";
		document.getElementById('articlechoice').style.display = "block";
		document.getElementById('doneButton').style.display = "";
		break;
		
		case "F":
		document.getElementById('loading').style.display = "none";
		document.getElementById('articlechoice').style.display = "none";
		document.getElementById('documentchoice').style.display = "none";
		document.getElementById('filechoice').style.display = "block";
		document.getElementById('doneButton').style.display = "";
		break;
		
		case "D":
		document.getElementById('loading').style.display = "none";
		document.getElementById('articlechoice').style.display = "none";
		document.getElementById('filechoice').style.display = "none";
		document.getElementById('documentchoice').style.display = "block";
		document.getElementById('doneButton').style.display = "";
		break;
		
		case "":
		document.getElementById('articlechoice').style.display = "none";
		document.getElementById('filechoice').style.display = "none";
		document.getElementById('documentchoice').style.display = "none";
		document.getElementById('loading').style.display = "none";
		document.getElementById('doneButton').style.display = "none";
		break;
	}
}

function uploadZip()  {
	zipWin = window.open('uploadzip.cfm','uploadzip','width=350,height=250,top=0,left=0,scrollbars=no');
	zipWin.focus();
}

function checkzeroNew(cbid,eSelect, n, numAvail) {
	var c = document.admin[cbid].length;
	var eCheck = document.admin[cbid][n];
	var positions = getPositions(numAvail, c);
	var numChecked = getChecked(cbid);
	if (eSelect.value == 0) {
		eCheck.checked = false;
		fillIn(cbid,numAvail);
	}
	else eCheck.checked = true;

	eCheck.previousSibling.src = eCheck.checked ? imgTrue : imgFalse;
	killDupes(cbid, n, numAvail);
}

function checkSortNew(cbid, n, numAvail){
	var c = document.admin[cbid].length;
	var positions = getPositions(numAvail,c); 
	var numChecked = getChecked(cbid);
	
	if((numAvail > 1 && !document.admin[cbid][n].checked) || (numAvail == 1 && !document.admin[cbid].checked)) {
		document.admin.sortorder[n].value = 0;
		positions = getPositions(numAvail,c);
		// merrazquin 1/2/2008 need to re-order so that there are no "holes"
		fillIn(cbid,numAvail);
	}
	if((numAvail > 1 && document.admin[cbid][n].checked && document.admin.sortorder[n].value == 0) || (numAvail == 1 && document.admin[cbid].checked && document.admin.sortorder.value == 0)){
		// merrazquin 1/2/2008 find next highest order num
		for(var i = 0; i < positions.length; i++){
			if(positions[i] == 0 && !document.admin.sortorder[n].disabled && document.admin[cbid][n].checked){
				document.admin.sortorder[n].value = (i+1);
				break;
			}
		}
	}
}

function killDupes(cbid, n, numAvail){
	var c = document.admin[cbid].length;
	var positions = getPositions(numAvail,c); 
	var numChecked = getChecked(cbid);

	var dupes = 0;
	for(var i = 0; i < c; i++){
		if(!document.admin.sortorder[i].disabled){
			for(var j = i+1; j < c; j++){
				if(!document.admin.sortorder[j].disabled){
					if(document.admin.sortorder[i].value == document.admin.sortorder[j].value){
						dupes++;
						if(n < j) {
							document.admin.sortorder[j].value = 0;
						} else { 
							document.admin.sortorder[i].value = 0;
						}
					}
				}
			}
		}
	}
	if(dupes) fillIn(cbid,numAvail);
}

function fillIn(cbid, numAvail){
	var numChecked = getChecked(cbid);
	var c = document.admin[cbid].length;
	var positions = getPositions(numAvail,c); 
	var missing = new Array();
	for(var i = 0; i < numChecked; i++){
		if(positions[i] == 0){
			var found = false;
			for(var j = 0; j < c; j++){
				if(document.admin.sortorder[j].value == (i+1)){
					found = true;
				} else found = false;
			}
			if(!found) missing.push(i+1);
		}
	}
	for(var i = 0; i < missing.length; i++){
		var lowPos = 0;
		var low = numAvail;
		for(var j=0; j < c; j++){
			if(!document.admin.sortorder[j].disabled && document.admin[cbid][j].checked){
				if((document.admin.sortorder[j].value == 0 || document.admin.sortorder[j].value > missing[i]) && document.admin.sortorder[j].value <= low){
					low = document.admin.sortorder[j].value;
					lowPos = j;
				}
			}
		}
		if(!document.admin.sortorder[lowPos].disabled && document.admin[cbid][lowPos].checked){
			document.admin.sortorder[lowPos].value = missing[i];
		}
	}

	missing = new Array();
	for(var i = 0; i < numChecked; i++){
		if(positions[i] == 0){
			var found = false;
			for(var j = 0; j < c; j++){
				if(document.admin.sortorder[j].value == (i+1)){
					found = true;
				} else found = false;
			}
			if(!found) missing.push(i+1);
		}
	}
	if(missing.length) fillIn(cbid,numAvail);
}

function getPositions(numAvail, c){
	var positions = new Array(numAvail-1);
		for(var i = 0; i < numAvail; i++){
			positions[i] = 0;
		}
		for(var i = 0; i < c; i++){
			if(document.admin.sortorder[i].value != 0) {
				positions[document.admin.sortorder[i].selectedIndex-1] = 1;
			}
	}
	return positions;
}

function getChecked(cbid){
	var c = document.admin[cbid].length;
	var count = 0;
	for(var i = 0; i < c; i++)
		if(document.admin[cbid][i].checked) count++;
	return count;
}

function toggleEmailForm(){
	if($('#email').is(":hidden"))
		$('#email').slideDown('slow');
	else
		$('#email').slideUp('slow');
}

function addRecipient(parent){
  var newrecip = document.createElement('div');
  newrecip.innerHTML = '<br/><input type="text" name="mailTo" value=""><br/>';

  var newbutton = document.createElement('div');
  newbutton.setAttribute("id","mailSend");
  newbutton.setAttribute("style","display:block;width:100%;text-align:right");
  newbutton.style.display = "block";
  newbutton.style.width = "100%";
  newbutton.style.textAlign = "right";
  newbutton.innerHTML = '<br/><input type="image" src="'+rootDir+'images/send_btn.jpg">';

  var mailform = document.getElementById("emailForm");
  var sb = document.getElementById("mailSend");

  mailform.removeChild(sb);
  mailform.appendChild(newrecip);
  mailform.appendChild(newbutton);
  
  /*var newrecip = document.createElement('input');
  newrecip.setAttribute("type","text");
  newrecip.setAttribute("name","mailTo");
  parent.appendChild(newrecip);*/
}

function showHideSubscription(){
	var bool = $("#subscription").is(":hidden");
	if(bool)
		$("#subscription").fadeIn("fast");
	else
		$("#subscription").fadeOut("fast");
}

function fadeAndReplace(o, text, timer){
	o.animate({ opacity: 0}, timer );
	o.queue(function () {
		$(this).empty().append(text);
		$(this).dequeue();
	});
	o.animate({ opacity: 1}, timer );
}

