﻿function SetSize(type) {

    //alert(document.body.offsetHeight);
    //alert(document.body.clientHeight);
    //alert(document.documentElement.clientHeight);
    //alert(window.innerHeight);
    if (document.location.toString().indexOf('ViewPages.html', 0) > 0)
        return;

    document.getElementById('MainPage').style.height = '';
    document.getElementById('TblBottom').style.top = '';

    if (type == false) {
        document.getElementById('MainPage').style.height = '';
        document.getElementById('TblBottom').style.top = '';
        return;
    }
    if (document.getElementById('MainPage') == null)
        return;
    var clientH;
    if (window.innerHeight)
        clientH = window.innerHeight;
    else
    //DOCTYPE?
        clientH = Math.max(document.documentElement.clientHeight, document.body.offsetHeight) - 5;
    var documentH = document.getElementById('MainPage').offsetHeight;
    if (documentH < clientH) {
        document.getElementById('MainPage').style.height = clientH + 'px';
        var top = parseInt(document.getElementById('TblBottom').style.top);
        document.getElementById('TblBottom').style.top = ((isNaN(top) ? 0 : top) + (clientH - documentH)) + 'px';
    }
}

var RSSRequestObject = false; // XMLHttpRequest Object
var Backend = 'http://www.noormags.com/view/controls/Rss.aspx'
//var Backend = 'http://localhost/view/controls/Rss.aspx'
window.setInterval("update_timer()", 1200000); // update the data every 20 mins

if (window.XMLHttpRequest) // try to create XMLHttpRequest
	RSSRequestObject = new XMLHttpRequest();
if (window.ActiveXObject)	// if ActiveXObject use the Microsoft.XMLHTTP
	RSSRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
/*
* onreadystatechange function
*/
function ReqChange() {
    try
    {
	    var content='';
    	
	    // If data received correctly
	    if (RSSRequestObject.readyState==4) {
		    // if data is valid
		    if (RSSRequestObject.responseText.indexOf('invalid') == -1) 
		    { 	
			    // Parsing RSS
			    var node = RSSRequestObject.responseXML.documentElement; 
    			
			    // Get Channel information
			    var channel = node.getElementsByTagName('channel').item(0);
			    var title = channel.getElementsByTagName('title').item(0).firstChild.data;
			    var link = channel.getElementsByTagName('link').item(0).firstChild.data;
    //			content = '<div class="channeltitle"><a href="'+link+'">'+title+'</a></div><ul>';
    			
			    content = '<ul>';
    			
			    // Browse items
			    var items = channel.getElementsByTagName('item');
    			
			    for (var n=0; n < items.length; n++)
			    {
			        if (n == 5)
			            break;
				    var itemTitle = items[n].getElementsByTagName('title').item(0).firstChild.data;
				    var itemLink = items[n].getElementsByTagName('link').item(0).firstChild.data;
    /*				try 
				    { 
					    var itemPubDate = '<font color=gray>['+items[n].getElementsByTagName('pubDate').item(0).firstChild.data+'] ';
				    } 
				    catch (e) 
				    { 
					    var itemPubDate = '';
				    }
    				
    */				var itemPubDate = '';

				    content += '<li style="margin-bottom:10px; text-align:justify;line-height:1.5;">' + itemPubDate + '» <a href="' + itemLink + '">' + itemTitle + '</a></li>';
				    //alert(itemTitle);
			    }
    			
			    content += '</ul>';
    			
			    // Display the result
			    document.getElementById("ajaxreader").innerHTML = content;

			    // Tell the reader the everything is done
			    document.getElementById("status").innerHTML = "Done.";
    			
		    }
		    else {
			    // Tell the reader that there was error requesting data
			    document.getElementById("status").innerHTML = "<div class=error>سرویس با مشکل روبروست.<div>";
		    }
    		
		    HideShow('status');
	    }
    }
    catch(ex)
    {
        document.getElementById("ajaxreader").innerHTML = "<div>سرویس خبر فعال نیست.<div>";
		HideShow('status');

    }
}

/*
* Main AJAX RSS reader request
*/
function RSSRequest() {

	// change the status to requesting data
	HideShow('status');
	document.getElementById("status").innerHTML = "در حال بارگزاری سرویس خبر ...";
	// Prepare the request
	RSSRequestObject.open("GET", Backend , true);
	
	// Set the onreadystatechange function
	RSSRequestObject.onreadystatechange = ReqChange;
	// Send
	RSSRequestObject.send(null); 
}

/*
* Timer
*/
function update_timer() {
	RSSRequest();
}


function HideShow(id){
	var el = GetObject(id);
	if(el.style.display=="none")
	el.style.display='';
	else
	el.style.display='none';
}

function GetObject(id){
	var el = document.getElementById(id);
	return(el);
}

//*************************************** Ajax *************************
var Ajax =
{
    http2: null,
    data: null,
    loading: null,
    fn_CallBack: "",
    f_FailCallBack: ""
}

Ajax.CreateXMLHttpRequest = function() {
    if (Ajax.http2 == null) {
        if (navigator.appName == "Microsoft Internet Explorer") {
            try {
                Ajax.http2 = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                try {
                    Ajax.http2 = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e)
        { }
            }
        }
        else {
            Ajax.http2 = new XMLHttpRequest();
            if (Ajax.http2.overrideMimeType) {
                Ajax.http2.overrideMimeType('text/html');
            }
        }
    }
}
Ajax.fnCallBackAjax = function() {
    if (Ajax.http2.readyState == 4) {
        Ajax.ShowLoading("hide");
        if (Ajax.http2.status == 200) {
            //Ajax.ShowLoading("hide");
            if (Ajax.http2.responseText != null) {
                eval(Ajax.fn_CallBack + '(Ajax.http2.responseText)');
            }
            else {

                eval(Ajax.f_FailCallBack + '()');
            }
        }
    }
}
Ajax.fnSendAjax = function(HttpHandlerURL, Data, f_CallBack, f_FailCallBack) {
    var data = Data + '&' + Ajax.data;
    Ajax.fn_CallBack = f_CallBack;
    Ajax.f_FailCallBack = f_FailCallBack;
    Ajax.CreateXMLHttpRequest();
    Ajax.ShowLoading("show");
    var t = Ajax.http2.readyState; //alert('s' + Ajax.http2);
    Ajax.http2.open("POST", HttpHandlerURL, true);
    Ajax.http2.onreadystatechange = Ajax.fnCallBackAjax;
    Ajax.http2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    Ajax.http2.setRequestHeader("Content-length", data.length);
    Ajax.http2.send(data);
    return false;
}
Ajax.ShowLoading = function(action) {
    if (this.loading == null) {
        this.loading = document.createElement('div');
        this.loading.style.backgroundColor = 'red';
        this.loading.style.padding = '3px';
        this.loading.style.fontSize = '11px';
        this.loading.style.position = 'absolute';
        this.loading.style.top = '0px';
        this.loading.style.right = '0px';
        this.loading.innerHTML = 'در حال ارتباط با سرور ...';
        document.body.appendChild(this.loading);
    }
    if (action == "show")
        this.loading.style.display = "block";
    else if (action == "hide")
        this.loading.style.display = "none";
    return false;
}
//*************************************** End Ajax *************************


//*************************************** Captcha *************************
function GetCaptchaCode(Id) {
    var tb = document.getElementById(Id).getElementsByTagName('input')[0];
    return tb.value;
}
function RefreshCaptchaCode(Id) {
    var img = document.getElementById(Id).getElementsByTagName('img')[0];
    img.src += '1';
}
//*************************************** End Captcha *************************

