// Submit token from StoneCo site to IFAST system
function submitToken(t){
	var token = document.getElementById("token");
	token.value = t;
	token.form.submit();
}

// Login to IFAST with specified account 
function LoginToIFast(account){
	var warning = 'No client account has been assigned to this dealer rep code.';
	if (!account || account.length==0) return alert(warning);
	loadXMLDoc('advisors.aspx?get_token='+account,function(success,req){
		if (success && /^[,A-F0-9]+$/.test(req.responseText))
			submitToken(req.responseText);
		else
			alert(warning);
	});
}

// Load an XML Doc from an url, executing f when the server sends the response back
function loadXMLDoc(url,f) {
    // branch for native XMLHttpRequest object
	var req = false;
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = function() {
			if (req.readyState == 4 && f) f(req.status==200,req);
		}
		req.open('GET', url, true);
		req.send('');
	} else {
		alert('unable to make the request');
	}
}

function swapAnchorClass(a,prefix,offon){
	if (!prefix) prefix = /favorite/.test(a.className) ? 'favorite' : 'treebutton';
	if (!offon) offon = prefix=='favorite' ? ['del','add'] : ['off','on'];
	var on = a.hasClassName(prefix+'-'+offon[1]);
	offon.each(function(s){ a.removeClassName(prefix+'-'+s); });
	a.addClassName(prefix+'-'+offon[on ? 0 : 1]);
	return !on;
}

function toggleCartDocument(id) {
	var a = $('ctl00_phMain_cart_link');
	var removed = !a || swapAnchorClass(a);
		if (a) a.down().update(removed ? 'Add to cart' : 'Remove from cart');
	var dic = $('docs_in_cart');
		if (dic) dic.update( parseInt(dic.innerHTML) + (removed ? -1 : 1) );
	new Ajax.Request("documents.aspx",{
		method:'get',
		parameters:{
			id:id,
			toggle:1
		},
		onComplete:function(req){}.bindAsEventListener(this)
	});
}

var DocumentCart = {
	TogglePrintCopies:function(){
		$$('#mailing_address,#cart_information').invoke('toggle');
	},
	RemoveDocument:function(b){
		var tr = $(b).up(1);
		var id = tr.readAttribute('rel');
		tr.remove();
		toggleCartDocument(id);
	},
	RemoveAll:function(){
		var removed = [];
		$$('form table tbody tr').each(function(tr){
			if (tr.readAttribute('rel')) {
				toggleCartDocument(tr.readAttribute('rel'));
				removed.push(tr);
			}
		});
		removed.invoke('remove');
	}
}

Event.observe(window,'load',function(){
	if ($$('body').shift().id=='cart'){
		$$('form').shift().style.display='block';
		if ($('mailing_address')) $('mailing_address').toggle();
	}

	if ($$('body').shift().id=='contact_sales'){
		$$('form').shift().style.display='block';
		if ($('booking_form')) $('booking_form').toggle();
	}
});

function BookMeeting(){
	$$('#booking_form,#sales_region_info').invoke('toggle');
}

