// JavaScript Document

	
function getWinW(windowRef)
{
  var width = 0;

  if (!windowRef)
    windowRef = window;
  
  if (typeof(windowRef.innerWidth) == 'number')
    width = windowRef.innerWidth;
  else if (windowRef.document.body && typeof(windowRef.document.body.clientWidth) == 'number')
    width = windowRef.document.body.clientWidth;  
   //alert("width="+width);
  return width;
}

function getWinH(windowRef)
{
  var height = 0;

  if (!windowRef)
    windowRef = window;
  
  if (typeof(windowRef.innerHeight) == 'number')
    height = windowRef.innerHeight;
  else if (windowRef.document.body && typeof(windowRef.document.body.clientHeight) == 'number')
    height = windowRef.document.body.clientHeight;  
   //alert("height="+height);
  return height;
}

var ml;
var tf;
var dir=0;
var ef;
var af;
var ed;
var ecd;
var ecf;
var ap;
var cf;
var ccf;
var mi;
var justmoved=0;
var str_html;
var ai;
var an;

		
var where = ""; // which link
var mousex,mousey,posy;
function getMouse(e) {
	if (!e) var e = window.event;
	alert("e="+e);
        if (document.all){
        	xCoord = event.clientX;
       		yCoord = event.clientY;
		}
        else {
        	xCoord = e.clientX;
        	yCoord = e.clientY;
		}
	
	if (e.pageX || e.pageY)
	{
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posy = e.clientY + document.body.scrollTop;
	}

    //self.status = "X= "+ xCoord + "  posy= " + posy+ " document.body.scrollTop=" +document.body.scrollTop+ ""; 
	mousex=xCoord;
	mousey=yCoord;
}





var winH = 0;
function init(){
	//alert("got here");
	winH = getWinH()+300;
	//var winW = getWinW();
	
	
	if(document.captureEvents) {document.captureEvents(Event.MOUSEDOWN);}
	if(document.forms["announcement"]){
		an = document.forms["announcement"];
	}
	if(document.getElementById('editCatDiv')){
		ecd=document.getElementById('editCatDiv');
	}
	if(document.getElementById('editCatForm')){
		ecf=document.getElementById('editCatForm');
	}
	if(document.getElementById('editDiv')){
		ed=document.getElementById('editDiv');
	}
	if(document.getElementById('editForm')){
		ef=document.getElementById('editForm');
	}
	if(document.getElementById('addForm')){
		af=document.getElementById('editForm');
	}
	if(document.getElementById('moveNode')){
		mn=document.getElementById('moveNode');
	}
	if(document.getElementById('moveImage')){
		mi=document.getElementById('moveImage');
	}
	if(document.getElementById('addImage')){
		ai=document.getElementById('addImage');
	} 
	
	if(document.getElementById('closeForm')){
		cf=document.getElementById('closeForm');
		initCloseBar(cf);
	}
	if(document.getElementById('closeCatForm')){
		ccf=document.getElementById('closeCatForm');
		initCloseBar(ccf);
	}
		
	if(justmoved){
		if(mn){
			enableMoveBtns(justmoved,'submitMoveNode');
		}
		if(mi){
			enableMoveBtns(justmoved,'submitMoveImage');
		}
	}
	
	if(document.getElementById("tf")){
		tf = document.getElementById("tf");			
	}
}


	
	
function initCloseBar(obj){
	obj.style.backgroundColor = "#ffffff";
	obj.style.color = "#000000";
	obj.style.cursor = "hand";
	
	obj.onmouseover= function(){
		obj.style.backgroundColor = "#cccccc";
		obj.style.color = "#ff9900";
	};
	obj.onmouseout= function(){
		obj.style.backgroundColor = "#ffffff";
		obj.style.color = "#000000";
	};
	obj.onmousedown= function(){
		ed.style.visibility = "hidden";
		ecd.style.visibility = "hidden";
	};
}





