﻿function isEmail(e){var r=/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;return r.test(e);}
function isNumeric(n){for (i=0;i<n.length;i++){if (!isNumber(n.substring(i, i+1)))return false;}return true;}
function isNumber(c){var r=/^[0-9]$/;return (r.test(c));}
function isUrl(u){var r=/^http\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?$/i;return r.test(u);}
function setChk(i,m,g){//i=the dom object, m=the message, g=bool is good or bad
    if(g){i.removeClass("popb");i.addClass("popg");}
    else{i.removeClass("popg");i.addClass("popb");}
    i[0].innerHTML=m;
    i.show("slow");
}
/** tabs **/
function selectTabOpt(){
    var o=location.href.toLowerCase();
    //code to work out which menu item should be selected
    if(o=="http://dis.cuss.it/" || o=="http://dim.charlie1.co.uk/"){
        $("#header ul li:eq(0)").addClass("sel");
        $("#footer ul li:eq(0)").addClass("sel");
    }
    else if(o.indexOf("download") > -1){
        $("#header ul li:eq(0)").addClass("sel");
        $("#footer ul li:eq(0)").addClass("sel");
    }
    else if(o.indexOf("engine") > -1){
        $("#header ul li:eq(1)").addClass("sel");
        $("#footer ul li:eq(1)").addClass("sel");
    }
    else if(o.indexOf("support") > -1){
        $("#header ul li:eq(3)").addClass("sel");
        $("#footer ul li:eq(3)").addClass("sel");
    }
    else{
        $("#header ul li:eq(1)").addClass("sel");
        $("#footer ul li:eq(1)").addClass("sel");
    }
}
/** tabs end **/
/** nav **/
function selectNavOpt(){
    var o=location.href.toLowerCase();
    //code to work out which menu item should be selected
     if(o=="http://dis.cuss.it/sites.mvc" || o=="http://dim.charlie1.co.uk/sites.mvc")
        $("#left ul li:eq(0)").addClass("sel");
}
/** nav end **/
/** homepage **/
var moderatorArr=new Array("Manage multiple blog comments within one application.","Receive desktop notifcations and moderate live comments.","Tweet and reply to messages.")
function nextImg(arr, n){
    var i;
    for (i=0;i<arr.length;i++){
        if($("#txtDemo").text()==arr[i])
            break;
    }
    if(i==arr.length-1)
        imgFadeSwap(0,arr[0],n);
    else
        imgFadeSwap(i+1,arr[i+1],n);
}
function imgFadeSwap(i,t,n){
    $("#imgDemo").hide("slide",{direction:"left"}, function(){
        $("#imgDemo").attr("src","/Content/i/di_"+n+Number(i)+".jpg");
        $("#txtDemo").text(t);
        $("#imgDemo").show("slide",{direction:"right"});
    });
}
function getBlogPosts(){
    $.get( "Home.mvc/BlogPosts",function(r){
        var json = eval('(' + r + ')');
        $(".blog > .col1 > img").hide();
        for (i=0;i<json.length;i++)
            $(".blog > .col1").html($(".blog > .col1").html()+"<div class='post'><img src='"+json[i].description.substring(json[i].description.indexOf('src=\"')+5,json[i].description.indexOf('\" alt=\"'))+"' /><a href='http://blog.dis.cuss.it/?p="+json[i].postid+"' class='title'>"+json[i].title+"</a><label class='body'>"+json[i].description.substr(json[i].description.indexOf('[/caption]\r\n\r\n')+12,270)+"...</label></div>");
        $(".blog > .col1 > div:last").css("background","none");
        $(".blog > .col1 > div:last").css("border","none");
    });
}
function getTweets(){
    $.get("Home.mvc/Tweets",function(r){
        var json = eval('(' + r + ')');
        $(".blog > .col2 > .twitter img").hide();
        for (i=0;i<json.length;i++)
            $(".blog > .col2 > .twitter").append("<div class='tweet'><label>"+json[i]+"</label></div>");
    });
}
function subscribe(){
    $("#txtEmail").focus();
    if(isEmail($("#txtEmail").val())){
        $.post("Home.mvc/Subscribe",{e:$("#txtEmail").val()});
        $("#newsSub").html("Thanks!");
        $("#txtEmail").val("");
        $("#newsSub").fadeOut(2500, function(){
            $("#newsSub").html("Newsletter Subscription");
            $("#newsSub").fadeIn(1500);
        });
    }
    else{
        $("#newsSub").html("Invalid Address");
        $("#newsSub").fadeOut(2500, function(){
            $("#newsSub").html("Newsletter Subscription");
            $("#newsSub").fadeIn(1500);
        });
    }
}
/** homepage end **/
/** download **/
function showDownload(){
    if($('#chkTC').attr("checked")){
        $('.downimg').css("display","none");
        $('#flashDownload').css("display","block");
        if(!chkEmail())
            alert("The email you specified is not a valid email address.\n\nIf you would like to recieve information about Dis.cuss.it Moderator please enter your correct email address.\n\nIf you entered jibberish in an attempt to bypass our highly sophisticated system, you have succeeded, please continue.");
        $('#flashDownload').click(function(){
            $('body').append($('<iframe src="/download.mvc/Download" style="display:none;" />')); 
        });
    }
    else{
        $('#flashDownload').css("display","none");
        $('.downimg').css("display","block");
    }
}
function clearEmail(){
    if ($('#txtEmail').val()=="enter your email...")
        $('#txtEmail').val('');
}
function download(){
    if(!$('#chkTC').attr("checked"))
        alert('please agree to the terms and conditions'); 
    else if(chkEmail())
        $.get( "Download.mvc/InterestedEmail",{Email:$("#txtEmail").val()});
}
function chkEmail(){
    return(isEmail($("#txtEmail").val())||$("#txtEmail").val()==""||$("#txtEmail").val()=="enter your email...");
}
/** download end **/
/** engine **/
var engineArr=new Array("Threaded comments allow you to easily follow a conversation.","Sign in with 3rd party identites, such as Facebook, Twitter, etc.","Push messages to social networking streams.");/*Automatic spam filtering. You only get the good stuff.*/
function getDevBlogPosts(){
    $.get( "Engine.mvc/DevBlogPosts",function(r){
        var json = eval('(' + r + ')');
        $(".blog > .col1 > img").hide();
        for (i=0;i<json.length;i++)
            $(".blog > .col1").html($(".blog > .col1").html()+"<div class='post'><img src='"+json[i].description.substring(json[i].description.indexOf('src=\"')+5,json[i].description.indexOf('\" alt=\"'))+"' /><a href='http://blog.dis.cuss.it/?p="+json[i].postid+"' class='title'>"+json[i].title+"</a><label class='body'>"+json[i].description.substr(json[i].description.indexOf('[/caption]\r\n\r\n')+12,300)+"...</label></div>");
        $(".blog > .col1 > div:last").css("background","none");
        $(".blog > .col1 > div:last").css("border","none");
    });
}
/** engine end **/
function logout(){
    $.post("/account.mvc/logout", function(){
        window.location=("/Engine.mvc");
    });
}

