﻿// JScript File

    var divClassOpening = false;
    var __isFireFox = navigator.userAgent.match(/gecko/i);
    
    // add beforeunload event
    function addBeforeunloadEvent(func) {
         var oldbeforeunload = window.onbeforeunload;
         if (typeof window.onbeforeunload != 'function') {
                window.onbeforeunload = func;
         } 
         else {
                window.onbeforeunload = function() {
                        if (oldbeforeunload) {
                                oldbeforeunload();
                        }
                        return func();
                }
         }
    }
    function unloadConfirm() {
        if (dirty)
		    return 'You did not save your changes!';
    }
    function body_click()
    {
        if (!divClassOpening)
        {
            var divClassView = document.getElementById('divClassView');
            if (divClassView.style.display != 'none')
                divClassView.style.display = 'none';
        }
        divClassOpening = false;
    }
    function findPosition( oElement ) {
        var res = new Object();
        res.x = 0; res.y = 0;
      if( typeof( oElement.offsetParent ) != 'undefined' ) {
        for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
          posX += oElement.offsetLeft;
          posY += oElement.offsetTop;
        }
        res.x = posX;
        res.y = posY;
        return res;
      } else {
        res.x = oElement.x;
        res.y = oElement.y;
        return res;
      }
    }

    function ViewClass(id, ClassScheduleID, ClassName, SpanClassDesc, StartDate, ClassTime, EndTime, NumberOfDays,
        FormatDesc, Price, RequiredDepositAmt)
    {
        var divClassView = document.getElementById('divClassView');
        document.getElementById('spnClassScheduleID').innerHTML =  ClassScheduleID;        
        document.getElementById('tdClassName').innerHTML =  ClassName;
        document.getElementById('tdClassDesc').innerHTML = 
            document.getElementById(SpanClassDesc).innerHTML;
        if (__isFireFox) {
            document.getElementById('tdClassDate').textContent = StartDate;
            document.getElementById('tdClassTime').textContent = 
                ClassTime.substr(0,2) + ':' + ClassTime.substr(2,2) + ' ' + ClassTime.substr(4) +
                ' - ' + 
                EndTime.substr(0,2) + ':' + EndTime.substr(2,2) + ' ' + EndTime.substr(4);
            document.getElementById('tdNumberOfDays').textContent = NumberOfDays;
            document.getElementById('tdFormat').textContent = FormatDesc;
            document.getElementById('tdPrice').textContent = Price;
            document.getElementById('tdDeposit').textContent = RequiredDepositAmt;
        }
        else {
            document.getElementById('tdClassDate').innerText = StartDate;
            document.getElementById('tdClassTime').innerText = 
                ClassTime.substr(0,2) + ':' + ClassTime.substr(2,2) + ' ' + ClassTime.substr(4) +
                ' - ' + 
                EndTime.substr(0,2) + ':' + EndTime.substr(2,2) + ' ' + EndTime.substr(4);
            document.getElementById('tdNumberOfDays').innerText = NumberOfDays;
            document.getElementById('tdFormat').innerText = FormatDesc;
            document.getElementById('tdPrice').innerText = Price;
            document.getElementById('tdDeposit').innerText = RequiredDepositAmt;
        }
        if (document.form1.hidClassType.value == '2')
            document.getElementById('trPrice').style.visibility = 'hidden';
        else
            document.getElementById('trPrice').style.visibility = 'visible';
        
        divClassView.style.display = 'inline';
        divClassView.style.position = 'absolute';
        var divCoord = findPosition(document.getElementById(id));
        divClassView.style.left = divCoord.x + 'px';
        divClassView.style.top = (divCoord.y - 200) + 'px';
        divClassOpening = true;
        return false;
    }
    function ViewClassUK(id, ClassName, SpanClassDesc, StartDate, ClassTime, EndTime, NumberOfDays, Price)
    {
        var divClassView = document.getElementById('divClassView');
        var spn;
        if (document.getElementById('pnlFootUK') != null)
            spn = document.getElementById('pnlFootUK');
        if (document.getElementById('pnlFootCA') != null)
            spn = document.getElementById('pnlFootCA');
        if (document.getElementById('pnlFootAU') != null)
            spn = document.getElementById('pnlFootAU');
            
        document.getElementById('tdClassName').innerHTML =  ClassName;
        document.getElementById('tdClassDesc').innerHTML = 
            document.getElementById(SpanClassDesc).innerHTML;
        document.getElementById('tdClassDate').innerHTML = StartDate;
        document.getElementById('tdClassTime').innerHTML = 
            ClassTime.substr(0,2) + ':' + ClassTime.substr(2,2) + ' ' + ClassTime.substr(4) +
            ' - ' + 
            EndTime.substr(0,2) + ':' + EndTime.substr(2,2) + ' ' + EndTime.substr(4);
        document.getElementById('tdNumberOfDays').innerHTML = NumberOfDays;
        document.getElementById('tdPrice').innerHTML = Price;
        document.getElementById('tdClassViewFoot').innerHTML = spn.innerHTML;
        document.getElementById('trFormat').style.display = 'none';
        document.getElementById('trDeposit').style.display = 'none';
        document.getElementById('trCartAddToCart').style.display = 'none';
        if (document.form1.hidDerm.value.length > 0)
            document.getElementById('trPrice').style.display = 'none';
        divClassView.style.display = 'inline';
        divClassView.style.position = 'absolute';
        var divCoord = findPosition(document.getElementById(id));
        divClassView.style.left = divCoord.x + 'px';
        divClassView.style.top = (divCoord.y - 200) + 'px';
        divClassOpening = true;
        return false;
    }
    function AddClassSched(ClassScheduleID)
    {
        var arySched = document.form1.hidSchedIDList.value.split(',');
        for (var idx = 0; idx < arySched.length; idx++)
        {
            if (arySched[idx] == ClassScheduleID)
            {
                alert('This class is already in your cart');
                return false;
            }
        }
        var clsSchedList = document.form1.hidSchedIDList.value;
        if (clsSchedList.length == 0)
        {
            clsSchedList = ClassScheduleID;
        }
        else
        {
            clsSchedList += ',' + ClassScheduleID;
        }
        ob_post.AddParam('ClassScheduleID', ClassScheduleID);
        ob_post.AddParam('ClassSchedIDs', clsSchedList);
    	ob_post.post(null, 'AddScheduleToBasket', AfterAddScheduleToBasket);
        return false;
    }
    function AfterAddScheduleToBasket(result, ex)
    {
        if (ex == null)
        {
            if (result.indexOf('ClassScheduleID=') > -1) {
                var ClassScheduleID = result.substr(16);
                if (document.form1.hidSchedIDList.value.length == 0)
                {
                    document.form1.hidSchedIDList.value = ClassScheduleID;
                }
                else
                {
                    document.form1.hidSchedIDList.value += ',' + ClassScheduleID;
                }
                dirty = true;
                alert('You have successfully added this class to your cart');
            }
            else {
                alert(result);
            }
        }
        else
            alert(ex);    
    }
    function DeleteBasketClass(ClassScheduleID)
    {
        if (window.confirm('Remove this class from your cart?'))
        {
            var val = document.form1.hidSchedIDList.value;
            var re = new RegExp(ClassScheduleID + ',')  // not at the end 
            val = val.replace(re, '');
            re = new RegExp(',' + ClassScheduleID)  // at the end
            val = val.replace(re, '');
            re = new RegExp(ClassScheduleID)  // only schedule id
            val = val.replace(re, '');
            document.form1.hidSchedIDList.value = val;
            ob_post.AddParam('ClassScheduleID', ClassScheduleID);
            ob_post.AddParam('ClassSchedIDs', document.form1.hidSchedIDList.value);
    	    ob_post.post(null, 'RemoveScheduleFromBasket', function(){});
    	    dirty = true;
        }
        return false;
    }
    function Checkout()
    {
        if (document.form1.hidSchedIDList.value.length > 0)
        {
            if (document.form1.hidDerm.value.length == 0) {
            // check if logged in
	            ob_post.post(null, 'IsLoggedIn', AfterIsLoggedIn);
            }
            else {
                Go2Checkout();
            }
        }
        return false;
    }
    function AfterIsLoggedIn( result, ex) {
        if (ex == null) {
            if (result == '0') {
                document.form1.hidCheckoutFlg.value = '1';
                ShowLogin();
            }
            else {
                Go2Checkout();
            }
        }
        else alert(ex);
    }
    function Go2Checkout() {
        var clsSchedID = document.form1.hidSchedIDList.value;
        if (clsSchedID.length > 0) {
            dirty = false; // bypass window.onbeforeunload
            window.location = 'Checkout.aspx?SchedIDList=' + clsSchedID;
        }
    }
    function CartAddClassSched() {
        var spn = document.getElementById('spnClassScheduleID');
        var clsSchedID;
        if (__isFireFox) clsSchedID = spn.textContent;
        else clsSchedID = spn.innerHTML;
        AddClassSched(clsSchedID);
        return false;
    }
    function ClosefoBasket2()
    {
        var divClassView = document.getElementById('divClassView');
        if (divClassView.style.display != 'none')
            divClassView.style.display = 'none';
        divClassOpening = false;
        return false;
    }
    function ClosedivLogin() {
        var divLogin = document.getElementById('divLogin');
        divLogin.style.display = 'none';
        return false;
        
    }
    function SetupAccount() {
        if (document.form1.hidDerm.value.length == 0) {
            if (document.form1.txtEmail.value.length == 0 ||
                !validEmail(document.form1.txtEmail.value)) {
                alert('Please enter a valid email address');
                return false;
            }
            ob_post.AddParam('Email', document.form1.txtEmail.value);
	        ob_post.post(null, 'VerifyEmailBeforeSignup', AfterVerifyEmailBeforeSignup);
            return false;
        }
        else {
            dirty = false; // bypass window.onbeforeunload
            window.location = 'SignupBusCenter.aspx';
        }
        return false;
    }
    function AfterVerifyEmailBeforeSignup(result, ex) {
        if (ex == null) {
            if (result.length == 0) {
                dirty = false; // bypass window.onbeforeunload
                window.location = 'SignupIDI.aspx?Email=' + document.form1.txtEmail.value +
                    '&SchedIDList=' + document.form1.hidSchedIDList.value +
                    '&Checkout=' + document.form1.hidCheckoutFlg.value;
                return;
            }
            else
                if (result.indexOf('Profile_ID=') > -1) { // in IDI no password
                    dirty = false; // bypass window.onbeforeunload
                    window.location = 'ContactProfile.aspx?' +
                                    'SchedIDlist=' + document.form1.hidSchedIDList.value +
                                    '&Checkout=' + document.form1.hidCheckoutFlg.value;
                    return;
                }
                else {
                    if (result.indexOf('login at our eBusiness') > -1) {
                        dirty = false; // bypass window.onbeforeunload
                        window.location = 'SignupBusCenter.aspx';
                        return;
                    }
                }
                alert(result);
        }
        else alert(ex);
    }
    function SendPassword() {
        if (!validEmail(document.form1.txtEmail.value)) {
            alert('Please enter a valid email address'); return;
        }
        ob_post.AddParam('Email', document.form1.txtEmail.value);
	    ob_post.post(null, 'SendPassword', AfterSendPassword);
        
    }
    function AfterSendPassword(result, ex) {
        if (ex == null) {
            alert(result);
        }
        else alert(ex);
    }
    function Authenticate() {
        if (document.form1.txtEmail.value.replace(/ /g,'').length == 0) {
            alert('Please enter your email address'); return;
        }
        if (!validEmail(document.form1.txtEmail.value)) {
            alert('Please enter a valid email address'); return;
        }
        if (document.form1.txtPassword.value.replace(/ /g,'').length == 0) {
            alert('Please enter a password'); return;
        }
        if (document.form1.hidDerm.value.length == 0) {
            ob_post.AddParam('Email', document.form1.txtEmail.value);
            ob_post.AddParam('Password', document.form1.txtPassword.value);
            ob_post.AddParam('SchedIDList', document.form1.hidSchedIDList.value);
	        ob_post.post(null, 'Authenticate', AfterAuthenticate);
        }
    }
    function AfterAuthenticate(result, ex) {
        if (ex == null) {
            if (result.indexOf('SchedIDList=') > -1) {
                document.form1.hidSchedIDList.value = result.substr(12);
                if (document.form1.hidCheckoutFlg.value == '1') {
                    dirty = false; // bypass window.onbeforeunload
                    window.location = 'Checkout.aspx';
                }
                else
                    HideLogin();
            }
            else
                alert(result);
        }
        else alert(ex);
    }
    function ExitToHome() {
        if (document.form1.hidReferrerUrl.value.length > 0) {
            if (dirty) {
                if (!window.confirm('You did not save your changes! Press "OK" to exit, "Cancel" to stay on this page')) {
                    return false;
                }
            }
            dirty = false; // bypass window.onbeforeunload
            window.location = document.form1.hidReferrerUrl.value;
        }
        return false;
    }
    function ShowLogin() {
        var divLogin = document.getElementById('divLogin');
        divLogin.style.display = 'inline';
        var imgBskt = document.getElementById('imgBasket2');
        var imgCoord = findPosition(imgBskt);
        divLogin.style.position = 'absolute';
        var divW = parseInt(divLogin.style.width.replace(/px/g,''), 10);
        divLogin.style.left = (imgCoord.x - divW) + 'px';
        divLogin.style.top = (imgCoord.y - 80) + 'px';
        if (document.form1.hidDerm.value.length > 0) {
            document.getElementById('trLoginIDI').style.display = 'none';
        }
        else {
            document.getElementById('trLoginDerm').style.display = 'none';
        }
        document.form1.txtEmail.focus();
    }
    function HideLogin() {
        var divLogin = document.getElementById('divLogin');
        divLogin.style.display = 'none';
    }
    function goContactProfile() {
        dirty = false; // bypass window.onbeforeunload
        window.location = 'ContactProfile.aspx?Derm=' + document.form1.hidDerm.value +
            '&SchedIDList=' + document.form1.hidSchedIDList.value
    }
    function goRegHistory() {
        dirty = false; // bypass window.onbeforeunload
        window.location = 'RegHistory.aspx?Derm=' + document.form1.hidDerm.value +
            '&SchedIDList=' + document.form1.hidSchedIDList.value;
    }
    function Logout() {
        ob_post.post(null, 'Logout', function(){});
        document.form1.hidSchedIDList.value = "";
        document.form1.txtPassword.value = "";
        return false;
    }