// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// Hide the element.
function hide(elem)
{
    if(typeof elem == 'string') {
        elem = document.getElementById(elem);
    }

    elem.style.display='none';
}

// Show the element incase it is hidden.
function show(elem)
{
    if(typeof elem == 'string') {
            elem = document.getElementById(elem);
    }
    if(elem) {
       elem.style.display='';
    }
}

// xmlLoad handles our ajax requests.
// doc = the name of the file to request server-side
// elem = the element to update with the result.
function xmlLoad(doc, elem, afterFinish)
{
  // xmlDoc is our global var for the ajax request.
  var xmlDoc = null ;
  if (typeof window.ActiveXObject != 'undefined' ) {
    xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
    xmlDoc.onreadystatechange = function () { xmlProcess(xmlDoc, elem, afterFinish); } ;
  }
  else {
    xmlDoc = new XMLHttpRequest();
    xmlDoc.onload = function () { xmlProcess(xmlDoc, elem, afterFinish); } ;
  }
  xmlDoc.open( "GET", doc, true );
  xmlDoc.send( null );
}

function xmlProcess(xmlDoc, elem, afterFinish)
{
  if ( xmlDoc.readyState != 4 ) return ;
  //alert('processed going to ' + elem);
  //alert(xmlDoc.responseText);

  var regexp = new RegExp("\\w");
  if(xmlDoc.responseText.match(regexp))
  {
    document.getElementById(elem).innerHTML = xmlDoc.responseText;
  }

  if(afterFinish) {
      // If we have an afterFinish function
      // try to run it here.
      if(typeof(afterFinish) == 'function') {
          afterFinish();
      } else if(typeof(afterFinish) == 'string') {
          eval("'" + afterFinish + "'");
      } else {
          alert(typeof(afterFinish));
      }
  }
}

//use as
/*
xmlLoad(thisurl,'updatediv');
*/


function enableEdit(fieldName, id) {
    var divName = fieldName + "_div";
    var field = document.getElementById(fieldName);
    if(field) {
        field.disabled=false;
        field.style.border="1px solid #cccccc";
        field.select();
    } else {
        alert(fieldName + ' does not exist');
        return;
    }

    var editLink = document.getElementById(fieldName + "_link");
    var saveLink = document.getElementById(fieldName + "_savelink");

    if(editLink) {
        hide(editLink);
        show(fieldName+"_savelink");
    } else {
        alert(fieldName + "_link does not exist");
    }
}

function saveEdit(fieldName, id) {
    var divName = fieldName + "_div";
    var newValue = document.getElementById(fieldName).value;

    url = window.location.href;
    url += "?action=update&field=" + fieldName + "&value=" + newValue + "&id=" + id;
    xmlLoad(url, divName, function(){disableEdit(fieldName, id);} );
}

function disableEdit(fieldName,id) {
    var divName = fieldName + "_div";
    var field = document.getElementById(fieldName);

    if(field) {
        field.disabled=true;
        field.style.border="1px solid #ffffff";
    } else {
        alert(fieldName + ' does not exist');
        return;
    }

    var editLink = document.getElementById(fieldName + "_link");
    var saveLink = document.getElementById(fieldName + "_savelink");

    if(editLink) {
        hide(saveLink);
        show(editLink);
    } else {
        alert(fieldName + "_link does not exist");
    }
}

function openProfileWindow(id){
	profilewindow = window.open("http://www.fairq.com/home/viewprofile?uid=" + id,
		"profilewindow", "location=0,toolbar=0,status=0,width=600,height=300,menubar=0,resizable=1");			
}


