//Dynamic Drive (http://www.dynamicdrive.com)

function createAjaxObj(){
	var httprequest=false
	if (window.XMLHttpRequest){ // if Mozilla, Safari etc
		httprequest=new XMLHttpRequest()
		if (httprequest.overrideMimeType)
			httprequest.overrideMimeType('text/xml')
	}
	else if (window.ActiveXObject){ // if IE
		try {
			httprequest=new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try{
				httprequest=new ActiveXObject("Microsoft.XMLHTTP");
			}	
			catch (e){}
		}
	}
	return httprequest
}

var ajaxpack=new Object()
ajaxpack.basedomain="http://"+window.location.hostname
ajaxpack.ajaxobj=createAjaxObj()
ajaxpack.filetype="txt"
ajaxpack.addrandomnumber=0 //Set to 1 or 0. See documentation.

ajaxpack.getAjaxRequest=function(url, parameters, callbackfunc, filetype){
	ajaxpack.ajaxobj=createAjaxObj() //recreate ajax object to defeat cache problem in IE
	if (ajaxpack.addrandomnumber==1) //Further defeat caching problem in IE?
	var parameters=parameters+"&ajaxcachebust="+new Date().getTime()
	if (this.ajaxobj){
		this.filetype=filetype
		this.ajaxobj.onreadystatechange=callbackfunc
		this.ajaxobj.open('GET', url+"?"+parameters, true)
		this.ajaxobj.send(null)
	}
}

ajaxpack.postAjaxRequest=function(url, parameters, callbackfunc, filetype){
	ajaxpack.ajaxobj=createAjaxObj() 
	if (this.ajaxobj){
		this.filetype=filetype
		this.ajaxobj.onreadystatechange = callbackfunc;
		this.ajaxobj.open('POST', url, true);
		this.ajaxobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.ajaxobj.setRequestHeader("Content-length", parameters.length);
		this.ajaxobj.setRequestHeader("Connection", "close");
		this.ajaxobj.send(parameters);
	}
}


//------	CHECK AVAILABLE EMAIL	------
function processGetPostEmail(){
	var ret_val = ""
	var myajax=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax.readyState == 4){ 
		if (myajax.status==200 || window.location.href.indexOf("http")==-1){ 
			if (myfiletype=="txt") {
				ret_val = myajax.responseText        
			}
			else {
				ret_val = myajax.responseXML		
			}
		}
	}
	manipulateEmail(ret_val);
}

function manipulateEmail(val){
	var msg = document.getElementById('idg_tbox_fieldChanged');
	var isRegistered = 0;
	var isFree = 1;
	
	var v = val.split("<!--")[0];
	var c = val.split("<!--")[1];
	
	if(msg && v){
		msg.innerHTML = "";
		msg.innerHTML = v;
		msg.style.display = "block";
		msg.style.className = "msg_err";
		opdfMan('i');
	}
	
	if(c){
		c = c.replace("-->", "");
		if(c == "email_exists"){
			opdfMan('r');
		}
	}
}

//--------- OPDF -------------
function opdfMan(type){
	var doc = document.getElementById('profileBox');
	var pass = document.getElementById('pass');
	var passc = document.getElementById('passC');
	
	if(type == 'r'){
		doc.style.display = "none";
		pass.value = "*******";
		passc.value = "*******";
	}
	
	if(type == 'i'){
		doc.style.display = "block";
		pass.value = "";
		passc.value = "";
	}
}


//------	CHECK AVAILABLE NICK	-------
function processGetPostNick(){
	var ret_val = ""
	var myajax=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax.readyState == 4){ 
		if (myajax.status==200 || window.location.href.indexOf("http")==-1){
			if (myfiletype=="txt") {
				ret_val = myajax.responseText        
			}
			else {
				ret_val = myajax.responseXML		
			}
		}
	}
	var msg = document.getElementById('idg_tbox_fieldChanged');
	msg.style.display = "block";
	msg.innerHTML = "";
	msg.innerHTML = ret_val;
}


//------	DELETE USERS'S AVATAR	-------
function processGetPostAvatar(){
	var ret_val = ""
	var myajax=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax.readyState == 4){
		if (myajax.status==200 || window.location.href.indexOf("http")==-1){
			if (myfiletype=="txt") {
				ret_val = myajax.responseText        
			}
			else {
				ret_val = myajax.responseXML		
			}
		}
	}
	if(ret_val){
		document.getElementById("avimg").src = 'http://idg.bg/users/user_default.gif';
		document.getElementById("avdel").style.display = "none";
	}
}


