﻿// JScript File

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}



//nav controls
var selected_tab;
var ignore_click;
var IE = document.all?true:false;  

/*      
function HideRibbon(e)
{
    var container = document.getElementById("frame-container");
    if (container.className != "ribbon-collapsed") container.className = "ribbon-collapsed";
    
    if (ignore_click == true)
    {   
        var selTab = tsMain.getSelectedTab().get_id();             
        if (selTab != "p_Home") tsMain.selectTabById('p_Home');                
    }
    
    ignore_click = true;    
}
function ToggleRibbon(sender,args)
{

    var container = document.getElementById("frame-container");
    var tab = args.get_tab().get_id();
    
    if (container.className == "ribbon-expanded") {
        container.className = "ribbon-collapsed";
        tsMain.selectTabById('p_Home');	
        
    }
    else
    {
        selected_tab = tab;
        switch (tab)
        {
            case 'p_Home':
                window.location.href = "/";
                break;
            case 'p_Calendar':
                window.location.href = "/Calendar";
                break;
            case 'p_Account':   
                window.location.href = "/Intranet";
                break
            default:
                container.className = "ribbon-expanded";                         
        }                        
        
    }                               
}

function CancelMouseUp(e)
{
    //if (!e) var e = window.event;
	//e.cancelBubble = true;
	//if (e.stopPropagation) e.stopPropagation();


    if (IE) {
        window.event.cancelBubble = true;
    }
    else{
        e.cancelBubble = true;    
        e.stopPropagation(); 
    }   

}
*/
function FindNewsletter()
{
    alert('');
    //window.document.frmFindNewsletter.submit();

        
    
    
}

function enterKeySubmit(e,btn) 
{
    
    var characterCode; //literal character code will be stored in this variable

    if (IE) {        
        characterCode = e.keyCode; //character code is contained in IE's keyCode property
    }
    else{
        characterCode = e.which; //character code is contained in NN4's which property
    }       


    if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
        if (!btn){

            document.aspnetForm.submit();
            //submit the form
        }
        else{
             //submit the form
             btn.click();             
            
        }
        
        return false ;
    }
    else{
        return true ;
    }


}

function Navigate(URL){
    if (IE) {        
        window.navigate(URL); //character code is contained in IE's keyCode property
    }
    else{
        window.location.href = URL; //character code is contained in NN4's which property
    }  
}
function DownloadFile(URL){
    if (IE) {        
        window.navigate(URL); //character code is contained in IE's keyCode property
    }
    else{
        window.location.href = URL; //character code is contained in NN4's which property
    }  
}             
function OpenURL(URL, Toolbars){
    
    /* Window Features
    status  The status bar at the bottom of the window. 
    toolbar  The standard browser toolbar, with buttons such as Back and Forward. 
    location  The Location entry field where you enter the URL. 
    menubar  The menu bar of the window 
    directories  The standard browser directory buttons, such as What's New and What's Cool 
    resizable Allow/Disallow the user to resize the window. 
    scrollbars  Enable the scrollbars if the document is bigger than the window 
    height Specifies the height of the window in pixels. (example: height='350') 
    width  Specifies the width of the window in pixels. 
    */

    var Features = new String('');

    if (Toolbars == false) {
        Features = 'toolbar=0,';
        Features = 'menubar=0,';
    }

    Features = 'location=1,status=1,resizable=1,scrollbars=1';

    window.open (URL, '', Features); 
}
//document.onmouseup = HideRibbon;
       

/* 
 * Cross-browser event handling, by Scott Andrew
 */
function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}

/* 
 * Kills an event's propagation and default action
 */
function knackerEvent(eventObject) {
    if (eventObject && eventObject.stopPropagation) {
        eventObject.stopPropagation();
    }
    if (window.event && window.event.cancelBubble ) {
        window.event.cancelBubble = true;
    }
    
    if (eventObject && eventObject.preventDefault) {
        eventObject.preventDefault();
    }
    if (window.event) {
        window.event.returnValue = false;
    }
}

/* 
 * Safari doesn't support canceling events in the standard way, so we must
 * hard-code a return of false for it to work.
 */
function cancelEventSafari() {
    return false;        
}

/* 
 * Cross-browser style extraction, from the JavaScript & DHTML Cookbook
 * <http://www.oreillynet.com/pub/a/javascript/excerpt/JSDHTMLCkbk_chap5/index5.html>
 */
function getElementStyle(elementID, CssStyleProperty) {
    var element = document.getElementById(elementID);
    if (element.currentStyle) {
        return element.currentStyle[toCamelCase(CssStyleProperty)];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(element, '');
        return compStyle.getPropertyValue(CssStyleProperty);
    } else {
        return '';
    }
}

/* 
 * CamelCases CSS property names. Useful in conjunction with 'getElementStyle()'
 * From <http://dhtmlkitchen.com/learn/js/setstyle/index4.jsp>
 */
