﻿function createAjaxObj(){
var httprequest=false
if (window.XMLHttpRequest){ // if Mozilla, Safari etc
httprequest=new XMLHttpRequest()
if (httprequest.overrideMimeType)
httprequest.overrideMimeType('text/xml')
}
else if (window.ActiveXObject){ // if IE
try {
httprequest=new ActiveXObject("Msxml2.XMLHTTP");
} 
catch (e){
try{
httprequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){}
}
}
return httprequest
}

var ajaxpack=new Object()
ajaxpack.basedomain="http://"+window.location.hostname
ajaxpack.ajaxobj=createAjaxObj()
ajaxpack.filetype="txt"
ajaxpack.addrandomnumber=0 //Set to 1 or 0. See documentation.

ajaxpack.getAjaxRequest=function(url, parameters, callbackfunc, filetype){
ajaxpack.ajaxobj=createAjaxObj() //recreate ajax object to defeat cache problem in IE
if (ajaxpack.addrandomnumber==1) //Further defeat caching problem in IE?
var parameters=parameters+"&ajaxcachebust="+new Date().getTime()
if (this.ajaxobj)
{
    this.filetype=filetype
    this.ajaxobj.onreadystatechange=callbackfunc
    this.ajaxobj.open('GET', url, true)
    this.ajaxobj.send(null)
    }
}

ajaxpack.postAjaxRequest=function(url, parameters, callbackfunc, filetype){

ajaxpack.ajaxobj=createAjaxObj() //recreate ajax object to defeat cache problem in IE
if (this.ajaxobj){
    this.filetype=filetype
    this.ajaxobj.onreadystatechange = callbackfunc;
    
    this.ajaxobj.open('POST', url, true);
    this.ajaxobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    this.ajaxobj.setRequestHeader("Content-length", parameters.length);
    this.ajaxobj.setRequestHeader("Connection", "close");
    this.ajaxobj.send(parameters);
}
}

function saveTagValue( tagNum, tagIndex )
{
    document.getElementById("divSave").className="loadShow";
    document.getElementById("lblMessage").innerHTML="Saving...";
    ajaxpack.postAjaxRequest('/scripts/write-tags', getParameters(tagNum, tagIndex), processGetPost, 'txt');
   // requestTagValues();
}

function requestTagValues( )
{
     
    ajaxpack.getAjaxRequest(findURL(), '', processGet, 'txt');
}

function getParameters( tagNum, tagIndex )
{
    var tagValue = document.getElementById("data" + tagNum).value;
    return "tag1=" + tagIndex + "&page=1&back=/user/input_w.htm&data1=" + tagValue;
}

function updateTags( responseText )
{
    var x;
    for(x=1;x<17;x++)
    {
        updateTag( x, responseText);
    }
    document.getElementById("divSave").className="loadHidden";
}

function updateTag( tagNum, responseText )
{
    var strValue;
    startIndex = responseText.indexOf("<tag"+tagNum+">") + tagNum.toString().length + 5;
    endIndex =  responseText.indexOf("</tag"+tagNum+">");
    //alert(responseText)
    if(document.getElementById("currentValue"+tagNum))
    {
        strValue = trimString( responseText.substring(startIndex,endIndex) );
        if(strValue=="On")
        {
            document.getElementById("currentValue"+tagNum).setAttribute("class", "inAlarm");
        }
        else
        {
            document.getElementById("currentValue"+tagNum).setAttribute("class", "noAlarm");
        }
        
            
        document.getElementById("currentValue"+tagNum).innerHTML = strValue;
        document.getElementById("data"+tagNum).value = strValue;
    }
    
}
function trimString( str )
{
    return str.replace(/^\s*/, "").replace(/\s*$/, "");
}

function processGetPost()
{
    //alert('in dound');
    var myajax=ajaxpack.ajaxobj
    var myfiletype=ajaxpack.filetype
    if (myajax.readyState == 4)
    { //if request of file completed
       
        if (myajax.status==200 || window.location.href.indexOf("http")==-1)
        { //if request was successful or running script locally
            requestTagValues();    
        }
    }
 }

function processGet()
{
    var myajax=ajaxpack.ajaxobj
    var myfiletype=ajaxpack.filetype
    if (myajax.readyState == 4)
    { //if request of file completed
       // if (myajax.status==200 || window.location.href.indexOf("http")==-1)
        //{ //if request was successful or running script locally
            if (myfiletype=="txt")
                updateTags(myajax.responseText)
            else
                alert(myajax.responseXML)
       // }
    }
}

function findURL()
{
    return document.getElementById("varURL").value;
}

function firstLoad()
{
    document.getElementById("divSave").className="loadShow";
    document.getElementById("lblMessage").innerHTML="Loading...";
    requestTagValues();
}

function changeContent( url )
{
    flag = 1;
    window.location=url;
    //ajaxpack.getAjaxRequest(url, '', updateContent, 'txt');
    //alert(url);
}

