function processData(data) {
	$('#msg').html(data.msg + "," + data.result);
	$('#msg').fadeIn();
	if(data.result=='true'){

		setCMSCookie("CMSSession",data.jSessionId,null,"/","");
		window.location=  unescape(window.location.search.split("=")[1]);
	} else {
		alert("Login failed.");
	}

}

function doLogin(thieField) {
	// $(thieField).attr('disabled', true);
	$('#loadingScreen').dialog('open');
	$.ajax({
		url : 'http://cms.softpower.com.tw/clientService/authUserAction.action',
		dataType : 'jsonp',
		jsonp : 'processData',
		data : {
			jSessionId : "",
			path : escape(document.URL),
			userName : $('#userName').val(),
			password : $('#password').val(),
			siteId : $('#siteId').val(),
			jsoncallback : 'processData'
		},
		error : function(xhr) {
			alert('Ajax request error');

			//$(e.target).attr('disabled', false);
		}
	});
}
function setCMSCookie(name, value, expires, path, domain, secure) {
//
	var today = new Date();

	document.cookie = name + "=" + escape(value);
			+ ((expires) ? ";expires="+expires.toGMTString() : "");
			+ ((path) ? ";path=" + path : "")
			+ ((domain) ? ";domain=" + domain : "");
}

function getCMSCookie() {

	var start = document.cookie.indexOf("CMSSession");
	var len = start + ("CMSSession".length )+ 1;
	if ((!start) && (name != document.cookie.substring(0, name.length))) {
		return null;
	}
	if (start == -1)
		return null;
	var end = document.cookie.indexOf(";", len);
	if (end == -1)
		end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}

function checkLogin(){
var cmsCookie = (getCMSCookie());
if((cmsCookie==null) || (cmsCookie==' ') || (cmsCookie=='')){
	window.location= "/login.html?target="+escape(document.URL);
}else{
	$(function() {
		$('#memberSection').html("<span style='float:right;cursor:pointer;' onclick='doDeleteCMSCookie(\""+document.URL+"\");'>Log out</span>");
	});
}
}
function doDeleteCMSCookie(directPath) {
	if(directPath){
		window.location= "/login.html?target="+escape(document.URL);
	}
}
function deleteCMSCookie(directPath) {
	var cookieValue = getCMSCookie();
	if ( cookieValue!=null && cookieValue!=''){
		var ddd= new Date();
		setCMSCookie("CMSSession"," ",ddd,"/","");
	}
	if(directPath){
		window.location= directPath;
	}
}

