﻿// JScript File


    function correctPostalCode(strCountryDDL_Name, strTextBox_Name)
    {
        var CANADA = 1;
        var ddlCountry = myGetElementById(strCountryDDL_Name);
        var strSelectedCountry = ddlCountry.options[ddlCountry.selectedIndex].value.toLowerCase();
        
        // just exit until the user selects a country
        if(strSelectedCountry == null || strSelectedCountry.length == 0)
        {
            return;            
        }
        
        var txtBox = myGetElementById(strTextBox_Name);
        var strText = txtBox.value.toUpperCase();
        
        // just exit until the user enters a postal code
        if(strText == null || strText.length == 0)
        {
            return;            
        }
        
        
        var strChar = "";
        var strNewPostalCode = "";
        
        if(myIsNumeric(strText) == false)
        {
            if(strSelectedCountry != CANADA)
            {
                alert("Invalid American Postal Code");
                return false;
            }
        
            if(strText.length < 6 || strText.length > 7)
            {
                alert("Invalid Canadian Postal Code");
                return false;
            }
            
            if(strText.length == 6)
            {
            
                strNewPostalCode = strText.substring(0,3) + " " + strText.substring(3)
                strText = strNewPostalCode;
            }
            
        }
        else
        {
            if(strSelectedCountry == CANADA)
            {
                alert("Invalid Canadian Postal Code");
                return false;
            }
            if(strText.length != 5)
            {
                alert("Invalid American Postal Code");
                return false;
            }            
        }
        
        // force upper case
        mySetText(strTextBox_Name, strText);
        
        
    }

    function setExpiryMonthValues(strExpiryYears_Name, strExpiryMonths_Name)
    {
        var ddlExpiryYears = myGetElementById(strExpiryYears_Name);
        var ddlExpiryMonths = myGetElementById(strExpiryMonths_Name);
    
        var dteNow = new Date();
        var strSelectedYear = ddlExpiryYears.options[ddlExpiryYears.selectedIndex].value;
        
        var intCurrentYear = dteNow.getYear();
        
        var intStartMonth = 1;
        var intNumberOfMonths = 12;
        
        if(strSelectedYear == intCurrentYear)
        {
            intStartMonth = dteNow.getMonth();
            intStartMonth = intStartMonth + 1;
            intNumberOfMonths = (13 - intStartMonth);
            
        }
        
        
        while(ddlExpiryMonths.options.length > 0)
        {
            ddlExpiryMonths.options[0] = null;
        }
        
        var strMonthText = '';
        
        // rebuild
        for(var intLoop = 0; intLoop < intNumberOfMonths; intLoop++) 
        {
        
            strMonthText = intStartMonth;
                        
            // add a "0" if the value is only 1 digit long
            if(intStartMonth < 10)
            {
                strMonthText = "0" + strMonthText;
            }
            
            ddlExpiryMonths.options[intLoop] = new Option(strMonthText, strMonthText);
            
            // increment for next loop            
            intStartMonth = intStartMonth + 1;
        }

    }
    

    function setProvStateVisible(strDDLCountry_Name, strLabelDisplayPostalCode, strProvStateDDLName_Canada, strProvStateDDLName_USA)
    {
        var objDDLCountry = myGetElementById(strDDLCountry_Name);
        var strCountry = objDDLCountry.options[objDDLCountry.selectedIndex].value;
        
        var objCanada = myGetElementById(strProvStateDDLName_Canada);
        var objUSA = myGetElementById(strProvStateDDLName_USA);
                
        if(strCountry == "1")
        {
            objCanada.style.visibility = "visible";
            objCanada.style.width = "205px";
            
            objUSA.style.visibility = "hidden";
            objUSA.style.width = "0px";
            
            mySetText(strLabelDisplayPostalCode, "(ex: N7M 1J3)");
        }
        else
        {
            // set the USA provstate to the same position as the CANADA provstate DDL
            var intaCoords = myGetTopLeftCoordinates(strProvStateDDLName_Canada);
            var intLeft = intaCoords.x;
            var intTop = intaCoords.y;
            
            objUSA.style.position = "absolute";
            objUSA.style.left = intLeft;
            objUSA.style.top = intTop;
            
             
            objCanada.style.visibility = "hidden";
            objCanada.style.width = "0px";
            
            objUSA.style.visibility = "visible";
            objUSA.style.width = "205px";
            
            mySetText(strLabelDisplayPostalCode, "(ex: 90210)");
        }
        
    }

    function setShippingCost(strRadioList, strLabelName)
    {
//        var strText = objRadioList.options[objRadioList.selectedIndex].value
        
        var strText = myGetRadioListOptionValue(strRadioList);
        
        // get only the number
        strText = "$" + myGetNumberFromString(strText);
        
        // set text
        mySetText(strLabelName, strText);       
        
    }

    function showCreditCardImage(objDropDownList, strImageControl_VISA_Name, strImageControl_MC_Name)
    {
        var strType = objDropDownList.options[objDropDownList.selectedIndex].value
        
        var objImageControl_VISA = myGetElementById(strImageControl_VISA_Name);
        var objImageControl_MC = myGetElementById(strImageControl_MC_Name);
        
        var VISA = "1";
        var MASTERCARD = "2";
        
        if(strType == VISA)
        {
            objImageControl_VISA.src = "Images/visa.jpg";
            objImageControl_MC.src = "Images/masterCard_opaque.png";
        }
        else
        {
            objImageControl_VISA.src = "Images/visa_opaque.png";
            objImageControl_MC.src = "Images/masterCard.jpg";
        }        
    }



        window.onerror = handleError;
        function handleError() 
        {
	        return true;
	    }

        function MaximizeWindow()
        {
        /*
            window.onerror = handleError;
            window.moveTo(0,0);
            window.screenX = 0;
            window.screenY = 0;
            window.resizeTo(screen.availWidth, screen.availHeight);
          */  
            setToScroll(true);
            
            return true;
        }
            

        
        
        
        function scrollTopNow()
        {
            window.onerror = handleError;
            //var strAttribute = myGetElementById("divDisplayBusy").style.fontSize;
            var strAttribute = myGetElementById("divDisplayBusy").getAttribute("myScroll");
            
            myAlert("scrollTopNow: " + strAttribute);
            if(strAttribute == "1")
            {

                //window.screenX = 0;
                //window.screenY = 0;
                //window.moveTo(0,0);
                window.scrollTo(0,0);

            }
            
            setToScroll(false);
            
            return true;
        }
        
        function forceScrollTop()
        {
            setToScroll(true);
            scrollTopNow();
            
        }
        function setToScroll(bolScroll)
        {
            var objDIV = myGetElementById("divDisplayBusy");
            
            if(objDIV)
            {
                if(bolScroll == true)
                {
                    objDIV.setAttribute("myScroll", "1");
                    myAlert("Set Scrolling 1");
                }
                else
                {
                    objDIV.setAttribute("myScroll", "0");
                    myAlert("Set NOT Scrolling 2");
                }
            }
            
            
        }
        function EndRequestHandler(sender, args)
        {
            if (args.get_error() == undefined)
            {
                myGetElementById("divDisplayBusy").style.visibility = "hidden";
                scrollTopNow();
            }
        }
        function BeginRequestHandler(sender, args)
        {
            var Element = myGetElementById("divDisplayBusy");
            objh = parseFloat(Element.style.height)/2;
            objw = parseFloat(Element.style.width)/2;
            Element.style.top = Math.floor(Math.round((document.documentElement.offsetHeight/2)+document.documentElement.scrollTop)-objh)+'px';
            Element.style.left = Math.floor(Math.round((document.documentElement.offsetWidth/2)+document.documentElement.scrollLeft)-objw)+'px'

            myGetElementById("divDisplayBusy").style.visibility = "visible";
    
        }
        function myLoad() 
        {
        
            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
            Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
        }
                
   
       
       function killEnterKey()
        {
            if (window.event.keyCode == 13)
            window.event.keyCode = 0;
        }
        
        
        
        // DEFAULT FUNCTIONS FOR BROWSER COMPATIBLITY //
           
                
        // getElementById Special to handle quirky browsers
        // most will use getElementById()
        function myGetElementById(id)
        {
            var obj = null;
            if(document.getElementById)
            {
                /* Prefer the widely supported W3C DOM method, if
                available:-
                */
                obj = document.getElementById(id);
            }
            else if(document.getElementNodeById)
            {
                obj = document.getElementNodeById(id);
            }
            else if(document.all)
            {
                /* Branch to use document.all on document.all only
                browsers. Requires that IDs are unique to the page
                and do not coincide with NAME attributes on other
                elements:-
                */
                obj = document.all[id];
            }
            
            /* If no appropriate element retrieval mechanism exists on
            this browser this function always returns null:-
            */
            return obj;
        }

        
        function myAlert(strMessage)
        {
            //alert(strMessage);
        }
        
        function myGetText(strTextObjectId)
        {
            var obj = myGetElementById(strTextObjectId);
            var strText = "";
            
            if(obj.innerText)
            {
                strText = obj.innerText;
            }
            else if(obj.innerHTML)
            {
                strText = obj.innerHTML;
            }
            
            return strText;
        }
        
        
        function mySetText(strTextObjectId, strText)
        {
            var obj = myGetElementById(strTextObjectId);

            if(obj.value)
            {
                obj.value = strText;
            }
            if(obj.innerText)
            {
                obj.innerText = strText;
            }
            
            if(obj.innerHTML)
            {
                obj.innerHTML = strText;
            }
               

            
        }
        
        function myGetRadioListOptionValue(strRadioListName)
        {
            var strSelected_RadioListElement = "";
            var objForm = document.forms[0];
            var objRadioListElements = objForm.elements[strRadioListName];

            for (var intLoop = 0; intLoop < objRadioListElements.length; intLoop++)
            {
                if (objRadioListElements[intLoop].checked)
                {
                    strSelected_RadioListElement = objRadioListElements[intLoop].value;
                    break;
                }
            }
            return strSelected_RadioListElement.toString();      
        
         }
         
         function myGetNumberFromString(strText)
         {
            var strNewString = "";
            strNewString = strText.match(/[\d\.]+/g);
            return strNewString.toString();
         }
         
         
         function myGetTopLeftCoordinates(strElementName)
         {
            var objElement = myGetElementById(strElementName);
            var straCoords = { x: 0, y: 0 };
            while (objElement) 
            {
                straCoords.x += objElement.offsetLeft;
                straCoords.y += objElement.offsetTop;
                objElement = objElement.offsetParent;
            }
            
            return straCoords;
         }
         
         function myGetLeftCoordinate(strElementName)
         {
            var straCoords = myGetTopLeftCoordinates(strElementName);
            return straCoords.y;
         }
         
         function myGetTopCoordinate(strElementName)
         {
            var straCoords = myGetTopLeftCoordinates(strElementName);
            return straCoords.x;
         }
         
         function myShowMessageBox(strMessage)
         {
            alert(strMessage);          
         }
         
         function myIsNumeric(sText)
        {
           var ValidChars = "0123456789";
           var IsNumber=true;
           var Char;

         
           for (i = 0; i < sText.length && IsNumber == true; i++) 
              { 
              Char = sText.charAt(i); 
              if (ValidChars.indexOf(Char) == -1) 
                 {
                 IsNumber = false;
                 }
              }
           return IsNumber;
           
           }