function login(){
    $.getJSON("Account.mvc/Login",{Email:$('#txtEmail').val(),Pas:$('#txtPass').val()},function(r){
        if (r==true){
            //implement ReturnUrl
            var g=getQSByName("ReturnUrl");
            if (g=="")
                window.location="/Sites.mvc";
            else
                window.location=urldecode(g);
        }
        else
            $('.err').css("display","inline");
    });
}

function getQSByName(n){
    n = n.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+n+"=([^&#]*)";
    var regex = new RegExp(regexS);
    var r = regex.exec(window.location.href);
    if(r == null)
        return "";
    else
        return r[1];
}

function urldecode(s){
    var hash_map = {}, ret = s.toString(), unicodeStr='', hexEscStr='';
    var replacer = function (search, replace, s) {
        var tmp_arr = [];
        tmp_arr = s.split(search);
        return tmp_arr.join(replace);
    };
    
    // The hash_map is identical to the one in urlencode.
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';
    hash_map['\u00C6'] = '%C3%86';
    hash_map['\u00D8'] = '%C3%98';
    hash_map['\u00C5'] = '%C3%85';
 
    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr]; // Switch order when decoding
        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
    }
    ret = decodeURIComponent(ret);
    return ret;
}
/*signup page*/
function chkUser(){
    var i=$("#chkUsr");
    var n=$("#txtUsr").val();
    if (n.length > 2 && n.length < 21)
        i.hide();
    else{
        setChk(i,"3-20 characters",false);
        return false;
    }
    return true;
}

function chkEmailSU(){
    var i=$("#chkEmail");
    var v=$("#txtEmail").val();
    if (isEmail(v))
        //ajax call to check if username available
        $.getJSON("/Engine.mvc/EmailAvailable", {email: v},
        function(r){
            if(r.toString()=="false")
                setChk(i,"Email already used",false);
            else
                i.hide();
        });
    else {
        setChk(i,"Invalid email",false);               
        return false;
    }
    return true;
}

function chkPas1(){
    var i=$("#chkPas1");
    var v=$("#txtPas1").val();
    if (v.length>4)
        i.hide();
    else {
        setChk(i,"5 or more characters",false);
        return false;
    }
    return true;
}

function chkPas2(){
    var i=$("#chkPas2");
    var v=$("#txtPas1").val();
    var v2=$("#txtPas2").val();
    if (chkPas1() || v2.length>0)
    {
        if (v==v2)
            i.hide();
        else
        {
            setChk(i,"Passwords do not match",false);
            return false;
        }
        return true;
    }
    else
        i.hide();
}

function checkAll(){            
    var p=true;
    if (!chkPas1())
        p=false;
    if (!chkPas2())
        p=false;
    if (!chkUser())
        p=false;
    if (!chkEmailSU())
        p=false;
    if (p)
        document.forms[0].submit();
}  
/*end of signup page*/