// 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 = captureSurveyMousePosition;
} else if (document.all) { // Internet Explorer
    document.onmousemove = captureSurveyMousePosition;
} else if (document.getSurveyElementById) { // Netcsape 6
    document.onmousemove = captureSurveyMousePosition;
}
// Global variables
xSurveyMousePos = 0; // Horizontal position of the mouse on the screen
ySurveyMousePos = 0; // Vertical position of the mouse on the screen
xSurveyMousePosMax = 0; // Width of the page
ySurveyMousePosMax = 0; // Height of the page

function captureSurveyMousePosition(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.
			xSurveyMousePos = e.pageX;
			ySurveyMousePos = e.pageY;
			xSurveyMousePosMax = window.innerWidth+window.pageXOffset;
			ySurveyMousePosMax = 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.
			xSurveyMousePos = window.event.x+document.body.scrollLeft;
			ySurveyMousePos = window.event.y+document.body.scrollTop;
			xSurveyMousePosMax = document.body.clientWidth+document.body.scrollLeft;
			ySurveyMousePosMax = document.body.clientHeight+document.body.scrollTop;
		} else if (document.getSurveyElementById) {
			// 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){
		//cant find mouse	
	}
}

function getObjectSurvey(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 SurveyContar(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);
}

/*


*/
function disableSurveyStoryButtons(xform){

	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
}

/*


	saveSurveyForm
	-this attempts to save and redirect to story edit page
*/
function saveSurveyForm(next_value){

	var bErrors = false;
	var bErrorStr = '';
	
	var xform;
	xform = getObjectSurvey("edit_form");

//alert(xform);

	//error checking
	if(xform.survey_title){
		if(xform.survey_title.value==''){
			bErrors = true;
			bErrorStr = 'The Survey Title field is required.';
			xform.survey_title.className = 'jserror';
		}
	}

	//this value tells the form where to redirect after saving
	xform.sm_next.value = next_value;
	
	//TODO: do some error checking
	
	//submit xform POST
	if(bErrors){
		alert(bErrorStr);
	}else{
		//disable all the survey form buttons
		//disableSurveyButtons(xform);
	
		//alert('submit');
		xform.submit();
	}

}


function showSurveyHelp(help_text){
	//alert('show '+help_id);
	toggleMoveLayer( 'editHelp', xMousePos, yMousePos, help_text );
}

function hideSurveyHelp(help_text){
	//alert('hide '+help_id);
	toggleMoveLayer( 'editHelp', xMousePos, yMousePos, help_text );
}

function toggleSurveyMoveLayer( whichLayer, new_x, new_y, help_text )
{
  var elem, vis;
  if( document.getSurveyElementById ) // this is the way the standards work
    elem = document.getSurveyElementById( 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 deleteSurvey(survey_id){
	url = "/access/account.php?m=survey&sm=delete&sid="+survey_id;
	message = "Are you sure you want to delete this survey?";
	confirmAction(url,message);
}

function deleteSurveyOption(sopt_id){
	url = "/access/account.php?m=survey&sm=del_options&sopt_id="+sopt_id;
	message = "Are you sure you want to delete this survey option?";
	confirmAction(url,message);
}
