// JavaScript Document by Illya G. Konovalov @2006 Project MusData
var SendData=false;
var b1=0;
var b2=0;
var progress=0;
var search_back='';

function validate(){
	return false;
}
// Timer Function Updates entries on the left side every XXX seconds
var secs;
var timerID = null;
var timerRunning = false;
var delay = 1000;
var offset = 1; 

function InitializeTimer(){
    // Set the length of the timer, in seconds
    secs = 30;
    StopTheClock();
    StartTheTimer();
}
function StopTheClock(){
    if(timerRunning){
      clearTimeout(timerID);
      timerRunning = false;
	}
}
function StartTheTimer(){
    if (secs==0){
        StopTheClock();
        makeRequest('cgi-bin/latest.cgi','latest_block','reload=1&offset='+offset);
	offset+=1;
	InitializeTimer();
    }
    else{
        self.status = secs;
        secs = secs - 1;
        timerRunning = true;
        timerID = self.setTimeout("StartTheTimer()", delay);
    }
}
// End Timer Functions

// E-mail verification. On the server side is another one..
function verify_email(email){
  var mail = email;
  var result = mail.search(/^.+@.+\..+$/);
  if(result!=-1){
  }else{
    alert("Please, enter a valid E-Mail address.");
	SendData=false;
  }
}
// Character Counter
function CountChar(chlimit){
  var string=document.getElementById('descript').value;
  if(string.length<=chlimit){
    document.getElementById('count').innerHTML='Character Count: '+string.length+'<br>('+chlimit+' character limit)';
  }else{
    string = string.substring(0, chlimit);
    document.getElementById('descript').value=string;
  }
}
// Solves strange problem with checkbox
function ChangeBool(bvar){
	if(bvar==1){if(b1==0){b1=1}else{b1=0}
	}else if(bvar==2){if(b2==0){b2=1}else{b2=0};};
}
// Alert and cancel data submition
function AlertAndStop(element){
  alert('"'+element+'" field is empty or your entry was insufficient. Please, fill in all the required fields.');
  b1=0;b2=0;
  SendData=false;
}
// Activate
function CheckKey(mode){
    var key_input=document.getElementById('key_input').value;
	if(key_input){
	  var query_string='mode='+mode+'&key_input='+key_input;
	  makeRequest('../cgi-bin/ajax_return.cgi','active_content',query_string);
	}else{
    	alert('Please, enter the key you have received with the message we sent to you.');
	}
}
// Go to orchestra update page, but check the key before
function KeyGo(){
    var key_input=document.getElementById('key_input').value;
	if(key_input){
	  var query_string='mode=9&key_input='+key_input;
	  makeRequest('../cgi-bin/ajax_return.cgi','active_content',query_string);
	}else{
    	alert('Please, enter the key of the orchestra you would like to update.');
	}
}
function ReplaceSpecial(stringconvert){
  stringconvert=(stringconvert.replace(/'/g, "\\'").replace(/;/g, "0SI0"));
  stringconvert=(stringconvert.replace(/&/g, "0SI1").replace(/\+/g, "0SI2"));
  stringconvert=stringconvert.replace(/=/g, "0SI3");
  return stringconvert;
}
// Count symbols
function SmbCount(el){
  var len=document.getElementById(el).length;
  if(len>=1200){
	alert('Maximum 1200');
  }
}
// go back using search_back var
function back_search(){
        if(search_back!=''){
	  makeRequest('../cgi-bin/ajax_return.cgi','active_content',search_back);
        }else{
          alert("Sorry, no history found.");
        }
}

// Live Search
function LiveSearch(){
	var search_it=ReplaceSpecial(document.getElementById('live_search').value);
	var art_type=document.getElementById('artist_search').value;
	var country=document.getElementById('country_search').value;
	if(search_it.length>=4){
	  if(country!=0){
            var query_string='mode=12&search_it='+search_it+'&country='+country+'&art='+art_type;
            search_back=query_string;  
	    makeRequest('../cgi-bin/ajax_return.cgi','active_content',query_string);
	  }else{
	    var query_string='mode=12&search_it='+search_it+'&art='+art_type;
            search_back=query_string;
	    makeRequest('../cgi-bin/ajax_return.cgi','active_content',query_string);      
	  }
	}
}
// Go to Orchestra
// Go to orchestra update page, but check the key before
function GoToOrchestra(id){
	if(id){
	  var query_string='mode=11&nr='+id;
	  makeRequest('../cgi-bin/ajax_return.cgi','active_content',query_string);
	}else{
    	alert('Hmmm.. please, don\'t change ID value.');
	}
}
// Go to artist
function GoToArtist(id){
	if(id){
	  var query_string='mode=19&nr='+id;
	  search_back=query_string;
	  makeRequest('../cgi-bin/ajax_return.cgi','active_content',query_string);
	}else{
    	alert('Hmmm.. please, don\'t change ID value.');
	}
}
// List orchestras in a country
function CountryList(){
    var country=document.getElementById('country_search').value;
	if(country!=0){
	  var query_string='mode=5&country='+country;
          search_back=query_string;
	  makeRequest('../cgi-bin/ajax_return.cgi','active_content',query_string);
	}else{
    	alert('Please, select a country from the list.');
	}
}
// List artists 
function ArtistList(){
	var country=document.getElementById('country_search').value;
    var art_type=document.getElementById('artist_search').value;
	if(art_type!=0){
	  if(country!=0){
	    var query_string='mode=21&art_type='+art_type+'&country='+country;
            search_back=query_string;
	    makeRequest('../cgi-bin/ajax_return.cgi','active_content',query_string); 
	  }else{
	    var query_string='mode=21&art_type='+art_type;
            search_back=query_string;
	    makeRequest('../cgi-bin/ajax_return.cgi','active_content',query_string);
	  }
	}else{
    	alert('Please, select an artist category/instrument from the list.');
	}
}
// Entry form validation, orchestra insertion
function FormValidateEntry(){
	var name=ReplaceSpecial(document.getElementById('name').value);
	var country=ReplaceSpecial(document.getElementById('country').value);
	var city=ReplaceSpecial(document.getElementById('city').value);
	var address=ReplaceSpecial(document.getElementById('addr').value);
	var person=ReplaceSpecial(document.getElementById('person').value);
	var lang=ReplaceSpecial(document.getElementById('lang').value);
	var tel=ReplaceSpecial(document.getElementById('tel').value);
	var fax=ReplaceSpecial(document.getElementById('fax').value);
	var site=ReplaceSpecial(document.getElementById('site').value);
	var descript=ReplaceSpecial(document.getElementById('descript').value);

	var mail=ReplaceSpecial(document.getElementById('mail').value);
	var mail_key=ReplaceSpecial(document.getElementById('mail_key').value);
	var mail_confirm=ReplaceSpecial(document.getElementById('mail_confirm').value);
	if(mail){verify_email(mail);};
	verify_email(mail_key);
	
	if(!name || name.length<3){AlertAndStop('name')}
	else if(country==0){AlertAndStop('country')}
	else if(!city || city.length<2){AlertAndStop('city')}
    //else if(!address || address.length<10){AlertAndStop('address')}
	//else if(!person || person.length<7){AlertAndStop('contact person')}
    else if(!lang){AlertAndStop('language')} 
	else if((!mail_key || !mail_confirm) || (mail_key!=mail_confirm)){
		alert('E-mail for activation fields are empty or your entries do not match, please retype.')
		SendData=false;
	}else{
	    SendData=true;
	}
	//alert(SendData);
	if(SendData==true && progress==0){
      document.getElementById('status').innerHTML = '&nbsp;&nbsp;<img src="img/processing.gif" width="87" height="21">';	
	  var query_string="mode=6&name="+name+"&country="+country+"&city="+city+"&addr="+address+"&person="+person+"&lang="+lang+"&tel="+tel+"&fax="+fax+"&descript="+descript+"&site="+site+"&mail="+mail+"&mail_key="+mail_key+"&mail_confirm="+mail_confirm;  
	  progress=1;
	  makeRequest('../cgi-bin/ajax_return.cgi','active_content',query_string);
	}
}
// Update form validation, orchestra update
function FormUpdateEntry(){
	var name=ReplaceSpecial(document.getElementById('name').value);
	var country=ReplaceSpecial(document.getElementById('country').value);
	var city=ReplaceSpecial(document.getElementById('city').value);
	var addr=ReplaceSpecial(document.getElementById('addr').value);
	var person=ReplaceSpecial(document.getElementById('person').value);
	var lang=ReplaceSpecial(document.getElementById('lang').value);
	var tel=ReplaceSpecial(document.getElementById('tel').value);
	var fax=ReplaceSpecial(document.getElementById('fax').value);
	var site=ReplaceSpecial(document.getElementById('site').value);
	var descript=ReplaceSpecial(document.getElementById('descript').value);
	var key=ReplaceSpecial(document.getElementById('key').value);
	
	var mail=ReplaceSpecial(document.getElementById('mail').value);
	var mail_key=ReplaceSpecial(document.getElementById('mail_key').value);
	if(mail){verify_email(mail);};
	verify_email(mail_key);
	
	if(!name || name.length<3){AlertAndStop('name')}
	else if(country==0){AlertAndStop('country')}
	else if(!city || city.length<2){AlertAndStop('city')}
	else if(!lang){AlertAndStop('language')
	}else{
	    SendData=true;
	}
	//alert(SendData);
	if(SendData==true){
	  var query_string='mode=10&name='+name+'&country='+country+'&city='+city+'&addr='+addr+'&person='+person+'&lang='+lang+'&tel='+tel+'&fax='+fax+'&descript='+descript+'&site='+site+'&mail='+mail+'&mail_key='+mail_key+'&key='+key;
	  makeRequest('../cgi-bin/ajax_return.cgi','active_content',query_string);
	}
}
// Form Val. for Artists
function FormValidateArtist(){
	var membership=1;
	var header=ReplaceSpecial(document.getElementById('header').value);
	var fname=ReplaceSpecial(document.getElementById('fname').value);
	var name=ReplaceSpecial(document.getElementById('name').value);
	var country=ReplaceSpecial(document.getElementById('country').value);
	var cat=ReplaceSpecial(document.getElementById('cat').value);
	var city=ReplaceSpecial(document.getElementById('city').value);
	var address=ReplaceSpecial(document.getElementById('addr').value);
	var tel=ReplaceSpecial(document.getElementById('tel').value);
	var mobile=ReplaceSpecial(document.getElementById('mobile').value);
	var fax=ReplaceSpecial(document.getElementById('fax').value);
	var site=ReplaceSpecial(document.getElementById('site').value);
	var lang=ReplaceSpecial(document.getElementById('lang').value);
	var mail=ReplaceSpecial(document.getElementById('mail').value);
	var mail_confirm=ReplaceSpecial(document.getElementById('mail_key').value);
	var descript=ReplaceSpecial(document.getElementById('descript').value)
	var newsletter=b1;
	var terms=b2;
	  
	if(mail){verify_email(mail);};
	verify_email(mail_confirm);
	if(!name || name.length<3){AlertAndStop('surname')}
	else if(!fname || fname.length<3){AlertAndStop('firstname')} 
	else if(country==0){AlertAndStop('country')}
	else if(cat==0){AlertAndStop('instrument/category')}
	else if(!city || city.length<2){AlertAndStop('city')}
	else if(!lang){AlertAndStop('language')} 
	else if((!mail || !mail_confirm) || (mail_confirm!=mail)){
		alert('E-mail does not match, please retype.');
		SendData=false;
	}else if(b2==0){
		alert('Please, read and accept our Terms and Conditions to proceed.');
		SendData=false;
	}else{
		SendData=true;
	}
	//alert(terms);
	if(SendData==true && progress==0){
	  var query_string="mode=17&fname="+fname+"&name="+name+"&country="+country+"&cat="+cat+"&city="+city+"&addr="+address+"&header="+header+"&lang="+lang+"&tel="+tel+"&fax="+fax+"&mobile="+mobile+"&descript="+descript+"&site="+site+"&mail="+mail+"&mail_confirm="+mail_confirm+"&membership="+membership+"&newsletter="+newsletter+"&terms="+terms;
	  b1=0; b2=0;
	  document.getElementById('status').innerHTML = '&nbsp;&nbsp;<img src="img/processing.gif" width="87" height="21">';
	  progress==1;
	  makeRequest('../cgi-bin/ajax_return.cgi','active_content',query_string);
	}
}
// Update form validation, artist update
function UpdateArt(){
	var header=ReplaceSpecial(document.getElementById('header').value);
	var fname=ReplaceSpecial(document.getElementById('fname').value);
	var name=ReplaceSpecial(document.getElementById('name').value);
	var country=ReplaceSpecial(document.getElementById('country').value);
	var cat=ReplaceSpecial(document.getElementById('cat').value);
	var city=ReplaceSpecial(document.getElementById('city').value);
	var addr=ReplaceSpecial(document.getElementById('addr').value);
	var tel=ReplaceSpecial(document.getElementById('tel').value);
	var mobile=ReplaceSpecial(document.getElementById('mobile').value);
	var fax=ReplaceSpecial(document.getElementById('fax').value);
	var site=ReplaceSpecial(document.getElementById('site').value);
	var lang=ReplaceSpecial(document.getElementById('lang').value);
	var descript=ReplaceSpecial(document.getElementById('descript').value);
	var key=ReplaceSpecial(document.getElementById('key').value);
	
	var mail=ReplaceSpecial(document.getElementById('mail').value);
	var mail_key=ReplaceSpecial(document.getElementById('mail_key').value);
	if(mail){verify_email(mail)};
	if(mail_key){verify_email(mail_key)};
	
	if(!name || name.length<3){AlertAndStop('surname')}
	else if(!fname || fname.length<3){AlertAndStop('firstname')}
	else if(country==0){AlertAndStop('country')}
	else if(!city || city.length<2){AlertAndStop('city')}
	else if(!lang){AlertAndStop('language')
	}else{
	    SendData=true;
	}
	
	//alert(SendData);
	if(SendData==true){
	  var query_string='mode=20'+'&header='+header+'&name='+name+'&fname='+fname+'&country='+country+'&cat='+cat+'&city='+city+'&addr='+addr+'&lang='+lang+'&tel='+tel+'&mobile='+mobile+'&fax='+fax+'&descript='+descript+'&site='+site+'&mail='+mail+'&mail_key='+mail_key+'&key='+key;
	  makeRequest('../cgi-bin/ajax_return.cgi','active_content',query_string);
	}
}
// Request Info
function FormValidateContact(){
	progress=0;
	SendData=true;
	var name=ReplaceSpecial(document.getElementById('name').value);
	var descript=ReplaceSpecial(document.getElementById('descript').value);
	if(!descript){AlertAndStop('information')};

	var mail=ReplaceSpecial(document.getElementById('email').value);
	if(mail){verify_email(mail);};
	if(!name || name.length<2){AlertAndStop('name')};
	
	if(SendData==true && progress==0){
        document.getElementById('status').innerHTML = '&nbsp;&nbsp;<img src="img/processing.gif" width="87" height="21">';	       
	  var query_string="mode=sendinforequest&name="+name+"&email="+mail+"&descript="+descript;  
	  progress=1;
	  makeRequest('../cgi-bin/contact.cgi','inforequest',query_string);
	}
}

// AJAX Stuff
function makeRequest(url, element, param) {
   document.getElementById('loading').style.visibility='visible';
   var http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
     http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType) {
        http_request.overrideMimeType('text/xml'); // See note below about this line
     }
   } else if (window.ActiveXObject) { // IE
     try {
       http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
     try {
       http_request = new ActiveXObject("Microsoft.XMLHTTP");
     } catch (e) {}
     }
   }
   if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            document.getElementById('loading').style.visibility='hidden';
            return false;
   }
   http_request.onreadystatechange = function() { alertContents(http_request,element);};
   http_request.open('POST', url, true);
   http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   http_request.send(param);
}

