///////////////////////////////////
//START - functions for star rating
///////////////////////////////////

  function SetStar(el, R, starLight, starDark)
  {
      el = el.substring(0, el.length - 1);
      for (var x=1; x <=5; ++x)
      {
          if ( x <= R ) image(el + "" + x).src = starLight;
          else image(el + "" + x).src = starDark;
      }
  }

  //UN-setting means repeating this function above for the original rating
  function UnsetStar(el, starLight, starDark)
  {
      var x = image(el.substring(0, el.length - 1) + "O").value;
      SetStar(el, x, starLight, starDark);
  }


  function image(a)
  {
  	if(document.getElementById&&document.getElementById(a))
        return document.getElementById(a)
      else if(document.all&&document.all(a))
        return document.all(a)
      else if(document.layers&&document.layers[a])
        return document.layers[a]
      else
        return false
  }  
//////////////////////////////////
//END - functions for star rating
//////////////////////////////////


//////////////////////////////////
//START - Input control functions
//////////////////////////////////
  var EnterDefault = "";

  //when text input gains focus, it needs to clear formatting and value of default text
  function TextInputFocus(obj)
  {
		clearDefaultText(obj);
		//alert(obj.value);
		//document.form1.CatName.defaultText = 'Mr. Fluffers';
		//document.form1.CatName.style.color = '#A8A8A8';
		//addEvent(document.form1.CatName, 'focus', clearDefaultText, false);
		//addEvent(document.form1.CatName, 'blur', replaceDefaultText, false);
		return true; 
  }
  
  //when text input loses focus and has no value then add formatting and value of default text
  function TextInputBlur(obj)
  {
		//alert('TextInputBlur');
		return true;
  }    

	function setDefaultText(str) 
	{
		this.defaultText = str;
		return; 
	}

	function clearDefaultText(el) 
	{
    if (el.value == el.defaultText || el.value == '')
    {
      el.value = '';
      el.style.fontStyle = 'normal';
      el.style.color = 'Black';
    }
  }

  function replaceDefaultText(e) 
  {
      var target = window.event ? window.event.srcElement : e ? e.target : null;
      if (!target) return;
      if (target.value == '' && target.defaultText)
      {
          target.value = target.defaultText;
          target.style.color = '#A8A8A8';
      }
  }

  //currently this is hardcoded to values useful to main page, need to make generic
  //document.getElementById('ModelNum').focus();
  //document.getElementById('ModelNum').select();
  function SetEnterDefault(staticname)
  {
      EnterDefault = staticname;
      return;
  }
  
  function checkEnter(evt) 
  {
      var evt  = (evt) ? evt : ((event) ? event : null);
      if (evt.keyCode == 13)
      {         
          var elems = getElementsByAttribute(document.body, "*", "staticname", EnterDefault);
          if (elems.length > 0) {
              elems[0].click();
          }
          return false;
      }
      return true;
  }
  
  document.onkeypress = checkEnter;

////////////////////////////////
//END - Input control functions
////////////////////////////////



/////////////////////
//START - Validation
/////////////////////
  function isValidEmail()
  {
    var emailAdd;
    emailAdd = document.all.infobar_EmailAdd.value;
    if(emailAdd == "" || emailAdd.indexOf("@") < 0 || emailAdd.indexOf(".") < 0 )
    {							
      alert("Please enter a valid email address");
      return false;												
    }
    else    return true;
  }	
///////////////////
//END - Validation
///////////////////



/////////////////////
//START - General
/////////////////////


  function EGPTrimString(str) {
      return str.replace(/^\s+|\s+$/g, '');
  }


  function addEvent(element, eventType, lamdaFunction, useCapture) 
  {
      if (element.addEventListener) 
      {
          element.addEventListener(eventType, lamdaFunction, useCapture);
          return true;
      } 
      else if (element.attachEvent) 
      {
          var r = element.attachEvent('on' + eventType, lamdaFunction);
          return r;
      } 
      else  return false;
  }
  
  //Copyright Robert Nyman, http://www.robertnyman.com Free to use if this text is included
  function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue)
  {
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
    var oCurrent;
    var oAttribute;
    for(var i=0; i<arrElements.length; i++)
    {
	    oCurrent = arrElements[i];
	    oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
	    if(typeof oAttribute == "string" && oAttribute.length > 0)
	    {
		    if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute)))
		    {
			    arrReturnElements.push(oCurrent);
		    }
	    }
    }
    return arrReturnElements;
  }


  function switchSchematic(newimage, mainImageId)
  {
      var mainImage = document.getElementById(mainImageId);
      if( mainImage != null )
      {
          mainImage.style.visibility = 'visible';
          mainImage.src = newimage;
      }
      
      // SchematicMain.src=newimage;
  }


  function ShowElement(staticname)
  {
      getElementsByAttribute(document.body, "*", "staticname", staticname)[0].visible="true";
  }
  
  
  function HideElement(staticname)
  {
      getElementsByAttribute(document.body, "*", "staticname", staticname)[0].visible="false";
  }
  
  function ToggleVisibility(obj) 
  {
      var el = document.getElementById(obj);
      if (el.style.display == 'block')
          el.style.display = 'none';
      else
          el.style.display = 'block';
  }

  // sets input focus to the designated element  
  function setFocusToInput( objID )
  {
    var elementToSet = getElementsByAttribute(document.body, "*", "staticname", objID)[0];
    if ( elementToSet != null ) 
        elementToSet.focus();
  }

