﻿var ClickCount = 0;


function webInit() {
    //document.forms[0].attachEvent("onsubmit", new Function("return CheckDoubleClick()"));
    addOnSubmitEvent();
    document.onkeydown = new Function("return click()");
    history.forward();
}

function addOnSubmitEvent() {
    if (document.forms[0].addEventListener) {
        document.forms[0].addEventListener('onsubmit', new Function("return CheckDoubleClick()"), false);
    } else if (document.forms[0].attachEvent) {
        document.forms[0].attachEvent('onsubmit', new Function("return CheckDoubleClick()"));
    }
}

function click() {
    var key = event.keyCode;
    if ((key == 115) || (key == 116) || (key == 122) || (key == 8 && !(event.srcElement.type == "text" || event.srcElement.type == "password" || event.srcElement.type == "textarea"))) {
        // F4,F5,F11,Back, so cancel
        event.keyCode = 0;
        event.cancelBubble = true;
        return false;
    }
}

function CheckDoubleClick() {
    
    if (document.activeElement.attributes["class"] != null 
        && document.activeElement.attributes["class"].value == 'NoDoubleClickCheck') {
        return; //no double click check
    }
    if (typeof (Page_IsValid) == 'undefined' || (Page_IsValid)) {
        ClickCount = ClickCount + 1;
        //show please wait
        document.getElementById("loadingWrapper").className = 'LockOn';
        document.getElementById("loading").className = 'LockOnLoading';
        setTimeout('document.images["imgLoading"].src="/static_files/images/loading.gif"', 50); 
     
        if (ClickCount > 1) {
            alert("Lütfen bekleyiniz, işleminiz gerçekleştiriliyor...");
            return false;
        }
    }
}

function ResetDoubleClick() {
    ClickCount = 0;
    document.getElementById("loadingWrapper").className = 'LockOff';
    document.getElementById("loading").className = 'LockOffLoading';
}
function ShowPleaseWait() {
    document.getElementById("loading").className = 'LockOnLoading';
}
function RemovePleaseWait() {
    document.getElementById("loading").className = 'LockOffLoading';
}
function InitLinkButtons() {
    var coll = document.all.tags('A');
    if (coll != null) {
        for (i = 0; i < coll.length; i++) {
            if ((coll[i].attributes["class"].value == 'NoDoubleClickCheck')) {//hypTop:BaşaDön
                continue;
            }
            if (coll[i].onclick == null) {
                coll[i].attachEvent("onclick", new Function("return CheckDoubleClick()"));
            }
        }
    }
}


function NewWindow(mypage, myname, w, h, scroll, resizable) {
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;

    winprops = 'height=' + h + ',width=' + w + ',top=' + wint + ',left=' + winl + ',scrollbars=' + scroll + ',resizable=' + resizable;
    win = window.open(mypage, myname, winprops);
    if (parseInt(navigator.appVersion) >= 4) {
        win.window.focus();
    }
}

var timeLeftRefresh = 0;
var timerRefresh;
var timerDivRefresh;
function updateTimerRefresh() {
    timeLeftRefresh -= 1000;
    var timerArea = document.getElementById(timerDivRefresh);
    var numSeconds = timeLeftRefresh / 1000;
    var displaySeconds = numSeconds % 60;
    var displayMinutes = (numSeconds - displaySeconds) / 60;

    // format display a little more by putting a leading 0 for small seconds
    if (displaySeconds < 10) {
        displaySeconds = "0" + displaySeconds;
    }

    timerArea.innerHTML = "Otomatik güncelleme için kalan süre: "+displayMinutes + ":" + displaySeconds;
    if (timeLeftRefresh <= 0) {
        timerArea.innerHTML = "<img src='/static_files/images/loadingAnimation.gif' align='absmiddle' alt='güncelleniyor'/>&nbsp;Güncelleniyor...";
        clearTimeout(timerRefresh);
    } else {
    timerRefresh = setTimeout("updateTimerRefresh()", 1000);
    }
}


function autoResize(id) {
    var newheight;
    var newwidth;

    if (document.getElementById) {
        newheight = document.getElementById(id).contentWindow.document.body.scrollHeight;
        newwidth = document.getElementById(id).contentWindow.document.body.scrollWidth;
    }

    document.getElementById(id).height = (newheight) + "px";
    document.getElementById(id).width = (newwidth) + "px";
}

//Character count script!
function textCounter(field, countfield, maxlimit) {
    var output = document.getElementById(countfield);
    
    if (output == null) { return; }

    if (field.value.length > maxlimit)
        field.value = field.value.substring(0, maxlimit);
    else
        output.value = field.value.length + " / " + maxlimit;
}

function insertAtCursor(myField, myValue) {
    //IE support
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)
                  + myValue
                  + myField.value.substring(endPos, myField.value.length);
    } else {
        myField.value += myValue;
    }
}
