/**********************************************************************************   
*   Copyright (C) 2002 Tiera Software, Inc. All rights reserved.
*********************************************************************************/

function openDialogue(pPath,pName,pWidth,pHeight,pScroll, newwindow)
{
    if (newwindow == true) 
    {
		var posLeft = (screen.availWidth - pWidth)/2;
		var posTop = (screen.availHeight - pHeight)/2;
		var strFeatures = "";
		var oWin;
		
		
		strFeatures += "width=" + pWidth + ",height=" + pHeight + ", left=" + posLeft;
		strFeatures += ",top=" + posTop + ",toolbar=0,scrollbars=" + pScroll + ",alwaysRaised=1,status=1,resizable=1";
	        
		oWin = window.open(pPath, pName, strFeatures);
		oWin.focus();	
    }
    else
    { 
	   document.location = pPath; 
	} 
 
}
/**********************************************************************************   
Show/Hide Div Tags
*	div.getElementById(arg) handles Netscape 6.x and IE
*   Script date: 03/11/2002 (keep this date to check versions)
*********************************************************************************/
function showLayer(pElement,pElementNum,pCount)
{
	document.getElementById(pElement + pElementNum).style.visibility = "visible";
	hideLayer(pElement,pElementNum,pCount);	//hide all other div elements
	return;
}
//---------------------------------------------------------------------------------
function hideLayer(pDiv,pNum,pCount)
{
	for (var i = 0; i < pCount; i++) {
		if (i == pNum)
		{	continue;	}
		else {
			document.getElementById(pDiv + i).style.visibility = "hidden";
		}
	}//end for loop
	return;
}
//------------------------------------------------------------------------------------------
function setTabFocus(pInd,pForm)
{
	var objForm=eval('document.' + pForm);
	
	//disable all bottom borders
	for (i=0; i < objForm.tab.length; i++) {
		objForm.tab[i].style.borderBottomStyle='none';
		objForm.tab[i].style.borderBottomColor=document.bgColor;
	}
	
	objForm.tab[pInd].style.color='Blue';
	objForm.tab[pInd].style.borderBottomStyle='none';
	objForm.tab[pInd].style.borderBottomColor=document.bgColor;
        objForm.tab[pInd].style.backgroundColor=document.bgColor;
	document.getElementById('tab' + pInd).style.borderBottomStyle='none';
	document.getElementById('tab' + pInd).style.borderBottomColor=document.bgColor;
	document.getElementById('tab' + pInd).style.borderBottomWidth='1';
	document.getElementById('tab' + pInd).style.backgroundColor=document.bgColor;
	
	for (i=0; i < objForm.tab.length; i++)
		if (i != pInd) {
			objForm.tab[i].style.color='Black';
			document.getElementById('tab' + i).style.borderBottomColor='black';
			document.getElementById('tab' + i).style.borderBottomStyle='solid';
			document.getElementById('tab' + i).style.borderBottomWidth='1';
		}
}
//------------------------------------------------------------------------------------------
function setSingleTabFocus(pInd,pForm)
{
	var objForm=eval('document.' + pForm);
	
	
	objForm.tab.style.color='Blue';
	objForm.tab.style.borderBottomStyle='none';
	objForm.tab.style.borderBottomColor=document.bgColor;
        objForm.tab.style.backgroundColor=document.bgColor;
	document.getElementById('tab' + pInd).style.borderBottomStyle='none';
	document.getElementById('tab' + pInd).style.borderBottomColor=document.bgColor;
	document.getElementById('tab' + pInd).style.borderBottomWidth='1';
	
}
//----------------------------------------------------------------------------------------------------
function loadDate(pUrl,pForm,pObject)
{
	var strUrl = pUrl + "?formName=" + pForm + "&fieldName=" + pObject;
	var strWin = "calendarWin";

	openDialogue(strUrl,strWin,176,185,0);
}
//
function setFilter(filtername, filter)
{
	var expDate = new Date();
	expDate.setFullYear(expDate.getFullYear() + 1);
    //alert(filtername); 
    //alert(filter); 
    //alert(expDate); 
    setCookieNoesc("Filter", filtername, expDate, '/') ;
    setCookieNoesc("FilterArgList", filter, expDate, '/') ;
}
function setCustomFilter(filtername, filter, name)
{
	var expDate = new Date();
	expDate.setFullYear(expDate.getFullYear() + 1);
    //alert(filtername); 
    //alert(filter); 
    //alert(expDate); 
    //alert(name); 
    setCookieNoesc("Filter", filtername, expDate, '/') ;
    setCookieNoesc("FilterArgList", filter, expDate, '/') ;
    setCookieNoesc("FilterName", name, expDate, '/') ;
}

