/*************************************************************************/
//
//                       UTILITY FUNCTIONS
//
/*************************************************************************/

var popupWin;

/***************************************/
//           closePopup()
/***************************************/
function closePopup()
{ if(popupWin)
    popupWin.close();
}


function WithoutSelectionValue(ss) {
for(var i = 0; i < ss.length; i++) {
if(ss[i].selected) {
if(ss[i].value.length) { return false; }
}
}
return true;
}

/*****************************************/
//             showDialog()
/*****************************************/
function showDialog(pagePath, args, width, height, top)
{ closePopup();
  return showModalDialog(pagePath, args, "dialogTop:"+top+"px;dialogWidth:"+width+"px;dialogHeight:"+height+"px;help:no;scroll:no;status:no");
}


/*****************************************/
//             setTextColor()
/*****************************************/
function setTextColor(rootpath, frm, current_color, colorTxt, colorBox)
{ var curr_color = current_color;
  if(curr_color.charAt(0)=='#')
    curr_color = curr_color.substr(1);
  else
    curr_color = 'Automatic';
    

  var color = showDialog(rootpath+"eZeditor/dialog/colorpicker.php?title=Text Color&curr_color="+curr_color, window, 370, 350);
  
  if(color)
  {   if(color=='Automatic'){
		    colorBox.style.background = 'url(' + rootpath + 'eZeditor/images/automatic.gif)';
		    colorTxt.value = 'transparent';
		  }else
		  { var regColor = /[A-Fa-f0-9]{6}/;
		    if(regColor.test(color)){
		      colorBox.style.background = color;
 					colorTxt.value = color;
		    }else{
		      colorBox.style.background = 'transparent';
					colorTxt.value = 'transparent';
				}
		  }
    	
  }
  
}

/*****************************************/
//      refreshBackgroundImgCell()
/*****************************************/
function refreshBackgroundImgCell(frm, rootpath)
{ var backgroundImgCell = document.getElementById('backgroundImgCell');

  	backgroundImgCell.style.backgroundImage    = 'url(' + rootpath + 'eZeditor/images/toolbars/bg_square.gif)';
    backgroundImgCell.style.backgroundRepeat   = frm.BackgroundRepeat.value;
    backgroundImgCell.style.backgroundPosition = frm.BgPositionTop.value + ' ' + frm.BgPositionLeft.value;
/*
  if(frm.BackgroundImageCheck.checked)
  { 

  }
  else
  { backgroundImgCell.style.backgroundImage    = '';
    backgroundImgCell.style.backgroundRepeat   = '';
    backgroundImgCell.style.backgroundPosition = '';
  }
*/  
}//end refreshBackgroundImg



function RefreshSelectList(frm, ListMatchValue, ListToRefresh, arrToSearch, arrMatchIndex, arrValueIndex, arrTextIndex){

		ListToRefresh.options.length = 1;
		var ctr = 0;

		if(ListMatchValue && ListToRefresh){		
	  for(i=0;i<arrToSearch.length;i++)
	  { 
			if( arrToSearch[i][arrMatchIndex] == ListMatchValue.value  )
			{ 
				var subValue 	=  arrToSearch[i][arrValueIndex];			
			  var subText 	=  arrToSearch[i][arrTextIndex];			

	    	ListToRefresh.options[ListToRefresh.options.length] = new Option(subText, subValue);						    		    
				ctr++;	
			} // end if			
			
	  }// end for loop
		}//end if 

}//end function

function CheckListSelected(frm, ListToCheck, ListType, ListToSelectType){

		if(ListToCheck){		
		if(WithoutSelectionValue(ListToCheck) || ListToCheck.value == 0)
	  { alert("Please select a [" + ListType + "] before selecting a [" + ListToSelectType + "].    "); 		
	 		ListToCheck.focus();
	 	}
		}else{
	   	alert("Please select a [" + ListType + "] before selecting a [" + ListToSelectType + "].    "); 	
	 		ListToCheck.focus();	
		}	
		
}//end function


function ValidateNumericInput(frm, inputVal, currVal){

		if(!currVal)
				currVal	=	0;
				
	  if(isNaN(inputVal.value))
	  { alert("Please enter numeric value only.    ");
  		inputVal.value = currVal;
  		inputVal.select();
	    return false;
	  }

	  if(inputVal.value < 0)
	  { alert("Please enter integer value [0] or above only.    ");
  		inputVal.value = currVal;
  		inputVal.select();
	    return false;
	  }

	  if(inputVal.value.indexOf('.') > -1)
	  { alert("Please enter integer value only.");
	    inputVal.value = currVal;
  		inputVal.select();
	    return false;
	  }

}//end function


