// the code below does conditional IE JS compilation;
// it sets one value for IE6 (JScript 5.6), IE7 (JScript 5.7) and earlier
// and another for other browsers
/*@cc_on
   /*@if (@_jscript_version <= 5.7)
      var LOAD_EVENT = 'load';
      var LOAD_ELEMENT = window;
   @else @*/
      var LOAD_EVENT = 'dom:loaded';
      var LOAD_ELEMENT = document;
   /*@end
@*/

function fire_fake_dom_loaded_event() {
    document.fire('my:dom:loaded');
}

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('<img src="'+spinner+'" alt="Loading..." title="Loading..." />');
    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 if ('undefined' == typeof(params.spinner)) {
        // 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');
}

// extract the named anchor from the current URL
function get_url_anchor() {
    var re = new RegExp('#([^#]+)$');
    var matches = re.exec(document.location);
    if (matches && matches.length >= 1) return matches[1];
    return '';
}


/**
* password strength test    
* 
* Orignal Author:  Steve Moitozo <god at zilla dot us> -- geekwisdom.com
*/
function testPassword(passwd)
{
        var intScore   = 0
        var strVerdict = "weak"
        var strLog     = ""
        
        // PASSWORD LENGTH
        if (passwd.length>0 && passwd.length<5)                         // length 4 or less
        {
            intScore = (intScore+3)
            //strLog   = strLog + "3 points for length (" + passwd.length + ")\n"
        }
        else if (passwd.length>4 && passwd.length<8) // length between 5 and 7
        {
            intScore = (intScore+6)
            //strLog   = strLog + "6 points for length (" + passwd.length + ")\n"
        }
        else if (passwd.length>7 && passwd.length<16)// length between 8 and 15
        {
            intScore = (intScore+12)
            //strLog   = strLog + "12 points for length (" + passwd.length + ")\n"
        }
        else if (passwd.length>15)                    // length 16 or more
        {
            intScore = (intScore+18)
            //strLog   = strLog + "18 point for length (" + passwd.length + ")\n"
        }
        
        
        // LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
        if (passwd.match(/[a-z]/))                              // [verified] at least one lower case letter
        {
            intScore = (intScore+1)
            //strLog   = strLog + "1 point for at least one lower case char\n"
        }
        
        if (passwd.match(/[A-Z]/))                              // [verified] at least one upper case letter
        {
            intScore = (intScore+5)
            //strLog   = strLog + "5 points for at least one upper case char\n"
        }
        
        // NUMBERS
        if (passwd.match(/\d+/))                                 // [verified] at least one number
        {
            intScore = (intScore+5)
            //strLog   = strLog + "5 points for at least one number\n"
        }
        
        if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/))             // [verified] at least three numbers
        {
            intScore = (intScore+5)
            //strLog   = strLog + "5 points for at least three numbers\n"
        }
        
        
        // SPECIAL CHAR
        if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/))            // [verified] at least one special character
        {
            intScore = (intScore+5)
            //strLog   = strLog + "5 points for at least one special char\n"
        }
        
                                     // [verified] at least two special characters
        if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/))
        {
            intScore = (intScore+5)
            //strLog   = strLog + "5 points for at least two special chars\n"
        }
    
        
        // COMBOS
        if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))        // [verified] both upper and lower case
        {
            intScore = (intScore+2)
            //strLog   = strLog + "2 combo points for upper and lower letters\n"
        }

        if (passwd.match(/([a-zA-Z])/) && passwd.match(/([0-9])/)) // [verified] both letters and numbers
        {
            intScore = (intScore+2)
            //strLog   = strLog + "2 combo points for letters and numbers\n"
        }
 
                                    // [verified] letters, numbers, and special characters
        if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))
        {
            intScore = (intScore+2)
            //strLog   = strLog + "2 combo points for letters, numbers and special chars\n"
        }

        /*
        if(intScore < 16)
        {
           strVerdict = "very weak"
        }
        else if (intScore > 15 && intScore < 25)
        {
           strVerdict = "weak"
        }
        else if (intScore > 24 && intScore < 35)
        {
           strVerdict = "mediocre"
        }
        else if (intScore > 34 && intScore < 45)
        {
           strVerdict = "strong"
        }
        else
        {
           strVerdict = "stronger"
        }
        */
    // acceptable max score is 50
    var strengthPercent = Math.floor(intScore * 100/45);
    if (strengthPercent > 100) {
        strengthPercent = 100;
    }
    return strengthPercent;
    
}

