var xmlDoc=false;
var xmlObj=false;
//Verify if XML is proper
function verifyXML()
{
	// 0 Object is not Initialized
	// 1 Loading object is loading data
	// 2 Loaded object has loading data
	// 3 Data from object can be worked with
	// 4 Object completely initialized
	
	if(xmlDoc.readyState !=4)
	{
		return false;
	}
}

//Function to change the Data Displayed in the tabDiv <div> tag at every tab Mouse-Over event
function changeTab(strXMLFile,strTabName,strPage)
{
	if(strPage=='')
	{
		//Find path of page
		var pagePath = window.location.pathname;

		//Find page name
		strPage = pagePath.substring(pagePath.lastIndexOf('/') + 1);
	}
	tabDiv.innerHTML="";
	var divText="";
	
	// Code for IE7+, Firefox, Chrome, Opera, Safari and other browsers
	if (window.XMLHttpRequest)
	{
		xmlDoc=new window.XMLHttpRequest();
		if (xmlDoc.overrideMimeType) 
		{
    		xmlDoc.overrideMimeType('text/xml');
		}
		xmlDoc.onreadystatechange = verifyXML;
		xmlDoc.open('GET',strXMLFile, false);
		xmlDoc.send(null);
		xmlObj=xmlDoc.responseXML;
	}
	
	// Code for IE6, IE5 and Lower versions of IE
	if (window.ActiveXObject)
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.onreadystatechange=verifyXML;
		xmlDoc.load(strXMLFile);
		xmlObj=xmlDoc.documentElement;
	}
	// Error condition if your browser doesnot support XMLHTTP
	if(!window.ActiveXObject && !window.XMLHttpRequest)
	{
		alert("Your browser does not support XMLHTTP!");
	}
	
	switch(strTabName)
	{
		case 'news':
					document.getElementById('aFeatures').style.backgroundPositionX="0%";
					document.getElementById('aFeatures').style.backgroundPositionY="0%";
					document.getElementById('sFeatures').style.backgroundPositionX="100%";
					document.getElementById('sFeatures').style.backgroundPositionY="0%";
					
					document.getElementById('aContactus').style.backgroundPositionX="0%";
					document.getElementById('aContactus').style.backgroundPositionY="0%";
					document.getElementById('sContactus').style.backgroundPositionX="100%";
					document.getElementById('sContactus').style.backgroundPositionY="0%";
					
					document.getElementById('aNews').style.backgroundPositionX="0%";
					document.getElementById('aNews').style.backgroundPositionY="-42px";
					document.getElementById('sNews').style.backgroundPositionX="100%";
					document.getElementById('sNews').style.backgroundPositionY="-42px";
					divText='<p align="justify">';
					for(i=0;i < xmlObj.getElementsByTagName("NEWSITEM").length;i++)
					{
						divText+='<strong style="color:red">'+xmlObj.getElementsByTagName("NEWSITEM")[i].getAttribute("date")+'</strong><br>'+xmlObj.getElementsByTagName("NEWSITEM")[i].childNodes[0].nodeValue;
					}
					divText+='</p>';
					tabDiv.innerHTML=divText;
					break;
		case 'features':
						document.getElementById('aNews').style.backgroundPositionX="0%";
						document.getElementById('aNews').style.backgroundPositionY="0%";
						document.getElementById('sNews').style.backgroundPositionX="100%";
						document.getElementById('sNews').style.backgroundPositionY="0%";
					
						document.getElementById('aContactus').style.backgroundPositionX="0%";
						document.getElementById('aContactus').style.backgroundPositionY="0%";
						document.getElementById('sContactus').style.backgroundPositionX="100%";
						document.getElementById('sContactus').style.backgroundPositionY="0%";
					
						document.getElementById('aFeatures').style.backgroundPositionX="0%";
						document.getElementById('aFeatures').style.backgroundPositionY="-42px";
						document.getElementById('sFeatures').style.backgroundPositionX="100%";
						document.getElementById('sFeatures').style.backgroundPositionY="-42px";
						divText='<p align="justify">';
						for(i=0;i < xmlObj.getElementsByTagName("FEATURE").length;i++)
						{
							if(xmlObj.getElementsByTagName("FEATURE")[i].getAttribute("page").toLowerCase()==strPage.toLowerCase())
							{
								divText+=xmlObj.getElementsByTagName("FEATURE")[i].childNodes[0].nodeValue+'<br><br>';
							}
						}
						divText+='</p>';
						
						tabDiv.innerHTML=divText;
						break;
		case 'contactus':
						 document.getElementById('aNews').style.backgroundPositionX="0%";
						 document.getElementById('aNews').style.backgroundPositionY="0%";
						 document.getElementById('sNews').style.backgroundPositionX="100%";
						 document.getElementById('sNews').style.backgroundPositionY="0%";
					
						 document.getElementById('aFeatures').style.backgroundPositionX="0%";
						 document.getElementById('aFeatures').style.backgroundPositionY="0%";
						 document.getElementById('sFeatures').style.backgroundPositionX="100%";
						 document.getElementById('sFeatures').style.backgroundPositionY="0%";
					
						 document.getElementById('aContactus').style.backgroundPositionX="0%";
						 document.getElementById('aContactus').style.backgroundPositionY="-42px";
						 document.getElementById('sContactus').style.backgroundPositionX="100%";
						 document.getElementById('sContactus').style.backgroundPositionY="-42px";
						 divText+=xmlObj.getElementsByTagName("CONTACTUS")[0].childNodes[0].nodeValue;
						 tabDiv.innerHTML=divText;
						 break;
	}
}

//Javascript validation function for Contact us form
function validateDetails()
{
	var name=document.getElementById('txtName').value;
	var email=document.getElementById('txtEmail').value;
	var phno=document.getElementById('txtPhone').value;
	var company=document.getElementById('txtCompany').value;
	var subject=document.getElementById('txtSubject').value;
	var message=document.getElementById('txtMessage').value;
	
	if(trim(name)=='')
	{
		alert("The Name field is Mandatory");
		return false;
	}
	
	if(isEmail(trim(email))==false)
	{
		alert("Invalid Email Address");
		return false;
	}
	
	if(trim(phno)=='')
	{
		alert("The Phone No. field is Mandatory");
		return false;
	}
	
	var i;
    for (i = 0; i < phno.length; i++)
    {   
        // Check that current character is number.
        var c = phno.charAt(i);
        if (((c < "0") || (c > "9"))) 
		{
			alert("Invalid Phone No.");
			return false;
		}
    }
	
	if(trim(company)=='')
	{
		alert("The Company field is Mandatory");
		return false;
	}
	
	if(trim(subject)=='')
	{
		alert("The Subject field is Mandatory");
		return false;
	}
	
	if(trim(message)=='')
	{
		alert("The Message field is Mandatory");
		return false;
	}
	return true;
}

//Javascript Trim function
function trim(str) 
{
        return str.replace(/^\s+|\s+$/g,"");
}


//Function to check validity of Email Address
function isEmail(str)
{

        if(str == "")
		{
		    return false;
		}
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		if (str.substring(ldot+1,ldot+2)==""){
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }
 		 return true;
}
