﻿function $(param) {
    if (typeof (param) == 'undefined' || !param) {
        return null;
    }
    else if (typeof (param.tagName) == 'undefined' || !param.tagName) {
        if (document.all)
            el = document.getElementById(param) ? document.getElementById(param) : document.all[param];
        else
            el = document.getElementById(param);

        if (el)
            return el;
        else
            return null;
    }
    else if (param.tagName) {
        return param;
    }
    else {
        return null;
    }
}

function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

var DivObj = "";
function ShowOverlay() {
    var pageSize = this.getPageSize();
    var bgObj = document.createElement("div");
    DivObj = bgObj;
    bgObj.setAttribute('id', 'bgDiv');
    bgObj.style.position = "absolute";
    bgObj.style.top = "0";
    bgObj.style.background = "#000000";
    bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=65";
    bgObj.style.opacity = "0.5";
    bgObj.style.left = "0";
    bgObj.style.width = pageSize[0] + "px";
    bgObj.style.height = pageSize[1] + "px";
    bgObj.style.zIndex = "9999";
    document.body.appendChild(bgObj);

}

function HideOverlay() {
    document.body.removeChild(DivObj);
}

function getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        if (document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }
    return [pageWidth, pageHeight];
}

var DisDiv = "";
function DisplayPopup(parent, divId) {
    ShowOverlay()
    var DivObj = $(divId);
    DivObj.style.display = 'block';
    DivObj.style.visibility = 'visible';
    DivObj.style.position = "absolute";
    DivObj.style.zIndex = "20001";
    if (parent != "" && parent != 0) {
        var placement = findPos(parent);
        DivObj.style.left = placement[0] - 170 + "px";
        DivObj.style.top = placement[1] - 20 + "px";
    } else {
        DivObj.style.left = '50%';
        DivObj.style.top = '50%';
        DivObj.style.marginLeft = "-270px";
        DivObj.style.marginTop = -190 + document.documentElement.scrollTop + "px";
    }

}
function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft, curtop];
}

function HidePopup(divId) {
    $(divId).style.display = 'none';
    HideOverlay()
}

function onEnterpress(e, func) {
    var KeyPress;
    if (e && e.which) {
        e = e
        KeyPress = e.which
    } else {
        e = event
        KeyPress = e.keyCode
    }
    if (KeyPress == 13) {
        func()
        return false
    } else {
        return true
    }
}

function ClearMsg(DivID) {
    var Div = $(DivID)
    Div.className = '';
    Div.innerHTML = '';

}
function ShowMessage(DivID, Msg) {
    var Div = $(DivID)
    Div.className = 'Divmessage';
    Div.innerHTML = Msg;
    setTimeout('ClearMsg(\'' + DivID + '\')', 5000)
}

function OpenIdUserValidate() {
    if ($('txtUserNameOpen').value.length == 0) {
        ShowMessage('ErrMsg', 'Please enter your Name!');
        return false;
    }
    if ($('txtEmailOpen').value.length == 0) {
        ShowMessage('ErrMsg', 'Please enter your Email!');
        return false;
    }
    var patemail = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    var result = $('txtEmailOpen').value.match(patemail)
    if (result == null) {
        ShowMessage('ErrMsg', 'Please enter valid E-Mail!');
        return false;
    }
    SaveInfo()
}
function SaveInfo() {
    var Url = 'SaveOpenIdValues.ashx?UN=' + $('txtUserNameOpen').value + '&EM=' + $('txtEmailOpen').value + '&Iden=' + $('hdnValue').value;
    MakeAjaxRequest(Url, LoadResult, true)
}
function LoadResult(response) {
    var Content = response.responseXML.documentElement.getElementsByTagName('Result');
    var Result = Content[0].getAttribute('R');
    if (Result.length == 4) {
        HidePopup('EmailPopup');
        window.location = "Index.aspx";
        $('hdnValue').value = "";
    }
}

var UpperBound = 0;
var Rows = null;
var n = 0;
var TimeSpan = 50;
var LowerBound = 1;
var IsExpanded = false;
var table = "";
var CurrentTable = "";
var PreviousTable = "";
function FireTable(Id) {
    table = $(Id);
    if (table != null) {
        table.style.display = table.style.display == 'block' ? 'none' : 'block';
        //table.style.display = 'block';
        Rows = table.getElementsByTagName('tr');
        UpperBound = Rows.length;
        if (UpperBound != 0) {
            ToggleRows();
        }
    } else {
        ShowMessage("divMsg", "No Nominations to display");
        return false;
    }
}

