// JavaScript Document

var global_currentNodeId = "";
var global_treeActionFile = "product.php";
var global_tree = "";

function checkBrowser(){
  var chkAgent1=navigator.userAgent;
  var chkVersion=navigator.appVersion.substring(0,3);
  var IE=chkAgent1.search(/MSIE/i);
  var Netscape=chkAgent1.search(/Netscape/i);
  var Opera=chkAgent1.search(/Opera/i);
  var Mozilla=chkAgent1.search(/Mozilla/i);
  var compatible=chkAgent1.search(/compatible/i);
  var Firefox=chkAgent1.search(/Firefox/i);
  var Gecko = chkAgent1.search(/Gecko/i);

  if (IE != -1 && compatible != -1 && Mozilla != -1 && Netscape == -1 &&
        chkVersion == "4.0" ){
  	Browser = "MSIE";
	  var startpos = navigator.userAgent.search(/MSIE/i);
	  var startid = startpos + 5;
	  var end = startpos + 8;
	  getVersion = navigator.userAgent.substring(startid,end);
	  return 1;
  }
  if (Opera != -1 && Mozilla != -1 && compatible != -1){
	  Browser = "Opera";
	  var startpos = navigator.userAgent.search(/Opera/i);
	  var startid = startpos + 6;
	  var end = startpos + 10;
	  getVersion = navigator.userAgent.substring(startid,end);
	  return 2;
  }
  if (Mozilla != -1 && Firefox == -1 && chkVersion == "5.0" &&
    compatible == -1 && Netscape == -1){
    Browser = "Mozilla";
	  var startpos = navigator.userAgent.search(/rv/i);
	  var startid = startpos + 3;
	  var end = navigator.userAgent.search(/G/i);
	  var endpos = end -2;
	  getVersion = navigator.userAgent.substring(startid,endpos);
	  return 3;
  }
  if (Mozilla != -1 && Firefox != -1 && chkVersion == "5.0"  &&
    compatible == -1 && Netscape == -1){
    Browser = "Firefox";
	  var startpos = navigator.userAgent.search(/rv/i);
	  var startid = startpos + 3;
	  var end = navigator.userAgent.search(/G/i);
	  var endpos = end -2;
	  getVersion = navigator.userAgent.substring(startid,endpos);
	  return 4;
  }
  if ((Mozilla != -1 && chkVersion == "4.8")){
    Browser = "Netscape";
	  getVersion = "4.8";
	  return 5;
  }
  if (Netscape != -1){
    Browser = "Netscape";
	  var startpos = navigator.userAgent.search(/Netscape/i);
	  var startid = startpos + 9;
	  var end = startpos + 12;
	  getVersion = navigator.userAgent.substring(startid,end);
	  return 6;
  }
}

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 submitBu(type,thisValue) {
  var lfile = "product.php?typeid=002&init=" + type + "&actid=" + thisValue;
  window.location.href = lfile;  
}

function showEmailForm ( typeid, eventid ) {
  var url = 'emailform.php?typeid=' + typeid + '&eventid=' + eventid;
  window.open ( url, 'emailform', 'width=550,height=530' );
}

function getEventsExcel (typeid,actid,strSort,strMode) {
  var lfile = "excelbuilder.php?typeid=" + typeid + "&actid=" + actid + 
              "&sort=" + strSort + "&mode=" + strMode;
  var intChk = checkBrowser ();
  if ( intChk == 1 )
    window.open(lfile,'_blank');
  else 
    window.location.href=lfile;
}

function getEvent(type,thisValue,typeid) {
  var lfile = "event.php?typeid="+typeid+"&actid=" + thisValue;
  window.location.href = lfile;
}

function mouseOver(menu) {
  var preText = document.getElementById( global_currentNodeId );
  if(preText == null) {
    menu.style.textDecoration = "underline";
    //menu.style.color="#316AC5";
    menu.style.color="#006666";
  }
  else if (preText.id != menu.id) {
    menu.style.textDecoration = "underline";
   // menu.style.color="#316AC5";
    menu.style.color="#006666";
  }
}

function mouseOut(menu) {
  var preText = document.getElementById ( global_currentNodeId );
  if(preText == null){
    menu.style.textDecoration = "none";
    //menu.style.color="#3366CC";
    menu.style.color="#006666";
  }
  else if (preText.id != menu.id){
    menu.style.textDecoration = "none";
    //menu.style.color="#3366CC";
    menu.style.color="#006666";
  }
}

