
// go to the register tab after logging out all accounts
function register_link(anchor){
	append_js();
	append_iframe();
	document.write('<a href="http://learn.digitalchalk.com/dc/course/registerCourses.dc" target="_blank">');
	document.write(anchor);
	document.write('</a>');
}

// url = where users should be redirected to
function login_link(id,anchor,origin,userEmail,userPass){
	append_js();
	append_iframe();
	append_form(id,origin,userEmail,userPass);
	append_link(id,anchor);
	append_jqueryLink(id);
}

// APPEND JQUERY LINKS TO MAKE THE ANCHORS EXECUTE THE FORMS
function append_jqueryLink(id){
	document.write('<script type="text/javascript">');
	document.write('$("#DC_LINK_' + id + '").click(function() { ');
	document.write('$("#DC_LOGOUT").attr("src","http://learn.digitalchalk.com/dc/customer/logout.dc");');
	document.write('$("#DC_FORM_' + id + '").submit(); return false; });');
	document.write('</script>');
}

// APPEND FORM FOR LOGIN
function append_form(id,origin,userEmail,userPass){
	document.write('<form action="http://learn.digitalchalk.com/dc/customer/login.dc" method="POST" id="DC_FORM_' + id + '" target="_blank" style="display:none">');
	document.write('<input type="hidden" name="origin" value="' + origin + '" />');
	document.write('<input type="hidden" name="emailAddress" value="' + userEmail + '" />');
	document.write('<input type="hidden" name="password" value="' + userPass + '" />');
	document.write('</form>');
}

// APPEND LINK USING THE PROVIDED ELEMENT
function append_link(id,anchor){
	document.write('<a href="#" id="DC_LINK_' + id + '">');
	document.write(anchor);
	document.write('</a>');
}

// APPEND IFRAME FOR LOGOUT, WILL CHECK IF ALREADY EXISTS
function append_iframe(){
	var iframeExists = document.getElementById('DC_LOGOUT');
	if (iframeExists == undefined){
		document.write('<iframe src="http://learn.digitalchalk.com/dc/customer/logout.dc" id="DC_LOGOUT" frameborder="0" height="0" width="0"></iframe>');
	}
}

// APPEND JQUERY TO CLIENT SITE <HEAD>
function append_js(){
	var hasJquery = false;
	var scriptTags = document.getElementsByTagName('script');
	for(var i = 0; i < scriptTags.length; i++) {
		if(scriptTags[i].src && scriptTags[i].src.indexOf('jquery') > -1) {
			hasJquery = true;
			break;
		}
	}
	
	if(!hasJquery) {
		var head = document.getElementsByTagName('head')[0];
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.src = 'http://www.digitalchalk.com/dc.library/jquery.js';
		head.appendChild(script);
	}
}

