﻿// JScript File
//this is a javascript library for Papirfly as
//which provides functions for getting and setting values of different
//types of webcontrols
//It will also contain function for enabling/disabling controls
//
var DEBUG=1;
function debug(file,func,what){
    if(DEBUG===1){    
        alert('DEBUG\nFile: '+file+'.\nFunction: '+func+'.\nOutput:\n'+what);
    }
}
//for javascript errors
onerror=handleError
//javascript error handler
function handleError(msg,url,l)
{
    var txt="Det var en feil på denne siden.\n\n"
    txt+="Feil: " + msg + "\n"
    txt+="URL: " + url + "\n"
    txt+="Linje: " + l + "\n\n"
    txt += "Vennligst kopier denne meldingen og send til feil@papirfly.no\n\n";
    txt+="Velg OK for å fortsette.\n\n"
    alert(txt)
    //let us handle the error NOT the browser (else set it to return false)
    return true
}
//MacroMedia function to get an object on the page
function MM_findObj(n, d) { //v4.01
    var p,i,x;  
    if(!d) 
	    d=document; 
    if((p=n.indexOf("?"))>0&&parent.frames.length) {
	    d=parent.frames[n.substring(p+1)].document; 
	    n=n.substring(0,p);
	}
    if(!(x=d[n])&&d.all) 
        x=d.all[n]; 
    for (i=0;!x&&i<d.forms.length;i++) 
	    x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
	    x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) 
	    x=d.getElementById(n); 
    	
    return x;
}

//A macromedia function for setting an obects property
 function MM_changeProp(objName,x,theProp,theValue) { //v6.0
    var obj = MM_findObj(objName);    
    if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
        eval("obj."+theProp+"='"+theValue+"'");
    }
}

//a wraparound for getting objects
function getObj(it){
    var retur = MM_findObj(it);
    if(retur){
	    return retur;
    }
    else{
        debug('cant find object: '+it);
    }
}

    function setDebug(value){
        DEBUG=value;
    }


//function to get an objects value
function getObjectValue(it){
	var ob = getObj(it);	
	if(ob){												
		if(ob.value.length>0){
		
			return ob.value;
		}
		else{
		   // debug('object ('+it+') value is empty');
		    return '';
		}
	}
	else{
	  //  debug('cant find object'+it);
	}	
}
//function to set an objects value
function setObjectValue(it,value){
	var ob = getObj(it);	
	if(ob){												
		ob.value=value;
	}
	else{
	    debug('Can\'t find object with id: '+it);
	    }
}	

//function to set an objects value
function setObjectAttribute(it,attribute,value){
	var ob = getObj(it);	
	if(ob){	
	    eval('ob.'+attribute+'=\"'+value+'\"');												    
	}
	else{
	    debug('Can\'t find object with id: '+it);
	}
}	


//function to get the value of an array embedded in the page somewhere
function getArrayListValue(arrayname, index){
   // debug('getting arraylist with name: ' +arrayname);   
    if(arrayname){														
   //     debug('arraylist value for index ' +index);
        
        if(eval(arrayname+'['+index+']')){
		    return eval(arrayname+'['+index+']').replace(',','.');	
		}
		else{
		 //   debug('no value for ' +arrayname+'['+index+']');
		}
	}
	else{
	  //  debug('no arraylist with name: ' +arrayname);
	}   
	return '';
}


//function to get the value of an selected option for a select control
function getSelectedValue(it){
	var ob = getObj(it);	
	if(ob){
		for(var i = 0; i < ob.length; i++) {
			if ((ob.options[i] != null) && (ob.options[i].selected)){
				return ob.options[i].value;
			}
		}
	}
	else{
	    //alert('Cant find '+it);
	}
	return '';
}

//function to get the text of an selected option for a select control
function getSelectedItem(it, index){
	var ob = getObj(it);	
	if(ob){
		return ob.options[ob.selectedIndex].text
	}
	else{
	    //alert('Cant find '+it);
	}
	return '';
}
//Function to clear selected item
function clearSelected(it){
    var ob = getObj(it);
    if(ob){
        for(var i=0; i < ob.options.length;i++){
            ob.options[i].selected='';
        }
    }
}
//Function to set a selected item value
function setSelectedIndexByValue(it,val){
    var ob = getObj(it);
    if(ob){
    debug('pf_library.js','setSelectedIndex','we found object: '+it+' we are traversing the options');
        for(var i=0; i < ob.options.length;i++){
            if(ob.options[i].value===val){
                ob.options[i].selected='selected';
                debug('pf_library.js','setSelectedIndex','verdi: '+ob.options[i].value);
            }
        }
    }    
}
//Function to set a selected item value
function setSelectedIndex(it,index){
    var ob = getObj(it);
    if(ob){
       debug('pf_library.js','setSelectedIndex','we found object: '+it+' we are traversing the options');
       ob.options[index].selected ='selected';
    }
}
//Function to set a selected item text
function setSelectedIndexByText(it,txt){
    var ob = getObj(it);
    if(ob){
        for(var i=0; i < ob.options.length;i++){
            if(ob.options[i].Text===txt){
            ob.options[i].selected='selected';
            }
        }
    }
}

