﻿var currentTabId = 'tab1';
    var bRotate = true;
    function clearBox(element, value){
        if(element){
            if(element.value == value){
                element.value = "";
            }
        }
    }
    
    function rotateDivs(){
       
        if(bRotate){
            if(currentTabId == 'tab1'){
                tabClick('tab2');
            }else if (currentTabId == 'tab2'){
                 tabClick('tab3');
            }else if (currentTabId == 'tab3'){
                 tabClick('tab1');
            }
        }
    }
    
    function tabClick(tabId){
        var currentTab = document.getElementById(currentTabId);
       
        //alert(currentTab.id);
        if(currentTab){
            currentTab.setAttribute('className', '');
            currentTab.className = '';
            currentTab.setAttribute('class', '');
           
        }
        var newTab = document.getElementById(tabId);
        if(newTab){
            newTab.setAttribute('className', 'current');
            newTab.setAttribute('class', 'current');
            newTab.className = 'current';
           
        }
        currentTabId = tabId;
        switchTabs();
        return false;
    }

    function switchTabs() {
        if (currentTabId) {
            var div;
            div = document.getElementById('what_tab');
            if (div) {div.style.display = 'none';}
            div = document.getElementById('how_tab');
            if (div) { div.style.display = 'none'; }
            div = document.getElementById('no_junk_tab');
            if (div) { div.style.display = 'none'; }
            if (currentTabId == 'tab1') {
                div = document.getElementById('what_tab');
                if (div) { div.style.display = 'block'; }
            } else if (currentTabId == 'tab2') {
                div = document.getElementById('how_tab');
                if (div) { div.style.display = 'block'; }
            } else if (currentTabId == 'tab3') {
                div = document.getElementById('no_junk_tab');
                if (div) { div.style.display = 'block'; }
            }
        }
    }

    setInterval('rotateDivs()', 10000);
    var imgArray = new Array();
    function preloadImages() {
        
        var img;
        
        //always indicate the index number and the purpose of each image getting preloaded
        
        
        //index 0 - loading animated gif
        img = new Image();
        img.src = "../../themes/images/offerclickimages/ani.gif";
        imgArray.push(img);
        
        
    }
    preloadImages();
    function showAnimation(stepCarouselId, narrow) {
        var div = document.getElementById(stepCarouselId);
        if (div) {
            if (narrow) {
                div.innerHTML = "<div class='loading_narrow'>&nbsp;<img alt='' src='" + imgArray[0].src + "'/></div>";
            } else {
                div.innerHTML = "<div class='loading'>&nbsp;<img alt='' src='" + imgArray[0].src + "'/></div>";
            }
            
        }
        return true;

    }

    function associateIndicationText(textDivId) {
        //return;
        var control;
        var div = document.getElementById('captcha_container');
        if (div) {
            var inputs = div.getElementsByTagName('input');
            if (inputs) {
                if (inputs.length > 0) {
                    control = inputs[0];
                }
            }
        }
        var indText = document.getElementById(textDivId);
        if (control) {
            if (control.style.backgroundColor != 'undefined') {
                control.style.backgroundColor = '#F9F2CC';
            }
            if (control.style.border != 'undefined') {
                control.style.border = '0.09em solid #BEC58D';
            }
            if (control.style.color != 'undefined') {
                control.style.color = '#888888';
            }

            //control.onkeypress = function(event) { skipEnterKey(event); };
            if (indText) {
                control.onfocus = function() { indText.style.display = 'block' };
                control.onblur = function() { indText.style.display = 'none' };
            }
        }

        var wizContainer = document.getElementById('dvWizContainer');
        if (wizContainer) {
            var inputControls = wizContainer.getElementsByTagName('input');
            if (inputControls) {
                for (var i = 0; i < inputControls.length; i++) {
                    var inputControl = inputControls[i];
                    if (inputControl.type == 'text') {
                        inputControl.onkeypress = function(event) { skipEnterKey(event); };
                    }
                }
            }
        }

    }

    function skipEnterKey(e) {
        if (e == null || e == 'undefined') {
            e = window.event;
        }
        if (e.which != 'undefined' && e.which > 0) {
            if (e.which == 13) {
                e.returnValue = false;
                if (e.preventDefault) {
                    e.preventDefault();
                }
                return false;
            }
        }else if (e.keyCode != 'undefined' && e.keyCode > 0) {
            if (e.keyCode == 13) {
                e.returnValue = false;
                if (e.preventDefault) {
                    e.preventDefault();
                }
                return false;
            }
        }
    }
    
    //Validation Function for Location in OCHome.aspx
    function validateLocation(sender, args) {        
        var location = args.Value;
        var objRegExp = /(,)/;  //Check for comma in the entered text    
        if (!objRegExp.test(location)) {
            var objRegEx = /(^\d{5}$)/;    //Check for 5 digit integer
            if (objRegEx.test(location))
                args.IsValid = true;    //Valid zip code               
            else
                args.IsValid = false;
        }
        else    //If there is comma in the entered text
        {
            var arrayLoc = location.split(/\s*,\s*/);
            if (parseInt(arrayLoc.length) == 2) {
                if (arrayLoc[1].length == 2) {
                    var objRegEx2 = /(^\d{2}$)/;
                    if (!objRegEx2.test(arrayLoc[1]))   //If second part is not numeric
                        args.IsValid = true;
                    else
                        args.IsValid = false;
                }
                else
                    args.IsValid = false;
            }
            else
                args.IsValid = false;
        }
    }

    //Validation Function for Location in LocationBar.ascx
    function validateLocation_LocBarControl(sender, args) {
        var location = args.value;
        var objRegExp = /(,)/;  //Check for comma in the entered text    
        if (!objRegExp.test(location)) {
            var objRegEx = /(^\d{5}$)/;    //Check for 5 digit integer
            if (objRegEx.test(location))
                return true;    //Valid zip code               
            else
                return false;
        }
        else    //If there is comma in the entered text
        {
            var arrayLoc = location.split(/\s*,\s*/);
            if (parseInt(arrayLoc.length) == 2) {
                if (arrayLoc[1].length == 2) {
                    var objRegEx2 = /(^\d{2}$)/;
                    if (!objRegEx2.test(arrayLoc[1]))   //If second part is not numeric
                        return true;
                    else
                        return false;
                }
                else
                    return false;
            }
            else
                return false;
        }
    }
    //Validation function for additional zip codes
    function validateZipCode(sender, args) {

        var val = args.Value;
        if (val == null) {
            args.IsValid = true;
        }
        else if (val == '') {
            args.IsValid = true;
        }
        else {
            var regZipCode1 = new RegExp('^[0-9]{5}$');
            if (regZipCode1.exec(val) == null) {
                args.IsValid = false;
            }
            else {
                args.IsValid = true;
            }
        }
    }
    //validation function to validate first ZIP CODE
    function validateFirstZipCode(sender, args) {
        var div = document.getElementById('chkZipCodeContainer');
        if (div) {
            var chkboxes = div.getElementsByTagName('input');
            if (chkboxes) {
                if (chkboxes.length > 0) {
                    var chk = chkboxes[0];
                    if (!chk.checked) {
                        args.IsValid = true;
                    } else {
                        validateZipCode(sender, args);
                    }
                }
            }
        }
    }

    //validation function to compare dates
    //returns text if first date is less than current date
    //return text if second date is less than current date
    //return text if current date is not between first date and second date
    //returns blank if everything is okay
    //assumes the text box objects and date format of mm/dd/yyyy
    function compareDateValidator(firstDate, secondDate) {
        var st = firstDate.value;
        var end = secondDate.value;
        if (st.length <= 0) {
            return "Start date cannot be empty.";
        }
        if (end.length <= 0) {
            return "End date cannot be empty.";
        }
        if (!compareToCurrentDate(st)) {
            return "Start date cannot be less than current date.";
        }

        if (!compareToCurrentDate(end)) {
            return "End date cannot be less than current date.";
        }


        var startDate = convertToJSDate(st);
        var endDate = convertToJSDate(end);
        var temp = new Date();
        var currentDate = new Date(temp.getFullYear(), temp.getMonth, temp.getDate, 0, 0, 0);


        if (startDate.getTime() > endDate.getTime()) {
            return "Start date cannot be greater than end date.";
        }

        return "";
    }

    //custom validator to check if the given date is >= current date or not
    function customCurrentDateValidator(sender, args) {
        args.IsValid = compareToCurrentDate(args.Value);
    }
    
    //validates if a date is greater than the current date
    //returns true if date is greater than or equal to the current date
    function compareToCurrentDate(argDate) {
        var dt = convertToJSDate(argDate);
        if (dt != null) {
            var temp = new Date();
            var currentDate = new Date(temp.getFullYear(), temp.getMonth(), temp.getDate(), 0, 0, 0);
            if (dt.getTime() >= currentDate.getTime()) {
                return true;
            }
        }
        return false;    

    }

    //converts a date of mm/dd/yyyy into actual javascript date object
    // the argument must be a value and not the control
    //returns null if not a valid date object
    function convertToJSDate(argDate) {
        var arDate = argDate.split("-");

        if (arDate.length != 3) {
            return null;
        }


        var stDay = arDate[1];
        var stMonth = arDate[0];
        var stYear = arDate[2];
        return new Date(stYear, stMonth-1, stDay, 0, 0, 0);
    }

    //in future, this function must be used for associating a text box with enter key
    //by LSA on Feb 25, 2010
    function associateButtonClick(e, buttonid) {
        var bt = document.getElementById(buttonid);
        if (bt) {
            if (navigator.appName.indexOf("Netscape") > (-1)) {
                if (e.keyCode == 13) {
                    bt.click();
                    return false;
                }
            }
            if (navigator.appName.indexOf("Microsoft Internet Explorer") > (-1)) {
                if (event.keyCode == 13) {
                    bt.click();
                    return false;
                }
            }
        }
    }      