/**********************************
 ** Author(s): Dmitriy Andreyev
 ** Date Created: 06/01/10
 ** Description: Ajax fnctions used throughout the site
 *********************************/

function createRequestObject() {
	if (window.XMLHttpRequest) {		// code for IE7+, Firefox, Chrome, Opera, Safari
  		return new XMLHttpRequest();
	} else {							// code for IE6, IE5
  		return new ActiveXObject("Microsoft.XMLHTTP");
  	}  
}

// poll vote function 
function countPollVote() {
	var arrOptions = "";
	var j = 0;
	var pollID = document.pollform.pollID.value;
	var blnSubmitted = document.pollform.blnSubmitted.value;
	for (var i = 0; i < document.pollform.optionID.length; i++){
   		if (document.pollform.optionID[i].checked){
   			if (++j > 1) {
   				arrOptions = arrOptions + "|";
   			}
      		arrOptions = arrOptions + document.pollform.optionID[i].value;		
		}
   	}
	if (arrOptions == "") return;

	xmlhttp = createRequestObject();
	xmlhttp.onreadystatechange=function(){
  		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    		document.getElementById("poll").innerHTML=xmlhttp.responseText;
    	}
  	};
	xmlhttp.open("POST", "/includes/ajax/poll_vote_add.php", true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("blnSubmitted="+blnSubmitted+"&pollID="+pollID+"&options="+arrOptions);
	xmlhttp.send();
}

// guestbook add message function
function addGuestbookMessage() {
	// get variables
	var strMessage = document.getElementById("str_message").value;
	var blnSubmitted = document.getElementById("bln_submitted").value;

	// replace & in order to pass it properly
	strMessage = strMessage.replace(/&/gi, "%26");
	// if message is empty return back
	
	if (strMessage == "") return;
	
	// block form and display "adding" image
	$('div.form').block({ 
		message: '<h1>Добавление...</h1><img src="../images/design/gb_load.gif" />',
		css: { 
            border: 'none', 
            padding: '5px', 
            backgroundColor: '#276409', 
            '-webkit-border-radius': '5px', 
            '-moz-border-radius': '5px', 
            opacity: 1, 
            color: '#fff' 
        },
        overlayCSS:  { 
            backgroundColor: '#FFF', 
            opacity: 0.6 
        }
	}); 	
	xmlhttp = createRequestObject();
	xmlhttp.onreadystatechange=function(){
  		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  			document.getElementById("gbform").reset();
  			$('div.form').unblock();
    		document.getElementById("gb_messages").innerHTML=xmlhttp.responseText;
    	}
  	};
	xmlhttp.open("POST", "/includes/ajax/gb_msg_add.php", true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("blnSubmitted="+blnSubmitted+"&message="+strMessage);
	xmlhttp.send();	
}

// article add comment function
function addArticleComment() {
	var comment = document.articleform.strMessage.value;
	comment = comment.replace(/&/gi, "%26");
	var blnSubmitted = document.articleform.blnSubmitted.value;
	var articleID = document.articleform.intArticleID.value;
	if (comment == "") return;
	// reset values
	document.articleform.strMessage.value = "";
	
	xmlhttp = createRequestObject();
	xmlhttp.onreadystatechange=function(){
  		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    		document.getElementById("article_comments").innerHTML=xmlhttp.responseText;
    	}
  	};
	xmlhttp.open("POST", "/includes/ajax/article_comment_add.php", true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("blnSubmitted="+blnSubmitted+"&comment="+comment+"&articleID="+articleID);
	xmlhttp.send();	
}

// fanclub guestbook add message function
function addFanclubMessage() {
	var strMessage = document.getElementById("str_message").value;
	var blnSubmitted = document.getElementById("bln_submitted").value;
	strMessage = strMessage.replace(/&/gi, "%26");
	if (strMessage == "") return;
	
	// block form and display "adding" image
	$('div.form').block({ 
		message: '<h1>Добавление...</h1><img src="../images/design/gb_load.gif" />',
		css: { 
            border: 'none', 
            padding: '5px', 
            backgroundColor: '#276409', 
            '-webkit-border-radius': '5px', 
            '-moz-border-radius': '5px', 
            opacity: 1, 
            color: '#fff' 
        },
        overlayCSS:  { 
            backgroundColor: '#FFF', 
            opacity: 0.6 
        }
	});
	xmlhttp = createRequestObject();
	xmlhttp.onreadystatechange=function(){
  		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  			document.getElementById("fanclubform").reset();
  			$('div.form').unblock();
    		document.getElementById("fanclub_messages").innerHTML=xmlhttp.responseText;
    	}
  	};
	xmlhttp.open("POST", "/includes/ajax/fanclub_msg_add.php", true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("blnSubmitted="+blnSubmitted+"&message="+strMessage);
	xmlhttp.send();	
}