function toCamelCase(CssProperty) {
    var stringArray = CssProperty.toLowerCase().split('-');
    if (stringArray.length == 1) {
        return stringArray[0];
    }
    var ret = (CssProperty.indexOf("-") == 0)
              ? stringArray[0].charAt(0).toUpperCase() + stringArray[0].substring(1)
              : stringArray[0];
    for (var i = 1; i < stringArray.length; i++) {
        var s = stringArray[i];
        ret += s.charAt(0).toUpperCase() + s.substring(1);
    }
    return ret;
}

/*
 * Disables all 'test' links, that point to the href '#', by Ross Shannon
 */
function disableTestLinks() {
  var pageLinks = document.getElementsByTagName('a');
  for (var i=0; i<pageLinks.length; i++) {
    if (pageLinks[i].href.match(/[^#]#$/)) {
      addEvent(pageLinks[i], 'click', knackerEvent, false);
    }
  }
}

/* 
 * Cookie functions
 */
function createCookie(name, value, days) {
    var expires = '';
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        var expires = '; expires=' + date.toGMTString();
    }
    document.cookie = name + '=' + value + expires + '; path=/';
}

function readCookie(name) {
    var cookieCrumbs = document.cookie.split(';');
    var nameToFind = name + '=';
    for (var i = 0; i < cookieCrumbs.length; i++) {
        var crumb = cookieCrumbs[i];
        while (crumb.charAt(0) == ' ') {
            crumb = crumb.substring(1, crumb.length); /* delete spaces */
        }
        if (crumb.indexOf(nameToFind) == 0) {
            return crumb.substring(nameToFind.length, crumb.length);
        }
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, '', -1);
}

function OnClick_Thumbnail(url){
    //temp code, later to be placed into a preview dialog in page
    DownloadFile(url);
}

function writeDocument(s){
    document.write(s);
}

function isNumberKey(e){    
    var k;
    document.all ? k = e.keyCode : k = e.which;    
    switch (true){
        case (k >= 48 && k <= 57):
            return k;
            break;
        case (k >= 96 && k <= 105):
            return k;
            break;
        case (k == 110 || k == 190): //decimal
            return false;
            break;
        case (k == 109 || k == 189): // Minus
            return false;
            break;
        case (k == 8 || k == 9 || k == 46 ): //Backspace!, Tab, Delete
            return k;
            break;
        case (k >= 35 && k <= 40): //arrows, home, end
            return k;
            break;
        default:
            return false;
    } 
}
function isMoneyKey(e){    
    var k;
    document.all ? k = e.keyCode : k = e.which;    
    
    switch (true){
        case (k >= 48 && k <= 57):
            return k;
            break;
        case (k >= 96 && k <= 105):
            return k;
            break;
        case (k == 110 || k == 190): //decimal
            return k;
            break;
        case (k == 109 || k == 189): // Minus
            return k;
            break;
        case (k == 8 || k == 9 || k == 46 ): //Backspace!, Tab, Delete
            return k;
            break;        
        case (k >= 35 && k <= 40): //arrows, home, end
            return k;
            break;
        default:
            //alert(k);
            return false;
    }                
}

function AddToCart_Results(e){
    if (e == "true") {
        Navigate('/shoppingcart');
    }
}
function EnableControl(ctl, enable){
    
    if (enable == true) {
        ctl.disabled = false;
    }
    else{
        ctl.disabled = true;
    }
}
// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.

// The function returns true if a valid date, false if not.
// ******************************************************************

function isDate(ele) {

    var dateStr = ele.value;
    if (dateStr.length == 0){
        return true;
    }

    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        alert("Please enter the date as either MM/DD/YYYY or MM-DD-YYYY.");
        ele.focus();
        return false;
    }

    month = matchArray[1]; // p@rse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        alert("The month must be between 1 and 12.");
        ele.focus();
        return false;
    }

    if (day < 1 || day > 31) {
        alert("The day must be between 1 and 31.");
        ele.focus();
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert("The month "+month+" doesn`t have 31 days.")
        ele.focus();
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            alert("February " + year + " doesn`t have " + day + " days.");
            ele.focus();
            return false;
        }
    }
    return true; // date is valid
}

function setfocus(ctl){
    ctl.focus();
}
function HideModalBackground(){
	try
	  {
		var d = document.getElementById("dvModal");
		d.className = 'hidden';
	  }
	catch(err)
	  {
	  }}
function ShowModalBackground(){

	try
	  {
		var d = document.getElementById("dvModal");
		d.className = 'modal';
	  }
	catch(err)
	  {
	  }

}
function cb_onBeforeCallback(sender, eventArgs){
	ShowModalBackground();
}
function cb_onCallbackComplete(sender, eventArgs){
	HideModalBackground();
}