var spinImg = document.getElementById("spinImg");
var ie = (typeof window.ActiveXObject!= 'undefined');
var moz = (typeof document.implementation!= 'undefined') &&
(typeof document.implementation.createDocument!= 'undefined');
var xmlDoc=null;
var httpreq;
var file=null;
var root=null;
var node = null;
var source = "";
var rootElement,subRootElement;
function executeOnfocusmethod(id){
	if(document.getElementById(id).value == "Enter Text"){
		document.getElementById(id).value = "";
	}

}

function resetForm(pageSource){
	source = pageSource;
	document.getElementById("startIndex").value=1;
	document.getElementById("endIndex").value=10;
	if(source=="press"){
		document.presscenter.prkeyword.value = "";
		document.presscenter.prMonth.value = "";
		document.presscenter.prYear.value = "";
		document.presscenter.prTopic.value = "";
	}else{
		document.newsForm.newsKeyWord.value = "";
		document.newsForm.newsMonth.value = "";
		document.newsForm.newsYear.value = "";
	}
	callAjaxRequest();
	//return false;
}

function submitForm(pageSource){
	document.getElementById("startIndex").value=1;
	document.getElementById("endIndex").value=10;
	source = pageSource;
	callAjaxRequest();
	return false;
}
function callAjaxRequest(){
	//document.body.style.cursor='busy';
	//document.getElementById("resettab").style.display="";
	if(spinImg){
		spinImg.style.display='block';
	}
	var weburl = "/about/news/index.esp";
	getXMLHttpRequest();
	var searchKey = "";
	var month = "";
	var year = "";
	if(source=="press"){
		searchKey = trim(document.presscenter.prkeyword.value);
		month = document.presscenter.prMonth.value;
		year = document.presscenter.prYear.value;
		topic = encodeURIComponent(document.presscenter.prTopic.value);
		document.getElementById("pressReset").style.display='';
	}else{
		searchKey = trim(document.newsForm.newsKeyWord.value);
		month = document.newsForm.newsMonth.value;
		year = document.newsForm.newsYear.value;
		document.getElementById("newsReset").style.display="";
	}
	var startIndex = document.getElementById("startIndex").value;
	var endIndex = document.getElementById("endIndex").value;
	if(searchKey == 'Enter Text'){
		searchKey = '';
	}
	
	var params = "";
	if(source=="press"){
		params ="prkeyword="+searchKey+"&prTopic="+topic+"&prYear="+year+"&prMonth="+month+"&startIndex="+startIndex+"&endIndex="+endIndex+"&source="+source;
	}
	else if(source == "news"){
		params = "newsKeyWord="+searchKey+"&newsYear="+year+"&newsMonth="+month+"&startIndex="+startIndex+"&endIndex="+endIndex+"&source="+source;
	}
	httpreq.open("POST", weburl, true);
	//Send the proper header information along with the request
	httpreq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpreq.setRequestHeader("Content-length", params.length);
	httpreq.setRequestHeader("Connection", "close");
	httpreq.onreadystatechange=getAjaxReq;
	httpreq.send(params);

}
		
//Get http request
function getXMLHttpRequest(){

	 if (window.XMLHttpRequest) 
	 {
	     httpreq = new XMLHttpRequest();
	     if(httpreq.overrideMimeType){                
			httpreq.overrideMimeType('text/xml');
		}

	  } 
	  else if (window.ActiveXObject)
	  {
	    try 
	    {
		httpreq = new ActiveXObject("Msxml2.XMLHTTP");
	    } 
	    catch (e1) 
	    {
		try {
		// Try version supported by older versions
		// of Internet Explorer
		   httpreq = new ActiveXObject("Microsoft.XMLHTTP");
	       } 
	       catch (e2){
	       }
	    }
	  }
}
 //Get Ajax request
function getAjaxReq() 
{
	
	if(moz){
		try{
			netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
		}
		catch(ex){
		}
	}
	

	if (httpreq.readyState == 4) {

		if (httpreq.status == 200) {

		    if(ie) {
			file = httpreq.responseXML;
			importxml();
		    }
		    else if(moz) {  
			file=httpreq.responseXML;
			importxml();
		    }
		}
	}          
}
	
function importxml(){

	if( moz )
	{ 
		xmlDoc =file;
		rootElement = xmlDoc.documentElement;
		renderFormDataFromXml();
	 }
	else if (ie)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = false;
		xmlDoc.load(file);
		rootElement = xmlDoc.documentElement;
		renderFormDataFromXml();

	}
}
	