function processEventOver(obj) {
  obj.style.backgroundColor = '#316AC5';
  obj.style.color = '#FFFFFF';
}

function processEventOut(obj) {
  obj.style.backgroundColor = '#D5E2FF';
  obj.style.color = '#000000';
}

function validateEmail(obj) {
  var email = obj.email.value;
  var contactNo = obj.contactNo.value;
  var sender = obj.sender.value;

  var senderFilter = /^[a-zA-Z\s]+$/;
  var contactFilter = /^[0-9\s-]+$/;	
  var emailFilter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
  if(!senderFilter.test(sender)) {
    alert("Invalid sender name! Please input sender name only with characters or space.");
    obj.sender.focus();
    obj.sender.select();
    return false;
  }
  else if(!emailFilter.test(email)){
    alert("Invalid email! Please input correct email.");
    obj.email.focus();
    obj.email.select();
    return false;
  }
  else if(!contactFilter.test(contactNo)){
    alert("Invalid contact number! Please input contact number only with digital,space or '-'");
    obj.contactNo.focus();
    obj.contactNo.select();
    return false;
  }

  return true;
}

function setTextareaMaxLength ( obj, intMax ) {
  if ( obj != null ) {
    var strText = obj.value;
    obj.value = strText.substring ( 0, intMax );
  } 
}

function setupWindow() {
  eventContent = document.getElementById("div_event");
  newPage = "<html><head><title>Printing...</title>" + 
            "<link rel='STYLESHEET' href='../web-css/styles.css' type='text/css'>" +
            "<script src='../web-js/functions.js'></script></head>" + 
            "<body style='margin:0px;padding-left:0px;'>"
  newPage += eventContent.innerHTML;
  newPage += "</body></html>";
  objWindow = window.open ("eventprint.php","myPrint","width=600,height=100"); 
  objWindow.document.write(newPage);
  objWindow.document.close();
  return objWindow;
}

function printDoc() {
  objWindow = setupWindow();
  try {
    objWindow.focus();
    if(objWindow.print) 
      objWindow.print();
    if(checkBrowser() != 1)
      objWindow.close();
  }catch(e){return false;} 
}

var currentImageIndex = 0;
var arrImages = new Array();
var rollTime = 50;
var isInit = true;

function rollImages() {
  var total = arrImages.length;
  var obj = document.getElementById('div_left');
  obj.style.backgroundImage = "url("+arrImages[currentImageIndex]+")";
  currentImageIndex ++;
  if(currentImageIndex >= total) {
    if (isInit) {
      isInit = false;
      rollTime = 4000;
    }
    currentImageIndex = 0;
  }
  setTimeout("rollImages()", rollTime);
}

function startRollImages(images, elementId, interval) {
  arrImages = images;
  rollImages();
}

function printProductTree ( strNodes, strCurrentNodeId ) {
  var arrNodes = unserialize ( strNodes );
  global_currentNodeId = "text_" + strCurrentNodeId;
  buildTree ( arrNodes, 'root', strCurrentNodeId );
  document.write ( global_tree );
  global_tree = "";
}