//------	UPDATE USER's PROFILE	-----
function processGetPostUpdateProfile(){
	var ret_val = ""
	var myajax=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax.readyState == 4){
		if (myajax.status==200 || window.location.href.indexOf("http")==-1){
			if (myfiletype=="txt") {
				ret_val = myajax.responseText        
			}
			else {
				ret_val = myajax.responseXML		
			}
		}
	}
	var field_name;
	
	if(ret_val==0){
		document.getElementById("idg_tbox_fieldChanged").innerHTML = "<font color='#9F4738'>Такъв e-mail вече е регистриран.</font>";
	}else{
		switch(ret_val){
			case 'signature': field_name = "Подпис"; break;
			case 'chat': 
			case 'chat_id': field_name = "Чат контакти"; break;
			case 'public_email': field_name = "Публичен email"; break;
			case 'site': field_name = "Личен сайт / блог"; break;
			case 'country': field_name = "Държава"; break;
			case 'sphere': field_name = "Бизнес сектор"; break;
			case 'disallowShow': field_name = "Скрит профил"; break;
		}
		document.getElementById("idg_tbox_fieldChanged").innerHTML = "Полето `" +field_name+ "` е запазено.";
	}
	
}

//----		UPDATE USER's PRIVATE		------
function processGetPostUpdatePrivate(){
	var ret_val = ""
	var myajax=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax.readyState == 4){
		if (myajax.status==200 || window.location.href.indexOf("http")==-1){
			if (myfiletype=="txt") {
				ret_val = myajax.responseText        
			}
			else {
				ret_val = myajax.responseXML		
			}
		}
	}
	
	var field_name;
	switch(ret_val){
		case 'sex': field_name = "Пол"; break;
		case 'birthYear': field_name = "Година на раждане"; break;
		case 'region': field_name = "Местоположение"; break;
		case 'education': field_name = "Образование"; break;
		case 'position': field_name = "Позиция"; break;
		case 'infoICT': field_name = "Информация от Ай Си Ти Медиа"; break;
		case 'infoPartners': field_name = "Информация от партньорите"; break;
		case 'sphere': field_name = "Бизнес сектор"; break;
	}
	document.getElementById("idg_tbox_fieldChanged").innerHTML = "Поле `" +field_name+ "` е запазено";
}

//------	UPDATE BULLETINS	-----
function processGetPostUpdateBulletins(){
	var ret_val = ""
	var myajax=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax.readyState == 4){
		if (myajax.status==200 || window.location.href.indexOf("http")==-1){
			if (myfiletype=="txt") {
				ret_val = myajax.responseText        
			}
			else {
				ret_val = myajax.responseXML		
			}
		}
	}
	var bulletin;
	switch(ret_val){
		case 'daily': bulletin = "eDaily"; break;
		case 'pcw': bulletin = "PCWorld"; break;
		case 'prg': bulletin = "Програмиране"; break;
		case 'isc': bulletin = "iСигурност"; break;
		case 'ntc': bulletin = "Мрежи и комуникации"; break;
		case 'dbs': bulletin = "Бази данни & ERP системи"; break;
		case 'cio': bulletin = "CIO"; break;
	}
	
	var msg = document.getElementById("msg_val");
	var bc = document.getElementById(ret_val);
	msg.style.display = "block";
	msg.innerHTML = "&nbsp;";
	
	if(bc.checked==true){
//		msg.style.display = "block";
		msg.className = "msg_ok";
		msg.innerHTML = "Абонаментът за бюлетин `"+bulletin+"` е запазен"
	} else {
		msg.className = "msg_ok";
		msg.innerHTML = "Абонаментът за бюлетин `"+bulletin+"` е премахнат"
	}
}