//----------------------------------------------------------------------------------------------------
function setCookieNoesc(name, value, expires, path, domain, secure)
{
	var curCookie = name + '=' + value +
		((expires) ? '; expires=' + expires.toGMTString() : '') +
		((path) ? '; path=' + path : '') +
		((domain) ? '; domain=' + domain : '') +
		((secure) ? '; secure' : '');
		
		//alert('setCookieNoEsc=' + curCookie); 
 
	document.cookie = curCookie;
}

//----------------------------------------------------------------------------------------------------
function setCookie(name, value, expires, path, domain, secure)
{
	var curCookie  = name + '=' + escape(value) +
		((expires) ? '; expires=' + expires.toGMTString() : '') +
		((path) ? '; path=' + path : '') +
		((domain) ? '; domain=' + domain : '') +
		((secure) ? '; secure' : '');
		
	document.cookie = curCookie;
}
//----------------------------------------------------------------------------------------------------
function getCookie(cookieName)
{
   var cookieValue=null;
   //alert(cookieName + '-' + document.cookie); 
   var posName=document.cookie.indexOf(escape(cookieName) + '=');

   if (posName != -1) {
      var posValue=posName+(escape(cookieName)+'=').length;
      var endPos=document.cookie.indexOf(';', posValue);
      if (endPos != -1)
         cookieValue=unescape(document.cookie.substring(posValue,endPos));
      else
         cookieValue=unescape(document.cookie.substring(posValue));
   }
   return cookieValue;
}
//----------------------------------------------------------------------------------------------------
function sortSelect(obj)
{
	var o = new Array();
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option(obj.options[i].text,obj.options[i].value,obj.options[i].defaultSelected,obj.options[i].selected);
	}
	
	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);

	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	}
}
//----------------------------------------------------------------------------------------------------
function getOS()
{
	var sAgent = navigator.userAgent; 
	
    split1 = sAgent.split("("); 
    if (split1.length < 2) 
		{return "unknown1";}
    split2 = split1[1].split(")");
    
    if (split2.length < 2) 
		{return "unknown2";}
		
    split3 = split2[0].split(";"); 
    if (split3.length < 3) 
		{return "unknown3";}

    if (split3[2].charAt(0) == " ") 
    {return split3[2].substring(1, split3[2].length);} 
    
    return split3[2];
    
} 
//---------------------------------------------------------------------------------------------
function checkStringForChar(instr, searchval)
{
    var checkStr = instr
    var searchchar = searchval.charAt(0)
      
    for (j = 0;  j < checkStr.length;  j++)
    {
      ch = checkStr.charAt(j);
      if (ch == searchchar) 
      {
	 return true; 
      } 
    }

  return false; 
} 
//---------------------------------------------------------------------------------------------
function ChangeSpecialCharacters(inString)
{
    var re = /&/g; 
    inString = inString.replace(re, "&amp;"); 
    var re = /</g; 
    inString = inString.replace(re, "&lt;"); 
    var re = />/g; 
    inString = inString.replace(re, "&gt;"); 
    //var re = /'/g; 
    //inString = inString.replace(re, "&apos;"); 
    var re = /"/g; 
    inString = inString.replace(re, "&quot;"); 
    return inString; 
}
// handle onreadystatechange event of req object
 var req;