var html;
function tf_DoFSCommand(command, args) 
{ 
	if (command == "html") 
		{ 
			//alert("Here's the Category: " + args);
			html = args;
			if(document.forms.content){
				document.forms.content.pcontent.value = escape(args);
			}
			//alert(html);
		}		
}
		
function getSelectedNubbins(frmObj,keyname){
	var chkObj = frmObj[keyname];
	var strNubbins='';

	for(var i=0;i<chkObj.length;i++){
		if(chkObj[i].checked) {
			strNubbins += ","+chkObj[i].value;
		} 
	}
	
	frmObj.selectedNubbins.value=strNubbins;
	return strNubbins;
	//alert(strNubbins);
}

function gotoC(frmObj,c){
	if(frmObj.c){
		frmObj.c.value='admin_download';
		//frmObj.action='http://www.maisondenaissance.org/admin.php?pid=41&c=admin_download';
		//alert(frmObj.action);
		frmObj.method="POST";
		frmObj.submit();
	} else {
		return false;
	}
	
}

function getSelectedEmails(frmObj,keyname){
	var chkObj = frmObj[keyname];
	var emailObj;
	var strEmails='';
	var key;
	for(var i=0;i<chkObj.length;i++){
		if(chkObj[i].checked){
			key = chkObj[i].value;
			emailObj = frmObj["email"+i];
			if(emailObj.value.length){
				if(strEmails.length){
					strEmails += ";"+emailObj.value;
				} else {
					strEmails += emailObj.value;
				}
			}
		}
	}
	//alert(strEmails);
	return strEmails;
}


function confirmDelete(msg,frmObj){
	var x = confirm(msg,"Yes,No");
	if(x){
		frmObj.submit();
	}
}




function submitAddLinkCategory(){
	//alert('addlink');
	var ready=true;
	var frm = document.getElementById('addCategory');
	if(frm.lname.value==''){
		alert('You must enter a link category before you can save.');
		frm.lname.focus();
		ready=false;
	}
	if(ready){
		frm.submit();
	}	
}

function submitAddPage(){
	//alert('addlink');
	var ready=true;
	if(ap.pname.value==''){
		alert('You must enter a link before you can save.');
		ap.pname.focus();
		ready=false;
	}
	if(ready){
		ap.submit();
	}	
}


var activeRow=0;
function enableMoveBtns(count,func){
	//alert('enabling');
	var moveBtns = new Array("a_beg","a_prev","a_next","a_end");
	for(var m=0;m<moveBtns.length;m++){
		if(document.getElementById(moveBtns[m]+activeRow)){
			document.getElementById(moveBtns[m]+activeRow).href = "";
		}
	}
	if(document.getElementById('moveBtns'+activeRow)){
		document.getElementById('moveBtns'+activeRow).style.visibility = "hidden";
	}
	
	if(document.getElementById('moveBtns'+count)){
		document.getElementById('moveBtns'+count).style.visibility = "visible";
	}
	if(document.getElementById('a_beg'+count)){
		document.getElementById('a_beg'+count).href = "javascript:"+func+"('beg');";
	}
	if(document.getElementById('a_prev'+count)){
		document.getElementById('a_prev'+count).href = "javascript:"+func+"('prev');";
	}
	if(document.getElementById('a_next'+count)){
		document.getElementById('a_next'+count).href = "javascript:"+func+"('next');";
	}
	if(document.getElementById('a_end'+count)){
		document.getElementById('a_end'+count).href = "javascript:"+func+"('end');";
	}
	activeRow = count;	
}


