function trimAllString(sInString){
    sInString = sInString.replace(/ /g,'');
    return sInString.replace(/(^\s+)|(\s+$)/g, '');
}
function trimString(sInString){
    return sInString.replace(/(^\s+)|(\s+$)/g, '');
}

function getRealWidth(obj){
    var w = 0;    
    if(obj && typeof obj == "object"){
        var list = obj.firstChild;        
        while(list){
            if(list.nodeType === 1 && !isNaN(parseInt(list.offsetWidth))){
                w += parseInt(list.offsetWidth);
                if(window.getComputedStyle){
                    w += !isNaN(parseInt(window.getComputedStyle(list, null).marginLeft))?parseInt(window.getComputedStyle(list, null).marginLeft):0;
                    w += !isNaN(parseInt(window.getComputedStyle(list, null).marginRight))?parseInt(window.getComputedStyle(list, null).marginRight):0;                    
                }else{
                    w += !isNaN(parseInt(list.currentStyle.marginLeft))?parseInt(list.currentStyle.marginLeft):0; 
                    w += !isNaN(parseInt(list.currentStyle.marginRight))?parseInt(list.currentStyle.marginRight):0;
                }                
            }
            list = list.nextSibling;
        }
    }
    return w;
}
function prepareMainMenu(){
    var menu = document.getElementById("main_menu");
    if(menu && typeof menu === "object"){
        var submenu = menu.getElementsByTagName("DIV");
        if(submenu && submenu.length > 0){
            for(var i in submenu){
                if(submenu[i].className === "submenu"){
                    submenu[i].parentNode.onmouseover = function(){
                        showHideSubmenu(this);
                    }
                    submenu[i].parentNode.onmouseout = function(){
                        showHideSubmenu(this);
                    }                    
                }
            }
        }
    }
}
function showHideSubmenu(obj){
    if(obj && typeof obj === "object"){
        var smenu = obj.getElementsByTagName("DIV");
        if(smenu && smenu.length > 0){
            for(var i in smenu){
                if(smenu[i].className === "submenu"){
                    if(smenu[i].style.display === "block"){
                        smenu[i].onmouseout = function(){
                            this.parentNode.className = "menu_elem";    
                        }
                        smenu[i].style.display = "none"; 
                    }else{           
                        smenu[i].style.display = "block";
                        smenu[i].onmouseover = function(){
                            this.parentNode.className = "menu_elem menu_elem_hover";
                        }
                    }
                }
            }
        }
    }   
}
function showHideSublist(obj){    
    if(obj && typeof obj === "object"){
      classname = obj.value;
	  var sublist = document.getElementById('online-data-table');
	  var odj_tr =  sublist.getElementsByTagName("tr");
	  var ua = navigator.userAgent;
	  if(odj_tr && odj_tr.length > 0){
            for(var i=0;i<odj_tr.length;i++){
	      if (odj_tr[i].id==classname)            

               {			    
		 if (ua.toLowerCase().indexOf('chrome') > -1) {
                       odj_tr[i].style.display = 'table-row';
	         } else 
		       odj_tr[i].style.display = "block"; 
	       } else if (odj_tr[i].id.indexOf('class_')!=-1)
                  odj_tr[i].style.display = "none"; 
                    	
	    }
      }
    }  
}
function showHideRayaon(obj){    
    if(obj && typeof obj === "object"){
      var znach = obj.value;
	  var sublist = document.getElementById('online-data-table');
	  var obj_tr =  sublist.getElementsByTagName("tr");
	  var ua = navigator.userAgent;
	  if(obj_tr && obj_tr.length > 0){
	     var match = false;
		 i = 0;
	     while (!match && i<obj_tr.length){
		    if (obj_tr[i].id=='rayon') {
			  if (znach=='teacher' || znach=='student') {
			     if (ua.toLowerCase().indexOf('ie') == -1) {
                     obj_tr[i].style.display = 'table-row';
	             } else 
				    obj_tr[i].style.display = "block"; 
			  } else obj_tr[i].style.display = "none"; 
			  match = true;
			};
			i++;
		 };
                    	
		}
    }
}  
function showHideFranReview (i, what_action) {
   div_text = document.getElementById ("div_text_"+i);
   more_text = document.getElementById ("link_more_text_"+i);
   less_text = document.getElementById ("link_less_text_"+i);
   if (what_action=='block') {
     div_text.style.display = 'block';
	 less_text.style.display = 'block';
	 more_text.style.display = 'none';
   } else {
     div_text.style.display = 'none';
	 less_text.style.display = 'none';
	 more_text.style.display = 'block';
   }
}
function pageScroller(){
    
    var self = this;
    var obj = document.getElementById("page_scroller");
    var pages = [];
    var curr = false;
    var ajax=false;
    this.action='0';
    this.ajax_class=new Class_Ajax();
    
    this.prepare = function(){
        if(obj && typeof obj == "object"){
            this.center();            
            var elems = obj.getElementsByTagName("A");
            if(elems && elems.length > 0){
                for(var i=0; i<elems.length; i++){
                    elems[i].onmouseover = function(){
                        self.hoverAction(this.parentNode);
                    }
                    elems[i].onmouseout = function(){
                        self.hoverAction(this.parentNode);
                    } 
                    elems[i].onclick = function(){
                        self.setActive(this);
                        return !self.ajax;
                    }
                    
                    if(elems[i].parentNode.className.toLowerCase() == "btn_pn" || elems[i].parentNode.className.toLowerCase() == "btn_pn_s"){
                        pages.push(elems[i]);
                    }                                        
                }
            }
            this.correctHidden();
            this.setActive();
        }
    }
    this.correctHidden = function(){
        var found = obj.getElementsByTagName("INPUT");
        if(found && found.length > 0){
            for(var i=0; i<found.length; i++){
                if(found[i].type.toLowerCase() == "hidden" && found[i].name == "page"){
                    if(trimAllString(found[i].value).length === 0){found[i].value = "1";}
                    curr = found[i];                    
                }
            }
        }
        if(!curr){
            var new_el = document.createElement("INPUT")
            with(new_el){
                type = "hidden";
                name = "page";
                value = "1"; 
            }
            obj.appendChild(new_el);
            curr = new_el;
        }
        if(this.foundInPages(curr.value) === false){
            curr.value = pages[0].innerHTML;
        }
    }
    this.center = function(){
        obj.style.width = getRealWidth(obj) > 0? getRealWidth(obj) + "px": "100%";
        if(obj.offsetWidth < obj.parentNode.offsetWidth){
            obj.style.marginLeft = ((obj.parentNode.offsetWidth - obj.offsetWidth) / 2) + "px"
        }
    }
    this.setActive = function(c_obj){
        c_obj = c_obj || false;
        if(curr && trimAllString(curr.value).length > 0 && pages && pages.length >0){
            var page = curr.value;
            if(!c_obj || c_obj.parentNode.className != "btn_pn_s"){
                this.clearButtons();
                if(c_obj){
                    if(c_obj.parentNode.className == "btn_pn" || c_obj.parentNode.className == "btn_pn_h"){
                        curr.value = c_obj.innerHTML;
                    }
                    if(c_obj.parentNode.className == "btn_fr" || c_obj.parentNode.className == "btn_fr_h"){
                        curr.value = pages[0].innerHTML;
                    }
                    if(c_obj.parentNode.className == "btn_ff" || c_obj.parentNode.className == "btn_ff_h"){
                        curr.value = pages[pages.length - 1].innerHTML;
                    } 
                    if(c_obj.parentNode.className == "btn_r" || c_obj.parentNode.className == "btn_r_h"){
                        var pos = this.foundInPages(curr.value);
                        if(pos !== false && pos > 0){
                            curr.value = pages[pos-1].innerHTML;
                        }
                    }
                    if(c_obj.parentNode.className == "btn_f" || c_obj.parentNode.className == "btn_f_h"){
                        var pos = this.foundInPages(curr.value);
                        if(pos !== false && pos < pages.length - 1){
                            curr.value = pages[pos+1].innerHTML;
                        }
                    }                            
                }       
                for(var i=0; i<pages.length; i++){
                    if(typeof pages[i] == "object" && pages[i].innerHTML == curr.value){
                        pages[i].parentNode.className = "btn_pn_s"; 
                    }
                }
            }
            if(page !== curr.value || !c_obj){
                this.finalAction(curr.value);
            }
        }
    }
    this.finalAction = function(page){     /*   ------------ Final Action on Selected Page -------------   */
        if(page && trimAllString(page).length > 0){
            //alert(page);
            
            switch(this.action){
                case 'teacher_athome':  
                    this.ajax_class.post('ajax_action=athome&course_id='+document.getElementById('course_id').value+'&page='+page,showTeacher)
            }
            return false;
        }
    }
    this.foundInPages = function(page){
        page = page || false;
        var posit = false;
        if(pages && pages.length > 0){
            for(var i=0; i<pages.length; i++){
                if(page && pages[i].innerHTML == page){
                    posit = i;
                }
            }
        }
        return posit;
    }
    this.clearButtons = function(){
        if(obj && typeof obj == "object"){
            var el = obj.firstChild
            while(el){
                if(el.nodeType === 1 && el.nodeName.toLowerCase() == "div" && el.className == "btn_pn_s"){
                    el.className = "btn_pn";
                }
                el = el.nextSibling;
            }
        }
    }
    this.hoverAction = function(h_obj){
        if(h_obj && typeof h_obj == "object"){
            switch(h_obj.className.toLowerCase()){
                case "btn_fr":
                    h_obj.className = "btn_fr_h";
                    break;
                case "btn_fr_h":
                    h_obj.className = "btn_fr";
                    break;
                case "btn_r":
                    h_obj.className = "btn_r_h";
                    break;
                case "btn_r_h":
                    h_obj.className = "btn_r";
                    break;                    
                case "btn_pn":
                    h_obj.className = "btn_pn_h";
                    break;
                case "btn_pn_h":
                    h_obj.className = "btn_pn";
                    break;                    
                case "btn_f":
                    h_obj.className = "btn_f_h";
                    break;
                case "btn_f_h":
                    h_obj.className = "btn_f";
                    break;                    
                case "btn_ff":
                    h_obj.className = "btn_ff_h";
                    break;
                case "btn_ff_h":
                    h_obj.className = "btn_ff";
                    break;                                         
            }
        }
    }
    
    
}
/*
var scroller = new pageScroller(); 
scroller.prepare();
*/


