
var TCP = new TColorPicker();

function TCPopup(field, palette) {
        this.field = field;
        this.initPalette = !palette || palette > 3 ? 0 : palette;
        var w = 194, h = 240,
        move = screen ?
                ',left=' + ((screen.width - w) >> 1) + ',top=' + ((screen.height - h) >> 1) : '',
        o_colWindow = window.open('admin_cpic.php', null, "help=no,status=no,scrollbars=no,resizable=no" + move + ",width=" + w + ",height=" + h + ",dependent=yes", true);
        o_colWindow.opener = window;
        o_colWindow.focus();
}

function TCBuildCell (R, G, B, w, h) {
        return '<td bgcolor="#' + this.dec2hex((R << 16) + (G << 8) + B) + '"><a href="javascript:P.S(\'' + this.dec2hex((R << 16) + (G << 8) + B) + '\')" onmouseover="P.P(\'' + this.dec2hex((R << 16) + (G << 8) + B) + '\')"><img src="<?php echo $kws_imagesdir; ?>/pxl.gif" width="' + w + '" height="' + h + '" border="0"></a></td>';
}

function TCSelect(c) {
        this.field.value = '#' + c.toUpperCase();
        this.win.close();
}

function TCPaint(c, b_noPref) {
        c = (b_noPref ? '' : '#') + c.toUpperCase();
        if (this.o_samp)
                this.o_samp.innerHTML = '<font face=Verdana size=2>' + c +' <font color=white>' + c + '</font></font>'
        if(this.doc.layers)
                this.sample.bgColor = c;
        else {
                if (this.sample.backgroundColor != null) this.sample.backgroundColor = c;
                else if (this.sample.background != null) this.sample.background = c;
        }
}

function TCGenerateSafe() {
        var s = '';
        for (j = 0; j < 12; j ++) {
                s += "<tr>";
                for (k = 0; k < 3; k ++)
                        for (i = 0; i <= 5; i ++)
                                s += this.bldCell(k * 51 + (j % 2) * 51 * 3, Math.floor(j / 2) * 51, i * 51, 8, 10);
                s += "</tr>";
        }
        return s;
}

function TCGenerateWind() {
        var s = '';
        for (j = 0; j < 12; j ++) {
                s += "<tr>";
                for (k = 0; k < 3; k ++)
                        for (i = 0; i <= 5; i++)
                                s += this.bldCell(i * 51, k * 51 + (j % 2) * 51 * 3, Math.floor(j / 2) * 51, 8, 10);
                s += "</tr>";
        }
        return s
}
function TCGenerateMac() {
        var s = '';
        var c = 0,n = 1;
        var r,g,b;
        for (j = 0; j < 15; j ++) {
                s += "<tr>";
                for (k = 0; k < 3; k ++)
                        for (i = 0; i <= 5; i++){
                                if(j<12){
                                s += this.bldCell( 255-(Math.floor(j / 2) * 51), 255-(k * 51 + (j % 2) * 51 * 3),255-(i * 51), 8, 10);
                                }else{
                                        if(n<=14){
                                                r = 255-(n * 17);
                                                g=b=0;
                                        }else if(n>14 && n<=28){
                                                g = 255-((n-14) * 17);
                                                r=b=0;
                                        }else if(n>28 && n<=42){
                                                b = 255-((n-28) * 17);
                                                r=g=0;
                                        }else{
                                                r=g=b=255-((n-42) * 17);
                                        }
                                        s += this.bldCell( r, g,b, 8, 10);
                                        n++;
                                }
                        }
                s += "</tr>";
        }
        return s;
}

function TCGenerateGray() {
        var s = '';
        for (j = 0; j <= 15; j ++) {
                s += "<tr>";
                for (k = 0; k <= 15; k ++) {
                        g = Math.floor((k + j * 16) % 256);
                        s += this.bldCell(g, g, g, 9, 7);
                }
                s += '</tr>';
        }
        return s
}

function TCDec2Hex(v) {
        v = v.toString(16);
        for(; v.length < 6; v = '0' + v);
        return v;
}

function TCChgMode(v) {
        for (var k in this.divs) this.hide(k);
        this.show(v);
}

function TColorPicker(field) {
        this.build0 = TCGenerateSafe;
        this.build1 = TCGenerateWind;
        this.build2 = TCGenerateGray;
        this.build3 = TCGenerateMac;
        this.show = document.layers ?
                function (div) { this.divs[div].visibility = 'show' } :
                function (div) { this.divs[div].visibility = 'visible' };
        this.hide = document.layers ?
                function (div) { this.divs[div].visibility = 'hide' } :
                function (div) { this.divs[div].visibility = 'hidden' };
        // event handlers
        this.C       = TCChgMode;
        this.S       = TCSelect;
        this.P       = TCPaint;
        this.popup   = TCPopup;
        this.draw    = TCDraw;
        this.dec2hex = TCDec2Hex;
        this.bldCell = TCBuildCell;
        this.divs = [];
}

