//
// Copyright (c) 2007 Christophe Van Durme (http://www.lithium.be)
//
var Gid_name;
var Gid;

function Contribute() {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 } 
	 
	var url= "list.ajax.php?mode=contribute";
	xmlHttp.onreadystatechange=MyWorksFill
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function SetDraggables (){
		
		var artworks = getElementsByClassName('artwork');
		for(i=0; i < artworks.length ;i++){
			//alert(artworks[i].id);
			new Draggable(artworks[i].id, {revert:true});
		}
		
}

function ChangeDetail(id_name,id){
	
	Gid_name = id_name;
	Gid = id;
	
	new Ajax.Updater('detail', "detail.ajax.php?"+id_name+"="+id,{onComplete:SetInstantEdit});
}
function ChangeDetailSort(id_name,id,sortfield,direction){
	
	Gid_name = id_name;
	Gid = id;

	new Ajax.Updater('detail', "detail.ajax.php?"+id_name+"="+id+"&sortfield="+sortfield+"&direction="+direction,{onComplete:SetInstantEdit});
}
function SetInstantEdit () {
	setVarsForm(Gid_name+"="+Gid);
	editbox_init()	;
}

function ToggleEdit (field,id) {
	
	//alert("test");
	
	if (document.getElementById('span_'+field).style.display == 'none')	{
		
		if (id && !this.confirmopen) {
			this.confirmopen = true;
			if(editable){
				if(confirm("Are you sure you want to edit this?")) {
						value = document.getElementById('edit_'+field).value;
						value = value.replace("\n","<br/>")
						new Ajax.Updater('span_'+field, "detail.script.php?artwork_id="+id+"&content="+value+"&fieldname="+field);
						
				}
			}else{
				alert("You have to log in to edit!");
			}
			this.confirmopen = false;
		}
		document.getElementById('edit_'+field).style.display = 'none'; 
		document.getElementById('span_'+field).style.display = 'inline'; 
	} else {
		document.getElementById('edit_'+field).style.display = 'inline'; 
		document.getElementById('span_'+field).style.display = 'none'; 
		
		document.getElementById('edit_'+field).focus();
		//document.getElementById('edit_'+field).click();
	}
}

function ToggleEditArtist (field,id){
	if (document.getElementById('span_'+field).style.display == 'none')	{
		
		
		if (id && !this.confirmopen) {
			this.confirmopen = true;
			if(editable){
				if(confirm("Are you sure you want to edit this?")) {
					value = document.getElementById('edit_'+field).value;
					value = value.replace("\n","<br/>")
					new Ajax.Updater('span_'+field, "detail.script.php?artist_id="+id+"&content="+value+"&fieldname="+field);
				}
			}else{
				alert("You have to log in to edit!");
			}
			this.confirmopen = false;
		}
		document.getElementById('edit_'+field).style.display = 'none'; 
		document.getElementById('span_'+field).style.display = 'inline'; 
	} else {
		document.getElementById('edit_'+field).style.display = 'inline'; 
		document.getElementById('span_'+field).style.display = 'none'; 
		
		document.getElementById('edit_'+field).focus();
		document.getElementById('edit_'+field).click();
	}
}

function ToggleEditGallery (field,id){
	if (document.getElementById('span_'+field).style.display == 'none')	{
		
		
		if (id && !this.confirmopen) {
			this.confirmopen = true;
			if(editable){
				if(confirm("Are you sure you want to edit this?")) {
					value = document.getElementById('edit_'+field).value;
					value = value.replace("\n","<br/>")
					new Ajax.Updater('span_'+field, "detail.script.php?gallery_id="+id+"&content="+value+"&fieldname="+field);
				}
			}else{
				alert("You have to log in to edit!");
			}
			this.confirmopen = false;
		}
		document.getElementById('edit_'+field).style.display = 'none'; 
		document.getElementById('span_'+field).style.display = 'inline'; 
	} else {
		document.getElementById('edit_'+field).style.display = 'inline'; 
		document.getElementById('span_'+field).style.display = 'none'; 
		
		document.getElementById('edit_'+field).focus();
		document.getElementById('edit_'+field).click();
	}
}

