
var addtoshortlist = {
    myConn:      false,
    body:        false,
    control:     false,
    error :      false,
    storage:     false,
    init:        function( controlname , storageId, errorId){
    if( !document.getElementById ||
        !document.getElementsByTagName ||
        !addtoshortlist.getElementByClassName('a', controlname ) ||
        !document.getElementById( storageId ) ||
        !document.getElementById( errorId )) return;
        
    addtoshortlist.myConn = new XHConn();
    if( !addtoshortlist.myConn ) return;
    addtoshortlist.body    = document.getElementsByTagName( 'body' )[0];
    addtoshortlist.control = addtoshortlist.getElementByClassName('a', controlname );
    addtoshortlist.storage = document.getElementById( storageId );
    addtoshortlist.error = document.getElementById( errorId );
    
    for (i=0; i<addtoshortlist.control.length; i++) {
    addtoshortlist.addEvent( addtoshortlist.control[i],'click', function(){ addtoshortlist.action(); return false;} );  
    }
            
  },  
  action:  function(){
        addtoshortlist.error.style.display = "none";
        var theform = document.getElementById('goform');
        var params = "";
        if(addtoshortlist.control[0].className.indexOf('loader') < 0) {
            addtoshortlist.buildLoader()
            if(addtoshortlist.control[0].className.indexOf('removeshortlist') >= 0) {
                params += "&shortlistid=" + addtoshortlist.storage.value;
                params += "&shortaction=REMOVE_DATA";               
            } else {
                for (i=0; i<theform.childNodes.length; i++) {
                    if( theform.childNodes[i].nodeName == 'INPUT' && 
                        theform.childNodes[i].id != 'pagename' &&
                        theform.childNodes[i].id != 'suitablehomeids' &&
                        theform.childNodes[i].id != 'suitablelotids')
                        params += "&" + theform.childNodes[i].id + "=" + theform.childNodes[i].value;
                }
                params += "&xcid=" + document.getElementById("xcid").value;
                params += "&xc=" + document.getElementById("xc").value;
                params += "&xpagename=" + document.getElementById("xpagename").value;
                params += "&shortaction=INSERT_DATA";
            }
            var fnWhenDone = function(oXML) { var fail=false;
              if(oXML.status ==200 || oXML.status ==304) {
                  if(oXML.responseText.indexOf('ADDTOSHORTLIST-CONFIRM') >= 0) 
                  {
                    addtoshortlist.storage.value = addtoshortlist.getValue(oXML.responseText);
                    addtoshortlist.killLoader('removeshortlist', 'Remove From Shortlist');
                  }
                  else if(oXML.responseText.indexOf('REMOVEFROMSHORTLIST-CONFIRM') >= 0)
                  {
                    addtoshortlist.storage.value = "";
                    addtoshortlist.killLoader('addshortlist', 'Add To My Shortlist');
                  }
                  else
                  {fail=true;}
              }
              else {fail=true;}
              if(fail){
                addtoshortlist.killLoader('addshortlist', 'Add To My Shortlist');
                addtoshortlist.storage.value = "";
                if(addtoshortlist.getError(oXML.responseText).length > 0) addtoshortlist.error.innerHTML = addtoshortlist.getError(oXML.responseText)
                else addtoshortlist.error.innerHTML = "Sorry, there was a network error at this time.";
                addtoshortlist.error.style.display = "block";
              }
            };
            var strServlet = 'ContentServer';
            if(window.location.href.indexOf("Satellite") >=0 ) strServlet = "Satellite";
            addtoshortlist.myConn.connect(strServlet + "?pagename=VicUrban/Actions/Shortlist" + params, "GET", "", fnWhenDone);
            
        }   
  },
  buildLoader: function(){
     for (i=0; i<addtoshortlist.control.length; i++) {
         removeClass(addtoshortlist.control[i], 'addshortlist');    
         addClass(addtoshortlist.control[i], 'loader'); 
         addtoshortlist.control[i].title = "Loading";
     } 
  },
  killLoader:  function(classValue, value){
     for (i=0; i<addtoshortlist.control.length; i++) {
        removeClass(addtoshortlist.control[i], 'removeshortlist');
        removeClass(addtoshortlist.control[i], 'addshortlist');
        removeClass(addtoshortlist.control[i], 'loader');
        addClass(addtoshortlist.control[i], classValue);    
        addtoshortlist.control[i].title = value;
     }
  },
  addEvent: function( obj, type, fn ){  // the add event function
    if (obj.addEventListener) obj.addEventListener( type, fn, false );
    else if (obj.attachEvent) {
      obj["e"+type+fn] = fn;
      obj[type+fn] = function() {
        obj["e"+type+fn]( window.event );
      };
      obj.attachEvent( "on"+type, obj[type+fn] );
    }
  },
  getValue: function(text){
      return text.substring(text.indexOf("<shortlist>") + "<shortlist>".length , text.indexOf("</shortlist>"))
  },
  getError: function(text){
      if(text && text.indexOf("<error>") > 0){
          try { return text.substring(text.indexOf("<error>") + "<error>".length , text.indexOf("</error>")); }
          catch(e) {return "";}
      } else { return "";}
  },
  getElementByClassName: function(type, classValue) {
     var found = new Array();
     var arry = document.getElementsByTagName(type);
     var pattern = new RegExp("(^| )" + classValue + "( |$)");
     for (i=0; i<arry.length; i++) {
         if(arry[i] && pattern.test(arry[i].className))
            found[found.length] = arry[i];       
     }
     return found;
  }
};
addtoshortlist.addEvent( window, 'load', function(){ addtoshortlist.init( 'add_to_shortlist' , 'shortlistid', 'shortlist_error' ); } );




