﻿var gLang="gr";
var aArticlesUrl='/Articles/GetArticles.aspx?lang='+gLang;
//var aArticlesUrl='http://localhost/Articles/GetArticles.aspx?lang='+gLang;
var xmlDoc=false;

var notWhitespace = /\S/;

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function cleanWhitespace(node) {
  for (var x = 0; x < node.childNodes.length; x++) {
    var childNode = node.childNodes[x];
    if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
      node.removeChild(node.childNodes[x]);
      x--;
    }
    if (childNode.nodeType == 1) {
      cleanWhitespace(childNode)
    }
  }
}


function getXmlObject(){
 if(!xmlDoc){
  if (window.ActiveXObject){//Internet Explorer
   xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  } else if (document.implementation && document.implementation.createDocument){//Firefox, Mozilla, Opera, etc.
   xmlDoc=document.implementation.createDocument("","",null);
  }else {
   alert('Your browser cannot handle this script');
  }
 }
 return xmlDoc;
}

function loadXmlDocument(aUrl){
// try {
  var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() ); 
  if (is_chrome){
   var xmlhttp = new window.XMLHttpRequest();
   xmlhttp.open("GET",aUrl,false);
   xmlhttp.send(null);
   xmlDoc = xmlhttp.responseXML.documentElement;
  } else {
   xmlDoc = getXmlObject();
   xmlDoc.async=false;
   xmlDoc.load(aUrl);
   cleanWhitespace(xmlDoc);
  } 
// }catch(e){
//  alert("loadXmlDocument:"+e.message);
// }
}

function ShowArticles(Articles, divDoc, aArticlePage){
 divDoc.innerHTML="";
 if(Articles.length>0){
    var aOldCategory="";
	for (i=0;i<Articles.length;i++)
	{
	//0:title
	//1:description
	//2:url
	//3:html_id
	//4:attachment id
	//5:html1
	//6:category
	//7:date
	//8:html2
	//9:html3
	//10:kind
	   var aNewCategory="";
	   if (Articles[i].childNodes[6].firstChild){
		aNewCategory=Articles[i].childNodes[6].firstChild.nodeValue;
	   }
	   if (aOldCategory!=aNewCategory){
	    if (aOldCategory!=""){
    	 var newDiv4=document.createElement('BR');
		 divDoc.appendChild(newDiv4);
		}

    	var newDiv2=document.createElement('DIV');
    	newDiv2.setAttribute("class", "clisthd")
    	newDiv2.innerHTML=aNewCategory;
		divDoc.appendChild(newDiv2);

    	var newDiv3=document.createElement('DIV');
    	newDiv3.setAttribute("class", "ctrline")
		divDoc.appendChild(newDiv3);


	    aOldCategory=aNewCategory;
	   }
	
    	var newDiv=document.createElement('DIV');
    	newDiv.setAttribute("class", "arthd")
		var aLink=document.createElement("a");

		if (Articles[i].childNodes[4].firstChild){
		 aLink.href=aArticlesUrl+'&AttachmentId='+Articles[i].childNodes[4].firstChild.nodeValue;
		}else if (Articles[i].childNodes[3].firstChild){
		 aLink.href=aArticlePage+'?id='+Articles[i].childNodes[3].firstChild.nodeValue;
		}else if (Articles[i].childNodes[2].firstChild){
		 aLink.href=Articles[i].childNodes[2].firstChild.nodeValue;
		}else{
		 aLink.href="#";
		}
		aLink.appendChild(document.createTextNode(Articles[i].childNodes[0].firstChild.nodeValue));
		newDiv.appendChild(aLink);
		divDoc.appendChild(newDiv);
		
    	if(Articles[i].childNodes[1].firstChild){
    	 newDiv=document.createElement('DIV');
    	 newDiv.setAttribute("class", "artdesc")
    	 newDiv.innerHTML = Articles[i].childNodes[1].firstChild.nodeValue;
	     //newDiv.appendChild(document.createTextNode(Articles[i].childNodes[1].firstChild.nodeValue));
		 divDoc.appendChild(newDiv);
	    }

    	newDiv=document.createElement('DIV');
    	newDiv.style.height=8;
		divDoc.appendChild(newDiv);
	}

 }
}