function ToggleNoEdit(field,id){
	alert("You have to be logged in to edit!");
}

function SetField(field,value){
	document.getElementById(field).value = value;
}

function getElementsByClassName(needle) {
   var my_array = document.getElementsByTagName("*");
   var retvalue = new Array();
   var i;
   var j;

   for (i=0,j=0;i<my_array.length;i++) {
	  var c = " " + my_array[i].className + " ";
	  if (c.indexOf(" " + needle + " ") != -1) retvalue[j++] = my_array[i];
   }
   return retvalue;
} 

var run = false;
var screencount=0;
var timerID = null;
var timerRunning = false;
var timerInc = 250;
var searchStringMem = new Array();
var searchString = new Array();

function RunSearch(searchField) {	
  var now = new Date();
  var nowtime = now.getTime();
  
  Search(searchField);  

  timerID = setTimeout("RunSearch('"+searchField+"')",timerInc);
  timerRunning = true;

}

function Search(searchField)
{ 
	var mode = document.getElementById("mode").value;
	
	if(document.getElementById(searchField)){
		searchString[searchField] = document.getElementById(searchField).value;
	}
	
	if (searchStringMem[searchField]!=searchString[searchField]) {
		
		document.getElementById('detail').innerHTML = '';
		
	if (searchField!='limit_start')
		document.getElementById('limit_start').value = '';
		
	  document.getElementById('list').innerHTML='<div id="loading">Searching...</div>';
	  run = false;
		searchStringMem[searchField] = searchString[searchField];
		
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		 {
		 alert ("Browser does not support HTTP Request")
		 return
		 } 
		
		var url = "list.ajax.php?";
	
		var searchfields = getElementsByClassName('searchfield');
		
		for(i=0; i < searchfields.length ;i++){
			if(searchfields[i].value != "" && searchfields[i].value != "--- Sort by ---") {
				url += searchfields[i].name + '=' + searchfields[i].value + '&';
				if (searchfields[i].value) run = true;
			}
		}
		
		if(mode == "myworks") {
			run = true;
		}
	
		xmlHttp.onreadystatechange=stateChanged 
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
		}
		
		function stateChanged() 
		{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		 { 
		 if (run) {
		 	 var inhoud = xmlHttp.responseText;
			 document.getElementById('list').innerHTML = inhoud;
			 
			 if(inhoud.search("No Results")>-1){
			 	document.getElementById("sortby_div").style.display = "none";
			 }else {
			 	
			 	document.getElementById("sortby_div").style.display = "block";
			 }

			 SetDraggables();
		 } else {
		 	document.getElementById('list').innerHTML='';
			 document.getElementById("sortby_div").style.display = "none";
		 }
			 
		 }
		}
		
		function GetXmlHttpObject()
		{
		var xmlHttp=null;
		try
		 {
		 // Firefox, Opera 8.0+, Safari
		 xmlHttp=new XMLHttpRequest();
		 }
		catch (e)
		 {
		 //Internet Explorer
		 try
		  {
		  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		  }
		 catch (e)
		  {
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		 }
		return xmlHttp;
	}
}

function SetFormField(field,value)
{
	document.getElementById('artist_name').value = "";
	document.getElementById('artist_born').value = "";
	document.getElementById('artist_died').value = "";
	document.getElementById('artist_country').value = "";
	document.getElementById('artwork_title').value = "";
	document.getElementById('artwork_medium').value = "";
	document.getElementById('artwork_categorie').value = "";
	document.getElementById('artwork_price_min').value = "";
	document.getElementById('artwork_price_max').value = "";
	document.getElementById('artwork_year').value = "";
	document.getElementById('artwork_h').value = "";	
	document.getElementById('artwork_w').value = "";
	document.getElementById('artwork_d').value = "";
	document.getElementById('gallery_name').value = "";
	document.getElementById('gallery_city').value = "";
	document.getElementById('gallery_country').value = "";
	document.getElementById('sortby').value = "";
	
	document.getElementById(field).value = value;
	RunSearch(field);
}
function ViewAddArtwork()
{
	xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		 {
		 alert ("Browser does not support HTTP Request")
		 return
		 } 
		 
		var url= "addartwork.ajax.php";
		xmlHttp.onreadystatechange=AddstateChanged 
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
}
function ViewAddArtist()
{
	xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		 {
		 alert ("Browser does not support HTTP Request")
		 return
		 } 
		 
		var url= "addartist.ajax.php";
		xmlHttp.onreadystatechange=AddstateChanged 
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
}
function ViewAddGallery()
{
	xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		 {
		 alert ("Browser does not support HTTP Request")
		 return
		 } 
		 
		var url= "addgallery.ajax.php";
		xmlHttp.onreadystatechange=AddstateChanged 
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
}
function AddstateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
	 document.getElementById('detail').innerHTML=xmlHttp.responseText ;
 }
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

