﻿var isEdited = false;

$(document).ready(function(){    
    if ($("#cms").length != 0) {
        var isPublished = $('.lblIsPublished').text();
        
        $("#cms").load("/ControlContainers/ToolbarContainer.aspx?isPublished=" + isPublished);
        if (isEdited) {
            $('.pnlIsPublished').show();
        }
    }
            
    $('body').supersleight({shim: '/images/blank.gif'});
    swapValues=[];
    $(".watermark").each( function(i) {
        swapValues[i]=$(this).val();
        $(this).focus(function() {
            if($(this).val()==swapValues[i]) {
                $(this).val("") } } ).blur(function() { if($.trim($(this).val())=="") { $(this).val(swapValues[i])
     } } )});
});

function ShowUnpublished()
{
    isEdited = true;
    $('.pnlIsPublished').show();
}
    
function HideUnpublished()
{
    $('.pnlIsPublished').hide();
}
                        
function RegisterUser()
{
    var exp = /^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
    var email = $('input[type=text][id*=txtRegister]').val();
    
    if (email == '') {
        $('#ErrorLabel').text('Enter email address');
        $('#ErrorLabel').css('display', 'block');
        $('#ErrorLabel').css('color', 'Red');
    }
    else
    {    
        if (exp.test(email)) {
            CMSService.RegisterEmail(email, OnRegisterSucceeded, OnRegisterFailed); 
        }
        else
        {
            $('#ErrorLabel').text('Enter valid address');
            $('#ErrorLabel').css('display', 'block');
            $('#ErrorLabel').css('color', 'Red');
        }    
    }
}

function SubmitQuery() {
    if (Page_ClientValidate("Contact")) {
        var queryType = $('.ddlQueryType').val();
        var name = $('input[type=text][id*=txtName]').val();;
        var telephone = $('input[type=text][id*=txtTelephone]').val();;
        var email = $('input[type=text][id*=txtEmail]').val();;
        var business = $('input[type=text][id*=txtBusiness]').val();;
        var message = $('.txtMessage').val();;
    
        CMSService.SubmitQuery(queryType, name, telephone, email, business, message, OnQuerySucceeded, OnQueryFailed); 
    }
}

function SetCellOverStyle(overlay){
    $('.' + overlay).removeClass("cellOff");
    $('.' + overlay).addClass("cellOver");
}
    
function SetCellNormalStyle(overlay){
    $('.' + overlay).removeClass("cellOver");
    $('.' + overlay).addClass("cellOff");
}

function SetSubSectionIDLabel(subSectionID) {
    $('.OverlaySelected').val(subSectionID);
}

function OnCloseDown(imgID) {
    $("#menu").toggle();

    switch ($(imgID).attr("id")) {
        
    case "sitemapButton":             
        $("#sitemap").toggle();
        break;
    case "newsButton":
        $("#news").toggle();
        break;
    default:
        break;
    }
}

function OnCloseOver(img) {
    $(img).attr("src", "../App_Themes/Default/images/CloseOver.gif");
}

function OnCloseOut(img) {
    $(img).attr("src", "../App_Themes/Default/images/CloseOut.gif");
}

function saveMainChanges(editor) {
    var editorContent = editor.getContent();
    ShowUnpublished(); 
    $('.HtmlContent').val(editorContent);
    CMSService.UpdateContent($('.SectionID').val(), $('.SectionElementID').val(), editorContent, $('.PositionIndex').val(), OnSucceeded, OnFailure); 
}

function OnSucceeded(msg) {
    $(".overlay" +  $('.PositionIndex').val()).html($('.HtmlContent').val()); 
    
}

function OnRegisterSucceeded(msg) {
    if (msg == '') {
        $('#ErrorLabel').text('Thank you');
        $('#ErrorLabel').css('display', 'block');
        $('#ErrorLabel').css('color', 'green');
    }
    else {
        $('#ErrorLabel').text('Already registered');
        $('#ErrorLabel').css('display', 'block');
        $('#ErrorLabel').css('color', 'Red');
    }
}

function OnQuerySucceeded(msg) {
    var name = $('input[type=text][id*=txtName]').val('');
    var telephone = $('input[type=text][id*=txtTelephone]').val('');
    var email = $('input[type=text][id*=txtEmail]').val('');
    var business = $('input[type=text][id*=txtBusiness]').val('');
    var message = $('.txtMessage').val('');
    
    $('#lblResult').text('Thank you for your enquiry');
}

function OnRegisterFailed() {

}

function OnQueryFailed(msg) {

}

function OnFailure(msg) {
     $(".overlay" +  $('.PositionIndex').val()).html(msg);
}

function GoToLive() {
    var nLocation = window.location.pathname;
    nLocation = nLocation.replace('/edit/', '/');
    window.location = nLocation;
}

function Logout() {
    window.location = "/logout";
}
