function addEvent(obj,evt,fnc,useCapture){
	if (!useCapture) {
		useCapture=false;
	}
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) {
		return obj.attachEvent("on"+evt,fnc);
	}
	else{
		myAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ myFireEvent(obj,evt) };
	}
} 

function myAttachEvent(obj,evt,fnc){
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}

function myFireEvent(obj,evt){
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}

String.prototype.ltrim = function () {
    for (var i = 0; i < this.length; i++) {
        if (this.charAt(i) != " ") {
            return this.substr(i, this.length);
        }
    }
}

String.prototype.rtrim = function () {
    for (var i = this.length - 1; i >= 0; i--) {
        if (this.charAt(i) != " ") {
            return this.substr(0, i + 1);
        }
    }
}

String.prototype.trim = function () {		var tmp = this.ltrim();
		if(!tmp)tmp='';
    if(tmp.length>0){
    	tmp = tmp.rtrim();	
    }
    return tmp;
}
function getBrawserSize(){	var x,y;
	if (self.innerHeight) {// all except Explorer	
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
		// Explorer 6 Strict Mode	
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) {		// other Explorers	
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	wnd = new Object();
	wnd.scrollTop = document.documentElement.scrollTop != undefined ? document.documentElement.scrollTop : (self.pageYOffset != undefined ? self.pageYOffset : 0);
	wnd.width=x;
	wnd.height=y;
	wnd.pageWidth = document.body.scrollWidth != undefined ? document.body.scrollWidth : (document.width != undefined ? document.width : (document.body.style.pixelWidth != undefined ? document.body.style.pixelWidth : x) );
  wnd.pageHeight = document.body.scrollHeight != undefined ? document.body.scrollHeight : (document.height  != undefined ? document.height : (document.body.style.pixelHeight != undefined ? document.body.style.pixelHeight : y) );
	return wnd;
}
function getLeft(el)
{
 var x;
 x = el.offsetLeft;
 if (el.offsetParent != null)
  x += getLeft(el.offsetParent);
 return x;
}
function getTop(el)
{
 var x;
 x = el.offsetTop;
 if (el.offsetParent != null)
  x += getTop(el.offsetParent);
 return x;
}

function open_w(URL,info,w,h) {
 var x = (window.screen.width - w)/2;
 var y = (window.screen.height - h) /2.5; 
 window.open(URL,info,'width='+w+',height='+h+',top='+y+',left='+x+',toolbar=no,personalbar=no,location=no,directories=no,statusbar=no,menubar=no,status=no,resizable=yes');
}

function htmlentities(str){	str=str.split('"').join('&quot;');
	str=str.split("'").join('&#039;');
	return str;	
}
function getID(id) {	return document.getElementById(id);	
}

function showID(id) {	getID(id).style.display = "";
}

function hideID(id) {
	getID(id).style.display = "none";
}

function showHideID(id) {	getID(id).style.display == 'none' ? showID(id) : hideID(id);	
}

function getDim(id) {  
	document.getElementById(id).style.height="auto";
	if (document.all) {
		dh = document.getElementById(id).offsetHeight+10;
	}
	else {
		dh = document.getElementById(id).offsetHeight;
	}
	return dh;
}

function removeElement(el){	el.parentNode.removeChild(el);
}

function dropFile(btn,type) {
 if(document.getElementById) {
	 tr = btn;
	  while (tr.tagName != 'TR') tr = tr.parentNode;
	  tr.parentNode.removeChild(tr);
	  checkForLast(type);
 }
}

function addFile(btn,type) {
 if(document.getElementById) {
 	tr = btn;
  while (tr.tagName != 'TR') tr = tr.parentNode;
  var newTr = tr.parentNode.insertBefore(tr.cloneNode(true),tr.nextSibling);
  thisChilds = newTr.getElementsByTagName('td');
  checkForLast(type);
 }
}
function checkForLast(type){
	btns = document.getElementsByName(type);
	for (i = 0; i < btns.length; i++){
		btns[i].disabled = (btns.length == 1) ? true : false;
	}
}
function findClass(cName){
	var list = new Array();
	var nodes = document.getElementsByTagName('*');
	for (i = 0; i<nodes.length; i++){
		if (nodes[i].className.indexOf(cName)!=-1){
	  	list.push(nodes[i]);
	 	}
	}
	return list;
}
function insertScript(target,script){
    try {
        var el = document.createElement('script');
        el.type="text/javascript";
        el.innerHTML = script;
        target.appendChild(el);
    } catch (e) {
        var el = document.createElement('span');
        target.appendChild(el);
        el.innerHTML = "<br /><scr"+"ipt type='text/javascript' defer='defer'>"+script+"</script" + ">";
    }
}

function showCities(region){
	ajax.load('/operator.php?action=getCities&region='+region,showCitiesResult,'j','POST');	
}
function showCitiesResult(data){
	sel = getID('citiesList');
	sel.length = 0;
	for (i=0; i<data.length;i++){
		opt = document.createElement('option');
	  opt.text = data[i].title;
	  opt.value = data[i].id;
	  try  {
 			sel.add(opt,null); // standards compliant
  	} catch(ex) {
  		sel.add(opt); // IE only
  	}
	}                              
}
function showModels(brand){
	ajax.load('/operator.php?action=getModels&brand='+brand,showModelsResult,'j','POST');	
}
function showModelsResult(data){
	sel = getID('modelsList');
	sel.length = 0;
	for (i=0; i<data.length;i++){
		opt = document.createElement('option');
	  opt.text = data[i].title;
	  opt.value = data[i].id;
	  try  {
 			sel.add(opt,null); // standards compliant
  	} catch(ex) {
  		sel.add(opt); // IE only
  	}
	}                              
}