//------	CALL AJAX FUNTION	-----
function callAJAX(type, value, userid){
	switch(type){
		case 'email':
			var params = "email=" +value;
			var url = window.location.href;
			var path = ""; 
			if(url.indexOf("page=paper") > 0) path = "login/";
			ajaxpack.getAjaxRequest(path + "login_AjaxResults.php", params, processGetPostEmail, "txt");
		break;
		case 'nick':
			var params = "nick=" +value+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostNick, "txt");
		break;
		case 'avatar':
			if(confirm("Наистина ли желаете да изтриете своя аватар?")){
				var params = "avatar=" +value+ "&uid=" +userid;
				ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostAvatar, "txt");
			}else{
				return false;
			}
		break;
		case 'signature':
			var params = "field=signature&signature=" +value+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostUpdateProfile, "txt");
		break;
		case 'chat':
			var params = "field=chat&chat=" +value+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostUpdateProfile, "txt");
		break;
		case 'chat_id':
			var params = "field=chat_id&chat_id=" +value+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostUpdateProfile, "txt");
		break;
		case 'public_email':
			var params = "field=public_email&public_email=" +value+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostUpdateProfile, "txt");
		break;
		case 'site':
			var params = "field=site&site=" +value+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostUpdateProfile, "txt");
		break;
		case 'country':
			var params = "field=country&country=" +value+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostUpdateProfile, "txt");
		break;
		case 'sphere':
			var params = "fieldprivate=sphere&sphere=" +value+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostUpdatePrivate, "txt");
		break
		case 'disallowShow':
			var params = "field=disallowShow&disallowShow=" +value+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostUpdateProfile, "txt");
		break;
		case 'sex':
			var params = "fieldprivate=sex&sex="  +value+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostUpdatePrivate, "txt");
		break;
		case 'birthYear':
			var params = "fieldprivate=birthYear&birthYear=" +value+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostUpdatePrivate, "txt");
		break
		case 'region':
			var params = "fieldprivate=region&region=" +value+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostUpdatePrivate, "txt");
		break
		case 'education':
			var params = "fieldprivate=education&education=" +value+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostUpdatePrivate, "txt");
		break
		case 'position':
			var params = "fieldprivate=position&position=" +value+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostUpdatePrivate, "txt");
		break
		case 'infoICT':
			var realval = value ? 1 : 0;
			var params = "fieldprivate=infoICT&infoICT=" +realval+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostUpdatePrivate, "txt");
		break;
		case 'infoPartners':
			var realval = value ? 1 : 0;
			var params = "fieldprivate=infoPartners&infoPartners=" +realval+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostUpdatePrivate, "txt");
		break;
		case 'bulletins':
			var realval = value.split("_");
			var params = "bullPrMail=" +realval[0]+ "&mailvalue=" +realval[1]+ "&uid=" +userid;
			ajaxpack.getAjaxRequest("login_AjaxResults.php", params, processGetPostUpdateBulletins, "txt");
		break;
	}
}

function callAJAX_oncommentsvote(commentID, vote, userID, currVote){
	var params = "commentID=" +commentID+ "&vote=" +vote+ "&userID=" +userID+ "&currVote=" +currVote;
	ajaxpack.getAjaxRequest("/login/login_AjaxResults.php", params, processGetPostCommentsRate, "txt"); 
}

function processGetPostCommentsRate(){
	var ret_val = ""
	var myajax=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax.readyState == 4){ //if request of file completed
		if (myajax.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful or running script locally
			if (myfiletype=="txt") {
				ret_val = myajax.responseText 
			}
			else {
				ret_val = myajax.responseXML
			}
		}
	}

	var ids = ret_val.split('*****');
	var commID = ids[0];
	var currVote = ids[1];
	var userVote = ids[2];
	
	if(commID!=''){
		
		var newVote = parseInt(currVote) + parseInt(userVote);
		
		var rtxt = document.getElementById("rating_text_" +commID);
		var yes = document.getElementById("comm_vote_yes_" +commID);
		var no = document.getElementById("comm_vote_no_" +commID);
	
		yes.style.display = "none";
		no.style.display = "none";
		rtxt.innerHTML = "";
		
		if(newVote > 0) newVoteString = "+ " +newVote;
		else newVoteString = newVote;
		rtxt.innerHTML = "" +newVoteString;
	}	
}

function callAJAX_onhaikuvote(hID, userID){
	var params = "hID=" +hID +"&userID=" +userID;
	ajaxpack.getAjaxRequest("/haiku_AjaxResults.php", params, null, "txt"); 
}


String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

document.onerror = function(){
	
}