function TCDraw(o_win, o_doc) {
        this.win = o_win;
        this.doc = o_doc;
        var
        s_tag_openT  = o_doc.layers ?
                'layer visibility=hidden top=54 left=5 width=182' :
                'div style=visibility:hidden;position:absolute;left:6px;top:54px;width:182px;height:0',
        s_tag_openS  = o_doc.layers ? 'layer top=32 left=6' : 'div',
        s_tag_close  = o_doc.layers ? 'layer' : 'div'

        this.doc.write('<' + s_tag_openS + ' id=sam name=sam><table cellpadding=0 cellspacing=2 border=0 width=181 align=center class=bd><tr><td align=center height=18><div id="samp"><font face=Verdana size=2>sample <font color=white>sample</font></font></div></td></tr></table></' + s_tag_close + '>');
        this.sample = o_doc.layers ? o_doc.layers['sam'] :
                o_doc.getElementById ? o_doc.getElementById('sam').style : o_doc.all['sam'].style

        for (var k = 0; k < 4; k ++) {
                this.doc.write('<' + s_tag_openT + ' id="p' + k + '" name="p' + k + '"><table cellpadding=0 cellspacing=2 border=0 align=center>' + this['build' + k]() + '</table></' + s_tag_close + '>');
                this.divs[k] = o_doc.layers
                        ? o_doc.layers['p' + k] : o_doc.all
                                ? o_doc.all['p' + k].style : o_doc.getElementById('p' + k).style
        }
        if (!o_doc.layers && o_doc.body.innerHTML)
                this.o_samp = o_doc.all
                        ? o_doc.all.samp : o_doc.getElementById('samp');
        this.C(this.initPalette);
        if (this.field.value) this.P(this.field.value, true)
}


//
function Code(theCode) {
document.form1.desc.focus();
if (ie) {
var rng = document.selection.createRange();
rng.text = '' + theCode + '';
} else if (ns) {
var selStart = document.form1.desc.selectionStart;
var selEnd = document.form1.desc.selectionEnd;
var before = document.form1.desc.value.substring(0, selStart);
var after = document.form1.desc.value.substring(selEnd, document.form1.desc.textLength);
document.form1.desc.value = before + '' + theCode + '' + after;
} else {
document.form1.desc.value += '' + theCode + '';
}
document.form1.desc.focus();
}
var ie = navigator.appName == 'Microsoft Internet Explorer' ? 1 : 0;
var ns = navigator.appName == 'Netscape' ? 1 : 0;