var i = 0;
function ToggleRows() {
    if (i < UpperBound) {
        Rows[i].style.display = Rows[i].style.display == 'block' ? 'none' : 'block';
        i++;
        setTimeout("ToggleRows()", TimeSpan);
    }
    else {
        i = 0;
        UpperBound = 0;
    }
}

var radio = "";
function GetVote(ContestId, AwardId) {
    if (!CheckUserLogin()) {
        ShowMessage("divMsg", "Please Login to vote");
        return false;
    }
    var Nominee = "";
    table = $('table_' + AwardId);
    radio = table.getElementsByTagName('input');
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            Nominee = radio[i].value;
            break;
        }
    }
    if (Nominee == "") { 
            ShowMessage("divMsg", "Please Check Nomination");
            return false;
     }

    var url = "DoVote.ashx?ContestId=" + ContestId + "&AwardId=" + AwardId + "&NomineeId=" + Nominee;
    MakeAjaxRequest(url, IsVotedSuccess, true);
}

function IsVotedSuccess(response) {
    var Node = response.responseXML.documentElement.getElementsByTagName("Vote");
    var Result = Node[0].getAttribute("Result");
    if (Result.length == 4) {
        ShowMessage("divMsg", "Thanks for voting");
       setTimeout(window.location.reload(true),2000);
    }

}
var A = "";
function MakeNomination() {
    if (!CheckUserLogin()) {
        ShowMessage("divMNMsg", "Please Login to Make Nomination");
        return false;
    }
    if (!Validate_MakeNominnation()) return false;
    var ContestTitle = $('lblContestTitle').innerHTML;
    var Id = $('hdContestId').value;
    var Cname = $('txtContactname').value;
    var Pname = $('txtProjectname').value;
    var Des = $('txtDescription').value;
    var Reason = $('txtReason').value;
    var url = "MakeNomination.ashx?CId=" + Id + "&cn=" + Cname + "&pn=" + Pname + "&D=" + Des + "&R=" + Reason + "&A=" + A;
    MakeAjaxRequest(url, IsNominated, true);
}

function CheckUserLogin() {
    if (UserId !== "")
        return true;
    else
        return false;
}
//function MakeNomination() {
//    var Id = $('hdContestId').value;
//    var Cname = $('txtContactname').value;
//    var Pname = $('txtProjectname');
//    var Des = $('txtDescription');
//    var Reason = $('txtReason');
//    //var myHandler = new SimpleHandler();
//    var callback = { success: InsertNomination, failure: function(statusCode) { alert('Failure: ' + statusCode); } };
//    var params = { CId: Id, cn: Cname, pn: Pname, D: Des, R: Reason };
//    makePOSTRequest('MakeNomination.ashx', InsertNomination, params);
//}
function IsNominated(response) {
    var Node = response.responseXML.documentElement.getElementsByTagName("Nominated");
    var Result = Node[0].getAttribute("Result");
    if (Result.length == 4) {
        ShowMessage("divMsg", "Thank you for making Nomination, Admin will approve your nomination soon");
        HidePopup('popupWindow');
        $('txtContactname').value = "";
        $('txtProjectname').value = "";
        $('txtDescription').value = "";
        $('txtReason').value = "";
    }
}

function ShowPopup(parent, Id, Awardname) {
    A = Id;
    $('txtContest').value = $('lblContestTitle').innerHTML;
    $('txtAwardname').value = Awardname;
    DisplayPopup(0, "popupWindow");
    $('txtContactname').value = "";
    $('txtProjectname').value = "";
    $('txtDescription').value = "";
    $('txtReason').value = "";
}

function Validate_MakeNominnation() {
    if ($('txtContactname').value.length == 0) {
        ShowMessage("divMNMsg", "Please enter contact name");
        return false;
    }
    if ($('txtProjectname').value.length == 0) {
        ShowMessage("divMNMsg", "Please enter Project name");
        return false;
    }
    if ($('txtDescription').value.length == 0) {
        ShowMessage("divMNMsg", "Please give description");
        return false;
    }
    if ($('txtReason').value.length == 0) {
        ShowMessage("divMNMsg", "Need a reason for making nomination!");
        return false;
    }
    if ($('txtDescription').value.length > 200) {
        ShowMessage("divMNMsg", "Description range must with in 200 characters");
        return false;
    }
    if ($('txtReason').value.length > 200) {
        ShowMessage("divMNMsg", "Reason range must with in 200 characters");
        return false;
    }
    return true;
}