function Login() {
	var password = document.getElementById('password').value;
	var username = document.getElementById('username').value;
	
	var error_flag = 0;
	var message = "The following errors occured:\n\n";
		
	if(username == ""){
		error_flag = 1;
		message += "\t- Please enter your username.\n";		
	}
	
	if(password == ""){
		error_flag = 1;
		message += "\t- Please enter your password.\n";		
	}
	
	message += "\nPlease correct them in order to continue.";
	
	if(error_flag == 0) {
		document.getElementById('login').submit();
	}
	
	else {
		alert(message);	
	}	
}

function Register()
{
	xmlHttp=GetXmlHttpObject()
	xmlHttp2=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 } 
		 
	var url= "../register_required.php";
	xmlHttp.onreadystatechange=ShowRegister1
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	var url2= "../register_optional.php";
	xmlHttp2.onreadystatechange=ShowRegister2
	xmlHttp2.open("GET",url2,true)
	xmlHttp2.send(null)
}
function ShowRegister1()
{
	
	/* OUD */
	//document.getElementById('column_2').innerHTML = xmlHttp.responseText;
	
	/* NIEUW MANOL */
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		document.getElementById('column_2').innerHTML = xmlHttp.responseText;
	}	
	
	var column4 = document.getElementById('column_4');
	var column4bis = document.getElementById('column_4bis');
	
	if(column4 != null) {
		column4.innerHTML = "";
	}
	
	if(column4bis != null) {
		column4bis.innerHTML = "";
	}
}
function ShowRegister2()
{
	/* OUD */
	//document.getElementById('column_3').innerHTML = xmlHttp2.responseText;
	
	
	/* NIEUW MANOL */
	if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete"){
		document.getElementById('column_3').innerHTML = xmlHttp2.responseText;
	}
}
function ViewChangePass()
{
	
	if(document.getElementById('list')){document.getElementById('list').innerHTML="";}
	if(document.getElementById('sortby_div')){document.getElementById('sortby_div').innerHTML="";}
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 } 
		 
	var url= "../change_pass.php";
	xmlHttp.onreadystatechange=ShowChangePass
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}
function ShowChangePass()
{
	/* OUD */
	//document.getElementById('column_2').innerHTML = xmlHttp.responseText;
	
	
	/* NIEUW MANOL */
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		document.getElementById('column_2').innerHTML = xmlHttp.responseText;
	}
}
function ChangePass()
{
	if(!(document.getElementById('new1').value == document.getElementById('new2').value || document.getElementById('new1').value==""))
	{
		alert("The passwords aren't the same or are empty!");
	} else {
		params = 'new_pass=' + document.getElementById('new1').value;
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		} 
		var url= "../change_pass.script.php?" + params;
		xmlHttp.onreadystatechange=Change
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
}
function ChangeProfile()
{
		params = 'lname=' + document.getElementById('lname').value + '&fname=' + document.getElementById('fname').value + '&address_street=' + document.getElementById('address_street').value + '&address_pccity=' + document.getElementById('address_pccity').value + '&address_city=' + document.getElementById('address_city').value + '&email=' + document.getElementById('email').value;
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		} 
		var url= "../change_profile.script.php?" + params;
		xmlHttp.onreadystatechange=Change
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	
}
function Change() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
	 document.getElementById('column_2').innerHTML = "Data succesfully changed";
	 document.getElementById('column_3').innerHTML = "";
 }
}
function FillMyWorks()
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 } 
	 
	var url= "list.ajax.php";
	xmlHttp.onreadystatechange=MyWorksFill
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)

	RunSearch('artist_name');
}
function MyWorksFill()
{
	
	
	/* OUD */
	//document.getElementById('list').innerHTML = xmlHttp.responseText;
	
	
	/* NIEUW MANOL */
	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		document.getElementById('list').innerHTML=xmlHttp.responseText;
		
	}
	
}
function ShowLinks(cat)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 } 
	 
	var url= "links.ajax.php?cat=" + cat;
	xmlHttp.onreadystatechange=Links
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}
function Links()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 { 
		document.getElementById('detail').innerHTML = xmlHttp.responseText;
	 }
}