function alertContents(http_request,element) {
   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
		 document.getElementById(element).innerHTML = http_request.responseText;  
		 document.getElementById('loading').style.visibility='hidden';
       //alert(http_request.responseText);
      } else {
        alert('There was a problem with the request.');
		document.getElementById('loading').style.visibility='hidden';
      }
   }
}
// Mouseover etc, effects
var lastel;

function ChangeFieldColor(id){
  if(lastel){lastel.style.backgroundColor="#FFFFFF"}
  if(id){
    var el = document.getElementById(id);
    lastel=el;
    el.style.backgroundColor = "#FFFFCC"; 
  }
}
// Create element
var InfoDiv = 'infodiv'; 
var offX = 15;  // X offset
var offY = 15;  // Y offset

function mouseX(evt){
	if (!evt) evt = window.event;
	if (evt.pageX) return evt.pageX;
	else if (evt.clientX){
	  return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft);
	}else return 0;
}
function mouseY(evt){
  if (!evt) evt = window.event;
  if (evt.pageY) return evt.pageY;
  else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
  else return 0;
}

function follow(evt){
  if (document.getElementById) {
  var obj = document.getElementById(InfoDiv).style;
  obj.visibility = 'visible';
  obj.left = (parseInt(mouseX(evt))+offX) + 'px';
  obj.top = (parseInt(mouseY(evt))+offY) + 'px';}
}

function InfoChange(text){
  if(!text){
    var obj = document.getElementById(InfoDiv);	
	obj.innerHTML = '';
  }else{
    var obj = document.getElementById(InfoDiv);	
    obj.style.visibility = 'visible';
    obj.innerHTML = text;
  }
}
// Newsletter
function verify_newsmail(){
  var mail = document.getElementById('infomail').value;
  if(mail){
    var result = mail.search(/^.+@.+\..+$/);
    if(result!=-1){
      mail='infomail='+mail;
      makeRequest('cgi-bin/newsletter.cgi','mailinfo',mail)
    }else{
      alert("Please, enter a valid E-Mail address.");
    }
  }else{
    alert('Please, enter a valid e-mail address.');
  }
}
// Open Window
function OpenWnd (adr,title) {
  wnd1 = window.open(adr,title, "width=640,height=400,scrollbars=yes,left=100,top=200");
  wnd1.focus();
}

// Image change functions (MM)
function MM_swapImgRestore() {
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function PreloadImages() {
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=PreloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) {
  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;
}

function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