function buildTree ( arrNodes, parentNodeId, strCurrentNodeId ) {
  var width = "menu_left_space";
  var subMenuStyle = "submenu";
  var tblId = "table_" + parentNodeId;
  global_tree += "<table id='" + tblId + "' class='menustyle' cellspacing='0'" + 
                 "cellpadding='0'>";
                
  for ( i in  arrNodes ) {  
    mnuId = "menu_" + arrNodes [ i ][ 'id' ];
    grpId = "group_" + arrNodes [ i ][ 'id' ];
    imgId = "img_" + arrNodes [ i ][ 'id' ];
    txtId = "text_" + arrNodes [ i ][ 'id' ];
    rowId = "tr_" + arrNodes [ i ][ 'id' ];
    treId = "tree_" + arrNodes [ i ][ 'id' ];
    
    strText = arrNodes [ i ][ 'text' ];
    lnkFile = global_treeActionFile + "?actid=menu_" + arrNodes [ i ][ 'id' ];   
    divStyle = "display:none;";
    //icon = "../web-images/tree/plus.gif";
    icon = "../web-images/JLM/tree/JLM_plus.gif";
    initText = "";
    initMenu = "mainMenu";
    
    imgOnclick = "onclick=\"clickIcon('" + arrNodes [ i ][ 'id' ] + "');\"";
    if ( !arrNodes [ i ][ 'haschild' ] ) {
      //icon = "../web-images/tree/circle.gif";
      icon = "../web-images/JLM/tree/JLM_circle.gif";
      imgOnclick = "";
    } 
    if ( arrNodes [ i ] [ 'obj' ] != null ) {
      divStyle = "display:block;";
      //icon = "../web-images/tree/minus.gif";
      icon = "../web-images/JLM/tree/JLM_minus.gif";
      initMenu = "initMenu";
    }
    if ( arrNodes [ i ][ 'id' ] == strCurrentNodeId ) {
      initText = "initText";
    }

    global_tree += "<tr id='" + rowId + "'><td class='" + width + "'></td>" + 
                   "<td class='" + subMenuStyle + "'><a id=\"" + treId + "\"" + 
                   "href=\"#\" onfocus=\"this.blur();\"" + imgOnclick + "><img " +
                   "src='" + icon + "' id='" + imgId + "' border='0'></a>" + 
                   "<a class=\"" + initMenu + "\" id=\"" + mnuId + "\" " +
                   "href=\"" + lnkFile + "\" onfocus=\"this.blur();\"><span " + 
                   "class=\"" + initText + "\" id=\"" + txtId + "\" " + 
                   "onmouseover=\"mouseOver(this);\" onmouseout=\"" + 
                   "mouseOut(this);\">" + strText + "</span></a><div " + 
                   "style='" + divStyle + "' id='" + grpId + "'>";
    
    if ( arrNodes [ i ] [ 'obj' ] != null ) {
      buildTree ( arrNodes [ i ][ 'obj' ], arrNodes [ i ][ 'id' ], strCurrentNodeId );
    }
    global_tree += "</div></td></tr>";
  }
  global_tree += "</table>" ;
}

function AjaxInit() {
  ajax = false;
  if ( window.XMLHttpRequest ) { // Mozilla, Safari....
    ajax = new XMLHttpRequest ();
    if ( ajax.overrideMimeType )
      ajax.overrideMimeType("text/xml");
  }
  else if ( window.ActiveXObject ) {//IE
    try {
      ajax = new ActiveXObject ( "Msxml2.XMLHTTP" );
    } catch ( e ) {
      try {
        ajax = new ActiveXObject ( "Microsoft.XMLHTTP" );
      }   catch ( e ) {}
    }
  }
  if ( !ajax ) {
    alert("Can't create an XMLHttp instance!");
  }
  return ajax;
}

function AjaxGetRequest ( thisURL, strNodeId ) {
  var elementID = "group_" + strNodeId;
  var u_today = new Date();
  var unique_call = u_today.getTime ();
  var chk = thisURL.indexOf ( "?" );
  if ( chk == -1 ) thisURL += "?";
  thisURL += "&u_call="+unique_call;
  var content = document.getElementById ( elementID );
  var ajax = AjaxInit();
  ajax.open ( "GET", thisURL, true );
  ajax.onreadystatechange = function() {
    if ( ajax.readyState == 4 && ajax.status == 200 ) {
      var arrSubNodes = unserialize ( ajax.responseText );
      buildTree ( arrSubNodes, strNodeId, '' );
      content.innerHTML = global_tree;
      global_tree = "";
    }
  }
  ajax.send(null);
}

function clickIcon ( strNodeId ) {
  var thisUrl = 'productajax.php?nodeid=' + strNodeId;
  AjaxGetRequest ( thisUrl, strNodeId );

  var menuid = 'menu_' + strNodeId;
  var groupid = 'group_' + strNodeId;
  var imgid = 'img_' + strNodeId;
  var menu = document.getElementById(menuid);
  var group = document.getElementById(groupid);
  var img = document.getElementById(imgid);
  var blnProd = false;
  if (menuid != null) {
    var arrIds = menuid.split ('_');
    if(arrIds[1] != null && arrIds[1] == 'prod')
      blnProd = true;
  }

  var display;

  if(group != null)
    display = group.style.display;
  if(menu != null && blnProd == false)
    menu.className = (display=="block")?"menu_1":"menu_2";

  if(img != null && blnProd == false)
    //img.src = (display=="block")?"../web-images/tree/plus.gif":"../web-images/tree/minus.gif";
    img.src = (display=="block")?"../web-images/JLM/tree/JLM_plus.gif":"../web-images/JLM/tree/JLM_minus.gif";

  if(display != null)
    group.style.display = (display=="block")?"none":"block";
}

