
fixIEBugIndexOf();

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function show_var(s,na,t){
	na ? na=true : na=false;
	t? t = t+"\t": t="\t";
	var r='';
	if (typeof(s)=="object") {
        var o = s;
        for (var i in o){
			if ((typeof(o[i])=="object" || typeof(o[i])=="array")&& o[i])
				o[i] = "\n"+	show_var(o[i],true,t);

            r += t+i+"="+o[i]+"\n";
		}
    }else if (typeof(s)=="array") {
        var a = s;
        for (var j=0; j< a.length; j++){
			if ((typeof(a[j])=="object" || typeof(a[j])=="array")&& a[j])
				a[j] = "\n"+	show_var(a[j],true,t);			
            r += t+j+"="+a[j]+"\n";
		}
    }else r=t+s;
	
	if (na) return r;
		else alert(r); 
}
function isSet( variable ){
	return( typeof( variable ) != 'undefined' );
}
function select_chbox_PSSearch(id){
	select_chbox(id);
	switch (id) {
		case 'parties':
			deselect_chbox('speakers');
		break;
		case 'speakers':
			deselect_chbox('parties');
			break;

	}
}
function select_chbox(id){
	var elem = document.getElementById('chbox_'+id); 
	if (elem){
		document.getElementById('chbox_'+id).checked = true;
	}
}
function deselect_chbox(id){
	var elem = document.getElementById('chbox_'+id); 
	if (elem){
		document.getElementById('chbox_'+id).checked = false;
	}
}
function toggle_checkbox(id){
	var elem = document.getElementById(id); 
	if (elem){
		
		elem.checked = !elem.checked;
	}
}

function getElementHeight(elem){
	if (document.getElementById(elem)){
		return document.getElementById(elem).offsetHeight;
	}else {
		return 0;
	}
}

function getClientWindSize(){
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	  //Non-IE
	  myWidth = window.innerWidth;
	  myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	  //IE 6+ in 'standards compliant mode'
	  myWidth = document.documentElement.clientWidth;
	  myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	  //IE 4 compatible
	  myWidth = document.body.clientWidth;
	  myHeight = document.body.clientHeight;
	}
	
	return Array(myWidth, myHeight);
}

function showHideElem(id){
	
	var elem = document.getElementById(id);

    if (elem){
        if (elem.style.display == 'none' || elem.style.display == ''){
            elem.style.display = 'block';
        }else {
            elem.style.display = 'none';
        }
    }
}

function checkFlashVersionWarning(){
	var version_good = DetectFlashVer(9,0,124);
	
	if (!version_good) {
		$(document).ready(function() {
			$('#main_warnings>.ui-state-error.ui-corner-all,#check_flash_version_warning').show();
		});
	}
} 

function fixIEBugIndexOf(){
	//Adding array.indexOf for compability with IE 
	if(!Array.prototype.indexOf){
	    Array.prototype.indexOf = function(obj){
	        for(var i=0; i<this.length; i++){
	            if(this[i]==obj){
	                return i;
	            }
	        }
	        return -1;
	    }
	}
}

   /*
   **  Returns the caret (cursor) position of the specified text field.
   **  Return value range is 0-oField.length.
   */
   function getCaretPosition (oField) {

     // Initialize
     var iCaretPos = 0;

     // IE Support
     if (document.selection) { 

       // Set focus on the element
       oField.focus ();
  
       // To get cursor position, get empty selection range
       var oSel = document.selection.createRange ();
  
       // Move selection start to 0 position
       oSel.moveStart ('character', -oField.value.length);
  
       // The caret position is selection length
       iCaretPos = oSel.text.length;
     }

     // Firefox support
     else if (oField.selectionStart || oField.selectionStart == '0')
       iCaretPos = oField.selectionStart;

     // Return results
     return (iCaretPos);
   }


function socButtonHow(target,h){
    var url = $("img",target).attr("src");
    if (h==1)
        url = url.replace(/(.+?)(\w+?)(\.(jpeg|gif|jpg|png))/i,'$1$2_h$3');
    else
        url = url.replace(/(.+?)(\w+?)(?:_h)(\.(jpeg|gif|jpg|png))/i,'$1$2$3');
    
    $("img",target).attr("src",url);


}

function parseFormElement(_this){
    var result = {};

    switch (_this.tagName.toLowerCase()) {
        case "select":
        case "button":
            result.name = _this.name;
            result.value= _this.value;
            break;

        case "input":
            switch (_this.type.toLowerCase()) {
                case 'checkbox':

                    result.name = _this.name;

                    if (_this.checked)
                        result.value = _this.checked;
                    else
                        result = {};

                    break;
                case "radio":
                case "button":
                case 'hidden':
                case 'text':
                default:
                    result.name = _this.name;
                    result.value= escape(_this.value);

                    break;
            }

            break;

        default:
            break;
    }

    return result;
}

function setCookie(c_name, value, expiredays){
    var exdate=new Date();

    exdate.setDate(exdate.getDate()+expiredays);

    document.cookie=c_name+ "=" +escape(value)+

    ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie( c_name ){
    if ( document.cookie.length>0 ){
        
      c_start = document.cookie.indexOf( c_name + "=" );
      
      if ( c_start != -1 ){
        c_start = c_start + c_name.length+1;

        c_end   = document.cookie.indexOf( ";", c_start);

        if (c_end==-1) c_end=document.cookie.length;

        return unescape(document.cookie.substring(c_start,c_end));

      }
    }
    return "";
}

function getPAVisits(){
    return  parseInt( getCookie('pa_visits') ) || 0;
}

function incrementPAVisits(  ){
    
    var visits =  getPAVisits();

    setCookie('pa_visits', visits + 1 , 365);
    
    return visits;
}
