﻿/// <reference path="jquery-1.2.6-vsdoc.js" />

/************************************************************************************************************
 * Set Cookies
 ***********************************************************************************************************/

function SetCookie(cookieName, cookieValue, nDays) {
	var today = new Date(),
		expire = new Date();
		
	if (nDays == null || nDays == 0) 
		nDays = 1;
	
	expire.setTime(today.getTime() + 3600000 * 24 * nDays);
	document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
}

/************************************************************************************************************
 * jQuery extentions
 ***********************************************************************************************************/

jQuery.fn.extend({
	validate: function (fn) {
		///<summary>Validate the element.</summary>
		///<param name="fn" optional="true">The validation function.</param>
		///<returns type="bool" />
	
		// make sure the calling object exists
		if (this[0] == undefined)
			return true;
	
		if (fn) {
			return jQuery.event.add(this[0], "validate", fn, null);
		} else {
			var ret = jQuery.event.trigger("validate", null, this[0], false, null);
			
			// if there was no return value then the even validated correctly
			if (ret === undefined)
				ret = true;
			
			return ret;
		}
	}
});

/************************************************************************************************************
 * Extention Methods
 ***********************************************************************************************************/

String.prototype.trim = function () {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

/************************************************************************************************************
 * YUI Rich Text Editor Configuration
 ***********************************************************************************************************/

function FileManagerPopup(field_name, url, type, win) {

    // alert("Field_Name: " + field_name + "\nURL: " + url + "\nType: " + type + "\nWin: " + win); // debug/testing


    var cmsURL = "/FileManager/Index?mode=popup" //window.location.toString();    // script URL - use an absolute path!
    if (cmsURL.indexOf("?") < 0) {
        //add the type as the only query parameter
        cmsURL = cmsURL + "?type=" + type;
    }
    else {
        //add the type as an additional query parameter
        // (PHP session ID is now included if there is one at all)
        cmsURL = cmsURL + "&type=" + type;
    }

    tinyMCE.activeEditor.windowManager.open({
        file: cmsURL,
        title: 'File Manager',
        width: 600,  // Your dimensions may differ - toy around with them!
        height: 420,
        resizable: "yes",
        inline: "yes",  // This parameter only has an effect if you use the inlinepopups plugin!
        close_previous: "no"
    }, {
        window: win,
        input: field_name
    });
    return false;
}



var __editorConfig = {
    theme: "advanced",
	mode: "textareas",	    
	plugins: "advhr,advimage,advlink,contextmenu,inlinepopups,media,paste,safari,spellchecker,xhtmlxtras",
	file_browser_callback: "FileManagerPopup",

	theme_advanced_toolbar_location: "top",
	theme_advanced_toolbar_align: "center",
	theme_advanced_statusbar_location: "bottom",
	theme_advanced_resizing_use_cookie: false,
	theme_advanced_resize_horizontal: false,
	theme_advanced_resizing: true,
	theme_advanced_resizing_min_height: 200,
	theme_advanced_buttons1: "undo,redo,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,image,link,unlink",
	theme_advanced_buttons2: "",
	theme_advanced_buttons3: "",

	convert_urls: false,
	
	gecko_spellcheck: true,
	dialog_type: "modal",
	
	paste_auto_cleanup_on_paste: true,
	paste_convert_headers_to_strong: true,
	paste_strip_class_attributes: "all"
    
};

var __editorConfigForumPost = {
    mode: "textareas",
    theme: "advanced",
    plugins: "media,paste,spellchecker",

    theme_advanced_toolbar_location: "top",
    theme_advanced_toolbar_align: "left",
    theme_advanced_statusbar_location: "bottom",
    theme_advanced_resizing_use_cookie: false,
    theme_advanced_resize_horizontal: true,
    theme_advanced_resizing: true,
    theme_advanced_resizing_min_height: 200,
    theme_advanced_buttons1: "bold,italic,underline,|,undo,redo,|,bullist,numlist,|,link,unlink",
    theme_advanced_buttons2: "",
    theme_advanced_buttons3: "",
    


    forced_root_block: false,
    force_br_newlines: true,
    force_p_newlines: false,
    
    convert_urls: false,

    gecko_spellcheck: true,
    dialog_type: "modal",

    paste_auto_cleanup_on_paste: true,
    paste_convert_headers_to_strong: true,
    paste_strip_class_attributes: "all"
};

/************************************************************************************************************
 * Global Form Policy
 ***********************************************************************************************************/

// set gloal blur for all fields to hide message when not in focus anymore
$("form .field :input").blur(function () { HideMessage(this); });

// set gloal submit for forms to call validation event
$("form").submit(function () {
	var valid = $(this).validate();
	
	// if the form didn't validate then focus the input on the first error
	if (!valid) 
		$(this).find(":input[error]:first").focus();
		
	return valid;
});

/************************************************************************************************************
 * Form Validation
 ***********************************************************************************************************/
 
function DisplayMessage (display, input, css, text) {
	var message = $(input).parent().children("span.input-message");
	
	// clear all old css
	message.removeClass("input-info input-error");
	
	if (display) {
		if (css == "input-error") {
			message.data("error", text);
			$(input).attr("error", "true");
		}
	
		message.text(text);
		message.addClass(css);
	} else {
		// remove the attribute the error isn't 
		// being displayed any more
		if (css == "input-error") {
			message.removeData("error");
			$(input).removeAttr("error");
		}
		
		// if the error attribute is still present then display
		// the error message else clear out the text
		if (message.data("error") != undefined && message.data("error") != "") {
			message.text(message.data("error"));
			message.addClass("input-error");
		} else {
			message.text("");
		}
	}
}

function DisplayError (display, input, message) {
	DisplayMessage(display, input, "input-error", message);
}

function ShowMessage (input, message) {
	DisplayMessage(true, input, "input-info", message);
}

function HideMessage (input) {
	DisplayMessage(false, input, "input-info", "");
}

function VerifyRequiredField (input, message, originalValue) {
	if (originalValue == undefined)
		originalValue = "";
		
	var valid = jQuery.trim($(input).val()) != jQuery.trim(originalValue);
	
	DisplayError(!valid, input, message);
	
	return valid;
}

function VerifyEmail (input, message) {
	return VerifyRegularExpression("([\\w-]+(?:\\.[\\w-]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})", input, message);
}

function VerifyInternetAddress (input, message) {
	return VerifyRegularExpression("((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s\"]*))", input, message);
}

function VerifyRegularExpression (exp, input, message) {
	var process = new RegExp(exp, "i");
	var match = process.exec($(input).val());
	var valid = match != undefined && match.length > 0;
	
	DisplayError(!valid, input, message);
	
	return valid;
}

function VerifyMatch (input1, input2, message) {
	var valid = $(input1).val() == $(input2).val();
	
	DisplayError(!valid, input2, message);
	
	return valid;
}

function VerifyLength (input, rangeStart, rangeEnd, message) {
	var valid = $(input).val().length >= rangeStart && $(input).val().length <= rangeEnd;
	
	DisplayError(!valid, input, message);
	
	return valid;
}

/** DROP DOWN MENU **/
var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open() {
    jsddm_canceltimer();
    jsddm_close();
    ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
}

function jsddm_close()
{ if (ddmenuitem) ddmenuitem.css('visibility', 'hidden'); }

function jsddm_timer()
{ closetimer = window.setTimeout(jsddm_close, timeout); }

function jsddm_canceltimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

$(document).ready(function() {
    $('#jsddm > li').bind('mouseover', jsddm_open);
    $('#jsddm > li').bind('mouseout', jsddm_timer);
});

document.onclick = jsddm_close;

function toggleEditor(id) {
    if (!tinyMCE.get(id))
        tinyMCE.execCommand('mceAddControl', false, id);
    else
        tinyMCE.execCommand('mceRemoveControl', false, id);
}

/************************************************************************************************************
* IsBreak Functions
***********************************************************************************************************/

function toggleIsBreak(checkbox) {
    //$("#RoomId").
    var visibleState = checkbox.checked;
    $('#RoomId').css("visibility", (visibleState ? "hidden" : "visible"));
    $('[for=RoomId]').css("visibility", (visibleState ? "hidden" : "visible"));
    $('#TrackId').css("visibility", (visibleState ? "hidden" : "visible"));
    $('[for=TrackId]').css("visibility", (visibleState ? "hidden" : "visible"));
    $('#speaker').css("visibility", (visibleState ? "hidden" : "visible"));
    $('#speaker').parent().css("visibility", (visibleState ? "hidden" : "visible"));
}