/*
*	Common javascript functions
*
*/
//alert(1);
var getObj = function(object, doc) {
	if(doc == undefined || !doc || typeof doc != typeof {}) {
		doc = self.document;
	}
	if(typeof object == typeof " "){
		return doc.getElementById(object);
	}
	else{
		return false;
	}
}

var formSubmit = function(formId, fieldsToModify, msg, onSubmit){
	var x = getObj(formId);
	msg1 = msg ? msg : 'Esti sigur?';
	if(typeof(fieldsToModify) == 'object') {
		for(var i in fieldsToModify){
			x.elements[i].value = fieldsToModify[i];
		}
	}
	else{
		
	}
	if(msg == 'none' ){
		if(onSubmit) {
			return true;
		}
		x.submit();
	}
	else if(confirm(msg1)){
		if(onSubmit) {
			return true;
		}
	return x.submit();
	}
	return false;
}


var getArray = function(inputString, mode){
	mode = mode ? parseInt(mode) : 0;
	var ret = new Array();
	switch(mode){
		case 1:
			try{
				eval("ret = " + inputString + ";");
			}
			catch(e){
				;
			}
			break;
		default:
			if(inputString.indexOf(';') > -1) {
				var pieces = inputString.split(';');
				for(var i in pieces) {
					var piece = pieces[i].split(':');
					ret[piece[0]] = piece[1];
				}
			}
			else {
				var piece = inputString.split(':');
				ret[piece[0]] = piece[1];
			}
		}
	return ret;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

var initializeObj = function(object,args){
        var expr = "new "+object+"('"+args+"');";
		var func = function(){
                var x =  eval(expr);
				return x;
            }
        if(window.addEventListener){ // Mozilla, Netscape, Firefox
            window.addEventListener('load', func, false);
        }
        else{// IE
            window.attachEvent('onload', func);
        }
    }
var initializeFun = function(func,args, evnt){
		var events = new Array();
		events['load'] = 'load';
		events['resize'] = 'resize';
		events['unload'] = 'unload';
		var ieEvents = {'load':'onload', 'resize':'onresize', 'unload':'onunload'};
		var mozEvents = {'load':'load', 'resize':'resize', 'unload':'unload'};
		evnt = evnt != undefined && events[evnt] ? evnt : 'load';
		if(args){
			var expr = func+"('"+args+"');";
		}
		else{
			var expr = func+"();";
		}
		func = function(){
			try{
                return eval(expr);
            }
			catch(ex){return false;}
		};
        if(window.addEventListener){ // Mozilla, Netscape, Firefox
            window.addEventListener(mozEvents[evnt], func, false);
        }
        else{ // IE
            window.attachEvent(ieEvents[evnt], func);
        }
    }

var setSelectValue = function(selectId, val){
	if(typeof selectId == typeof "string"){
		selectId = getObj(selectId);
	}
	if(selectId.tagName.toLowerCase() == 'select'){
		var j = -1;
		for(var i in selectId.options){
			if(i == parseInt(i) && typeof selectId.options[i] == typeof {});
			else{
				continue;
			}
			if(selectId.options[i].value == val){
				selectId.selectedIndex = i;
				selectId.focus();
				j = i;
				return j;
			}
		}
	}
	selectId.selectedIndex = null;
	for(var i in selectId.options){
		if(i == parseInt(i) && typeof selectId.options[i] == typeof {});
		else{
			continue;
		}
		selectId.options[i].selected=false;
	}
	return j;
}

var setSelectOptions = function(selectId,array, defaultOption){
	if(typeof selectId == typeof "string"){
		selectId = getObj(selectId);
	}
	if(selectId.tagName.toLowerCase() == 'select'){
		selectId.options.length = 0;
		var i=0;
		if(defaultOption){
			selectId.options[i] = new Option(defaultOption, 0);
		}
		if(typeof array == typeof "string"){
			array = getArray(array);
		}
		for(var x in array){
			if(array[x] != undefined){
				selectId.options[++i] = new Option(array[x], x);
			}
		}
	}
}

var selSelectedItem = function(selectId, array){
	if(typeof selectId == typeof "string"){
		selectId = getObj(selectId);
	}
	if(selectId.tagName.toLowerCase() == 'select'){
		if(typeof array == typeof "string"){
			if(array.indexOf("{")!=-1){
				array = getArray(array,1);
			}
			else {
				if(array.indexOf("[")!=-1){
					array = getArray(array,2);
				}
				else{
					array = getArray(array,0);
				}
			}
		}
		if(array.length){
			for(var i in array){
				var isInList = false;
				for(var j in selectId.options){
					isInList |= selectId.options[j].value == i;
					if(isInList){
						break;
					}
				}
				if(isInList){
					setSelectValue(selectId, i);
				}
				else{
					var newId = selectId.options.length;
					selectId.options[newId] = new Option(array[i],i);
					selectId.selectedIndex = newId;
				}
				break;// doar o valoare poate fi 'selected'
			}
		}
	}
}

var openWnd = function (url, wndName, wndScroll, wndWidth, wndHeight){
	url = url != undefined && url > ' ' ? url : "/";
	wndScroll = wndScroll!=undefined && (wndScroll+'')!='undefined' && (wndScroll+'')>'' ? (wndScroll?1:0) : 1;
	wndName = wndName ? wndName : 'wndDefault';
	wndWidth = wndWidth ? wndWidth : 750;
	wndHeight = wndHeight ? wndHeight : 500;
	var wndLeft = parseInt((screen.availWidth - wndWidth) / 2);
	var wndUp = parseInt((screen.availHeight - wndHeight) / 2);
	var setUp='left='+wndLeft+',top='+wndUp+',width='+wndWidth+',height='+wndHeight + ',scrollbars='+wndScroll + ',status=0,toolbar=0,location=0,hotkeys=0,titlebar=0,directories=0,menubar=0,resizable='+wndScroll;
	var w = window.open(url, wndName, setUp); 
	w.focus();
	return w;
}



