var actualVer = "1,0,21";
var plugin_name = "Camfrog Video Chat Web Client"

function detectPlugin(searchPlugin)
{
    var pluginFound = false;
    if (navigator.plugins && navigator.plugins.length > 0)
    {
        var pluginsArrayLength = navigator.plugins.length;
        var numFound = 0;
        for(pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++)
        {
            if(navigator.plugins[pluginsArrayCounter].description.indexOf(searchPlugin) >= 0)
            {
                verString = navigator.plugins[pluginsArrayCounter].description.substr(plugin_name.length + 1);
                return checkVersion(verString, actualVer);
            }
        }
    }
    return false;
}

/* Returns true if currentVer is greater than desiredVer */
function checkVersion(currentVer, desiredVer) {
    var currentVerArray = currentVer.split(".");
    var desiredVerArray = desiredVer.split(",");
    var i = 0;
    while ((i < currentVerArray.length) && (i < desiredVerArray.length)) {
        if (parseFloat(currentVerArray[i]) > parseFloat(desiredVerArray[i])) {
            return true;
        } else if (parseFloat(currentVerArray[i]) < parseFloat(desiredVerArray[i])) {
            return false;
        } else {
            i += 1;
        }
    }
    if (i >= desiredVerArray.length) {
        return true;
    }
    return false;
}

function refreshPlugins()
{
    navigator.plugins.refresh();
    if(!detectPlugin(plugin_name))
        setTimeout(refreshPlugins, 5000)
    else
        location.reload(true);
}
function utf8_to_cp1251($utf8) {

    $windows1251 = "";
    $chars = $utf8.split("//");
    return $chars;
}

function InsertPlugin()
{   
    var isErr=false;
    room_name = utf8_to_cp1251(room_name);

    res = valid_size(width,height);
    if (res[0]=="Err" ){document.write("Error Incorrect Width Room<br>");isErr=true;};
    if (res[1]=="Err" ){document.write("Error Incorrect Height Room<br>");isErr=true;};
    if (!valid_room_name(room_name)){document.write("Error Incorrect Room Name<br>");isErr=true;};
    //if (isErr) {document.write("<a href='http://www.camfrogapp.dms.ds.tomsksoft/help/cwe/'>Look Info Here</a>");exit;}
    if (isErr) {document.write("<a href='http://www.camfrog.com/help/cwe/'>Look Info Here</a>");exit;}
    
    
    //var help_url = "http://www.camfrogapp.dms.ds.tomsksoft/help/cwe/installation/";
    help_url = "http://camfrog.com/help/cwe/installation/";
    var download_cwe_url = 'http://download2.camfrog.com/CamfrogWebClient.exe'; //download/cwe.exe - installer

    var show_text = '<link type="text/css" rel="stylesheet" href="http://camfrog.com/css/btn_yellow_big.css">';
    show_text += '<div id="cwe_block"><div class="icon_img"></div><br/><br/>';
    
    var is64 = (navigator.userAgent.indexOf("x64") != -1);
    
    if (is64){
        show_text += '<p style="color: #979797;">You are using a 64-bit web browser.</p>';
        show_text += '<p style="color: #979797;">Camfrog Web Client currently supports 32-bit browsers only.</p>';
    } else {
        show_text += '<p style="color: #979797;">Please download the latest</p><p style="margin-top:-14px;color: #979797;">version of Camfrog Video Chat Web Client.</p>';
        show_text += '<a class="btn_yellow_big" style="margin-left: -174px" href="' + download_cwe_url + '"> <ins class="btn_yellow_big_l"></ins> <ins class="btn_yellow_big_c">Download Camfrog Web Client</ins> <ins class="btn_yellow_big_r"></ins></a>';
        show_text += '<div class="clear"></div><a href="' + help_url + '" target="_blank" style="white-space: nowrap;font-size:11px;">How to Install</a></div>'        
    }
    
    if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length)
    {
        // FF
        
        if(!detectPlugin(plugin_name))
        {   
            document.write(show_text);
            
            refreshPlugins();
        }
       else
        {   
            document.write('<embed id="bla1" type="application/cwc-plugin" width='+width+' height='+height
            + ' room_name=\'' + room_name + '\''
            + '</embed>');
            
        }
    }
    else if (is64){
        //IE64 && maybe other64
        document.write(show_text);
    } else {
      // IE
      document.write('<object classid="CLSID:C4207369-B9E6-482C-82A2-5E42C99BBCAF" width='+width+' height='+height+' CODEBASE="' + download_cwe_url + '#version=' + actualVer + '">'
        + '<param name="room_name" value=\'' + room_name + '\'>'
        + '</object>');
         document.write('<br/><a href="' + help_url + '" style="white-space: nowrap;margin-left:340px;font-size:11px;">How to Install</a>');
    }
    
    
}

function valid_size(width,height){
    var res=[];
    var regexp = /^[0-9]{0,4}$/;
    if ( (regexp.test(width)) && (width>=800) && (width<=2000))res[0]="ok"; else res[0]="Err";
    if ( (regexp.test(height)) &&(height>=600) && (height<=1500))res[1]="ok"; else res[1]="Err";
    return res;
}

function valid_room_name(name){
    var regexp = /^[A-Za-z0-9\~\`\!\@\#\$\%\^\&\*\(\)\_\-\+\=\{\[\}\]\|\\\:\;\"' . "\'\<\,\>\.\?\/]{1,40}$/;
        if (regexp.test(name)){
            return true;
        } else {
            return false; 
        } 
}

InsertPlugin();