function testPassword(passwd)
{
var description = new Array();
description[0] = "<table width='49%' border='0' cellpadding='2' cellspacing='1' bgcolor='#000000'><tr><td bgcolor='#FFFFFF'><table width='20%' border='0' cellpadding='0' cellspacing='0' bgcolor='#FF0000'><tr><td height='15'></td></tr></table></td></tr></table>";
description[1] = "<table width='49%' border='0' cellpadding='2' cellspacing='1' bgcolor='#000000'><tr><td bgcolor='#FFFFFF'><table width='40%' border='0' cellpadding='0' cellspacing='0' bgcolor='#FF7700'><tr><td height='15'></td></tr></table></td></tr></table>";
description[2] = "<table width='49%' border='0' cellpadding='2' cellspacing='1' bgcolor='#000000'><tr><td bgcolor='#FFFFFF'><table width='60%' border='0' cellpadding='0' cellspacing='0' bgcolor='#FFaa00'><tr><td height='15'></td></tr></table></td></tr></table>";
description[3] = "<table width='49%' border='0' cellpadding='2' cellspacing='1' bgcolor='#000000'><tr><td bgcolor='#FFFFFF'><table width='80%' border='0' cellpadding='0' cellspacing='0' bgcolor='#99cc00'><tr><td height='15'></td></tr></table></td></tr></table>";
description[4] = "<table width='49%' border='0' cellpadding='2' cellspacing='1' bgcolor='#000000'><tr><td bgcolor='#FFFFFF'><table width='100%' border='0' cellpadding='0' cellspacing='0' bgcolor='#00CC00'><tr><td height='15'></td></tr></table></td></tr></table>";
description[5] = "<table width='49%' border='0' cellpadding='2' cellspacing='1' bgcolor='#000000'><tr><td bgcolor='#FFFFFF'><table width='100%' border='0' cellpadding='0' cellspacing='0'><tr><td height='15'></td></tr></table></td></tr></table>";

                var intScore   = 0
                var strVerdict = 0
                
                // PASSWORD LENGTH
                if (passwd.length==0 || !passwd.length)                         // length 0
                {
                        intScore = -1
                }
                else if (passwd.length>0 && passwd.length<5) // length between 1 and 4
                {
                        intScore = (intScore+3)
                }
                else if (passwd.length>4 && passwd.length<8) // length between 5 and 7
                {
                        intScore = (intScore+6)
                }
                else if (passwd.length>7 && passwd.length<12)// length between 8 and 15
                {
                        intScore = (intScore+12)
                }
                else if (passwd.length>11)                    // length 16 or more
                {
                        intScore = (intScore+18)
                }
                
                
                // 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)
                }
                
                if (passwd.match(/[A-Z]/))                              // [verified] at least one upper case letter
                {
                        intScore = (intScore+5)
                }
                
                // NUMBERS
                if (passwd.match(/\d+/))                                 // [verified] at least one number
                {
                        intScore = (intScore+5)
                }
                
                if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/))             // [verified] at least three numbers
                {
                        intScore = (intScore+5)
                }
                
                
                // SPECIAL CHAR
                if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/))            // [verified] at least one special character
                {
                        intScore = (intScore+5)
                }
                
                                                                                                                                 // [verified] at least two special characters
                if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/))
                {
                        intScore = (intScore+5)
                }
        
                
                // COMBOS
                if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))        // [verified] both upper and lower case
                {
                        intScore = (intScore+2)
                }

                if (passwd.match(/(\d.*\D)|(\D.*\d)/))                    // [FAILED] both letters and numbers, almost works because an additional character is required
                {
                        intScore = (intScore+2)
                }
 
                                                                                                                                  // [verified] letters, numbers, and special characters
                if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))
                {
                        intScore = (intScore+2)
                }
        
        
                if(intScore == -1)
                {
                   strVerdict = description[5];
                }
                else if(intScore > -1 && intScore < 16)
                {
                   strVerdict = description[0];
                }
                else if (intScore > 15 && intScore < 25)
                {
                   strVerdict = description[1];
                }
                else if (intScore > 24 && intScore < 35)
                {
                   strVerdict = description[2];
                }
                else if (intScore > 34 && intScore < 45)
                {
                   strVerdict = description[3];
                }
                else
                {
                   strVerdict = description[4];
                }
        
        document.getElementById("Words").innerHTML= (strVerdict);
        
}


function VerifyField(field1, field2, field3, field4, field5, field6, result_id_12, result_id_34, result_id_56, match_state, nomatch_state, default_state) {
 this.field1 = field1;
 this.field2 = field2;
 this.field3 = field3;
 this.field4 = field4;
 this.field5 = field5;
 this.field6 = field6;
 this.result_id_12 = result_id_12;
 this.result_id_34 = result_id_34;
 this.result_id_56 = result_id_56;
 this.match_state = match_state;
 this.nomatch_state = nomatch_state;
 this.default_state = default_state;

 this.FieldCheck = function() {
      if (!this.result_id_12) { return false; }
      if (!this.result_id_34) { return false; }
      if (!this.result_id_56) { return false; }
      if (!document.getElementById){ return false; }
      show1 = document.getElementById(this.result_id_12);
      show3 = document.getElementById(this.result_id_34);
      show5 = document.getElementById(this.result_id_56);

      if (!show1){ return false; }
      if (!show3){ return false; }
      if (!show5){ return false; }
      
      if(this.field1.value != "")
      {
      if (this.field1.value == this.field2.value) {
      show1.innerHTML = this.match_state;
      } else {
      show1.innerHTML = this.nomatch_state;
      }
      } else {
      show1.innerHTML = this.default_state;
      }
  
      if(this.field3.value != "")
      {
      if (this.field3.value == this.field4.value) {
      show3.innerHTML = this.match_state;
      } else {
      show3.innerHTML = this.nomatch_state;
      }
      } else {
      show3.innerHTML = this.default_state;
      }
      
      if(this.field5.value != "")
      {
      if (this.field5.value == this.field6.value) {
      show5.innerHTML = this.match_state;
      } else {
      show5.innerHTML = this.nomatch_state;
     }
    } else {
   show5.innerHTML = this.default_state;
  }
 }
}


function SField(field1, field2, result_id, match_state, nomatch_state, default_state) {
 this.field1 = field1;
 this.field2 = field2;
 this.result_id = result_id;
 this.match_state = match_state;
 this.nomatch_state = nomatch_state;
 this.default_state = default_state;

 this.CheckSField = function() {
      if (!this.result_id) { return false; }
      if (!document.getElementById){ return false; }
      show = document.getElementById(this.result_id);

      if (!show){ return false; }

      if(this.field1.value != "")
      {
      if (this.field1.value == this.field2.value) {
      show.innerHTML = this.match_state;
      } else {
      show.innerHTML = this.nomatch_state;
      }
      } else {
      show.innerHTML = this.default_state;
  }
 }
}