function submitMoveImage(dest_seq){
	//alert(mi);
	if(mi){
		var iids = new Array();
		var x = activeRow-1;
		str = mi.iids.value;
		str = str.substring(1,str.length-1);
		iids = str.split(",");
		
		switch(dest_seq){
			case "beg":
				var insertid=iids.splice(x,1);
				iids.unshift(insertid);
			break;
			case "next":
				var n = x+1;
				if(n<0)n=0;
				if(n>iids.length-1)n=iids.length-1;
				if(x!=n){
					var temp = iids[x];
					iids[x]=iids[n];
					iids[n]=temp;
				}
			break;
			case "prev":
				var n = x-1;
				if(n<0)n=0;
				if(n>iids.length-1)n=iids.length-1;
				if(x!=n){
					var temp = iids[x];
					iids[x]=iids[n];
					iids[n]=temp;
				}
			break;
			case "end":
				var insertid=iids.splice(x,1);
				iids.push(insertid);
			break;
		}
		
		mi.iids.value = ","+iids.join()+",";
		mi.just_moved.value= eval('iid'+activeRow);
		mi.submit();
	}
}

function submitMoveNode(dest_seq){
	if(document.getElementById('moveNode')){
		var mn=document.getElementById('moveNode');
	}
	if(mn){
		mn.table.value = table;
		mn.key_name.value = key_name;
		mn.key_value.value = eval(key_name+activeRow);
		mn.seq_name.value = seq_name;
		mn.seq_value.value = eval(seq_name+activeRow);
		mn.parent_name.value = parent_name;
		mn.parent_value.value = eval(parent_name+activeRow);
		mn.dest_seq.value = dest_seq;
		mn.submit();
	}
}


function submitMe(frmObj,adm){
	var ok = true;
	updateRTEs();
	if(!adm){
		var tempObj;
		var required_fields = new Array();
		var strFields = "e_cname,conname,firstname,lastname,email";
		var strFieldNames = "Name,Name,First Name,Last Name,E-mail Address";
		required_fields = strFields.split(",");
		required_field_names = strFieldNames.split(",");
		for(var i=0;i<required_fields.length;i++){
			tempObj = frmObj[required_fields[i]];
			if(tempObj){
				if(tempObj.value==''){
					alert(required_field_names[i] + " is a required field.  Please try again.");
					tempObj.focus();
					ok=false;
					break;
				}
			}
		}
	}
	return ok;
}

function checkAll(frmObj,state){
	if(!state) state = false;
	if(frmObj){
		for(var i=0; i<frmObj.length; i++){
			if(frmObj.elements[i].type == "checkbox"){
				frmObj.elements[i].checked = state;
			}
		}
	}
}

function updateString(objName,maxChecked,thisChecked){
	var strValue='';
	var intChecked=0;
	var obj = getObject(objName);
	var chkObj;
	var thisObj= getObject("chk_"+objName+thisChecked);
	var countObj = getObject(objName +'_count');
	if(obj && countObj && thisObj){
		for(var i=0;i<=countObj.value;i++){
			chkObj = getObject('chk_'+objName+i);
			if(chkObj){
				if(chkObj.checked){
					intChecked++;
				}
			}
		}
		if((intChecked > maxChecked) && maxChecked){
			alert("Only "+maxChecked+" may be selected.");
			thisObj.checked = false;
			//chkObj.focus();
		} else {
			for(var i=0;i<=countObj.value;i++){
				chkObj = getObject('chk_'+objName+i);
				if(chkObj){
					if(chkObj.checked){
						strValue+=','+chkObj.value;
					}
				}
			}
			obj.value=strValue+',';
		}
	}
}


function isWindowOpen(windowhandle){
	if (windowhandle && windowhandle.open && !windowhandle.closed){
		return true;
	} else {
		return false;
	}
}			


var emailWin;

function viewEmail(e_aid,pid){
	var newWindowURL = "admin_email_viewer.php?pid="+pid+"&view1=1&keyval="+e_aid;
	if (!isWindowOpen(emailWin))		
	{
		emailWin = window.open(newWindowURL,"emailWin","resizable=yes,address=no,scrollbars=yes,height=750,width=900");
	}
	else
	{
		emailWin.location = newWindowURL;
		emailWin.focus();
	}	

}
