/**
 * Various functions for the voting system in the halloween module of www.desarbatori.ro
 * @author Teodor Muraru
 */
 
function vote(value, el_id, devels, old_value){   // alert('value: ' + value + ', el_id: ' + el_id);
    script = "/halloween/concurs/l/vote";
    new Ajax.Request(script, {
        parameters: "element_id=" + el_id + "&vote_type=" + value,
        onSuccess: function(tr){
            if (tr.responseText.indexOf('succes!')>-1) {
                //alert('Ai votat cu success!\n Voturile se vor actualiza la reincarcarea paginii');
                $('voteaza_' + el_id).hide();
                $('votat_' + el_id).show();
                new_val = eval( Number( intval( old_value ) ) + Number( intval( value ) ) );
                $('text_voteaza_' + el_id).innerHTML = 'Voturi: ' + new_val;
                //show_false('voteaza_'+el_id);                show_true('votat_'+el_id);                show_false('voturi_'+el_id);
            }
            else{
                if (tr.responseText.indexOf('duplicate')>-1) {
                    alert('Ai mai votat odata de pe aceasta adresa IP!'); //alert('Mesajul a mai fost votat de la aceasta adresa IP!');
                }
                else{
                    alert('Eroare! Te rugam sa incerci mai tarziu!');
                }
            }
        }
    });
}     
          
var validator = new FormValidator({
			onSuccess: function( id ){
				$(id).style.border = "1px solid #00FF00";
				$('err_' + id).hide();
			},
			onFailure: function( id, message ){
				//parent.Windows.getFocusedWindow().updateHeight();
				$(id).style.border = "1px solid #FF0000";
				$('err_' + id).show();
			}
	 	});
		validator.add({
			id:	'name',
			name:	'name',
			type:	['required','length|min=3'],
			message:'Nu ai nume?! Buna gluma! ;) ',
			onFailure:	function(id, message){
				$(id).style.border = '1px solid #FF0000';
				if ($('err_'+id)){
					$('err_'+id).innerHTML = message;
					$('err_'+id).show();
				}
	 		},
	 		onSuccess:	function(id, message){
				$(id).style.border = '1px solid #00FF00';
				if ($('err_'+id)) $('err_'+id).hide();
	 		}
	 	});
	 	validator.add({
			id:	'title',
			name:	'title',
			type:	['required','length|min=3'],
			message:'Gluma fara titlu?! Cred ca glumesti! ;) ',
			onFailure:	function(id, message){
				$(id).style.border = '1px solid #FF0000';
				if ($('err_'+id)){
					$('err_'+id).innerHTML = message;
					$('err_'+id).show();
				}
	 		},
	 		onSuccess:	function(id, message){
				$(id).style.border = '1px solid #00FF00';
				if ($('err_'+id)) $('err_'+id).hide();
	 		}
	 	});
	
	
	 	validator.add({
	 		id:	'email',
	 		name:	'email',
	 		type:	['email','length|min=3'],
	 		message:'Gluma, gluma, dar macar emailul sa fie corect!! ;) ',
	 		onFailure:	function(id, message){
				$(id).style.border = '1px solid #FF0000';
				if ($('err_'+id)){
					$('err_'+id).innerHTML = 'Gluma, gluma, dar macar emailul sa fie corect!! ;) ';
					$('err_'+id).show();
				}
	 		},
	 		onSuccess:	function(id, message){
				$(id).style.border = '1px solid #00FF00';
				if ($('err_'+id)) $('err_'+id).hide();
	 		}
	
	 	})

	 	function submit_concurs(){
	 		if(validator.validateAll()){
	 			$('form_halloween').submit();
	 		}
	 		else{ return false; }
	 	}
function intval( mixed_var, base ) {
    // Get the integer value of a variable
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_intval/
    // +       version: 809.522
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: stensi
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: intval('Kevin van Zonneveld');
    // *     returns 1: 0
    // *     example 2: intval(4.2);
    // *     returns 2: 4
    // *     example 3: intval(42, 8);
    // *     returns 3: 42
    // *     example 4: intval('09');
    // *     returns 4: 9

    var tmp;

    if( typeof( mixed_var ) == 'string' ){
        tmp = parseInt(mixed_var*1);
        if(isNaN(tmp) || !isFinite(tmp)){
            return 0;
        } else{
            return tmp.toString(base || 10);
        }
    } else if( typeof( mixed_var ) == 'number' && isFinite(mixed_var) ){
        return Math.floor(mixed_var);
    } else{
        return 0;
    }
 }