function renderFormDataFromXml()
{

	if(rootElement==null)
	{

		setErrorMessagetoDiv();
	}
	else
	{
		var Count = rootElement.childNodes.length;
		if(Count<=4){
			setErrorMessagetoDiv();
		}
		else{
			var innerText="";
			for(var k=0;k < 6; k++) 
			{

				subRootElement = rootElement.childNodes[k];
				if(k==4)
				{
				
					if(subRootElement.nodeName=="pagination_tag")
					{
						if(source == "press"){
							document.getElementById("prPaginationtop").style.display = 'block';
							document.getElementById("prPaginationBottom").style.display = 'block';
						}
						else if(source == "news"){
							document.getElementById("newsPaginationTop").style.display = 'block';
							//alert(document.getElementById("newsPaginationBottom"));
							document.getElementById("newsPaginationBottom").style.display = 'block';
						}
						if(subRootElement.firstChild !=null){
							var pageTag = subRootElement.firstChild.nodeValue;
							var topLine = "<div class='contentHalfMidNoTabBodySeparator marginTop10'></div>";
							var divTag = "<div class='clearBoth'></div> <div class='marginTop10'><img height='1' width='1'  src='/images/common/spacer.gif'/></div>"+
								     "<div class='searchResultsPagination floatLeft' name='pagination' style='text-align:left;margin:0;width:275px;'>";
							if(source == "press"){
								if(document.getElementById("prPaginationtop"))
								{
									divTag = divTag+pageTag+"</div>";
									document.getElementById("prPaginationtop").innerHTML = topLine+divTag;
								}
								if(document.getElementById("prPaginationBottom")){
									document.getElementById("prPaginationBottom").innerHTML = divTag;
								}
								
							}
							else if(source =="news"){
								if(document.getElementById("newsPaginationTop"))
								{
									divTag = divTag+pageTag+"</div>";
									document.getElementById("newsPaginationTop").innerHTML = topLine+divTag;
								}
								if(document.getElementById("newsPaginationBottom")){
									document.getElementById("newsPaginationBottom").innerHTML = divTag;
								}
							
							}
						}else{
							if(source == "press"){
								document.getElementById("prPaginationtop").style.display = 'none';
								document.getElementById("prPaginationBottom").style.display = 'none';
							}
							else if(source == "news"){
								document.getElementById("newsPaginationTop").style.display = 'none';
								document.getElementById("newsPaginationBottom").style.display = 'none';
							}
						}
					}
				}
				if(k==5)
				{
					document.getElementById("scmrImage").style.display='block';
					var metadata = xmlDoc.getElementsByTagName("pressMetadata");
					var metdataTagCount = metadata.length;
					var date = "";
					var awebcUrl = "";
					var title = "";
					var multimediaFormat = "";
					var innerTableText = "<table class='eventsTable' style='border:0;'><tbody>";
					var imageSrc="";
					for(var j=0;j<metdataTagCount;j++)
					{
						if(metadata[j].childNodes[0] != null){
						
							awebcUrl = metadata[j].childNodes[0].firstChild.nodeValue;
						}	
						if(metadata[j].childNodes[1] != null){
						
							date = metadata[j].childNodes[1].firstChild.nodeValue;
						
						}			
						if(metadata[j].childNodes[2] != null){
						
							title = metadata[j].childNodes[2].firstChild.nodeValue;
						
						}		
						if(metadata[j].childNodes[3] != null){
						
							multimediaFormat = metadata[j].childNodes[3].firstChild.nodeValue;
						
						}
						innerTableText = innerTableText+"<tr><td class='thinDate'>"+date+"</td><td class='icon'>";
						if(source == "press"){
							if(multimediaFormat!=""){
								var imageSrc = "/images/events/icons/"+multimediaFormat+".gif";
								innerTableText = innerTableText+"<img height='16' width='16' class='verticalMiddle' title="+multimediaFormat+" src="+imageSrc+"></td>";
							}
							else{

								innerTableText = innerTableText+" </td>";
							}
						}
						if(awebcUrl.startsWith("http")){
							innerTableText =innerTableText +"<td class='blueLink'><a href='"+awebcUrl+"' target='_blank'>"+title+"<img class='verticalMiddle' src='/images/common/icon_new_site.gif' alt=''/></a></td></tr>";
						}else{
						
							innerTableText =innerTableText +"<td class='blueLink'><a href='"+awebcUrl+"'>"+title+"</a></td></tr>";
						}

					}
					innerTableText = innerTableText+"</table>";
					if(source == "press"){
						if(document.getElementById("pressResults"))
						{
							document.getElementById("pressResults").innerHTML = innerTableText;
							if(document.getElementById("newsResults")!=null && document.getElementById("newsResults").innerHTML=="&nbsp;") {
								submitForm("news");
							}

						}
					}
					if(source == "news"){
						if(document.getElementById("newsResults"))
						{
							document.getElementById("newsResults").innerHTML = innerTableText;
						}
					}
		
				}
			}
		}
	}
	if(spinImg){
		spinImg.style.display='none';
	}

}
		
function setErrorMessagetoDiv()
{
	var errorMsg = "No Result Found. Please try different search.";
	if(source == "press"){
		document.getElementById("scmrImage").style.display='none';
		document.getElementById("prPaginationtop").innerHTML = "";
		document.getElementById("prPaginationBottom").innerHTML = "";
		document.getElementById("pressResults").innerHTML = errorMsg;
	}
	else if(source == "news"){
		document.getElementById("newsPaginationTop").innerHTML = "";
		document.getElementById("newsPaginationBottom").innerHTML = "";
		document.getElementById("newsResults").innerHTML = errorMsg;
	}
}

function showPage(start,range,pagesource)
{
	document.getElementById("startIndex").value=start;
	document.getElementById("endIndex").value = range;
	source = pagesource;
	callAjaxRequest();
}

function replaceAll(OldString, FindString, ReplaceString) {
	var SearchIndex = 0;
	var NewString = "";
	while (OldString.indexOf(FindString,SearchIndex) != -1){
		NewString += OldString.substring(SearchIndex,OldString.indexOf(FindString,SearchIndex));
		NewString += ReplaceString;
		SearchIndex = (OldString.indexOf(FindString,SearchIndex) + FindString.length);
	}
	NewString += OldString.substring(SearchIndex,OldString.length);
	return NewString;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