//Function to set a selected item index
function setSelectedValue(it,index){
    var ob = getObj(it);
    if(ob){
        if(ob.options[index]){
            ob.options[index].selected='selected';
        }
    }
}

//Function to get a selected index from a select html element
function getSelectedIndex(it){
    var ob = getObj(it);
    if(ob){
        for(var i=0; i < ob.options.length;i++){
            if(ob.options[i].selected){
                return i;
            }
        }
    }
    return -1;
}

//a function to expand or colapse a div tag
function expandCollapse(divid, imageid, openimage, closedimage) {
//debug('looking for '+ divid);
    var tmp = getObj(divid);    
    var img = getObj(imageid);
    if(tmp){
     //   debug('we found div node: '+divid);
        if (tmp.style.display == "block") {
		    tmp.style.display = "none";
		    if(img){
		      //  debug('we found image node: '+imageid);
		        img.src = closedimage;//"../../admin/images3/ec_closed.gif";
		    }
        }
	    else {
		    tmp.style.display = "block";
		    if(img){
		       // debug('we found image node: '+imageid);
		        img.src = openimage;//"../../admin/images3/ec_open.gif";
		    }
	    }
	}
	else{
	    debug('No node found with id: '+divid);
	}
}
function getDisplayStyleForObject(divid){
    var tmp = getObj(divid);
    if(tmp){
        return tmp.style.display;
    }
}





//function to format an input string which it returns
//the format must be in the way "### ### ###" where '#' -represents any char(alphanumeric) and ' ' denotes a space
function format(input, format){
    var arrays = format.split(' ');
    var retur ='';
    
    var start =0;
    var tmp='';
    var tmp1 ='';
    var length=0;
    tmp1= ''+removeSpace(input.toString());
    var ptr= tmp1.length;
   // debug('input is: '+tmp1);
   // debug('arrays length is: '+arrays.length);
    for(i = arrays.length-1;i>=0;i--){        
        tmp = arrays[i];
        if(tmp){
     //       debug('handling arrays index: '+i+'. value is: '+tmp);
     //       debug('ptr: '+ptr+'. tmp.length: '+tmp.length);
            length = tmp.length;
            if(tmp.length>ptr)
                length=ptr;
                
            start = ptr - tmp.length;
            
            ptr = start;
            if(start<0){
                start=0;
            }
            if(ptr<0){
                ptr =0;
            }
           
      //      debug('start is: '+start);
      //      debug('length is: '+tmp.length);        
      //      debug('tmp1.substring is: '+tmp1.substr(start,length));
            retur = trim(tmp1.substr(start,length))+' '+ retur;
       }
        tmp='';
    }
    return trim(retur.replace('.',','));
}

//removes spaces from a string
function removeSpace(value){
    retur = trim(value);
    var tmp = retur.split(' ');
    retur ='';
    for(var i=0;i<tmp.length;i++){
        if(trim(tmp[i]).length>0){
            retur += trim(tmp[i]);
        }
    }
    return retur;
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
}

//function to convert a string with spaces to an int
function convertToInt(num,def){
    if(num){
        num = trim(num);
        if(num.length === 0){
            return def;
        }
        if(num.indexOf(' ')>-1){
            var nums = num.split(' ');
            var tmp ='';
            for(i=0; i < nums.length;i++){
                tmp += nums[i];
            }
            num = tmp;
        }
        
        if(isNaN(num)){
            return def;
        }
        else{
            return parseInt(num,10);
        }
    }
    else{
        return def;
    }
}
//function to convert a string with spaces to an int
function convertToFloat(num,def){
    if(num){
        num = trim(num);
        if(num.length === 0){
            return def;
        }
        if(num.indexOf(' ')>-1){
            var nums = num.split(' ');
            var tmp ='';
            debug('parsefloat\nWefound '+nums.length+' parts of a number');
            for(i=0; i < nums.length;i++){
                tmp += nums[i];
            }
            num = tmp;
        }
        
        if(isNaN(num)){
            debug(num+" is not a number");
            return def;
        }
        else{
            return parseFloat(num,10);
        }
    }
    else{
        return def;
    }
}
	
	
	//function to get a XMLHTTP OBJECT for AJAX use should be browser independent
function getXMLHttpObj(){
	if(typeof(XMLHttpRequest)!='undefined')
		return new XMLHttpRequest();

	var axO=['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.4.0',
		'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'], i;
	for(i=0;i<axO.length;i++)
		try{
			return new ActiveXObject(axO[i]);
		}catch(e){}
	return null;
}
