
DOONE= false;
function validatepark(item, words){
    if(!item.originalv) item.originalv = item.innerHTML;
    if(!words){
        item.innerHTML = item.originalv;
        return true;
    }
    var tx = item.originalv;
    var txf = tx;
    for(var w=0; w < words.length; w++){
        var regexp = new RegExp(words[w]);
        var m = regexp.exec(tx);
        if(!m || !m.length){
            return false;
        }
        txf = txf.replace(m[0],'<b>' + m[0] + '</b>');
    }
    item.innerHTML = txf;
    return true;
}


CAN_BLUR = true;
CURRENT_PARK_SEL_X = null;


function trim(str){
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function filterprojects(parkid){
	var pchn = $('id_project');
	if(pchn){
		var chns = pchn.getElementsByTagName('option');
		for(var i=0; i < chns.length;i++){
			var v = 0;
			try{v = chns[i].getAttribute('value').length;}catch(e){}
			
			if((chns[i].getAttribute('prkid') != parkid && v > 0) || chns[i].getAttribute('prkid') == '0'){
				chns[i].style.display = 'none';
				if(pchn.selectedIndex == i) pchn.selectedIndex = 0;
			}else{
				chns[i].style.display = '';
				console.log(chns[i].getAttribute('prkid') + ', '+parkid);
			}
		}
	}	
}

window.addEvent('domready', function() {
    var span= new Element("span");
    span.innerHTML = 'US$';
    span.id="amount_sym";
    $("id_amount").parentNode.insertBefore(span, $("id_amount") );
	
	var pchn = $('id_project');
	if(pchn){
		var chns = pchn.getElementsByTagName('option');
		for(var i=0; i < chns.length;i++){
			if(chns[i].value==''){
				chns[i].innerHTML = ' - ' + ALL_PROJECTS_TEXT + ' - ';
			}else{
				var arr = trim(chns[i].innerHTML).split('.');
				chns[i].setAttribute('prkid', trim(arr[0]));
				arr.splice(0,1);
				chns[i].innerHTML = arr.join('.');
			}
		}
		filterprojects(PARK_ID);
	}

    var op1= new Element("input");
    op1.type = "radio";
    op1.value = "25";
    op1.name = "value1";
    //op1.checked=true;
    
    
    
    op1.addEvent('click', function(){
        if(this.checked == true){
        $("id_amount").value=25;
        $("id_amount").style.visibility='hidden';
        }
    });
    var span25= new Element("span");  
    span25.innerHTML = '$25';
    
    $("id_amount").parentNode.insertBefore(new Element("br"), $("id_amount") );
    $("id_amount").parentNode.insertBefore(op1, $("id_amount") );
    $("id_amount").parentNode.insertBefore(span25, $("id_amount") );
    
    var op2= new Element("input");
    op2.type = "radio";
    op2.value = "50";
    op2.name = "value1";
    
    
    op2.addEvent('click', function(){
        if(this.checked == true){
        $("id_amount").value=50;
        $("id_amount").style.visibility='hidden';
        }
    });    
    var span50= new Element("span");  
    span50.innerHTML = '$50';
    $("id_amount").parentNode.insertBefore(new Element("br"), $("id_amount") );
    $("id_amount").parentNode.insertBefore(op2, $("id_amount") );
    $("id_amount").parentNode.insertBefore(span50, $("id_amount") );
    
    var op3= new Element("input");
    op3.type = "radio";
    op3.value = "100";
    op3.name = "value1";

    op3.addEvent('click', function(){
        if(this.checked == true){
        $("id_amount").value=100;
        $("id_amount").style.visibility='hidden';
        }
    });    
    var span100= new Element("span");  
    span100.innerHTML = '$100';
    $("id_amount").parentNode.insertBefore(new Element("br"), $("id_amount") );
    $("id_amount").parentNode.insertBefore(op3, $("id_amount") );
    $("id_amount").parentNode.insertBefore(span100, $("id_amount") );
 
     var op4= new Element("input");
    op4.type = "radio";
    op4.value = "500";
    op4.name = "value1";

    op4.addEvent('click', function(){
        if(this.checked == true){
        $("id_amount").value=500;
        $("id_amount").style.visibility='hidden';
        }
    });    
    var span500= new Element("span");  
    span500.innerHTML = '$500';
    $("id_amount").parentNode.insertBefore(new Element("br"), $("id_amount") );
    $("id_amount").parentNode.insertBefore(op4, $("id_amount") );
    $("id_amount").parentNode.insertBefore(span500, $("id_amount") );
          
     var op5= new Element("input");
    op5.type = "radio";
    op5.value = "500";
    op5.name = "value1";
    op5.addEvent('click', function(){
        if(this.checked == true){
        $("id_amount").value="";
        $("id_amount").style.visibility='visible';
        
        }
    });    

    var span5= new Element("span");  
    span5.innerHTML = 'Other &nbsp;';
    $("id_amount").parentNode.insertBefore(new Element("br"), $("id_amount") );
    $("id_amount").parentNode.insertBefore(op5, $("id_amount") );
    $("id_amount").parentNode.insertBefore(span5, $("id_amount") );
               
    $("id_amount").style.visibility='hidden';
    $("id_amount").style.width='90px';

    $("allcontent").addEvent('mouseup', function(){
        CAN_BLUR = true;
    });
    
    if (($("id_amount").value)== "25") {op1.checked=true;}
    else if (($("id_amount").value)== "50") {op2.checked=true;}    
    else if (($("id_amount").value)== "100") {op3.checked=true;}    
    else if (($("id_amount").value)== "500") {op4.checked=true;}    
    else  { op5.checked=true;  $("id_amount").style.visibility='visible';}
        
    $("park_selector").getElements("LI").each(function(item, index){
        item.addEvent('mouseover', function(){
            if(CURRENT_PARK_SEL_X){
                CURRENT_PARK_SEL_X.removeClass('park_over');
                CURRENT_PARK_SEL_X = null;
            }
            this.addClass('park_over');
        });
        item.addEvent('mouseout', function(){
            this.removeClass('park_over');
        });
        item.addEvent('click', function(){
            $("search_park").value = item.originalv;
            $("search_park").focus();
            CAN_BLUR = true;
            var idx = this.getAttribute("idx");
			filterprojects(idx +'');
            if(parseInt(idx) > 0 ){
                $("donation_frm").action = BASE_URL + "donation/" + idx + '-' + this.getAttribute("urlname") +  ".html"
            }else{
                $("donation_frm").action = BASE_URL + "donation.html"
            }
            $("selected_park").innerHTML = this.originalv;
            window.setTimeout("$('search_park').fireEvent('blur');",100);
        });
        item.addEvent('mousedown', function(){
            CAN_BLUR = false;
        });
    });
    
    $("search_park").addEvent("keyup", function(event){
        try{
            if(event.code==40 || event.code==38 || event.code==37 || event.code==39 || event.code == 13) return;
        }catch(e){}
        $("park_selector").style.display = 'block';
        var words = this.value.toLowerCase().match(/\w+/g);
        var CNT = 0;
        var prev = null;
        var first = null;
        if(CURRENT_PARK_SEL_X) CURRENT_PARK_SEL_X.removeClass('park_over');
        $("park_selector").getElements("LI").each(function(item, index){
            if(CNT < 9 && validatepark(item, words)){
                CNT++;
                item.style.display = '';
                if(prev) prev.nnext = item;
                item.nprev = prev;
                item.nnext = null;
                prev = item;
                if(!first) first = item;
                item.style.display = 'block';
            }else{
                item.style.display = 'none';
            }
        });
        if(!CNT) $("park_selector").style.display = 'none';
        if(first){
            CURRENT_PARK_SEL_X = first;
            first.addClass('park_over');
        }else{
            CURRENT_PARK_SEL_X = null;
        }
    });
       
    $("search_park").addEvent("keydown", function(event){
        if(!CURRENT_PARK_SEL_X) return;
        if(event.code==40 && CURRENT_PARK_SEL_X.nnext){//abajo
            CURRENT_PARK_SEL_X.removeClass('park_over');
            CURRENT_PARK_SEL_X=CURRENT_PARK_SEL_X.nnext;
            CURRENT_PARK_SEL_X.addClass('park_over');
        }else if(event.code==38 && CURRENT_PARK_SEL_X.nprev){//arriba
            CURRENT_PARK_SEL_X.removeClass('park_over');
            CURRENT_PARK_SEL_X=CURRENT_PARK_SEL_X.nprev;
            if(CURRENT_PARK_SEL_X) CURRENT_PARK_SEL_X.addClass('park_over');
        }else if(event.code == 13 && CURRENT_PARK_SEL_X){
            CURRENT_PARK_SEL_X.fireEvent('click');
            CURRENT_PARK_SEL_X = null;            
            (new Event(event)).stop();
        }
    });
    

    $("search_park").addEvent("focus", function(event){
        $("park_selector").style.display = 'block';
        this.select();
        this.fireEvent('keyup',null,true);
    });

    $("search_park").addEvent("click", function(){
        this.select();
        this.fireEvent('keyup',null, true);
    });


    $("search_park").addEvent("blur", function(){
        if(!CAN_BLUR){return;}
        $("park_selector").style.display = 'none';
        if($('search_park').value.length){
            $('search_park').value = $('selected_park').innerHTML;
        }else{
            $("park_selector").getElement("LI").fireEvent('click',null,0);
        }
    });

});  
