// This function will alert a error or special message whenever the page is loaded with such message information
function showSpcMessage(mesg){
    if (mesg != "") alert(mesg);
}

// This will show the element if it is hidden, or it will hide if it is already displayed.
function showHide(elementId){
    if (document.getElementById(elementId).style.display == 'none') document.getElementById(elementId).style.display=''; 
    else document.getElementById(elementId).style.display='none';
}

// This will show or hide specific elements.
function showAndHide(elementId, doWhat){
    elementId = elementId.split(',');
    doWhat = doWhat.split(',');
    
    for(var i=0; i<elementId.length; i++){
        if (doWhat[i] == "show") document.getElementById(elementId[i]).style.display='';
        else if (doWhat[i] == "hide") document.getElementById(elementId[i]).style.display='none';
        else{
            if (document.getElementById(elementId[i]).style.display == 'none') document.getElementById(elementId[i]).style.display=''; 
            else document.getElementById(elementId[i]).style.display='none';
        }
    }
}

// determine if a variable is numeric or not
function isNumeric(VARIABLE){
    if(String(VARIABLE).match(/^\d+$/)) return true;
    return false;
}


// These two function is for any password box. It will indicate the security level of the password input
// passwordSecurityCheck(), loadFormPassSecCheck()
var securityLevel = new Array();
    securityLevel[0] = 'zero';
    securityLevel[1] = 'very low';
    securityLevel[2] = 'low';
    securityLevel[3] = 'average';
    securityLevel[4] = 'good';
    securityLevel[5] = 'very good';
    securityLevel[6] = 'maximum';
function passwordSecurityCheck(password, whichIdBox){
    var sLevel = 0;
    if (password.length <= 3) sLevel = 0;
    else if (password.length > 3 && parseInt(password.length) <= 5) sLevel = 1;
    else if (password.length > 5 && parseInt(password.length) <= 7) sLevel = 2;
    else if (password.length > 7) sLevel = 3;
    
    if (String(password).match( /\d+/ )) sLevel++;
    if (String(password).match( /[A-Z]/ )) sLevel++;
    if (String(password).match( /[^\w\d]/ )) sLevel++;
    
    if (document.getElementById(whichIdBox)) document.getElementById(whichIdBox).innerHTML = securityLevel[sLevel];
}
function loadFormPassSecCheck(){
    if (document.getElementById('password_sec_input')){
        passwordSecurityCheck(document.getElementById('password_sec_input').value, 'password_security');
    }
}

// This will change the Male/Female basic picture in the COX-Character page.
function changePicInBio(valMF, gTag){
    if (valMF == 1) document.getElementById(gTag + '_image_fileName').src = "client_file/images/" + gTag + "_character_pics/nopic_m.gif";
    else if (valMF == 2) document.getElementById(gTag + '_image_fileName').src = "client_file/images/" + gTag + "_character_pics/nopic_f.gif";
}


// This will bloc the maximum character for the Shoutbox input textare. If the user reaches the maximum as set in the admin control pannel, he will be warned and his message will be troncated to the maximum value allowed.
var previousMessage = "";
var alertOnce = 0;
function shoutWrite(elementId, message){
    // max 165 characters * 2
    var maxChars2 = shoutMaxCharsI - 1;
    if (message.length < maxChars2) document.getElementById('textareaMaxLength_' + elementId).innerHTML = 'characters: ' + String(message.length) + ' / ' + shoutMaxCharsS;
    else{
        document.getElementById('textareaMaxLength_' + elementId).innerHTML = 'characters: ' + String(message.length) + ' / ' + shoutMaxCharsS;
        document.getElementById('shoutMessageId_' + elementId).value = message.substring(0,maxChars2);
        if (alertOnce == 0){
            alert('You have reached ' + shoutMaxCharsS + ' characters for your post. You can not exceed ' + shoutMaxCharsS + ' characters.');
            alertOnce = 1;
        }
    }
}

// check if a field input is a value A-Z, a-z or 0-9. Used so template names won't contain symbol characters since the name will be saved and used to create a new file.
var lastCheckKeyVal = '';
function checkIfAZaz09(tVal, elId){
    var len = String(tVal).length;
    var iVal = '';
    var foundIllegalChar = 0;
    for(var i=0; i<len; i++){
        iVal = String(tVal).substring(i, i+1);
        if (!iVal.match(/\w/)) foundIllegalChar = 1;
    }
    
    if (foundIllegalChar == 0) lastCheckKeyVal = tVal;
    else{
        alert(' / The template name can contain only letters A-Z and a-z or numbers 0-9. No other symbol or space or key will be accepted');
        document.getElementById(elId).value = lastCheckKeyVal;
    }
}
function checkIfAZaz09_straight(tVal){
    var len = String(tVal).length;
    var iVal = '';
    var foundIllegalChar = 0;
    for(var i=0; i<len; i++){
        iVal = String(tVal).substring(i, i+1);
        if (!iVal.match(/\w/)) foundIllegalChar = 1;
    }
    
    if (foundIllegalChar == 0) return true;
    else return false
}