//---------------------------------------------------------------------------------------------
function loadXMLDocprocessReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) 
        {
         } 
         else 
         {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
         }
    }
}
//--------------------------------------------------------------------------------------------- 
function loadXMLDoc(action, url, async, putData) 
{
    // branch for native XMLHttpRequest object
    this.putData = putData;
    
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = loadXMLDocprocessReqChange;
//        req.overrideMimeType("text/xml");
        req.open(action, url, async);
        if (action == 'POST')
        {
           req.send(putData);
        } 
        else
        {
           req.send(null);
        } 
        this.respTxt = req.responseText;
        this.respDom = req.responseXML;
        this.status  = req.status;
        this.statusText = req.statusText;

        //alert(statusText+status); 
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = loadXMLDocprocessReqChange;
            req.open(action, url, async);
            if (action == 'POST')
            { 
              req.send(putData);
            } 
            else
            {
               req.send(null);
            } 
            this.respTxt = req.responseText;
            this.respDom = req.responseXML;
            this.status  = req.status;
            this.statusText = req.statusText; 
        }
    }
}
//--------------------------------------------------------------------------------------------- 
function loadXMLDocument(xml) 
{
    var xmlDoc; 
    if (window.XMLHttpRequest) 
    {
      //xmlDoc=document.implementation.createDocument("","",null);
      //xmlDoc.loadXML = xml;
      var parser = new DOMParser(); 
      xmlDoc = parser.parseFromString(xml, "text/xml"); 


    // branch for IE/Windows ActiveX version
    } 
    else if (window.ActiveXObject)
    {
     xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
     xmlDoc.async="false";
     xmlDoc.loadXML(xml);    
    }

    return xmlDoc; 
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//----------------------------------------------------------------------------------------------------
function openAdmin()
{
    var username; 
    var password; 
    var cparms = getCookie('autoreconnect')
    var parms = cparms.split('&'); 
    if (parms.length > 1)
    {
      passwordparm = parms[0].split('='); 
      usernameparm = parms[1].split('='); 
      
      if (usernameparm.length > 1)
		{username = usernameparm[1];}  
      if (passwordparm.length > 1)
		{password = passwordparm[1];} 
    }
    
    openDialogue("admin/main.aspx","admin",1100,600,1, true); 
}
function gotoDefectDetailPage(Id, tab)
{
  if(typeof(tab) == 'undefined')
  {
    document.location = 'defectDetails.aspx?defectId=' + Id; 
  }
  else
  {
    document.location = 'defectDetails.aspx?defectId=' + Id + '&s=' + tab
  } 
  return; 
}
function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1)
        {IsNumber = false;}
   }
   return IsNumber;
}
function AnyApos(sText)
{
var Any=false;
var Char;
for (i = 0; i < sText.length; i++) 
{ 
Char = sText.charAt(i); 
//alert(char)
if (Char == "'") 
{
Any = true;
}
}
return Any;
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function openReportVwr(pPath,pName,pWidth,pHeight,pScroll)
{//open all centered dialogue boxes with this routine
	var posLeft = (screen.availWidth - pWidth)/2;
	var posTop = (screen.availHeight - pHeight)/2;
	var strFeatures = "";
	var oWin;
	
	strFeatures += "width=" + pWidth + ",height=" + pHeight + ", left=" + posLeft;
	strFeatures += ",top=" + posTop + ", menubar=1, toolbar=1, scrollbars=" + pScroll + ", alwaysRaised=1,status=1,resizable=1";
	oWin = window.open(pPath, pName, strFeatures);
	oWin.focus();	
}
function setPagingCookie(page)
{
	var expDate = new Date();
	expDate.setFullYear(expDate.getFullYear() + 10);
	setCookie('currentpage', page ,expDate);
} 


