﻿/* site-wide javascript functions */
/// <reference path="jquery-1.4.2.min-vsdoc.js" />


function vid(src, wid, hei)
{
	var out = '';

	out += '<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" id="Player" width="' + wid + '" height="' + hei + '">';
	out += '<param name="url" value="' + src + '" />';
	out += '<param name="src" value="' + src + '" />';
	out += '<param name="showcontrols" value="true" />';
	out += '<param name="autostart" value="true" />';
	out += '<!--[if !IE]>-->';
	out += '<object type="video/x-ms-wmv" data="' + src + '" width="' + wid + '" height="' + hei + '">';
	out += '<param name="src" value="' + src + '" />';
	out += '<param name="autostart" value="true" />';
	out += '<param name="controller" value="true" />';
	out += '</object>';
	out += '<!--<![endif]-->';
	out += '</object>';

	document.write(out);
}


function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		};
	}
}


function ScrollIntoView(eltId) {
	var el = document.getElementById(eltId);
	if (el !== null) {
		el.scrollIntoView();
		el.focus();
	}
}
function SmoothScrollIntoView(sel, fun, totop) {
	fun = (typeof fun == 'undefined') ? null : fun;
	totop = (typeof totop == 'undefined') ? false : totop;
	var elt = jQuery(sel);
	var bodySel = (window.opera) ? (document.compatMode == "CSS1Compat" ? "html" : "body") : "html,body";
	var $body = jQuery(bodySel);
	var destTop = 0, destHeight, windowHeight;
	if (elt.length == 1) { // check element exists
		if (totop) {
			destTop = elt.offset().top;
		} else {
			windowHeight = jQuery(window).height();
			destHeight = elt.height();
			if (destHeight < windowHeight)
				destTop = elt.offset().top - ((windowHeight - destHeight) / 2);
			else
				destTop = elt.offset().top;
		}
	}

	$body.animate({ scrollTop: destTop }, 1000, "swing", fun);
}

function loginPopup() {

    $.fn.colorbox({ inline: true,
        'href': '#loginBox',
        open: true,
        onOpen: function () {
            $('iframe').css('visibility', 'hidden');
        },
        onClosed: function () {
            $('iframe').css('visibility', 'visible');
        }
    });

    
    
}

function registerPopup() {

    $.fn.colorbox({ inline: true,
        overlayClose: false,
        'href': '#registerBox',
        onOpen: function () {
            $('iframe').css('visibility', 'hidden');
        },
        onClosed: function () {
            $('iframe').css('visibility', 'visible');
        }
    });

}

function emailDetailsPopup() {

    $.fn.colorbox({ inline: true,
        'href': '#emailDetailsBox',
        overlayClose: false,
        open: true,
        onLoad: function(){
            $('#cboxClose').remove();
        },
        onOpen: function () {
            $('iframe').css('visibility', 'hidden');
        },
        onClosed: function () {
            $('iframe').css('visibility', 'visible');
        }
    });

}

function loginGuest(instTypeId) {

    var cid = "1"; //countryId = England

    if (instTypeId == 19) //international
        cid = "112"; //countryId = Ireland

    var url = "LoginHandler.ashx?action=guest";
    if (window.location.href.indexOf('/streaming/') > 0)
        url = "../LoginHandler.ashx?action=guest";

    $.ajax({
        type: "POST",
        url: url,
        data: "itid=" + instTypeId + "&cid=" + cid,
        success: function (msg) {
            if (msg == "ok")
                document.location.href = document.location.href.replace("#", "");
        }
    });
}


