var isloaded = false;     
var paletteImgs = new Array();
var controlImgs = new Array();
var mTime = null;
var inputTime = null;
var windowWidth = 0;
var windowHeight = 0;
var diaName = "name";
var first = false;
var myApplet = null;
var javaVersion = 0.0;
var loadingTime = 0;
var actualSessionId;
var sendFilename = "";
var loadAppletTime = null;
var appletLoaded = false;
var goodConnection = true;
var checkedConnection = true;

function appletLoaded(thisApp){
    myApplet = thisApp;
//    alert("appletLoaded: " + myApplet.toSource());
    myApplet.saveActualSessionId(actualSessionId);
}

function load(){
    mTime = setTimeout(checkApplet, 1000);
}

function load(_sessionId){
    mTime = setTimeout(checkApplet, 1000);
    saveActualSessionId( _sessionId);
}

function changeSize(width, height) {
    //alert('applet loaded!!');
    //scribAppletEl
    //pageDiv
    appletLoaded = true;
    //if (isloaded) {
        loadAppletTime = null;
        var flashDiv = document.getElementById("flashDiv");
        flashDiv.style.display = "none";
        
        //alert('height='+height+';;width='+width);
        document.getElementById('pageDiv').style.width = width+'px';
        //Fix for Ad - it was shownon the top of the board
        if (height == '868') {
            document.getElementById('pageDiv').style.height = '915px';
        }
        else {	    
           //document.getElementById('pageDiv').style.height = height+'px';
		document.getElementById('pageDiv').style.height = '614px';
        }
        document.getElementById('scribAppletEl').style.width = width+'px';
        document.getElementById('scribAppletEl').style.height = height+'px';
        document.getElementById('scribApplet').style.width = width+'px';
        document.getElementById('scribApplet').style.height = height+'px';
        
        //set height for bottom log
        if (document.getElementById('poweredByScriblinkDiv')) {
            document.getElementById('poweredByScriblinkDiv').style.height ='24px';
            document.getElementById('poweredByScriblinkImg').style.height ='24px';
        }
        
        
    //}
    //else {
    //    loadAppletTime = setTimeout("changeSize("+width+","+height+")", 200);
    //}
    
}

function hideIntroFlash(){
		//alert('here1');
    if(mTime != null){
        clearTimeout(mTime);
        mTime = null;
    }
    if (!appletLoaded) {  
        mTime = setTimeout(hideIntroFlash, 200);
        return;
    }
    var borderTwo = document.getElementById("secondBorder");
    var conHeight = borderTwo.offsetHeight;
    var drawArea = document.getElementById("drawDiv");
    var conWidth = drawArea.offsetWidth;
    var flashDiv = document.getElementById("flashDiv");
    flashDiv.style.display = "none";
    
    //var appletDiv = document.getElementById("scribAppletEl");
    //alert('here2 '+appletDiv.style.display);
    //appletDiv.style.visibility= 'visible';
    //appletDiv.style.display= '';
    //appletDiv.style.width = conWidth + "px";
    //appletDiv.style.height = (conHeight - 4) + "px";
    //appletDiv.style.height = "566px";
    //!!!
    var appl = document.getElementById('scribApplet');
    //alert(appl);
    //appl.height = "566px";
    isloaded = true;
    //alert('height='+appletDiv.style.height+'## width= '+appletDiv.style.width);
    //alert('here25 '+appletDiv.style.height+'##'+appletDiv.style.width+';;t,l= '+appletDiv.style.top+',,'+appletDiv.style.left);
    //alert('here3 '+appletDiv.style.display);
//    myApplet.doAction("getname", "");
//    setChatTextBox();
}

function showMessageDiv(){
    var borderTwo = document.getElementById("secondBorder");
    var conHeight = borderTwo.offsetHeight;
    var flashDiv = document.getElementById("flashDiv");
    flashDiv.style.display = "none";
    var msgDiv = document.getElementById("msgDiv");
    msgDiv.style.width = "100%";
    msgDiv.style.height = (conHeight - 4) + "px";
    msgDiv.style.fontSize = "24px";
    msgDiv.style.display = "block";
}

function checkApplet(){
    clearTimeout(mTime);
    mTime = null;
    if (false && navigator && navigator.userAgent.indexOf("Firefox") >= 0  && navigator.userAgent.indexOf("Mac OS X") >= 0)
    {
      showMessageDiv();
      getSupportMessage();
      return;
    }
    //alert('connection:'+checkedConnection);
    
    if(isObjLoaded("flashLogo") && checkedConnection){
        //alert("good Connection: "+goodConnection);
        if (goodConnection == "false") {
           showMessageDiv();
           getConnectionErrorMessage(); 
        }
        else {
            //alert("in else");
            //alert(javaVersion);
            /*
            if (javaVersion==0)  
            {
                javaVersion=1.5;
            }
            */
            playFlash();
//             if(javaVersion >= 1.5){
//               playFlash();
//                }else if(javaVersion < 0){
//                    showMessageDiv();
//                    getDownloadMessage();
//                }else{
//                    loadingTime++;
//                    if(loadingTime > 30){
//                        showMessageDiv();
//                        getSupportMessage();
//                    }else{
//                        if(javaVersion < 0){
//                            showMessageDiv();
//                            getDownloadMessage();
//                        }else{
//                            mTime = setTimeout(checkApplet, 1000);
//                        }
//                    }
//                }
        }
    }else{
        mTime = setTimeout(checkApplet, 1000);
    }
}