function ShowNews(divDoc, aKind, aFilter, aArticlePage){
 var aUrl = aArticlesUrl+'&Kind='+aKind+'&'+aFilter;
 loadXmlDocument(aUrl);
 var Articles = xmlDoc.getElementsByTagName('article');

 divDoc.innerHTML="";
 if(Articles.length>0){
	for (i=0;i<Articles.length;i++){
	//0:title
	//1:description
	//2:url
	//3:html_id
	//4:attachment id
	//5:html1
	//6:category
	//7:date
	//8:html2
	//9:html3
	//10:kind
	
    	var newDiv1=document.createElement('DIV');
      	newDiv1.setAttribute("class", "pressdate");
    	if(Articles[i].childNodes[7].firstChild){
		 newDiv1.appendChild(document.createTextNode(Articles[i].childNodes[7].firstChild.nodeValue));
	    }
		divDoc.appendChild(newDiv1);

    	var newDiv2=document.createElement('DIV');
      	newDiv2.setAttribute("class", "pressrel");

		var aLink=document.createElement("a");
		if (Articles[i].childNodes[4].firstChild){
		 aLink.href=aArticlesUrl+'&AttachmentId='+Articles[i].childNodes[4].firstChild.nodeValue;
		}else if (Articles[i].childNodes[3].firstChild){
		 aLink.href=aArticlePage+'?id='+Articles[i].childNodes[3].firstChild.nodeValue;
		}else if (Articles[i].childNodes[2].firstChild){
		 aLink.href=Articles[i].childNodes[2].firstChild.nodeValue;
		}else{
		 aLink.href="#";
		}
		aLink.appendChild(document.createTextNode(Articles[i].childNodes[0].firstChild.nodeValue));
	    newDiv2.appendChild(aLink);
		divDoc.appendChild(newDiv2);

	}

 }
}

function ShowHomepageNews(divDoc, aFilter, aArticlePage){
 loadXmlDocument(aArticlesUrl+'&'+aFilter);
 var Articles = xmlDoc.getElementsByTagName('article');

 divDoc.innerHTML="";
//alert(Articles.length); 
 if(Articles.length>0){
	for (i=0;i<Articles.length;i++){
	//0:title
	//1:description
	//2:url
	//3:html_id
	//4:attachment id
	//5:html1
	//6:category
	//7:date
	//8:html2
	//9:html3
	//10:kind
	    var aClass="";
	    var aHeader="";
	    if (Articles[i].childNodes[10].firstChild.nodeValue=="Articles"){
	     aClass = " feature_ARTICLE";
	     aHeader="Άρθρο";
	    } else if (Articles[i].childNodes[10].firstChild.nodeValue=="Events"){
	     aClass = " feature_HEWITT_NEWS";
	     aHeader="Νέα της Aon Hewitt";
	    } else if (Articles[i].childNodes[10].firstChild.nodeValue=="News"){
	     if (Articles[i].childNodes[6].firstChild.nodeValue=="News"){
	      aClass = " feature_HEWITT_NEWS";
	      aHeader = "Νέα της Aon Hewitt";
	     }else{
	      aClass = " feature_"+Articles[i].childNodes[6].firstChild.nodeValue.replace(/ /g, "_");
	      aHeader = Articles[i].childNodes[6].firstChild.nodeValue;
	     }
	    }
	    
	    //var aClass=Articles[i].childNodes[10].firstChild.nodeValue=="Articles"?" feature_ARTICLE":" feature_HEWITT_NEWS";
	    //var aHeader=Articles[i].childNodes[10].firstChild.nodeValue=="Articles"?"Article":"Aon Hewitt News";
	    
    	var newDiv=document.createElement('DIV');
    	newDiv.setAttribute("class", "newItem"+aClass)
    	var aHref="";
		if (Articles[i].childNodes[4].firstChild){
		 aHref=aArticlesUrl+'&AttachmentId='+Articles[i].childNodes[4].firstChild.nodeValue;
		}else if (Articles[i].childNodes[3].firstChild){
		 aHref=aArticlePage+'?id='+Articles[i].childNodes[3].firstChild.nodeValue;
		}else if (Articles[i].childNodes[2].firstChild){
		 aHref=Articles[i].childNodes[2].firstChild.nodeValue;
		}else{
		 aHref="#";
		}
    	newDiv.setAttribute("onclick", "parent.location='"+aHref+"'");
    	
   	    var tmpPar=document.createElement('P');
   	    tmpPar.appendChild(document.createTextNode(aHeader));
		newDiv.appendChild(tmpPar);
		newDiv.appendChild(document.createTextNode(Articles[i].childNodes[0].firstChild.nodeValue));
		divDoc.appendChild(newDiv);
   	    var tmpImg=document.createElement('img');
   	    tmpImg.setAttribute("src","Lib/assets/global/images/dottedrule.gif");
   	    tmpImg.setAttribute("border","0");
		divDoc.appendChild(tmpImg);
	}
 }
}



