﻿//Root level javascript namespace
var TicketingHelper = new Function();
//Popups static class
TicketingHelper.Popups = new Function();



/* Popups STATIC class static functions */
TicketingHelper.Popups.Show = function(pageUrl, title, width, height) {
    showTicketingPopup(pageUrl, title, width, height);
}

TicketingHelper.Popups.ShowEmailExistsPopup = function(email, returnUrl) {
    showTicketingPopup("/Assets/PopupPages/CustomerEmailExists.aspx?email=" + email + "&returnUrl=" + returnUrl, "Email already registered", 450, 250);
}


TicketingHelper.Popups.ShowHelpPopup = function(helpResourceVar) {
	showTicketingPopup("/Assets/PopupPages/helpPopup.aspx?variable=" + helpResourceVar, "Help", 500, 300);
}


TicketingHelper.Popups.ShowImageView = function(url) {
window.open("/Assets/PopupPages/SingleImagePopup.aspx?url=" + encodeURI(url), "imageWin", "status=0,location=0,width=200,height=200");
}

/* END: Popups STATIC class static functions */


TicketingHelper.captureEnterKey = function(evt, buttonIdToClick) {
    evt = (evt) ? evt : window.event;

    if (evt.keyCode == 13) {
        TicketingHelper.clickLink(buttonIdToClick);
        evt.returnValue = false;
        return false;
    }
}


TicketingHelper.set_Hidden = function(elementId, isHidden) {    
    if (document.getElementById(elementId))
        $get(elementId).style.display = (isHidden) ? "none" : "block";
}


TicketingHelper.set_Visible = function(elementId, isVisible) {
    if (document.getElementById(elementId))
        $get(elementId).style.visibility = (isVisible) ? "visible" : "hidden";
}


TicketingHelper.writeFlashObject = function(flashString) {
   	document.write(flashString);
}



//Can be put on a textbox's onKeyPressEvent to ensure only numbers. Also allows periods (.)
TicketingHelper.isNumberKey  = function(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
        return false;
    return true;
} //end of isNumberKey


TicketingHelper.clickLink = function(linkId) {
    var objRef = $get(linkId);
    if (!objRef) return;

    if (objRef.click) { //ie
        objRef.click();
    }
    else if (objRef.href) { //firefox
        top.location = objRef.href;
    }
   }



TicketingHelper.ensureTextareaLength = function(txtareaRef, maxLength) {
   	if (txtareaRef.value.length > maxLength)
   		txtareaRef.value = txtareaRef.value.substring(0, maxLength);
}


/*
* The input parameters are: the field name;
* field that holds the number of characters remaining;
* the max. numb.	of characters.
*/
TicketingHelper.showInputRemainingCharacters = function(txtareaRef, txtCount, maxLength) {
   
    if (txtareaRef.value.length > maxLength) // if the current length is more than allowed
        txtareaRef.value = txtareaRef.value.substring(0, maxLength); // don't allow further input
    
    txtCount.value = maxLength - txtareaRef.value.length; // set the display field to remaining number
}



TicketingHelper.changeQueryStringValueAndRedirect = function(qsField, newValue) {
	var currentLocation = location.href.toLowerCase();

	var newLocation;

	var qsWithEquals = qsField + "=";
	qsWithEquals = qsWithEquals.toLowerCase();

	if (currentLocation.indexOf(qsWithEquals) > -1) {
		//it already has it in the querystring
		var qsIndex = currentLocation.indexOf(qsWithEquals) + qsWithEquals.length;
		var qsFieldSpan = currentLocation.substring(qsIndex, currentLocation.length).indexOf("&");
		qsFieldSpan = (qsFieldSpan > -1) ? qsFieldSpan + qsIndex : currentLocation.length;

		newLocation = currentLocation.substring(0, qsIndex);
		newLocation += newValue;
		newLocation += currentLocation.substring(qsFieldSpan, currentLocation.length);
	}
	else if (currentLocation.indexOf("?") > -1)
		newLocation = currentLocation + "&" + qsWithEquals + newValue;
	else
		newLocation = currentLocation + "?" + qsWithEquals + newValue;

	top.location.href = newLocation;
}