// JavaScript Document

var xmlHttp = createXmlHttpRequestObject();

var RSS2EmailUrl = 'rss2email.php';

var xslRSS2EmailUrl = "rss2email.xsl";

var stylesheetDocRSS2Email;

function createMsxml2DOMDocumentObject(){
	var msxml2DOM;
	var msxml2DOMDocumentVersions = new Array("Msxml2.DOMDocument.6.0",
											  "Msxml2.DOMDocument.5.0",
											  "Msxml2.DOMDocument.4.0");
	for (var i=0; i<msxml2DOMDocumentVersions.length && !msxml2DOM; i++){
		try{
			msxml2DOM = new ActiveXObject(msxml2DOMDocumentVersions[i]);
		}
		catch (e) {}
	}
	if (!msxml2DOM)
	alert("Please upgrade your MSXML version from \n" +
		  "http://msdn.microsoft.com/XML/XMLDownloads/default.aspx");
	else
	return msxml2DOM;
}
function hGlobal(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			response = xmlHttp.responseText; //alert(response);
			if (response.indexOf("ERRNO") >= 0
								 || response.indexOf("error") >= 0
								 || response.length == 0)
			{
				alert(response.length == 0 ? "Server serror." : response);
				return;
			}
			xmlResponse = xmlHttp.responseXML;
			if (window.XMLHttpRequest && window.XSLTProcessor && window.DOMParser)
			{
				var xsltProcessor = new XSLTProcessor();
				xsltProcessor.importStylesheet(stylesheetDocRSS2Email);
				page = xsltProcessor.transformToFragment(xmlResponse, document);
				var div = document.getElementById("mainDiv");
				div.innerHTML = '';
				div.appendChild(page);
			}
			// Internet Explorer code
			else if (window.ActiveXObject)
			{
				var theDocument = createMsxml2DOMDocumentObject();
				theDocument.async = false;
				theDocument.load(xmlResponse);
				// display the page of products
				var div = document.getElementById("mainDiv");
				div.innerHTML = theDocument.transformNode(stylesheetDocRSS2Email);
			}            
		}
		else
		{
			alert("Error reading server response.")
		}
	}
}
//
function loadStylesheet(){
	if(xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
		xmlHttp.open("GET", xslRSS2EmailUrl, false);
		xmlHttp.send(null);
		if (this.DOMParser)
		{
			var dp = new DOMParser();
				stylesheetDocRSS2Email = dp.parseFromString(xmlHttp.responseText, "text/xml");
		}
		else if (window.ActiveXObject) // Internet Explorer?
		{
				stylesheetDocRSS2Email = createMsxml2DOMDocumentObject();
				stylesheetDocRSS2Email.async = false;
				stylesheetDocRSS2Email.load(xmlHttp.responseXML);
		}
	}
}
//

function init(){
	loadStylesheet();
	loadSubscribe('1', 1);
}

function loadSubscribe(mode, page){
	if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
		xmlHttp.open("POST", RSS2EmailUrl, true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.onreadystatechange = hGlobal;
		
		xmlHttp.send('action=LOAD_SUBSCRIBE&mode=' + mode + '&page=' + page);
	}
}
//
function loadAddFeedForm(){
	if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
		xmlHttp.open("POST", RSS2EmailUrl, true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4){
				if (xmlHttp.status == 200){
					op(document.getElementById('body'), 5);
					document.getElementById('addFeedForm').innerHTML = xmlHttp.responseText;
					document.getElementById('addFeedForm').style.display = 'block';
				}
			}
		};
		xmlHttp.send('action=LOAD_ADD_FEED_FORM');
	}	
}
//
function addFeed(){
	if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
		if(!checkEmpty(document.getElementById('aFeedUrl').value)){
			var feedUrl = document.getElementById('aFeedUrl').value;
		}else{
			alert(checkEmpty(document.getElementById('aFeedUrl').value));
			return;
		}
		/*if(!checkEmail(document.getElementById('aEmail').value)){
			var email = document.getElementById('aEmail').value;
		}else{
			alert(checkEmail(document.getElementById('aFeedUrl').value));
			return;
		}*/
		xmlHttp.open("POST", RSS2EmailUrl, true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.onreadystatechange = function(){
			 if (xmlHttp.readyState == 4){
				if (xmlHttp.status == 200){
					//alert(xmlHttp.responseText);
			op(document.getElementById('body'), 10);
			document.getElementById('addFeedForm').style.display = 'none';
            loadSubscribe('1', 1);
				}
			 }
		};
		xmlHttp.send('action=ADD_FEED&feedUrl=' + feedUrl);
	}	
}
function loadChangeEmailForm(){
	if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
		xmlHttp.open("POST", RSS2EmailUrl, true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4){
				if (xmlHttp.status == 200){
					op(document.getElementById('body'), 5);
					document.getElementById('changeEmailForm').innerHTML = xmlHttp.responseText;
					document.getElementById('changeEmailForm').style.display = 'block';
				}
			}
		};
		xmlHttp.send('action=LOAD_CHANGE_EMAIL_FORM');
	}
}
//
function changeEmail(){
	if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
		if(!checkEmail(document.getElementById('cEmail').value)){
			var email = document.getElementById('cEmail').value;
		}else{
			alert(checkEmail(document.getElementById('cEmail').value));
			return;
		}
		xmlHttp.open("POST", RSS2EmailUrl, true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.onreadystatechange = function(){
			op(document.getElementById('body'), 10);
			document.getElementById('changeEmailForm').style.display = 'none';
		};
		xmlHttp.send('action=CHANGE_EMAIL&email=' + email);
	}	
}
//
function Subscribe(id, mode){
	if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
		xmlHttp.open("POST", RSS2EmailUrl, true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.onreadystatechange = function(){
			loadSubscribe('1', 1);
		};
		xmlHttp.send('action=SUBSCRIBE&id=' + id + '&mode=' + mode);
	}	
}
//
function checkEmail(email){
	if(!email.match(/[^\s]/g)) return "Не вказаний e-mail";
	else if(!email.match(/^[\w-]+(\.[\w-]+)*@([\w-]+)\.+[a-zA-Z]{2,3}$/)) return "Невірний формат e-mail.";
	return false;
}
//
function checkEmpty(s){
	if(!s.match(/[^\s]/g)) return "Не заповнені обов'язкові поля";
	return false;
}
//
function op(m,n){
	var b = new browser();
	if(b.isIE){
		m.style.zoom = 1;
		m.style.background = '#e6fde3';
		m.style.filter = 'alpha(opacity=' + n*10 + ')';
	}else{
		m.style.opacity = n/10;
	}
}
function browser(){
	var n = navigator.userAgent;
	if(n.indexOf("Opera") != -1) this.isOpera = 1;
	else if(n.indexOf("MSIE") != -1) this.isIE = 1;
	else if(n.indexOf("Netscape") != -1) this.isNets = 1;
	else if(n.indexOf("Firefox") != -1) this.isMoz = 1;
	return this;
}
function iHide(id){
	document.getElementById(id).style.display = 'none';
	op(document.getElementById('body'), 10);
}
