var popupWindow = {
  open: function(options)
  {
    this.options = {
      url: '#',
      width: 340,
      height: 360,
      name:"_blank",
      location:"no",
      menubar:"no",
      toolbar:"no",
      status:"no",
      scrollbars:"no",
      resizable:"no",
      left:"",
      top:"",
      normal:false
    }
    Object.extend(this.options, options || {});

    if (this.options.normal){
        this.options.menubar = "yes";
        this.options.status = "yes";
        this.options.toolbar = "yes";
        this.options.location = "yes";
    }

    this.options.width = this.options.width < screen.availWidth?this.options.width:screen.availWidth;
    this.options.height=this.options.height < screen.availHeight?this.options.height:screen.availHeight;
    var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
    if (this.options.top!="")openoptions+=",top="+this.options.top;
    if (this.options.left!="")openoptions+=",left="+this.options.left;
    window.open(this.options.url, this.options.name, openoptions);
    return false;
  }
}

function is_email(email) {
	var splitted = email.match("^(.+)@(.+)$");
	if(splitted == null) return false;
	if(splitted[1] != null ){
		var regexp_user=/^\"?[\w-_\.]*\"?$/;
		if(splitted[1].match(regexp_user) == null) return false;
	}
	if(splitted[2] != null)	{
		var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
		if(splitted[2].match(regexp_domain) == null){
			var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
			if(splitted[2].match(regexp_ip) == null) return false;
		}// if
		return true;
	}
	return false;
}
 
function getAttributesValue(attributes, attribname) {
	var result = null;
	if (attributes && attributes.length > 0) {
		for (var i=0; i<attributes.length-1; i++)
			if (attributes[i].name==attribname) 
				break ;
		result = attributes[i].value;
	}
	return result;
	
}

function getAttributesIndexOf(attributes, attribname) {
	var result = null;
	if (attributes && attributes.length > 0) {
		for (var i=0; i<attributes.length-1; i++)
			if (attributes[i].name==attribname) 
				break ;
		result = i;
	}
	return result;
}

function urlencode(str) {
	str = escape(str);
	str = str.replace(new RegExp('[\+]','g'), '%2B');
	str = str.replace('[\s]', '%20');
	str = str.replace(new RegExp('[\*]','g'), '%2A');
	str = str.replace(new RegExp('/','g'), '%2F');
	str = str.replace(new RegExp('@','g'), '%40');
	return str;
}

function makeSearchUrl() {
	var objPage = document.getElementById('search_page');
	var objText = document.getElementById('search_text');
	var objDtDeb = document.getElementById('search_dtdeb');
	var objDtFin = document.getElementById('search_dtfin');
	
	if (objPage && objText && objDtDeb && objDtFin) {
		
		var args = new Array();
		var i = 0;
		if (objText.value.length > 0) {
			args[i] = 'text=' + urlencode(objText.value);
			i++;
		}
		if (objDtDeb.value.length > 0) {
			args[i] = 'dtdeb=' + urlencode(objDtDeb.value);
			i++;
		}
		if (objDtFin.value.length > 0) {
			args[i] = 'dtfin=' + urlencode(objDtFin.value);
			i++;
		}
		
		var url = objPage.value;
		if (i > 0) {
			url = url + '?search=1&' + args.join('&');
		}
	}
	
	if (url && url.length > 0) {
		window.location.replace(url);
	}
}

function clearSearchForm() {
	var objPage = document.getElementById('search_page');
	var objText = document.getElementById('search_text');
	var objDtDeb = document.getElementById('search_dtdeb');
	var objDtFin = document.getElementById('search_dtfin');
	
	if (objPage && objText && objDtDeb && objDtFin) {
		objText.value = objText.attributes[getAttributesIndexOf(objText.attributes, 'emptyvalue')].value;
		objDtDeb.value = objDtDeb.attributes[getAttributesIndexOf(objDtDeb.attributes, 'emptyvalue')].value;
		objDtFin.value = objDtFin.attributes[getAttributesIndexOf(objDtFin.attributes, 'emptyvalue')].value;
		var url = objPage.value + '?search=0';
	}
	
	if (url && url.length > 0) {
		window.location.replace(url);
	}
	
}

function getNodeValue(node){
	if (Prototype.Browser.IE || typeof node.text != 'undefined') 
		return node.text;
	else
		return node.textContent;
}

function getSelectedValue(select){
	var select = $(select);
	return select.options[select.selectedIndex].value;
}

function setSelectedClass(container, tagName, idTag, nameTag, cssTag){
	if (container && container.childNodes.length) {
		for (var i=0; i<container.childNodes.length-1; i++) {
			var n = getAttributesValue(container.childNodes[i].attributes,'name');
			if (n){
				container.childNodes[i].className = container.childNodes[i].className.replace(new RegExp(' sel','gi'),'');
			}
			if (container.childNodes[i].id && container.childNodes[i].id == idTag) {
				container.childNodes[i].className = container.childNodes[i].className + cssTag;
			}
		}
	}
}

function selectVideo(info) {
	setSelectedClass($('elements'), 'div', 'element'+info.p1, 'element', ' sel');
	ajaxRequest(info);
}

function selectAlbum(info) {
	setSelectedClass($('albums'), 'div', 'album'+info.p1, 'album', ' sel');
	ajaxRequest(info);
}

function selectPhoto(info) {
	ajaxRequest(info);
	//ajaxRequest({script:"updatePhotoNav()",target:"photonav",p1:null,waitFlag:false});
}

function selectMedia(info) {
	setSelectedClass($('medias'), 'div', 'media'+info.p1, 'media', ' sel');
	ajaxRequest(info);
}


function ajaxRequest(info, waitFlag) {
    var test = $('selectChanel');
	var url = 'ajax.php';
	var h = $H(info);
	if (typeof info.waitFlag == 'undefined')
		$(info.target).innerHTML = '<div class="throbber"><img src="style/rsc/throbber.gif" alt="" title=""></div>';
	var myAjax = new Ajax.Request(
		url,
		{
			method: 'post',
			encoding: 'UTF-8',
			parameters: h.toQueryString(),
			onComplete: ajaxShowResponse
		}
	);
}
        
function ajaxShowResponse(requete, JSON) {
	var xml = requete.responseXML.childNodes[0];
	if (xml) {
		var target = getNodeValue(xml.childNodes[0]);
		$(target).innerHTML = getNodeValue(xml.childNodes[1]);
		if (xml.childNodes[2]) {
			scriptJS = getNodeValue(xml.childNodes[2]);
			eval(scriptJS);
		}
	}
}