function updateContent()
{
    var myajax=ajaxpack.ajaxobj
    var myfiletype=ajaxpack.filetype
    if (myajax.readyState == 4)
    { //if request of file completed
       // if (myajax.status==200 || window.location.href.indexOf("http")==-1)
        //{ //if request was successful or running script locally
            if (myfiletype=="txt")
            {
               document.getElementById('cimMainContent').innerHTML = parseContent(myajax.responseText);
            }
            else
                alert(myajax.responseXML);
       // }
    }
    //changeTest();
}

function parseContent(responseText)
{
   var innerContent = responseText.toString().slice(responseText.toString().indexOf('<!--SOC-->'),responseText.toString().indexOf('<!--EOC-->')); 
   return innerContent;
}

//Image slideshow code

gSlideshowInterval = 8;
gNumberOfImages = 9;
flag = 0;

gImages = new Array(gNumberOfImages);
gImages[0] = "images/FrontPage/Cimlogic.png";
gImages[1] = "images/FrontPage/MES.png";
gImages[2] = "images/FrontPage/TrakSYS.png";
gImages[3] = "images/FrontPage/Virtualisation.png";
gImages[4] = "images/FrontPage/RFID.png";
gImages[5] = "images/FrontPage/Automation.png";
gImages[6] = "images/FrontPage/Partners.png";
gImages[7] = "images/FrontPage/Customers.png";
gImages[8] = "images/FrontPage/LEANTrak.png";

function changeImg(pic)
{
    if(pic <9)
    {
        document.getElementById('img').src = gImages[pic];
        document.getElementById('Button'+pic).style.backgroundPosition = '0px 110px';
        flag = 1;
    }
    else {
        document.getElementById('RightBtnHead'+pic).style.color = 'Red';
        document.getElementById('RightBtnDesc'+pic).style.color = 'Red';
        //document.getElementById('Button'+pic).style.color = 'Red';
    }
}

function setZero(pic)
{
  if(pic <9)
  {
    document.getElementById('Button'+pic).style.backgroundPosition = '0px 0px';
    flag = 0;
  }
  else
  {
    document.getElementById('RightBtnHead'+pic).style.color = 'Black';
    document.getElementById('RightBtnDesc'+pic).style.color = 'Black';
    //document.getElementById('Button'+pic).style.color = 'Black';
  }
    
}

function changeImgLeft(imgNo)
{
     if (document.getElementById('liImageLeft'+imgNo).className != "NavSelected")
     {
        document.getElementById('liImageLeft'+imgNo).style.backgroundPosition = '-4px -49px';     
     }
}    
function restoreImgLeft(imgNo)
{
     if (document.getElementById('liImageLeft'+imgNo).className == "NavSelected")
     {
        document.getElementById('liImageLeft'+imgNo).style.backgroundPosition = '-4px -94px';     
     }
     else
     {
        document.getElementById('liImageLeft'+imgNo).style.backgroundPosition = '-4px -4px'; 
     } 
}

function canManipulateImages() {
	if (document.images)
		return true;
	else
		return false;
}
function loadSlide(imageURL) {
    if (gImageCapableBrowser) {
        if (document.slide != undefined) {
            document.slide.src = imageURL;
        }
		return false;
	}
	else {
		return true;
	}
}
function nextSlide() {
	gCurrentImage = (gCurrentImage + 1) % gNumberOfImages;
	if(flag == 0)
	{
	loadSlide(gImages[gCurrentImage]);
	initImage();
	}
}
gImageCapableBrowser = canManipulateImages();
gCurrentImage = 0;
setInterval("nextSlide()",gSlideshowInterval * 1000);


//image fade in code

function initImage() {
    imageId = 'img';
    if (document.getElementById(imageId)) {
        image = document.getElementById(imageId);
        setOpacity(image, 0);
        image.style.visibility = 'visible';
        fadeIn(imageId, 0);
    }
}

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 10;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
    }
  }
}

function pageLoad()
{
    changeContent('content/main.htm');
    //changeTest();
}

function changeTest()
{
    var url = "content/testies/" + Math.floor(Math.random()*1) + ".htm";
    ajaxpack.getAjaxRequest(url, '', updateTest, 'txt');
}

function updateTest()
{
    var myajax=ajaxpack.ajaxobj
    var myfiletype=ajaxpack.filetype
    if (myajax.readyState == 4)
    { //if request of file completed
       // if (myajax.status==200 || window.location.href.indexOf("http")==-1)
        //{ //if request was successful or running script locally
            if (myfiletype=="txt")
               document.getElementById('testies').innerHTML = myajax.responseText;
            else
                alert(myajax.responseXML);
       // }
    }
    
}

////////////NEWS PREVIEW CODE//////////

function updateHeadline()
{
    document.getElementById('ctl00_MainContent_preHeadline').firstChild.nodeValue = document.getElementById('ctl00_MainContent_headline').value;
}

function updateSnippet()
{
     document.getElementById('ctl00_MainContent_preSnippet').firstChild.nodeValue = document.getElementById('ctl00_MainContent_snippet').value;
}

	  
	 
	 