function getC(Name) {
	var search = Name + "=";
	var end="";
	if (document.cookie.length > 0)
	{
			var offset = document.cookie.indexOf(search);
			if (offset != -1)
			{
				offset += search.length;
				end = document.cookie.indexOf(";", offset);
				if (end == -1) end = document.cookie.length;
				return unescape(document.cookie.substring(offset, end));
			}
			else
			{
				return false;
			}
	}
	else
	{
		return false;
	}
	return;
}

function setC(name, value, expire,domain,path) {
	document.cookie = name + "=" + escape(value)+((expire == null) ? "" : ("; expires=" + expire) )+"; domain="+domain+"; path=" + path;
}

var win= false;
function open_popup_new(to,idcountry)
{
	var c_exist=getC("newchat");
	var exp=new Date();
	exp.setTime(exp.getTime()+3600*1000*24);
	if(!c_exist)
	{
		win=window.open(NEW_CHAT_URL+"?to="+to,"neochat","width=820,height=650,status=0,status=no,statusbar=0,toolbar=0,scrollbars=0,resizable=yes");

	}
	else {
		setC("inscontact", to+"|1", exp.toGMTString(), "."+DOMAIN_NAME,"/");
	}
}

function verifyVip(id){
	$.ajaxRequest('ajax/ajax-user/verify-vip',{userId:id},function(data){
		if(data.response =="success"){
			cookieExist=getC("newchat");	
			exp=new Date();
			exp.setTime(exp.getTime()+3600*1000*24);
			if(cookieExist) setC("inscontact", id+"|1", exp.toGMTString(), "."+DOMAIN_NAME,"/");
		} else {
			if(win) win.close();	
			document.location = 'http://www.'+DOMAIN_NAME + '/vip/buy/chat';			
		}
	});	
}

$.openChat = function(id){
	var cookieExist=getC("newchat");	
	if(!cookieExist)open_popup_new(id,'ro');
	verifyVip(id);
};