function ValidateDecimalInput(frm, inputVal, currVal){

		if(!currVal)
				currVal	=	0;

	  if(isNaN(inputVal.value))
	  { alert("Please enter numeric value only.    ");
  		inputVal.value = currVal;
  		inputVal.select();
	    return false;
	  }

	  if(inputVal.value < 0)
	  { alert("Please enter integer value [0] or above only.    ");
  		inputVal.value = currVal;
  		inputVal.select();
	    return false;
	  }


}//end function


function ValidateInteger(frm, inputVal, currVal){

		if(!currVal)
				currVal	=	0;

	  if(isNaN(inputVal.value))
	  { alert("Please enter numeric value only.    ");
  		inputVal.value = currVal;
  		inputVal.select();
	    return false;
	  }

}//end function


/*************************/
/*      validateDay      */
/*************************/
function validateDay(cur, mm, dd, yy)
{ // adjust the end day of the month

  for(i=29;i<=31;i++)
  { if(!(dd.options[i]))
    { opt = new Option(i);
      dd.options[i] = opt;
    }
  }
  switch(mm.selectedIndex)
  { case  2 : dd.options[31] = null;
              dd.options[30] = null;
              var year = yy.options[yy.selectedIndex].value;
              if(!isLeapYear(year))
                dd.options[29] = null;
              break;
    case  4 : dd.options[31] = null;
        break;
    case  6 : dd.options[31] = null;
          break;
    case  9 : dd.options[31] = null;
          break;
    case 11 : 
    					dd.options[31] = null;
        break;
        
  }//end switch month


}//end validate day function

/*************************/
/*      isLeapYear       */
/*************************/
function isLeapYear(year)
{ // the value is leap year?
  var leap = false;
  if(year%400 == 0)
    leap = true;
  else if(year%100 == 0)
    leap = false;
  else if(year%4 == 0)
    leap = true;
  return leap;
}


function autoGeneratePwd(theForm)
{
    if (parseInt(navigator.appVersion) <= 3) { 
        alert("Sorry this only works in 4.0 browsers"); 
        return true; 
    }
    
    var length=8;
    var sPassword = "";
//    length = document.aForm.charLen.options[document.aForm.charLen.selectedIndex].value;
    
    var noPunction = true;
    var randomLength = false;
    
    if (randomLength) { 
        length = Math.random(); 
        
        length = parseInt(length * 100);
        length = (length % 7) + 6
    }
    
    
    for (i=0; i < length; i++) {
    
        numI = getRandomNum();
        if (noPunction) { while (checkPunc(numI)) { numI = getRandomNum(); } }
        
        sPassword = sPassword + String.fromCharCode(numI);
    }

		theForm.password.value = sPassword;
		theForm.confirm.value = sPassword;
		
    return true;

}


function getRandomNum() {
        
    // between 0 - 1
    var rndNum = Math.random()

    // rndNum from 0 - 1000    
    rndNum = parseInt(rndNum * 1000);

    // rndNum from 33 - 127        
    rndNum = (rndNum % 94) + 33;
            
    return rndNum;
}

function checkPunc(num) {
    
    if ((num >=33) && (num <=47)) { return true; }
    if ((num >=58) && (num <=64)) { return true; }    
    if ((num >=91) && (num <=96)) { return true; }
    if ((num >=123) && (num <=126)) { return true; }
    
    return false;
}



/*************************************************************************/
//
//                        SHOW / HIDE INSTRUCTIONS
//
/*************************************************************************/
var show_instructions = false;

/***************************************/
//         showHideInstructions
/***************************************/
function showHideInstructions(oImg, rootpath)
{ var oDiv = document.getElementById('div_instructions');
  show_instructions = !show_instructions;
  if(show_instructions)
  { oImg.src = rootpath + "images/buttons/topmenu_hide.gif";
    oImg.alt = "Hide Instructions";
    oDiv.style.display = "block";
  }
  else
  { oImg.src = rootpath + "images/buttons/topmenu_show.gif";
    oImg.alt = "Show Instructions";
    oDiv.style.display = "none";
  }
}



/*************************************************************************/
//
//                        IMAGE UPLOAD FUNCTIONS
//
/*************************************************************************/

