// the code below does conditional IE JS compilation
// and loads one value for IE6 (JScript==5.6)
// and another for other browsers
/*@cc_on
   /*@if (@_jscript_version <= 5.6)
      var WINDOW_LOAD_EVENT = 'load';
   @else @*/
      var WINDOW_LOAD_EVENT = 'dom:loaded';
   /*@end
@*/

function switchDisplay(id, arr) {
    if(arr.length > 0) {
        for(q = 0; q < arr.length; q++) {
            var currentId = arr[q];
            
            if(document.getElementById(currentId)) {
                var type = (currentId == id)?'block':'none';
                document.getElementById(currentId).style.display = type;
            }    
        }
    }    
}

function flip(id1,id2){
	document.getElementById(id1).style.display="none";
	document.getElementById(id2).style.display="block";
	return false;
}

function addComment(){
	if(!loggedIn){
			alert('Trebuie sa fii membru al site-ului pentru a putea posta comentarii la acest joc!');
			flip('addComment','loginComment')
			return false;
	}
}


function timedPreloader(){
	setTimeout("flip('preloader','joc')",2000);
}

function is_email(value) {
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    return (pattern.test(value))?true:false;
}

// simple ajax update
function ajax_get_updater(id, url, spinner, onComplete) {
    var e = $(id);
    if (!e) return;
    if (!spinner) var spinner = HTTP_SPINNER_IMAGE;
    e.update('<p style="margin-left: 20px;"><img src="'+spinner+'" alt="Loading..." title="Loading..." /></p>');
    var params = {
        method: 'get',
        evalScripts: true
    };
    if (onComplete) {
        params.onComplete = onComplete;
    }
    new Ajax.Updater(e, url, params);
}

// improved version of ajax update
function ajax_updater(params) {
    if (!params) params = {};
    // check target element
    if (!params.element) return;
    var target = $(params.element);
    if (!target) return;
    // check url
    if (!params.url) return;
    // has a spinner element been provided?
    if (params.spinner && $(params.spinner)) {
        // make it appear
        $(params.spinner).style.display = 'block';
    }
    else {
        // inject an ad-hoc inline spinner in the target element;
        // it will be overwritten when the target content updates
        var style = params.spinner_style ? params.spinner_style : '';
        target.update('<p style="'+style+'"><img src="'+HTTP_SPINNER_IMAGE+'" alt="Loading..." title="Loading..." /></p>');
        params.spinner = false;
    }
    // prepare Ajax
    var ajax_params = {
        method: params.method ? params.method : 'get',
        parameters: params.parameters ? params.parameters : {},
        evalScripts: params.evalScripts ? true : false,
        onComplete: function() {
            if (params.spinner) {
                $(params.spinner).style.display = 'none';
            }
            if (params.onComplete) {
                params.onComplete();
            }
        }
    };
    // we force a waiting period, if requested
    var forced_wait = params.wait == null ? 0 : params.wait;
    setTimeout(function() {
        new Ajax.Updater(target, params.url, ajax_params);
    }, forced_wait);
}

// this function loads ads into their proper containers;
// see lib/plugins/function.ads.php
function load_ads() {
    for (var i in SlowLoadAds) {
        // shortcut to the definition
        var a = SlowLoadAds[i];
        // locate source
        var s = $(a.source);
        // no source, no go
        if (!s) continue;
        // locate destination
        var d = $(a.destination);
        // no destination?
        if (!d) {
            // eliminate source
            s.remove();
            // skip this
            continue;
        }
        // move source to destination
        d.parentNode.replaceChild(s, d);
        // make sure the ad is displayed;
        // yes, we still refer to s, since the object reference
        // hasn't changed, just its position in the DOM
        s.style.display = 'block';
    }
}

// this function refreshes an image
function reload_image(image_id, url) {
    var img = $(image_id);
    if (!img) return;
    img.src = HTTP_SPINNER_IMAGE;
    var s = new Image();
    Element.extend(s);
    s.src = url;
    Event.observe(s, 'load', function(e){        
        img.src = s.src;
    });
}

// used to redirect to a new location,
// generally in order to bypass the effect of having a named anchor (#anchor)
// at the end of the url, in which case location.href alone is not enough
function js_redirect(url) {
    window.location.href = url;
    window.location.reload(true);
}

//check and uncheck form checkboxes 
function checkAll(field, bgnd) {
    if(typeof(field) == 'object' && field.length) {
        for (i = 0; i < field.length; i++) {
            field[i].checked = true;
            
            if(bgnd) {
	            var id = 'contentPictId' + field[i].value;
	        	if(document.getElementById(id)) {
	        	    document.getElementById(id).style.background = bgnd;
	        	}
            }	
        }
    } else if (typeof(field) == 'object'){
        field.checked = true;           	  
    }      	
}

function uncheckAll(field, disable)
{
    if(typeof(field) == 'object' && field.length) {
        for (i = 0; i < field.length; i++) {
            field[i].checked = false;
            
            if(disable) {
	            var id = 'contentPictId' + field[i].value;
	        	if(document.getElementById(id)) {
	        	    document.getElementById(id).style.background = 'transparent';
	        	}
            }	
        }    
            
        if(disable) {
        	if(disable.description) {
        	   for (i = 0; i < disable.description.length; i++) {
        	       disable.description[i].disabled = false;
        	   }    
        	}
        	
        	if(disable.bells_request) {
        	   for (i = 0; i < disable.bells_request.length; i++) {
        	       disable.bells_request[i].disabled = false;
        	   }    
        	}
        }        
    } else if (typeof(field) == 'object'){
        field.checked = false;
        if(disable) {
            disable.description.disabled = false;
            disable.bells_request.disabled = false;
            
            var id = 'contentPictId' + field.value;
        	if(document.getElementById(id)) {
        	    document.getElementById(id).style.background = 'transparent';
        	}
        }    	  
    }	
}

function validateAndSendFeedback(params) {
    var msgErrors = '';

    if(params.nume.value.length < 2) {
        msgErrors = 'Completeaza in campul de nume mai mult de 2 caractere!\n';
    }
    
    if(!is_email(params.email.value)) {
        msgErrors += 'Emailul este invalid!\n';
    }
    
    if(!params.subiect.value) {
        msgErrors += 'Alege un subiect!\n';
    }
    
    if(params.propunere.value.length < 10) {
        msgErrors += 'Completeaza in campul de propunere mai mult de 10 caractere!\n';
    }
    
    params.url = location.href;
    
    if(msgErrors != '') {
        alert(msgErrors);
        return false;        
    } else {           
        params.url = location.href;
        smartBox.getFeedbackPopupInfo(HTTP + 'ajaxCallbacks/feedback.php', params);
    }
}

// extend the regexp builtin object with
// a method that escapes special regexp characters
RegExp.escape = function(text) {
  if (!arguments.callee.sRE) {
    var specials = [
      '/', '.', '*', '+', '?', '|',
      '(', ')', '[', ']', '{', '}', '\\'
    ];
    arguments.callee.sRE = new RegExp(
      '(\\' + specials.join('|\\') + ')', 'g'
    );
  }
  return text.replace(arguments.callee.sRE, '\\$1');
}