function showTeacher(content){
	var content_holder=document.getElementById("teacher_list_content");
	if (content_holder){
		content_holder.innerHTML=content;
	}
}

function setRegion(region){
	if (region=='spb'){
		window.location='http://www.muz-school.ru/';
	}else{
	   window.location='http://'+region+'.muz-school.ru/';
    }   
}

function wpSpoilerToggle(spoiler, link, showtext, hidetext) {
                if (spoiler.style.display != 'none') {
                    spoiler.style.display = 'none';
                    link.innerHTML = showtext;
                    link.className = 'spoiler_link_show';
                } else {
                    spoiler.style.display = 'block';
                    link.innerHTML = hidetext;
                    link.className = 'spoiler_link_hide';
                }
            }
            
            
            
















function changePallButton(obj, act){
    if(obj && (act == "on" || act == "off")){
        var inp = obj.getElementsByTagName("INPUT");
        if(inp.length > 0){
            for(i=0; i<inp.length; i++){
                if(inp[i].type == "submit" && (trimString(inp[i].className) == "some_subm_butt" || trimString(inp[i].className) == "some_subm_butt_err")){
                    if(act == "off"){
                        inp[i].className = "some_subm_butt_err"; 
                        inp[i].blur();
                    }
                    if(act == "on"){
                        inp[i].className = "some_subm_butt"; 
                        inp[i].blur();
                    }                    
                }
            }
        } 
    }   
}