var blkImg = new Image();
var objImg = new Image();

/***************************************/
//         event handler
/***************************************/
//objImg.onload = displayThumbnail;

/***************************************/
//         fileSelectChanged()
/***************************************/
function fileSelectChanged(frm, file_path, thumbObj, maxSize)
{ // reset thumbnail & image info

  //var frm = currForm;
  //var ary = frm.name.split('_');
  //var num = ary[1];
  //var thumbnail = 'thumb_' + num;

  document.images[thumbObj].src = blkImg.src;
		
  // is image file?
  var reg_type  = /\.(gif|jpg|jpe|jpeg|png|bmp)$/i;
  var file_type = '';
  if(reg_type.test( file_path.toLowerCase() ))
  { objImg.src = file_path;
    file_type = 'IMAGE';
    displayThumbnail(frm, file_path, thumbObj, maxSize);

  }
  else
  { objImg.src = blkImg.src;
    file_type = 'FILE';
  }

  if(file_path.indexOf("/") > -1)
    var ary = file_path.split("/");
  else
    var ary = file_path.split("\\");
  var file_name = ary[ary.length-1];

}// end file changed function

/***************************************/
//         displayThumbnail()
/***************************************/
function displayThumbnail(frm, file_path, thumbObj, maxSize)
{ 
	  
  var imgWidth  = objImg.width;
  var imgHeight = objImg.height;


  if(document.images[thumbObj])
  { var thumb_scale;
   
    if(imgWidth <= maxSize && imgHeight <= maxSize)
    { document.images[thumbObj].width  = imgWidth;
      document.images[thumbObj].height = imgHeight;
      thumb_scale = 100;
    }
    else if(imgWidth > imgHeight)
    { document.images[thumbObj].width  = maxSize;
      document.images[thumbObj].height = Math.round(imgHeight*maxSize/imgWidth);
      thumb_scale = Math.round(maxSize/imgWidth*100);
    }
    else
    { document.images[thumbObj].height = maxSize;
      document.images[thumbObj].width  = Math.round(imgWidth*maxSize/imgHeight);
      thumb_scale = Math.round(maxSize/imgHeight*100);
    }
    document.images[thumbObj].src = objImg.src;
 
    
  }

/*
  var imgFilesize = objImg.fileSize;
  var strFilesize = '';

  if(!imgFilesize || imgFilesize <= 0)
    strFilesize = "";
  else if(imgFilesize < 1024)
    strFilesize = imgFilesize + " bytes";
  else
    strFilesize = formatInt(Math.round(imgFilesize / 1024)) + " KB";

  if(objImg.src == blkImg.src)
  { frm.image_size.value  = '';
    frm.file_size.value   = '';
  }
  else
  { frm.image_size.value  = formatInt(imgWidth)+' x '+formatInt(imgHeight)+' pixels';
    frm.file_size.value   = strFilesize;
  }
*/

}//end display thumbnail


/***************************************/
//           formatInt()
/***************************************/
function formatInt(num)
{ // format int value : 1,234,567
  if(isNaN(num))
    num = 0;
  else
  { num += '';
    for(var i=0; i< Math.floor((num.length-(1+i))/3); i++)
      num = num.substring(0,num.length-(4*i+3))+','+
      num.substring(num.length-(4*i+3));
  }
  return num;
}//end format int function



/*************************************************************************/
//
//                     MOUSEOVER FUNCTIONS
//
/*************************************************************************/


/***************************************/
/*            imgChange()              */
/***************************************/
function imgChange(imgName, imgSrc)
{ if(document.images)
  { document.images[imgName].src = imgSrc;
  }
}


/*************************************************************************/
//
//                     CHANGE ORDER FUNCTIONS
//
/*************************************************************************/

// eg and_id = link_id
// where_id = sub_id

/***************************************/
//           changeDisplayOrder()
/***************************************/
function changeDisplayOrder(table_name, table_id_column, and_id, where_id, column_order, dir, rootpath, path, order_by_column, where_column, and_column)
{ window.location = rootpath + path + 'exec_change_order.php?' 
                                             + 'table_name=' + table_name
                                             + '&table_id_column=' + table_id_column
                                             + '&and_id=' + and_id
                                             + '&where_id=' + where_id
                                             + '&column_order=' + column_order
                                             + '&order_by_column=' + order_by_column
                                             + '&where_column=' + where_column
                                             + '&and_column=' + and_column
                                             + '&dir=' + dir;
}