function disableEnterKey(e){
     var key;     
     if(window.event) key = window.event.keyCode; //IE
     else key = e.which; //firefox     

     return (key != 13);
}

var holdingKey_shift = "no";
var holdingKey_e = "no";
function holdPlusClickEdit(e, doingWhat){
     var key;     
     if(window.event) key = window.event.keyCode; //IE
     else key = e.which; //firefox     

     if (doingWhat == "down" && key == 69) holdingKey_shift = "yes";
     else if (doingWhat == "up" && key == 69) holdingKey_shift = "no";
     
     if (doingWhat == "down" && key == 16) holdingKey_e = "yes";
     else if (doingWhat == "up" && key == 16) holdingKey_e = "no";
     
     if (doingWhat == "mouseDown" && holdingKey_shift == "yes" && holdingKey_e == "yes"){
        if (actualPageCanAdmin == "YES") window.location = 'http://' + actualSiteUrl + '/index.php?pageId=' + actualPageId + '&edit=1';
        holdingKey_e = "no";
        holdingKey_shift = "no";
     }
}

function resizeMainWrap(){
    if (parseInt(navigator.appVersion) > 3) {
        if (navigator.appName == "Netscape") {
            var winW = window.innerWidth;
            var winH = window.innerHeight;
        }
        else if (navigator.appName.indexOf("Microsoft")!=-1) {
            var winW = document.body.offsetWidth - 10;
            var winH = document.body.offsetHeight - 10;
        }
        
        if (document.getElementById('left_index_td')) var leftWidth = document.getElementById('left_index_td').offsetWidth;
        else var leftWidth = 0;
        if (document.getElementById('right_index_td')) var rightWidth = document.getElementById('right_index_td').offsetWidth;
        else var rightWidth = 0;
        var centerWidth = winW - leftWidth - rightWidth;
        document.getElementById('whole_page').style.width = centerWidth;

        if (document.getElementById('index_height_set')) var topHeight = document.getElementById('index_height_set').offsetHeight;
        else var topHeight = 0;
        var centerHeight = winH - topHeight;
        document.getElementById('whole_page').style.height = centerHeight;
        if(document.getElementById('left_index_div')) document.getElementById('left_index_div').style.height = centerHeight;
        if(document.getElementById('right_index_div')) document.getElementById('right_index_div').style.height = centerHeight;
    }
}

// determine if a variable is numeric or not
function checkOnKeyUpMustBeNumber(myVal, myId, myAlertMessage){
     if(isNumeric(myVal) == false){
         document.getElementById(myId).value = myVal.substr(0, myVal.length - 1);
     }
}

// PHP IMPLODE Equivalent
function implode( glue, pieces ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Waldo Malqui Silva
    // *     example 1: implode(' ', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: 'Kevin van Zonneveld'
 
    return ( ( pieces instanceof Array ) ? pieces.join ( glue ) : pieces );
}

function closeThisWindow(){
    window.close();
}


// ======================== Fonction For POP-UP DIVs following mouse position ==================================
var whichDivShowing = 'NA';
var showClickIs = 'ON';
// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
if (navigator.appName.indexOf("Microsoft") != -1) var IE = true;
else var IE = false;

// If NS -- that is, !IE -- then set up for mouse capture
// if (!IE) document.captureEvents(Event.MOUSEMOVE);
if (!IE) document.addEventListener("mousemove", getMouseXY, false);


// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  
  if (whichDivShowing != 'NA' && showClickIs == "ON"){
      document.getElementById(whichDivShowing).style.top = tempY;
      document.getElementById(whichDivShowing).style.left = tempX;
      document.getElementById(whichDivShowing).style.display = '';
  }
  
  return true
}



// ======================== Fonction to pop-up the printing of a specific content within a page ==================================
function popupPrintThisPage(mydDivId){
    var el = document.getElementById(mydDivId);
    var myContent = el.innerHTML;
    myContent = myContent.replace(/&/g, '__ANDTAG__');
    myContentEx = myContent.split(' ');
    
    var contentSplit = '';
    for(var i=0; i<myContentEx.length; i++){
        contentSplit = contentSplit + '&content[' + i + ']=' + myContentEx[i];
    }
    
    // Get this actual element style
    var myBackground = el.style.backgroundColor;
    var myColor = el.style.color;
    while(myBackground == "" || myBackground == "transparent"){
        el = el.parentNode;
        myBackground = el.style.backgroundColor;
        
        if(myBackground != "") myColor = el.style.color;
        
        if(el.nodeName == "body") break;
    }
    
    ajax.replace('index.php', 'ajaxActions_div', 'post', 'ajaxAction=system/print.php&keepPostsIntegrality=1&color=' + myColor + '&bgcolor=' + myBackground + '&action=printthispage' + contentSplit, doNothing, openPrintThispage);
}
function openPrintThispage(){
    window.open ("index.php?blank_popup=system/print.php&action=printthispage","printThisPage","status=0,toolbar=0,location=0, menubar=0, directories=0, resizable=1, scrollbars=1, height=600, width=800");
}