// JavaScript Document


// Set Netscape up to run the "captureMousePosition" function whenever
// the mouse is moved. For Internet Explorer and Netscape 6, you can capture
// the movement a little easier.
if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
} else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
    document.onmousemove = captureMousePosition;
}
// Global variables
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

function captureMousePosition(e) {
	try{
		if (document.layers) {
			// When the page scrolls in Netscape, the event's mouse position
			// reflects the absolute position on the screen. innerHight/Width
			// is the position from the top/left of the screen that the user is
			// looking at. pageX/YOffset is the amount that the user has
			// scrolled into the page. So the values will be in relation to
			// each other as the total offsets into the page, no matter if
			// the user has scrolled or not.
			xMousePos = e.pageX;
			yMousePos = e.pageY;
			xMousePosMax = window.innerWidth+window.pageXOffset;
			yMousePosMax = window.innerHeight+window.pageYOffset;
		} else if (document.all) {
			// When the page scrolls in IE, the event's mouse position
			// reflects the position from the top/left of the screen the
			// user is looking at. scrollLeft/Top is the amount the user
			// has scrolled into the page. clientWidth/Height is the height/
			// width of the current page the user is looking at. So, to be
			// consistent with Netscape (above), add the scroll offsets to
			// both so we end up with an absolute value on the page, no
			// matter if the user has scrolled or not.
			xMousePos = window.event.x + document.body.scrollLeft;
			yMousePos = window.event.y + document.body.scrollTop;
			xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
			yMousePosMax = document.body.clientHeight+document.body.scrollTop;
		} else if (document.getElementById) {
			// Netscape 6 behaves the same as Netscape 4 in this regard
			xMousePos = e.pageX;
			yMousePos = e.pageY;
			xMousePosMax = window.innerWidth+window.pageXOffset;
			yMousePosMax = window.innerHeight+window.pageYOffset;
		}
	}catch(err){
		//not able to find mouse	
	}
}



function getObject(obj) {
  var theObj;
  if(document.all) {
    if(typeof obj=="string") {
      return document.all(obj);
    } else {
      return obj.style;
    }
  }
  if(document.getElementById) {
    if(typeof obj=="string") {
      return document.getElementById(obj);
    } else {
      return obj.style;
    }
  }
  return null;
}

//Contador de caracteres.
function Contar(source_field,target_update,texto,max_characters) {
  var entradaObj=getObject(source_field);
  var salidaObj=getObject(target_update);
  var longitud=max_characters - entradaObj.value.length;
  if(longitud <= 0) {
    longitud=0;
    texto='<span class="disable"> '+texto+' </span>';
    entradaObj.value=entradaObj.value.substr(0,max_characters);
  }
  salidaObj.innerHTML = texto.replace("{CHAR}",max_characters-longitud);
}

/*
	disableNewsStoryButtons
	-this disables all the news story buttons.
*/
function disableNewsStoryButtons(xform){
	mySetStyleDisplay('newsMan','none');
	mySetStyleDisplay('tabsave','');
	if(xform.save_button)
		xform.save_button.disabled=true; //DISABLE THE SUBMIT BUTTON
	
	if(xform.save_close_button)
		xform.save_close_button.disabled=true; //DISABLE THE SUBMIT BUTTON
}


function mySetStyleDisplay (myDiv, myStyle){
	if (document.getElementById) {
		try{
			obj = document.getElementById(myDiv);
			obj.style.display = myStyle;
		}catch(err){
			//can't find the obj	
		}
	} else 	if (document.all){
		try{
			obj = document.all(myDiv);
			obj.style.display = myStyle;
		}catch(err){
			//can't find the obj	
		}
	} else {
		//error don't know how to set browser
		//alert (Err_Browser);
	}
}


/*
	saveNewsStoryForm
	-this attempts to save and redirect to story edit page
*/
function saveNewsStoryForm(next_value){

	var bErrors = false;
	var bErrorStr = '';
	
	var xform = getObject('edit_form');

	//error checking
	if(xform.news_title){
		if(xform.news_title.value==''){
			bErrors = true;
			bErrorStr = 'The Headline field is required.';
			xform.news_title.className = 'jserror';
		}
	}

	//this value tells the form where to redirect after saving
	xform.nm_next.value = next_value;
	
	//TODO: do some error checking
	
	//submit xform POST
	if(bErrors){
		alert(bErrorStr);
	}else{
		//disable all the news story form buttons
		disableNewsStoryButtons(xform);
	
		//alert('submit');
		xform.submit();
	}

}