function registerUser(instTypeId) {

   
    //reset fields
    $("#errorMessage").html("");
    

    // General Details
    var title = $("#regTitle").val();
    var firstname = $("#regFirstName").val();
    var surname = $("#regSurname").val();
    var email = $("#regEmail").val();
    var password = $("#regPassword1").val();
    var password2 = $("#regPassword2").val();
    var institution = $("#regOrganisation").val();
    var orgtype = $(".regOrgType").val();
    var position = $("#regPosition").val();
    var countryid = $(".regCountryId").val();
    // e-Club Details
    var eclub = $("#eClubCheckBox").attr("checked");
    var subject1 = $(".regSubject1").val();
    var subject2 = $(".regSubject2").val();
    var subject3 = $(".regSubject3").val();

    if (title == "" || firstname == "" || surname == "" || password == "" || institution == "" || position == "" ||
        email == "" || orgtype == "-1" || countryid == "-1" || subject1 == "-1" || password == "") {


        $("#errorMessage").html("Please complete all the mandatory fields.");


    }
    else if (password != password2) {

        $("#errorMessage").html("The passwords do not match.");
    }
    else {

        var url = "LoginHandler.ashx?action=register";
        if (window.location.href.indexOf('/streaming/') > 0)
            url = "../LoginHandler.ashx?action=register";

        $('#cboxClose').hide();
        $('#cboxLoadingGraphic').show();
        $.ajax({
            type: "POST",
            url: url,
            data: "title=" + title + "&fistname=" + firstname + "&surname=" + surname + "&email=" + email + "&password=" + password + "&institution=" + institution + "&orgtype=" + orgtype + "&position=" + position + "&countryid=" + countryid + "&eclub=" + eclub + "&subject1=" + subject1 + "&subject2=" + subject2 + "&subject3=" + subject3,
            success: function (msg) {
                if (msg == "ok") {
                    $.fn.colorbox({ inline: true, 'href': '#registerSuccessBox' });
                }
                else if (msg == "emailexists")
                    $("#errorMessage").html("This email address is already registered.");
                else
                    $("#errorMessage").html("Unknown error. Please try again later.");

                $('#cboxClose').show();
                $('#cboxLoadingGraphic').hide();
            }
        });
    }
}

function emailDetails() {

    //reset fields
    $("#emailDetailsErrorMessage").html("");


    // General Details
    var name = $("#emailDetailsName").val();
    var jobTitle = $("#emailDetailsJobTitle").val();
    var organisation = $("#emailDetailsOrganisation").val();
    var email = $("#emailDetailsEmail").val();


    if (name == "" || jobTitle == "" || organisation == "" || email == "") {


        $("#emailDetailsErrorMessage").html("Please complete all the mandatory fields.");
        $.fn.colorbox.resize();
    }
    else {
        
        var url = "LoginHandler.ashx?action=emaildetails";

        $.ajax({
            type: "POST",
            url: url,
            data: "name=" + name + "&jobTitle=" + jobTitle + "&organisation=" + organisation + "&email=" + email,
            success: function (msg) {
                if (msg == "ok") {
                    $.fn.colorbox.close();
                }
                else {
                    $("#emailDetailsErrorMessage").html("Unknown error. Please try again later.");
                    $.fn.colorbox.resize();
                }
            }
        });
    }
}

function loginUser() {

    // General Details
    var email = $("#loginEmail").val();
    var password = $("#loginPassword").val();

    var url = "LoginHandler.ashx?action=login";
    if (window.location.href.indexOf('/streaming/') > 0)
        url = "../LoginHandler.ashx?action=login";

    $.ajax({
        type: "POST",
        url: url,
        data: "email=" + email + "&password=" + password,
        success: function (msg) {      
            if (msg == "ok")
                document.location.href = document.location.href.replace("#", "");
            else if (msg == "invalid")
                $("#loginError").html("Invalid credentials.");
            else
                $("#loginError").html("Unknown error. Please try again later.");
        }
    });

}

function resetPassword() {
    
    // General Details
    var email = $("#loginEmailReset").val();

    if (email == "") {
        $("#loginErrorReset").html("Please provide your Email address.");
        $.colorbox.resize();
    }
    else {
        var url = "LoginHandler.ashx?action=resetpassword";
        if (window.location.href.indexOf('/streaming/') > 0)
            url = "../LoginHandler.ashx?action=resetpassword";

        $.ajax({
            type: "POST",
            url: url,
            data: "email=" + email,
            success: function (msg) {

                $("#loginErrorReset").html(msg);
                $.colorbox.resize();
            }
        });
    }

}

// temporary replacement from teachers.tv to teachersmedia.co.uk
$(document).ready(function () {
    $("a[href^='http://www.teachers.tv']")
   .each(function () {
       this.href = this.href.replace(/^http:\/\/www.teachers\.tv/,
         "http://www.teachersmedia.co.uk");
   });



    $(".loginLink").click(function () {
        loginPopup();
        return false;
    });

    $("#lnkRegister").click(function () {
        registerPopup();
        return false;
    });

    $("#registerHeadLink").click(function () {
        registerPopup();
        return false;
    });

    $("#lnkLogin").click(function () {
        loginUser();
        return false;
    });

    $("#lnkGuestLogin").click(function () {
        loginGuest($("#ddInstType").val());
        return false;
    });

    $("#btnRegister").click(function () {
        registerUser();
        return false;
    });

    $("#lnkEmailDetails").click(function () {
        emailDetails();
        return false;
    });

    $("#loginForgotPassword").click(function () {
        $("#resetPassword").css("display", "block");
        $.colorbox.resize();
        return false;
    });

    $("#loginResetPassword").click(function () {
        resetPassword();
        return false;
    });
});