function checkAddArtwork(){
	var artist = document.getElementById('artist').value;
	var title = document.getElementById('title').value;
	var gallery = document.getElementById('gallery').value;
	var type = document.getElementById('type').value;
	var category = document.getElementById('category').value;
	
	var name = document.getElementById('gal_name').value;
	var city = document.getElementById('gal_city').value;
	var country = document.getElementById('gal_country').value;
	var email = document.getElementById('gal_email').value;
	var website = document.getElementById('gal_url').value;
	var text = document.getElementById('gal_text').value;
	
	var year = document.getElementById('year').value;
	var width = document.getElementById('awidth').value;
	var height = document.getElementById('aheight').value;
	var depth = document.getElementById('adepth').value;

	var price_observation_currency = document.getElementById('price_observation_currency').value; 
	var price_observation_date = document.getElementById('price_observation_date').value; 
	var price_observation_price = document.getElementById('price_observation_price').value; 
	var price_observation_type = document.getElementById('price_observation_type').value; 
	
	var error_flag = 0;
	var message = "The following errors occured:\n\n";
		
	if(title == ""){
		error_flag = 1;
		message += "\t- Please enter the title of the artwork.\n";		
	}
	
	if(artist == ""){
		error_flag = 1;
		message += "\t- Please specify the artist of the artwork.\n";		
	}
	
	if(document.getElementById('add_new_artist').style.display == "block") {
		var name = document.getElementById('art_name').value;
		var fname = document.getElementById('art_fname').value;
		var nationality = document.getElementById('art_nationality').value;
		
		if(name == "") {
			error_flag = 1;
			message += "\t- Please enter the last name of the artist.\n";
		}
		
		if(fname == "") {
			error_flag = 1;
			message += "\t- Please enter the first name of the artist.\n";
		}
		
		if(nationality == "") {
			error_flag = 1;
			message += "\t- Please specify the nationality of the artist.\n";				
		}
	}
	
	if(gallery == ""){
		error_flag = 1;
		message += "\t- Please specify the gallery.\n";		
	}
	
	if(document.getElementById('add_new_gallery').style.display == "block") {
		if(name == "") {
			error_flag = 1;
			message += "\t- Please enter the name of the gallery.\n";
		}
		
		if(city == "") {
			error_flag = 1;
			message += "\t- Please enter the city of the gallery.\n";
		}
		
		if(country == "") {
			error_flag = 1;
			message += "\t- Please enter the country of the gallery.\n";
		}
		
		if(email != "") {
			if(!email.match("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")) {
				error_flag = 1;
				message += "\t- Please enter a correct e-mail address.\n";
			}	
		}
		
		if(website != "" && website != "http://") {
			if(!website.match("http://([a-zA-Z0-9]+\.[a-zA-Z0-9\-]+|[a-zA-Z0-9\-]+)\.[a-zA-Z\.]{2,6}(/[a-zA-Z0-9\.\?=/#%&\+-]+|/|)")) {
				error_flag = 1;
				message += "\t- Please enter a correct website.\n";
			}	
		}	
	}
	
	if(price_observation_type == ""){
		error_flag = 1;
		message += "\t- Please enter the type of the price observation.\n";		
	}
	
	if(price_observation_currency == ""){
		error_flag = 1;
		message += "\t- Please enter the currency of the price observation.\n";		
	}
	
	if(price_observation_price == ""){
		error_flag = 1;
		message += "\t- Please enter the price of the price observation.\n";		
	}
	
	else {
		price_observation_price = price_observation_price.replace(",", ".");
		
		if(isNaN(price_observation_price)) {
			error_flag = 1;
			message += "\t- The price for the price observation must be numeric.\n";			
		}	
	}
	
	if(price_observation_date == ""){
		error_flag = 1;
		message += "\t- Please enter the date of the price observation.\n";		
	}
	
	else {
		price_observation_date = price_observation_date.replace(/[^0-9]/g,'');
		
		if(price_observation_date.length == 8) {
			day = price_observation_date.substr(6, 2);
			month = price_observation_date.substr(4, 2);
			year = price_observation_date.substr(0, 4);
			
			if(day < 1 || day > 31) {
				error_flag = 1;
				message += "\t- Please enter a day between 1 and 31.\n";
			}
			
			if(month < 1 || month > 12) {
				error_flag = 1;
				message += "\t- Please enter a month between 1 and 12.\n";
			}
			
			if(isNaN(year)) {
				error_flag = 1;
				message += "\t- The year must be numeric.\n";			
			}	
		}
		
		else {
			error_flag = 1;
			message += "\t- Please enter the date of the price observation in the correct format.\n";
		}
	}	
	
	if(type == ""){
		error_flag = 1;
		message += "\t- Please specify the type of the artwork.\n";		
	}
	
	if(category == ""){
		error_flag = 1;
		message += "\t- Please specify the category of the artwork.\n";		
	}
	
	if(year != "") {
		if(isNaN(year)) {
			error_flag = 1;
			message += "\t- The year of creation of the artwork must be numeric.\n";		
		}
	}
	
	if(width != "") {
		width = width.replace(",", ".");
		
		if(isNaN(width)) {
			error_flag = 1;
			message += "\t- The width of the artwork must be numeric.\n";		
		}
	}
	
	if(height != "") {
		height = height.replace(",", ".");
		
		if(isNaN(height)) {
			error_flag = 1;
			message += "\t- The height of the artwork must be numeric.\n";		
		}
	}
	
	if(depth != "") {
		depth = depth.replace(",", ".");
		
		if(isNaN(depth)) {
			error_flag = 1;
			message += "\t- The depth of the artwork must be numeric.\n";		
		}
	}
	
	message += "\nPlease correct them in order to continue.";
	
	if(error_flag == 0){
		document.getElementById('frm_add_artwork').submit();
	}
	
	else {
		alert(message);	
	}
}







/* ------------------------------------------------- myworks --------------------------------------------------- */
function laadMyworks()
{
	xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		 {
		 alert ("Browser does not support HTTP Request")
		 return
		 } 
		 
		var url= "dropzone.ajax.php";
		xmlHttp.onreadystatechange=myworksLaden 
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
		
		
		
		
}
function myworksLaden() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
	 document.getElementById('dropzone').innerHTML=xmlHttp.responseText ;
	 
	 FillMyWorks();
 }
}