function GetArticle(aId, aTitleDiv, aBodyDiv){
 aTitleDiv.innerHTML = "";
 aBodyDiv.innerHTML = "";
 if (parseInt(aId)>0){
  loadXmlDocument(aArticlesUrl+"&id="+aId);
  var Articles = xmlDoc.getElementsByTagName('article');
  if(Articles.length==1){
   aTitleDiv.innerHTML = Articles[0].childNodes[0].firstChild.nodeValue;
   aBodyDiv.innerHTML = Articles[0].childNodes[5].firstChild.nodeValue;
   if(Articles[0].childNodes[8].firstChild){
    aBodyDiv.innerHTML = aBodyDiv.innerHTML + Articles[0].childNodes[8].firstChild.nodeValue;
   }
   if(Articles[0].childNodes[9].firstChild){
    aBodyDiv.innerHTML = aBodyDiv.innerHTML + Articles[0].childNodes[9].firstChild.nodeValue;
   }
   document.title = "Aon Hewitt - "+Articles[0].childNodes[0].firstChild.nodeValue;
  }
 } 
}


function ShowArticle(divDoc, aKind, aFilter, aArticlePage){
 loadXmlDocument(aArticlesUrl+'&Kind='+aKind+'&'+aFilter);
 var x = xmlDoc.getElementsByTagName('article');
 ShowArticles(x, divDoc, aArticlePage);	
}


function AddArticleYearsToSelect(edType){
 loadXmlDocument(aArticlesUrl+'&sql=select distinct ayear from articlesview where kind="News" and coalesce(ayear,"")<>"" and lang="'+gLang+'" order by ayear desc');
 var Years = xmlDoc.getElementsByTagName('article');
 for (i=0;i<Years.length;i++){
  if (Years[i].childNodes[0].firstChild) {
   var y=Years[i].childNodes[0].firstChild.nodeValue;
   edType.options[edType.options.length]=new Option(y,"ayear="+y);
  }
 }
}

function AddArticleCategoriesToSelect(edType){
 loadXmlDocument(aArticlesUrl+'&sql=select distinct category from articlesview where kind="Articles" and lang="'+gLang+'" and id>0 and category<>"OtherAttachments" order by category');
 var Categories = xmlDoc.getElementsByTagName('article');
 for (i=0;i<Categories.length;i++){
  if (Categories[i].childNodes[0].firstChild) {
   var c=Categories[i].childNodes[0].firstChild.nodeValue;
   edType.options[edType.options.length]=new Option(c,c);
  }
 }
}

