﻿// JScript File

function OpenPlayWindow(lang,url)
{
    var h = 480;
    var v = 620;
    OpenExWindow(lang,url,h,v);
}

function OpenExWindow(lang,url,height,width,name)
{
    if (url && url != "")
    {
        var o = "";
        if (lang != undefined && lang != "")
        {
            if (url.indexOf("?") > 0)
                o = "&lang=" + lang;
            else
                o = "?lang=" + lang;
        }
        var h = height;
        var v = width;
        var opts = "height=" + h + ",width=" + v/* + ",status=1"*/;
        var top  = window.screen.height/2 - h/2;
	    var left = window.screen.width/2 - v/2;
	    opts = "left=" + left + ",top=" + top + "," + opts;
	    if (!name)
	        name = "_blank";
        var w = window.open(url + o,name,opts,"true");
        if (w) w.focus();
     }
}

function MyCompareValidatorEvaluateIsValid(val) {
    var value = ValidatorGetValue(val.controltovalidate);
    var compareTo = "";
    if ((typeof(val.controltocompare) !== "string") ||
        (typeof(document.getElementById(val.controltocompare)) === "undefined") ||
        (null == document.getElementById(val.controltocompare))) {
        if (typeof(val.valuetocompare) === "string") {
            compareTo = val.valuetocompare;
        }
    }
    else {
        compareTo = ValidatorGetValue(val.controltocompare);
    }
    var operator = "Equal";
    if (typeof(val.operator) === "string") {
        operator = val.operator;
    }
    return ValidatorCompare(value, compareTo, operator, val);
}

function IsNumeric(source,args)
{
    args.IsValid = Number(args.Value) != Number.NaN;
    return args.IsValid;
}

function keypressed()
{
    if (window.event && window.event.keyCode == 27 && typeof(CloseLogin) !== 'undefined') CloseLogin();
    return true;
}

function ShowBlur()
{
    var blur = $get("blur");
    $common.setElementOpacity(blur,0.35);
    var h = Math.max(getDocumentHeight(), getClientHeight());//$common.getSize(document.body).height
    blur.style.height = h + "px";
    blur.style.display = '';
}

function ValidatePswStrength(source,args)
{
    var b = $find("psw_strength_b");
    args.IsValid = b._getPasswordStrength() >= 80;
    return args.IsValid;
}

function CloseAndRefreshParent()
{
    if (opener)
    {
        opener.window.location.href = opener.window.location.href;
    }
    SmartClose();
}
function SmartClose()
{
    window.opener=self;
    window.close();
}
function chessStatus(seek)
{
    //Sys.Debug.trace(seek);
    
    if (seek)
    {
        ChessService._intSeek = seek;
        ChessService.GetSeekGame(seek,ChessService_GetSeekGame_SucceededCallback);
    }
}

function ChessService_GetSeekGame_SucceededCallback(result)
{
    if (Number(result) > 0)
    {
        if (soundManager)
            soundManager.play("newgames",{url:chimemp3,volume:10});
        location.href = "game.aspx?gameId=" + result;
    }
    else if (Number(result) == -1)
        location.href = "seek2.aspx?cust=1";
    else if (Number(result) == -2)
        location.href = "accept.aspx?d=1";
    else
        setTimeout(function(){chessStatus(ChessService._intSeek)},2000);
}

