/*
placeholder.js
copyright: Placeholder Media 2006-2209, all rights reserved.
 */
function get(id)
{
    debug =true;
    
	if (document.getElementById && document.createElement) {}
	else  alert('Your browser doesn\'t support the Level 1 DOM');
	
   el = document.getElementById(id);
   
   if (el==null) // iframes may be on page
    {       
         el = top.window.document.getElementById(id);
    }

    if (document.layers) // iframes may be on page
    {
        el = document.layers[id]; 
    }
    if(el==null && debug==true)
         alert('problem: id '+id+' not found'); 
     
    return el;
}

function hide(id)
{
	get(id).style.display = 'none';
	get(id).style.visibility = 'invisible';
        //get('html').style.height = '100%';
}
function show(id)
{
	get(id).style.display = 'block';
	get(id).style.visibility = 'visible';
        //get('html').style.height = 'auto';
}

function active(id)
{
	get(id).display = 'block';
	get(id).style.visibility = 'visible';
}
function setClassName(id,classname)
{
    get(id).className = classname;
}

function showInline(id)
{
	get(id).style.display = 'inline';
}

function addEvent( obj, type, fn ) 
{
  if ( obj.attachEvent)   // IE way
  {
    obj["e"+type+fn] = fn;
    obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
    obj.attachEvent( "on"+type, obj[type+fn] );
  } else                  //w3c way
    obj.addEventListener( type, fn, false );  // false = bubble up 
}

// need to remove to free memory.  IE uses reference counting.
// Memory leaks will occur if events aren't removed because
// DOM element <-> javascript event  stay connected.
// Another way would be be to walk the DOM and remove all events.
function removeEvent( obj, type, fn ) 
{
  if ( obj.detachEvent ) 
  {
    obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}

// Chap = Challenge Handshake Authentication Protocol 
// 1. generate challenge phrase via PHP.  Note that this .js file gets served 
//    by the serer and therefore php can insert a challenge phrase before 
//    the client gets this static code (w/ server generated challenge).
//
 
function doChap()
{
    //var link = get('forgotPassword');
    var user = get('username');  // get from form input
    var pass = get('password');  // get from form input
    var passMd5OnForm = get('passwordMd5');  // get from form input
    var challengeOnForm = get('challenge');  // get hidden form input
   // var challengeFromServer = '<?php echo $_SESSION['challenge']?>');
// FIX: move to User
    
    passMd5OnForm.value = hex_md5(pass.value);    
    pass.value="";     // clear it out bcz were going to send it to the server.
    challengeOnForm.value= 'imachallenge'; //str_md5(passMd5OnForm.value+challengeFromServer);
    
    return true;
}

/*
 * display error messages
 */
function showError(obj,message)
{
  alert(message);
  obj.focus();
}

function breakout()
{ 
    if (window != top) 
        top.location.href = location.href;        

    if (top.location != self.location)
      top.location = self.location.href;

}

/* The XMLHttpRequest  object can be in five states: 
UNSENT, OPENED, HEADERS_RECEIVED, LOADING and DONE. 

The current state is exposed through the readyState  attribute. The method definitions 
below define when a state transition takes place.
*/
function getHttpRequest()
{
    var httpRequest=null;
    try// Firefox, Opera 8.0+, Safari
    {
        httpRequest=new XMLHttpRequest();
    }
    catch (e) // Internet Explorer
    {
        try
        { 
            httpRequest=new ActiveXObject("Msxml2.XMLHTTP");
        }

        catch (e)
        {
            httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
        }
    } 
    return httpRequest;
}


function stateChanged() 
{ 
    if (httpRequest.readyState==4)  // 4 = DONE
    { 
        id = httpRequest.targetId;
        get(id).innerHTML=httpRequest.responseText; 
        //parent.document.getElementById(id).innerHTML=httpRequest.statusText; 
    }
}

// FIX: Refactor to innerHtmlViaAjax(id,url); or getHtmlFromServer(id,url)
function ajax(id,url)
{
    httpRequest = getHttpRequest();
    httpRequest.targetId = id;
    httpRequest.onreadystatechange=stateChanged;  // called 
    httpRequest.open("GET",url,true);
    httpRequest.send(null);
}

// FIX: Refactor to innerHtmlViaAjax(id,url); or getHtmlFromServer(id,url)
function ajaxPost(id,url)
{
    httpRequest = getHttpRequest();
    httpRequest.targetId = id;
    httpRequest.onreadystatechange=stateChanged;  // called 
    httpRequest.open("POST",url,true);
    httpRequest.send(null);
}

// From http://www.anyexample.com/webdev/javascript/javascript_getelementsbyclass_function.xml
function getElementsByClass( searchClass, domNode, tagName) { 
	if (domNode == null) domNode = document;
	if (tagName == null) tagName = '*';
	var el = new Array();
	var tags = domNode.getElementsByTagName(tagName);
	var tcl = " "+searchClass+" ";
	for(i=0,j=0; i<tags.length; i++) { 
		var test = " " + tags[i].className + " ";
		if (test.indexOf(tcl) != -1) 
			el[j++] = tags[i];
	} 
	return el;
} 