function pollFormSubmit(obj){
    if(obj){
        var check_inp = document.getElementsByTagName("INPUT");
        var flag = false;
        if(check_inp.length > 0){
            for(i=0; i<check_inp.length; i++){
                if(check_inp[i].type == "radio"){
                    check_inp[i].onclick = function(){
                        //changePallButton(this.parentNode, "on");
                    }
                    if(check_inp[i].checked){flag = true;}   
                }
            }  
        }else{return false;}
        if(flag){
            return true;
        }else{//changePallButton(obj, "off"); return false;
        }
    }else{return false;}
}

function preparePollList(){
    var main_form = document.getElementById("poll_form");
    if(main_form){
        main_form.onsubmit = function(){
            if(pollFormSubmit(this)){
                return true;    
            }else{return false;}
        }
    }
    var second_form = document.getElementById("poll_form2");
    if(second_form){
        second_form.onsubmit = function(){
            if(pollFormSubmit(this)){
                return true;    
            }else{return false;}
        }
    }    
}


function submitAction(){
    var self = this;
    this.act_form = document.getElementById("form_online");
    
    this.prepareActions = function(){
        if(self.act_form && typeof self.act_form == "object" && self.act_form.nodeName.toLowerCase() == "form"){
            self.act_form.onsubmit = function(){
                return self.checkFields(this);
            } 
        }
    }
    
    this.checkFields = function(obj){
        var has_err = true;
        if(obj && typeof obj == "object" && obj.nodeName.toLowerCase() == "form"){
            var all_inp = obj.getElementsByTagName("INPUT");
            if(all_inp && all_inp.length > 0){
                for(var i=0; i<all_inp.length; i++){
                    if(all_inp[i].type.toLowerCase() == "text" && all_inp[i].className && all_inp[i].className.indexOf("error_fill") !== -1){
                        if(trimString(all_inp[i].value).length === 0){
                            has_err = false;
                            all_inp[i].style.width = (all_inp[i].offsetWidth - 2) + "px";
                            all_inp[i].style.padding = "2px 0"; 
                            all_inp[i].style.border = "1px solid #FF0101";
                            all_inp[i].style.backgroundColor = "#FFC6C6";
                            all_inp[i].onfocus = function(){
                                this.style.border = "1px solid #7F9DB9";
                                this.style.backgroundColor = "#FFFFFF";
                            }
                        }
                    }
                }
            }
        }
        return has_err;
    }
    
    
    this.prepareActions();
}


