﻿
// 
var Page = {

    // urlencoded original query
    url: null,

    onload : function() { 
        if( this.load ){
            this.load();
        }
              
        if ( $('try_ufb') ){
            $('try_ufb').submit();
        }
    },
    
    onunload : function() { 
    
        if(this.unload){
            this.unload();
        }
    },
    
    unSelect: function( selectId ) { 
        
        var oSelect = $(selectId);
        
        if( oSelect.selectedIndex > 0 ) { 
            oSelect.selectedIndex = 0;
        }
    },
    
    disableSelect: function( id, disable ) {
        if( disable ) {
            this.unSelect( id );
        }
        
        $(id).disabled = disable;
    },
    
     replaceOptions: function( selectId, objList ) {       
        var selectObj = $(selectId);       
        
        var currentValue = $F(selectId);
        //alert( "Current value of [" + selectId + "]: [" + currentValue  + "]");
        
        // remove all current options, except the first one
	    this.removeAllButFirst( selectObj );
        
        var selected = null;
        
        // add new options 
        for( i = 0; i < objList.length; i++ ) {
        
            var obj = objList[i];
            
            var optionElement = document.createElement( "option" );
            optionElement.value = obj.value;
   
            optionElement.appendChild( document.createTextNode( unescape( obj.text ) ) );
                  
            if( currentValue == obj.value ) { 
                optionElement.selected = "true";
                selected = currentValue;
            }     
                  
            selectObj.appendChild( optionElement );
        }   
        
        selectObj.disabled = (objList.length == 0);
        
        return selected;
    },
    
    removeAllButFirst: function( node ) { 
    
        // remove all current child nodes, except the first one
	    while(node.childNodes.length > 1 ){ 
	        var removedNode = node.removeChild( node.lastChild );
	        
	        if (/MSIE/.test(navigator.userAgent)) {
	            removedNode.outerHTML = '';
	        }
	        
	    }
    
    }
    
}

var DomLib = {

    removeChildren : function(/*Element*/ node){
	    //	summary:
	    //		removes all children from node and returns the count of children removed.
	    //		The children nodes are not destroyed. Be sure to call destroyNode on them
	    //		after they are not used anymore.
	    var count = node.childNodes.length;
	    while(node.hasChildNodes()){ 
	        var node = node.firstChild.removeNode(); 
	        node.destroyNode();
	    }
	    return count;
    },
    
    
    removeNode : function(/*Node*/node){
	    // summary:
	    //		if node has a parent, removes node from parent and returns a
	    //		reference to the removed child.
	    //		To prevent IE memory leak, call destroyNode on the returned node when
	    //		it is no longer needed.
	    //	node:
	    //		the node to remove from its parent.

	    if(node && node.parentNode){
		    // return a ref to the removed child
		    return node.parentNode.removeChild(node); //Node
	    }
	}
}

// http://xavisys.com/blog/2007/03/01/using-prototype-javascript-to-get-the-value-of-a-radio-group/
function $RF(el, radioGroup) {
	if($(el).type == 'radio') {
		var el = $(el).form;
		var radioGroup = $(el).name;
	} else if ($(el).tagName.toLowerCase() != 'form') {
		return false;
	}
	
	return $F($(el).getInputs('radio', radioGroup).find(
		function(re) {return re.checked;}
	));
}


var PreSearch = {
    onKeywordsClick: function( input ) {
        if( input.value == 'Start search' ) {
            input.value = '';
        }       
    },
    
    submit: function() { 
     
        var form = $('site_search_form');
        
        var type = $F('site_search_form_type');
        
        var keywords = $F('presearch_keywords');
        
        if( keywords == 'Start search' || keywords == '' ) { 
            $('presearch_keywords').disabled = true;
        }
               
        if( type != '' ) { 
            if(type == 'companylistings')
            { 
            var url="/companylistings/";
             if( keywords != 'Start search' && keywords != '' ) { 
                       url = url + "-/-/-/" +  convertkeyword(keywords);
                }
               window.location.href =url;
            }
            else
            {
                form.action = '/search.html';
                 form.submit();
            }
        }
        else {
            $('site_search_form_type').disabled = true;
             form.submit();
        }
        
       
    }

}

function convertkeyword(val)
{
//replacing any nonaz09   character
   var test = /[^\*\+\s0-9a-zA-Z-]/g; 
   var val1 = val.replace(test, "-");
  
  //replacing multiple consecutive occurrence of '-' character
  var t1 = /-+/g;
  val1 = val1.replace(t1, "-")
  
  //removing last occurrence of '-' character
  var the_length=val1.length;
  var last_char=val1.charAt(the_length-1);

  if(last_char == '-')
  {
    val1 = val1.substring(0,the_length-1);
    
  }
  
  if(val1 == ''){  val1 = '-';}
  
  var test11 = /[\+]/g;
  val1 = val1.replace(test11,'%2B');
  return (val1);
}
function convertName(val){

    
   //replacing any nonaz09   character
   var test = /[^0-9a-zA-Z]/g; 
   var val1 = val.replace(test, "-");
  
  //replacing multiple consecutive occurrence of '-' character
  var t1 = /-+/g;
  val1 = val1.replace(t1, "-")
  
  //removing last occurrence of '-' character
  var the_length=val1.length;
  var last_char=val1.charAt(the_length-1);

  if(last_char == '-')
  {
    val1 = val1.substring(0,the_length-1);
    
  }
  
  if(val1 == ''){  val1 = '-';}
  
  return (val1);

}


var DataCache = {}