function playFlash(){
    var flashMovie = getFlashObject("flashLogo");
    if(flashMovie){
        flashMovie.Play();
    }
}

function setAppletColor(clr, divName){
    if(isloaded){
        var appletDiv = document.getElementById("scribApplet");
        try{
            appletDiv.doAction("SetColor", clr);
        }catch(err){
            alert("Error : " + err.description);
        }
        //clear all border
        document.getElementById("blackClr").style.border = 'none';
        document.getElementById("blueClr").style.border = 'none';
        document.getElementById("greenClr").style.border = 'none';
        document.getElementById("redClr").style.border = 'none';
        //set the current div border
        document.getElementById(divName).style.border = "2px solid #07BDF2";
    }
}

function validateEmail(email) {
    var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
    if(email.value.length == 0){
        return false;
    }else{
        if(!re_mail.test(email.value))
            return false;
        else
            return true;
    }
}

function disableLinks() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  var divObj = document.getElementById("backdiv");
  divObj.style.width = (myWidth) + "px";
  divObj.style.height = (myHeight) + "px";
  divObj.style.display = "block";
  windowWidth = myWidth;
  windowHeight = myHeight;
}

function enableLinks(){
    var divObj = document.getElementById("backdiv");
    divObj.style.display = "none";
}

function getFlashObject(movieName){
  var retVal = null;
  
    if(window.document[movieName]){
        retVal = window.document[movieName];
    }else if(navigator.appName.indexOf("MSIE") == -1){
        if (document.embeds && document.embeds[movieName]){
            retVal = document.embeds[movieName]; 
        }
    }else{
        retVal = document.getElementById(movieName);
    }
    
  return retVal;
}

function setChatTextBox(){
    var txtBox = document.getElementById("chatInput");
    if(txtBox != null){
        var chatForm = document.getElementById("chatForm");
        txtBox.style.width = (chatForm.offsetWidth - 50) + 'px';
    }
}


function isObjLoaded(elemID) {
    var elem = (document.getElementById) ? document.getElementById(elemID) : ((document.all) ? document.all[elemID] : null);
    if(elem){
        return true;
    }else{
        return false;
    }
}

function getBrowser(){
    var agt = navigator.userAgent.toLowerCase();
    var retValue = null;

    var is_major = parseInt(navigator.appVersion);

    if((agt.indexOf('mozilla') != -1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera') == -1) && (agt.indexOf('safari') == -1)){
        if(is_major >= 4){
            retValue = "navigator";
        }
    }else if(((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1))){
        if(is_major >= 4){
            retValue = "msie";
        }
    }else if(agt.indexOf("opera") != -1){
        var is_opera2 = (agt.indexOf("opera 2") != -1 || agt.indexOf("opera/2") != -1);
        var is_opera3 = (agt.indexOf("opera 3") != -1 || agt.indexOf("opera/3") != -1);
        var is_opera4 = (agt.indexOf("opera 4") != -1 || agt.indexOf("opera/4") != -1);
        var is_opera5 = (agt.indexOf("opera 5") != -1 || agt.indexOf("opera/5") != -1);
        if(!is_opera2 && !is_opera3 && !is_opera4){
            retValue = "opera";
        }
    }else if(agt.indexOf("safari") != -1){
        retValue = "safari";
    }
    return retValue;
}

function getMyApplet(){
    return myApplet;
}

function setConnectionFlag(conn) {
    //alert("flag: "+conn);
    goodConnection = conn;
    checkedConnection = true;
}

function writeJavaVersion(ver){
  //alert("java version number is " + ver);
		
    javaVersion = ver;
 // alert("java version number is " + javaVersion);
}

//
// save the actual session id as soon as possible to compare with the old
// session id to depertime if we have quit the site or not.

function saveActualSessionId( _sessionId ){
    actualSessionId = _sessionId;
}

//
// save the session id just before leaving the page.
function saveOldSessionId(_sessionId){
    if(myApplet != null){
        myApplet.saveOldSessionId(_sessionId);
    } else {
        alert( "scriblink.js::saveOldSessionId: no applet available ...");
    }
}

function setRoomId(_roomId){
    roomId = _roomId;
}

function getRoomId(){
    return roomId;
}
