
var mcfSourceId = '';
var mcfWebservice = '';
var mcfFormDiv = '';
var mcfScriptCount = 0;
var mcfFrameworkName = '';
var mcfFramework;
var mcfCaptchaCount = 0;
var mcfSelectedState = "";
var mcfSubmitStatus = "";
var mcfSubmitFunction = "";
var mcfXmlForm = "";
var mcfCaptchaKeyCode = "";
var mcfResponsePage = "";

var mcfMagazine = "";




function mcFormsFramework()
{
	
	// =================================
	// = Public Get And Set Properties =
	// =================================

    this.formDiv = function(val) { mcfFormDiv = val };
    this.theFrameworkName = function(val) { mcfFrameworkName = val };
    this.theFrameworkObject = function(val) { mcfFramework = val };
    this.submitFunction = function(val) { mcfSubmitFunction = val };
    this.responsePage = function(val) { mcfResponsePage = val };
     
	// ==========================
	// = Initialization Methods =
	// ==========================

    //Returns the form
    this.TheForm = function(xmlForm) {
        $("#loading").show();
        mcfXmlForm = xmlForm;
        var fullUrl = "";
        fullUrl = "http://stage.sharedregistration.millercoors.com/GenerateCaptchaCode.aspx?framework=" + mcfFrameworkName + "&first=true";
        var noCacheIE = '&noCacheIE=' + (new Date()).getTime();
        var headLoc = document.getElementsByTagName("head").item(0);
        mcfScriptCount += 1;
        // Create script object
        var scriptObj = document.createElement("script");
        scriptObj.setAttribute("type", "text/javascript");
        scriptObj.setAttribute("charset", "utf-8");
        scriptObj.setAttribute("src", fullUrl + noCacheIE);
        scriptObj.setAttribute("id", mcfScriptCount);
        // Append script object to document head
        headLoc.appendChild(scriptObj);

    };

    this.newCaptcha = function() {
        var fullUrl = "";
        fullUrl = "http://stage.sharedregistration.millercoors.com/GenerateCaptchaCode.aspx?framework=" + mcfFrameworkName;
        var noCacheIE = '&noCacheIE=' + (new Date()).getTime();
        var headLoc = document.getElementsByTagName("head").item(0);
        mcfScriptCount += 1;
        // Create script object
        var scriptObj = document.createElement("script");
        scriptObj.setAttribute("type", "text/javascript");
        scriptObj.setAttribute("charset", "utf-8");
        scriptObj.setAttribute("src", fullUrl + noCacheIE);
        scriptObj.setAttribute("id", mcfScriptCount);
        // Append script object to document head
        headLoc.appendChild(scriptObj);
    }

    this.BuildForm = function() {
        mcfCaptchaCount = Math.floor(Math.random() * 100)
        var fullUrl = "";
        if (mcfSelectedState !== "") {
            fullUrl = "http://stage.sharedregistration.millercoors.com/ReturnForm.aspx?callback=addForm&framework=" + mcfFrameworkName + "&state=" + mcfSelectedState + "&code=" + mcfCaptchaKeyCode + "&xmlForm=" + mcfXmlForm + "&responsePage=" + mcfResponsePage;
        }
        else {
            fullUrl = "http://stage.sharedregistration.millercoors.com/ReturnForm.aspx?callback=addForm&framework=" + mcfFrameworkName + "&code=" + mcfCaptchaKeyCode + "&xmlForm=" + mcfXmlForm + "&responsePage=" + mcfResponsePage;
        }
        var noCacheIE = '&noCacheIE=' + (new Date()).getTime();
        var headLoc = document.getElementsByTagName("head").item(0);
        mcfScriptCount += 1;
        // Create script object
        var scriptObj = document.createElement("script");
        scriptObj.setAttribute("type", "text/javascript");
        scriptObj.setAttribute("charset", "utf-8");
        scriptObj.setAttribute("src", fullUrl + noCacheIE);
        scriptObj.setAttribute("id", mcfScriptCount);
        // Append script object to document head
        headLoc.appendChild(scriptObj);
    }


	//Adds form to page
    this.addForm = function(data) {
        $("#loading").hide();
        var content = document.getElementById(mcfFormDiv);
        data = unescape(data);
        $(content).append(data);

        if (mcfSelectedState != "") {
            $("#ddlState").val(mcfSelectedState);
        }
        
        //this.getCaptcha();
        //this.setCaptcha();
    };

    //Reset Captcha
    this.getCaptcha = function() {
        $('#captchaImage').attr("src", "http://stage.sharedregistration.millercoors.com/JpegImage.aspx?code=" + mcfCaptchaKeyCode);
    }

    //Set captcha cookie and get captcha image
    this.setCaptcha = function(code) {
        mcfCaptchaCount += 1;
        $('#captchaImage').attr("src", "http://stage.sharedregistration.millercoors.com/JpegImage.aspx?count=" + mcfCaptchaCount);

    };
    
    //check captcha
    this.checkCaptcha = function(captchaValid) {
        if (captchaValid == true) {
            mcFormSubmitted = true;
            $("#mcSharedForm").submit();
            //Change on CheckCaptcha.aspx
            //$.cookie("mcfCode", null);
            $('#mcErrorPrompt').hide();
            $('#captchaText').css("background", "#ffffff");
            
        }
        else {
            $('#mcErrorPrompt').show();
            $('#captchaText').css("background", "#f9e0de");
        }
        $("#btnSubmit").removeAttr("disabled")
    }
}

function reloadCaptcha() {
    var fullUrl = "";
    fullUrl = "http://stage.sharedregistration.millercoors.com/GenerateCaptchaCode.aspx?framework=" + mcfFrameworkName;
    var noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    var headLoc = document.getElementsByTagName("head").item(0);
    mcfScriptCount += 1;
    // Create script object
    var scriptObj = document.createElement("script");
    scriptObj.setAttribute("type", "text/javascript");
    scriptObj.setAttribute("charset", "utf-8");
    scriptObj.setAttribute("src", fullUrl + noCacheIE);
    scriptObj.setAttribute("id", mcfScriptCount);
    // Append script object to document head
    headLoc.appendChild(scriptObj);
}


/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/

$.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = $.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function launchRules(url) {
    var win = window.open(url, "sweeps_rules", "toolbar=0,menubar=0,width=640,height=500,status=0,resizable=1,scrollbars=1");
}



