$(document).ready(function () {
    // store data as they type
    $('input, select').not('.password, .username, .btn').blur(function () {
        var t = $(this).attr("id").replace(/ctl00_site_content_body_uclOpenAccount_ddl/i, "");
        t = t.replace(/ctl00_site_content_body_uclOpenAccount_txt/i, "");
        store("ROLOA" + t, $(this).val())
    });
});
    
// verify local storage can be used and create vault if it can
var ls = false;  //Modernizr.localstorage; // will reactivate when storage is stable on all browsers all protocols.
if (ls) { var vault = window.localStorage; }

function store(k,v){
    if (ls) { vault[k] = v; }
	else storeCookie(k,v,10);
}

function retrieve(k){
	if(ls) return vault[k];
	else return findCookie(k);
}

function storeCookie(c_name,value,exdays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie = c_name + "=" + c_value + "; path=/";
    document.cookie = "r_bank" + "=" + findCookie("r_bank") + "|" + c_name;
}

function findCookie(c_name)
{
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name) return unescape(y);
	}
}

function empty() {
    if (ls) localStorage.clear()
    else {
        var x = retrieve("r_bank").split('|');
        for (i = 0; i < x.length; ++i) {storeCookie(x[i], "", -1);}
        storeCookie("r_bank", "", -1);
    }
}

function cl(tv) {
    $.ajax({
        type: "POST",
        url: "/__secure/final.aspx/cl",
        data: "{'firstname':'" + retrieve("ROLOAFirstName") + "','lastname':'" + retrieve("ROLOALastName") + "','email':'" + retrieve("ROLOAEmail") + "','company':'" + retrieve("ROLOACompany") + "','phone':'" + retrieve("ROLOAPhone") + "','website':'" + retrieve("ROLOAWebsite") + "','currency':'" + retrieve("ROLOACurrency") + "','regcount':'" + retrieve("ROLOARegCount") + "','tv':'" + tv + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}