/////////////////////
//END - General
/////////////////////

///////////////////////////////////////////////////////////
//START - PartDetail Image Popups and HTML document popups
///////////////////////////////////////////////////////////

	// Displays a schematic for the customer in a popup window
	function DisplayFullImage(ctrlimg)
	{
	    var txtCode = "<html><head><title>Schematic</title>"
	    +  "</head><body onload=\"window.focus()\"><div align=\"left\"> "
	    + "<img src=\"" + ctrlimg + "\" border=\"0\" name=\"Schematic\" />" 
	    + "</div></body></html>";
	    var mywindow = window.open('Schematic','image',  'toolbar=0,location=0,menuBar=0,scrollbars=1,resizable=1');
	    mywindow.document.open();
	    mywindow.document.write(txtCode);
	    // this code raises the window if hidden and a new one is opened in this window
	    if (window.focus)
	    {
	        mywindow.blur();
	        mywindow.focus();
	    }
	    mywindow.document.close();
	}
	
	// Displays a file
	function DisplayLink(urlToDisplay)
	{
	    var mywindow = window.open('DisplayLink','display',  'toolbar=0,location=0,menuBar=0,scrollbars=1,resizable=1');
	    mywindow.document.open();
	    mywindow.document.location = urlToDisplay;
	    // this code raises the window if hidden and a new one is opened in this window
	    if (window.focus)
	    {
	        mywindow.blur();
	        mywindow.focus();
	    }
	    mywindow.document.close();
	}
	
	// Displays a file
	function DisplayLinkInSizedWindow(urlToDisplay, X, Y)
	{
	    var mywindow = window.open('DisplayLink','display','toolbar=0,location=0,menuBar=0,scrollbars=1,resizable=1,height=' + Y + ',width=' + X);
	    mywindow.document.open();
	    mywindow.document.location = urlToDisplay;
	    
	    // this code raises the window if hidden and a new one is opened in this window
	    if (window.focus)
	    {
	        mywindow.blur();
	        mywindow.focus();
	    }
	    mywindow.document.close();
	}



///////////////////////////////////////////////////////////
//END - PartDetail Image Popups and HTML document popups
///////////////////////////////////////////////////////////


///////////////////
//START - Cookies
///////////////////
    function EGPSetCookie(name, value, exdays, path, domain, secure)
    {
        var cookie_string = name + "=" + enc(escape(value));

        var expires = new Date();
        expires.setDate(expires.getDate() + exdays);
        cookie_string += "; expires=" + expires.toGMTString();

        if ( path )
            cookie_string += "; path=" + escape ( path );

        if ( domain )
            cookie_string += "; domain=" + escape ( domain );

        if ( secure )
            cookie_string += "; secure";

        document.cookie = cookie_string;
    }


    function EGPGetCookie(cookie_name)
    {
        var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

        if (results)
        {
            if (results[2]) return unescape(dec(results[2]));
            else return null;
        }
        else
        {
            return null;
        }
    }

    function EGPDeleteCookie(cookie_name)
    {
        var cookie_date = new Date();  // current date & time
        cookie_date.setTime ( cookie_date.getTime() - 1 );
        document.cookie = cookie_name + '=; expires=' + cookie_date.toGMTString() + '; path=/;';
        document.cookie = cookie_name + '=; expires=' + cookie_date.toGMTString() + '; path=/secure;';//@@@remove eventually
    }

    function enc(str)
    {
        if (str)
        {
            var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789a';
            var pos, res = '';
            
	        for(i=0; i < str.length; i++)
	        {
	            pos = alpha.indexOf(str.charAt(i));
	            if (pos == -1)  res += str.charAt(i);
	            else  res += alpha.charAt(pos+1)
	        }
            return res;
        }
        return '';
    }

    function dec(str)
    {
        if (str)
        {
            var alpha = '9876543210ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba9';
            var pos, res = '';
            
	        for(i=0; i < str.length; i++)
	        {
	            pos = alpha.indexOf(str.charAt(i));
	            if (pos == -1)  res += str.charAt(i);
	            else  res += alpha.charAt(pos+1)
	        }
            return res;
        }
        return '';
    }
///////////////////
//END - Cookies
///////////////////