function pollSet(id, region){
    
    var self = this;
    this.area_obj = document.getElementById(id);
    this.region = region;
    
    this.prepareDDowns = function(){
        if(self.area_obj && typeof self.area_obj == "object"){
            var get_sel = self.area_obj.getElementsByTagName("SELECT");
            if(get_sel && get_sel.length > 0){
                for(var i=0; i<get_sel.length; i++){
                    if (get_sel[i].id=='inschool'){
                        get_sel[i].onchange = function(){
                            changeInstrument(this.value);
                            self.selCollector(this);
                        }
                    }else{
                        get_sel[i].onchange = function(){
                            self.selCollector(this);
                        }
                    }
                    
                    
                    /*if(get_sel[i] && typeof get_sel[i] == "object" && get_sel[i].id){
                        alert(get_sel[i].options[get_sel[i].selectedIndex].value);
                    }*/
                    
                    
                    
                    
                }
            } 
        }
    }
    
    this.selCollector = function(obj){
        var to_one = [];
        var all_on = false;
        if(obj && typeof obj == "object" && obj.nodeName.toLowerCase() == "select"){
            //if(trimString(obj.options[obj.selectedIndex].value).length > 0){








                if(self.area_obj && typeof self.area_obj == "object"){
                    var get_sel = self.area_obj.getElementsByTagName("SELECT");
                    if(get_sel && get_sel.length > 0){
                        for(var i=0; i<get_sel.length; i++){
                            if(get_sel[i].id){
                                to_one[i] = new Array();
                                to_one[i].push(get_sel[i].id);
                                to_one[i].push(get_sel[i].options[get_sel[i].selectedIndex].value);
                            }
                            if(trimString(get_sel[i].options[get_sel[i].selectedIndex].value).length < 1){
                                all_on = true;
                            }                            
                        }
                    } 
                }
                var course_result=document.getElementById('course_result');
                if(!all_on){     
                    var courses=new Array();
                    courses[90]={title:'ÃÈÒÀÐÀ',link:'guitar', max: 72};
                    courses[91]={title:'ÔÎÐÒÅÏÈÀÍÎ',link:'piano', max: 140};
                    courses[92]={title:'ÑÊÐÈÏÊÀ',link:'violin', max: 96};
                    courses[93]={title:'ÂÎÊÀË', link:'singing', max: 48};
                    courses[96]={title:'ÔËÅÉÒÀ', link:'fleyta', max:0};
                    courses[95]={title:'ÑÀÊÑÎÔÎÍ', link:'saksofon', max:96};
                    courses[115]={title:'ÓÄÀÐÍÛÅ', link:'drums', max: 48};
                    courses[116]={title:'ÁÀßÍ', link:'bayan', max:0};                    
                    
                                       
                    var hours=0;
                    switch(to_one[2][1]){
                        case '1':
                        case '2':   hours=8;break;
                        case '3':   hours=24;
                                    if (to_one[1][1]==91 || to_one[1][1]==92 || to_one[1][1]==116 || to_one[1][1]==95 || to_one[1][1]==115){
                                        hours = 48;
                                    }
                                    break;
                        case '4':   hours=48;break;
                        case '5':   hours=48;
                                    if (courses[to_one[1][1]].max>48){
                                        hours=courses[to_one[1][1]].max;
                                    } 
                                    break;
                    }
                    
                    var oplata_txt='ÎÏËÀÒÀ ÂÑÅÃÎ ÊÓÐÑÀ';
                    switch(to_one[3][1]){
                    	case '1':	break;
                    	case '2':	oplata_txt = 'Â 2 ÏËÀÒÅÆÀ';
                    				if ((to_one[1][1]==90 || to_one[1][1]==92) && hours==72){ // ãèòàðà,ñêðèïêà 72 çàíÿòèÿ
                    					oplata_txt = 'Â 3 ÏËÀÒÅÆÀ';
                    				}
                    				break;
                    	case '3':	var divideby = 12;                    				
									var lessons_count=Math.ceil(hours/divideby);
                    				if (lessons_count<1) lessons_count=2;
                    				else if (lessons_count==1){
                    					oplata_txt='ÎÏËÀÒÀ ÂÑÅÃÎ ÊÓÐÑÀ';
                    					break;
                    				}
                    				if (to_one[1][1]==91 && hours==140){	//ôîðòåïèàíî
                    					lessons_count=7;
                    				}else if (to_one[1][1]==92 && hours==96){	//Ñêðèïêà
                    					lessons_count=6;
                    				}else if (to_one[1][1]==95 && hours==96){	//ñàêñîôîí
                    					lessons_count=6;
                    				}
									oplata_txt = "ÎÏËÀÒÀ Â "+lessons_count+" "+((lessons_count<5)?"ÏËÀÒÅÆÀ":"ÏËÀÒÅÆÅÉ");	
									break;	
						case '4':	var divideby = 8;
									var lessons_count=Math.ceil(hours/divideby);
                    				if (lessons_count<=1){
                    					oplata_txt='ÎÏËÀÒÀ ÂÑÅÃÎ ÊÓÐÑÀ';
                    					break;
                    				}
                    				if (to_one[1][1]==91 && hours==140){	//ôîðòåïèàíî
                    					lessons_count=14;
                    				}else if (to_one[1][1]==92 && hours==96){	//Ñêðèïêà
                    					lessons_count=12;
                    				}else if (to_one[1][1]==95 && hours==96){	//ñàêñîôîí
                    					lessons_count=12;
                    				}
									oplata_txt = "ÎÏËÀÒÀ Â "+lessons_count+" "+((lessons_count<5)?"ÏËÀÒÅÆÀ":"ÏËÀÒÅÆÅÉ");
									break;	
						case '5':	var divideby = 4;	
									var lessons_count=Math.ceil(hours/divideby);
                    				if (lessons_count<=1){
                    					oplata_txt='ÎÏËÀÒÀ ÂÑÅÃÎ ÊÓÐÑÀ';
                    					break;
                    				}
                    				if (to_one[1][1]==91 && hours==140){	//ôîðòåïèàíî
                    					lessons_count=28;
                    				}
									oplata_txt = "ÎÏËÀÒÀ Â "+lessons_count+" "+((lessons_count<5)?"ÏËÀÒÅÆÀ":"ÏËÀÒÅÆÅÉ");
									break;					
                    }
                    
                    
                    var link_text="ÂÀØ ÊÓÐÑ: "+courses[to_one[1][1]].title+" "+to_one[0][1]+" "+hours+" ÇÀÍßÒÈÉ, "+oplata_txt; 	  				
                    var link = 'http://'+((this.region == 'spb')? 'www':this.region)+'.muz-school.ru/courses/'+courses[to_one[1][1]].link+"/#"+hours;
                    //alert(to_one + " --- " + all_on);
                    
                    if (course_result){
                        course_result.innerHTML="<a href=\""+link+"\">"+link_text+"</a>";
                    }
                }else{                    
                    if (course_result){
                        course_result.innerHTML="";
                    }
                }
            //}
        }
    }
    
    self.prepareDDowns();
}

function changeInstrument(value){
    var inschool=(value =='Â ØÊÎËÅ (ì. Ïåòðîãðàäñêàÿ)');
        var obj=document.getElementById('instrument');
        if (obj){
            obj.selectedIndex=0;
            for (var i=0; i<obj.options.length; i++){
                if(obj.options[i].value=='115' || obj.options[i].value=='116'){
                    obj.options[i].style.display=(inschool)?'none':'block';
                    obj.options[i].style.visibility=(inschool)?'hidden':'visible';
                }
            }
        }
    
}



window.onload = function(){
        preparePollList();
}