function UpdatePanel(pageId, upd_panel, str)
{
    var mgr = Sys.WebForms.PageRequestManager.getInstance();
    var form = mgr._form;
    mgr._postBackSettings = mgr._createPostBackSettings(true, [mgr._scriptManagerID + '|' + pageId], $get(upd_panel));
    form.__EVENTTARGET.value = pageId;
    form.__EVENTARGUMENT.value = str;
    mgr._onFormSubmit();
    //Sys.Debug.trace("submited");
}
function CenterWindow(width, height)
{
    var top  = window.screen.height/2 - height/2;
	var left = window.screen.width/2 - width/2;
	try { moveTo(left, top); }
	catch(ex){}
}
function activity_handleError(webServiceError, userContext, methodName)
{
    //if (webServiceError.get_message().endsWith("sql"))
    //{
        setTimeout(function(){
            var pa = $find("pa");
            pa.populate();
        },30000);
        return 0;   //don't update
    //}
}
function SetText(tn, s)
{
    tn.innerHTML = s;
//    if (Sys.Browser.agent == Sys.Browser.Opera)
//    {
//        tn.innerText = s;
//    }
//    else
//    {
//        var ni = tn.childNodes[0];
//        if (!ni)
//        {
//            ni = document.createTextNode(s);
//            tn.appendChild(ni);
//        }
//        else
//        {
//            ni.nodeValue = s;
//        }
//    }
}
function SetTimeControl(idx,time,inc,timeMetric,incMetric,tmValue,imValue,hmValue,hm2Value)
{
    switch(idx)
    {
        case 0:
            time.value = "";
            inc.value = "";
            SetText(timeMetric,tmValue);
            SetText(incMetric,imValue);
            break;
        case 1:
            time.value = "1";
            inc.value = "0";
            SetText(timeMetric,tmValue);
            SetText(incMetric,imValue);
            break;
        case 2:
            time.value = "3";
            inc.value = "0";
            SetText(timeMetric,tmValue);
            SetText(incMetric,imValue);
            break;
        case 3:
            time.value = "5";
            inc.value = "0";
            SetText(timeMetric,tmValue);
            SetText(incMetric,imValue);
            break;
        case 4:
            time.value = "10";
            inc.value = "0";
            SetText(timeMetric,tmValue);
            SetText(incMetric,imValue);
            break;
        case 5:
            time.value = "10";
            inc.value = "10";
            SetText(timeMetric, hmValue);
            SetText(incMetric, tmValue);
            break;
        case 6:
            //time.value = "0";
            //inc.value = "0";
            //Sys.Debug.trace(timeMetric.innerText);
            SetText(timeMetric, imValue);
            SetText(incMetric,imValue);
            break;
    }
}
function notification_ok(id)
{
    GeneralService.RemoveNotification(id);
    var pa = $find("pa");
    if (pa){
        pa.populate();
        //Sys.Debug.trace("ok pressed");
    }
}
function show_hide_v(id)
{
    var b = $get(id);
    if (b)
    {
        if (b.collapsed == undefined)
            b.collapsed = false;
        
        if (!b.collapsed)
        {
            b.collapsed = true;
            b.oldSize = $common.getSize(b);
            var p = $common.getLocation(b);
            var ra = new $AA.ResizeAnimation(b, 0.5, 15, b.oldSize.width, 0, "px");
            var fo = new $AA.FadeOutAnimation(b, 0.5, 15);
            //scrollTo(p.x,p.y-20);
            $AA.ParallelAnimation.play(b, 0.5, 15, [fo,ra]);
        }
        else
        {
            b.collapsed = false;                    
            //$AA.ResizeAnimation.play(b, 0.5, 15, b.oldSize.width, b.oldSize.height, "px");
            var ra = new $AA.ResizeAnimation(b, 0.5, 15, b.oldSize.width, b.oldSize.height, "px");
            var fo = new $AA.FadeInAnimation(b, 0.5, 15);
            $AA.ParallelAnimation.play(b, 0.5, 15, [fo,ra]);
        }
    }
}
function switch_sw(hide,show)
{
    hide.style.display='none';
    show.style.display='';
}
function _Login(lp,tbl)
{    
    var v = $find("always_vis");
    var anim;
    if (v)
    {
        ShowBlur();
        lp.style.display = '';
        if (v)
        {
            anim = v._animation;
            v._animation = null;
            v._reposition();
            if (anim)
            {
                //anim._duration = 0.001;
                v._animation = anim;
                //setTimeout(function(){v._animation = anim;},500);
            }
            //anim.stop();
        }
        if (tbl)
        {
            tbl.focus();
            tbl.select();
        }
    }
}
function _CloseLogin(lp)
{
    var blur = $get("blur");
    if (blur)
        blur.style.display = 'none';
    if (lp)
        lp.style.display = 'none';
}
function ValidateWithStrengthExtender(source,args)
{
    var tb = $get(source.controltovalidate);
    var b = $create(AjaxControlToolkit.PasswordStrengthExtenderBehavior, {"CalculationWeightings":"80;0;20;0","DisplayPosition":2,"HelpHandleCssClass":"","HelpHandlePosition":1,"MinimumNumericCharacters":0,"MinimumSymbolCharacters":0,"PreferredPasswordLength":6,"RequiresUpperAndLowerCaseCharacters":true,"TextCssClass":"psw_lbl","TextStrengthDescriptionStyles":"","TextStrengthDescriptions":"","id":"psw_strength_b"}, null, null, tb);
    args.IsValid = b._getPasswordStrength() == 80;
    b.dispose();
    return args.IsValid;
}
function containsDOM(container, containee)
{
    var isParent = false;
    do
    {
        if ((isParent = container == containee))
            break;
        try{containee = containee.parentNode;}catch(ex){
            return true;
        }
    }
    while (containee != null);
    return isParent;
}

function checkMouseEnter (element, evt)
{
    if (element.contains && evt.fromElement)
    {
        return !element.contains(evt.fromElement);
    }
    else if (evt.relatedTarget)
    {
        return !containsDOM(element, evt.relatedTarget);
    }
}

function checkMouseLeave(element, evt)
{
    if (element.contains && evt.toElement)
    {
        var c = !element.contains(evt.toElement);
        if (c)
        {
            if (Sys.Browser.agent == Sys.Browser.Opera && document.activeElement)
                c = !element.contains(document.activeElement);
        }
        return c;
    }
    else if (evt.relatedTarget)
    {
        return !containsDOM(element, evt.relatedTarget);
    }
}
function show_hide(m)
{
    if (!$common.getVisible(m))
        $common.setVisible(m,true);
    else
        $common.setVisible(m,false);
}
function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}
function getDocumentHeight()
{
	return (document.body.scrollHeight > document.body.offsetHeight)?document.body.scrollHeight:document.body.offsetHeight;
}
function OnShown(source,args){
    var f = source;
    var p = f.get_popupElement();
    var pb = f._popupBehavior;
    var x = pb.get_x();
    pb.set_x(x+$common.getSize(p).width);
}
function Quote(str) {
//    var re = ;
//    while (str.match(re))
//        str = str.replace(re, ">$&");
//    re = />$/m;
//    while (str.match(re))
//        str = str.replace(re, "");
    return replaceAll(/>$/m,replaceAll(/^[^>]/m,str,">$&"),"");
}
function replaceAll(re, str, repl) {
    while (str.match(re))
        str = str.replace(re, repl);
    return str;
}
function foreach(root, type, re, delegate) {
    if (root != null)
        if (root.tagName == "INPUT" && root.type == type && root.id.toString().match(re))
            delegate(root);
        else
            for (var i = 0; i < root.childNodes.length; i++)
                foreach(root.childNodes[i], type, re, delegate);
}
