// -*- mode: java -*-
/*
 * Javascript utilities 
 *
 * provides utility functions
 *
 * REQUIRES: 
 * PROVIDES:
 *
 * clickOnce() -- set up all forms on page to disable all submit buttons on
 *    submit
 *
 * compositeFunc(first,second) -- create a composite function that calls the
 *     first function then the second, returning the return value of the 
 *     second
 *
 * swapDisplay(elid1,elid2) -- swap display settings of two elements.
 */

function addOnloadEvent(fnc){
 if ( typeof window.addEventListener != "undefined" )
 window.addEventListener( "load", fnc, false );
 else if ( typeof window.attachEvent != "undefined" ) {
 window.attachEvent( "onload", fnc );
 }
 else {
 if ( window.onload != null ) {
 var oldOnload = window.onload;
 window.onload = function ( e ) {
 oldOnload( e );
 window[fnc]();
 };
 }
 else
 window.onload = fnc;
 }
}

function fillIf(id, current, fill)
{
 element = document.getElementById(id);
 if (element.value == current) {
 element.value = fill;
 }
}

function clickOnce(submits)
{
    for (var i = 0; i < submits.length; i++) {
	i.disabled = true;
    }

    return true;
}

function compositeFunc(first,second)
{
    return function() {
        first();
        return second();
    }
}

function installClickOnce()
{
    if (!document.getElementById) return;

    var frms = document.getElementsByTagName('FORM');
    for (var i = 0; i < frms.length; i++) {
	var sbmts = new Array();
	var inpts = frms[i].getElementsByTagName('INPUT');
	for (var j = 0; j < inpts.length; j++) {
	    if (inpts[i].getAttribute('type') == 'submit') {
		sbmts.push(inpts[i]);
	    }
	}

	if (frms[i].onsubmit) {
	    frms[i].onsubmit = compositeFunc(curryClickOnce(sbmts),
					     frms[i].onsubmit);
	} else {
	    frms[i].onsubmit = curryClickOnce(sbmts);
	}
    }
}

function swapDisplay(elid1,elid2)
{
    if (!document.getElementById) return;

    var el1 = document.getElementById(elid1);
    var el2 = document.getElementById(elid2);

    var tmp = el1.style.display;
    el1.style.display = el2.style.display;
    el2.style.display = tmp;
}

// private

function curryClickOnce(submits)
{
    return function () {
	clickOnce(submits);
    }
}

function dreLoad()
{
   address_text = '*Direcci\u00f3n';
   zip_text = '*C\u00f3digo postal';
   suite_text ='#Apt/Unidad';

 // Add the blur and click handlers to the textboxes
   address = document.getElementById('id_user_address');
   zip = document.getElementById('id_user_zipcode');
   suite = document.getElementById('id_user_suite');

   address.onclick = function() {fillIf('id_user_address', address_text, '')};
   zip.onclick = function() {fillIf('id_user_zipcode', zip_text, '')};
   suite.onclick = function() {fillIf('id_user_suite', suite_text, '')};

   address.onblur = function() {fillIf('id_user_address', '', address_text)};
   zip.onblur = function() {fillIf('id_user_zipcode', '', zip_text)};
   suite.onblur = function() {fillIf('id_user_suite', '', suite_text)};

   // Do the initial clearing
   fillIf('id_user_address', '', address_text);
   fillIf('id_user_zipcode', '', zip_text);
   fillIf('id_user_suite', '', suite_text);

   // set up the form to clear on submit
   var clearAll = function() {
   fillIf('id_user_address', address_text, '');
   fillIf('id_user_zipcode', zip_text, '');
   fillIf('id_user_suite', suite_text, '');
 }
 if (address.form.onsubmit) {
   address.form.onsubmit = compositeFunc(clearAll, address.form.onsubmit);
 } else {
   address.form.onsubmit = clearAll;
 }
}
addOnloadEvent(dreLoad);