function editStoryPreview(nid){
	var bErrors = false;
	var bErrorStr = '';
	
	var xform = getObject('edit_form');

	//error checking
	if(xform.news_title){
		if(xform.news_title.value==''){
			bErrors = true;
			bErrorStr = 'The Headline field is required.';
			xform.news_title.className = 'jserror';
		}
	}

	//TODO: do some error checking
	
	//submit xform POST
	if(bErrors){
		alert(bErrorStr);
	}else{
		//disable all the news story form buttons
		disableNewsStoryButtons(xform);
	
		//alert('submit');
		xform.action = '/access/account.php?m=newsmgr&nm=preview&nid='+nid;
		xform.submit();
	}
}

function showHelp(help_text){
	//alert('show '+help_id);
	toggleMoveLayer( 'editHelp', xMousePos, yMousePos, help_text );
}

function hideHelp(help_text){
	//alert('hide '+help_id);
	toggleMoveLayer( 'editHelp', xMousePos, yMousePos, help_text );
}

function toggleMoveLayer( whichLayer, new_x, new_y, help_text )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';

elem.innerHTML = help_text;

	vis.left = new_x+20 + 'px';	
	vis.top = new_y-50 + 'px';
}

function hideAllEditPhotoDisplay(){

	hideXDiv('EditPhotoThumbnail');
	hideXDiv('EditPhotoSmall');
	hideXDiv('EditPhotoMedium');
	hideXDiv('EditPhotoSource');

}

function hideXDiv(div_name){
	eSmall = getObject(div_name);
	if(eSmall){
		eSmall.style.visibility = 'hidden'; 
		eSmall.style.display = 'none';
	}
}

function showXDiv(div_name){
	eSmall = getObject(div_name);
	if(eSmall){
		eSmall.style.visibility = 'visible'; 
		eSmall.style.display = 'block';
	}
}

function toggleEditThumbnail(){
	//alert('thumb');
	hideAllEditPhotoDisplay();
	showXDiv('EditPhotoThumbnail');
}

function toggleEditSmall(){
	//alert('small');
	hideAllEditPhotoDisplay();
	showXDiv('EditPhotoSmall');
}

function toggleEditMedium(){
	//alert('small');
	hideAllEditPhotoDisplay();
	showXDiv('EditPhotoMedium');
}

function toggleEditSource(){
	//alert('small');
	hideAllEditPhotoDisplay();
	showXDiv('EditPhotoSource');
}

function confirmAction(url,message){
	var box = confirm(message);
	if(box){
		window.location = url;
	}
}

function deleteScoreboard(sb_id){
	url = "/access/account.php?m=scoreboard&nm=delete&sid="+sb_id;
	message = "Are you sure you want to delete this scoreboard?";
	confirmAction(url,message);
}

function deleteObituary(ob_id){
	url = "/access/account.php?m=obits&nm=delete&sid="+ob_id;
	message = "Are you sure you want to delete this obituary?";
	confirmAction(url,message);
}

function deleteVideoStream(stream_id){
	url = "/access/account.php?m=media&nm=delete&sid="+stream_id;
	message = "Are you sure you want to delete this video stream?";
	confirmAction(url,message);
}

function deleteMarqueItem(marque_id){
	url = "/access/account.php?m=marque&nm=delete&sid="+marque_id;
	message = "Are you sure you want to delete this marque link?";
	confirmAction(url,message);
}

function deletePhotoBlockItem(pb_id){
	url = "/access/account.php?m=pblock&nm=delete&sid="+pb_id;
	message = "Are you sure you want to delete this photo?";
	confirmAction(url,message);
}

function deleteFoodInsItem(xid){
	url = "/access/account.php?m=foodins&nm=delete&sid="+xid;
	message = "Are you sure you want to delete this food inspection?";
	confirmAction(url,message);
}

function toggleCommentsButton(xform){
	xform.nm_update_comments.value="true";
	xform.submit();
}

function deleteUser(user_id){
	url = "/access/account.php?m=users&nm=delete&uid="+user_id;
	message = "Are you sure you want to delete this user?";
	confirmAction(url,message);
}

function setFormValue(field,n_value){
	document.getElementById(field).value = n_value;
}

function cancelPreview(nid){
	window.location = '/access/account.php?m=newsmgr&nm=edit&nid='+nid;
}

function quickOverride(xform,xtext){
	//alert(xtext);
	xform.author_override.value=xtext;
}