var popupWin;

function checkUncheckAll(frm, field) {
  var elements = frm.elements;
	for(var i=0; i<elements.length; i++) {
	  if(elements[i].type == "checkbox" && elements[i].disabled == false) {
		  if(field.value == "Check All") {
			  elements[i].checked = true;
			} //end if check all
			else {
			  elements[i].checked = false;
			} //else clear all
		} //end if its a checkbox element
	} //end for

	if(field.value == "Check All") {
	  field.value = " Clear All ";
	} //end if check all
	else {
	  field.value = "Check All";
	} //else clear all 
	
	return true;
} //end function check_uncheck

/***************************************/
/*            imgChange()              */
/***************************************/
function imgChange(imgName, imgSrc)
{ if(document.images)
  { document.images[imgName].src = imgSrc;
  }
}

/***************************************/
/*          imgChangeByObj()           */
/***************************************/
function imgChangeByObj(imgName, imgObj)
{ if(document.images)
  { document.images[imgName].src = imgObj.src;
  }
}


/***************************************/
//           closePopup()
/***************************************/
function closePopup()
{ if(popupWin)
    popupWin.close();
}


/***************************************/
//         openPopupWindow()
/***************************************/
function openPopupWindow(addr, winwidth, winheight, ststus, leftpos, toppos, scrollbar, tool_bar)
{ closePopup();

	if(ststus != 'yes')
    ststus = 'no';
	if(scrollbar != 'yes')
    scrollbar = 'no';
	if(tool_bar != 'yes')
    tool_bar = 'no';

	if(!leftpos)
	{ var scrwidth  = screen.availWidth
    leftpos= (scrwidth/2) -(winwidth/2);
  }
  if(!toppos)
  { var scrheight = screen.availHeight
    toppos = (scrheight/2)-(winheight/2);
  }

  popupWin = window.open(
  addr
  , 'popupWin'
  , config='width='+winwidth
  +',height='+winheight
  +',left='+leftpos
  +',top='+ toppos
  +',modal=yes' 
  +',toolbar='+tool_bar+', menubar=no, '
  +',scrollbars='+scrollbar+', resizable=no, '
  +',location=no, directories=no, status=' + ststus );
  
  popupWin.focus();
}

/*************************************************************************/
//
//                        FILE UPLOAD HANDLER
//
/*************************************************************************/
var blkImg = new Image();
var blkBannerImg = new Image();
var objImg = new Image();
var bannerImg = new Image();
var currForm;

/***************************************/
//         event handler
/***************************************/
objImg.onload = displayThumbnail;
bannerImg.onload = displayBanner;


/***************************************/
//         fileSelectChanged()
/***************************************/
function fileSelectChanged(frm, file_path)
{ // reset thumbnail & image info

  var frm = currForm;
  var ary = frm.name.split('_');
  var num = ary[1];
  var thumbnail = 'thumb_' + num;

  document.images[thumbnail].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';
  }
  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];

}

/***************************************/
//         bannerSelectChanged()
/***************************************/
function bannerSelectChanged(frm, file_path)
{ // reset thumbnail & image info

  var frm = currForm;
  var ary = frm.name.split('_');
  var num = ary[1];
  var thumbnail = 'banner_' + num;

  document.images[thumbnail].src = blkBannerImg.src;
		
  // is image file?
  var reg_type  = /\.(gif|jpg|jpe|jpeg|png|bmp)$/i;
  var file_type = '';
  if(reg_type.test( file_path.toLowerCase() ))
  { bannerImg.src = file_path;
    file_type = 'IMAGE';
  }
  else
  { bannerImg.src = blkBannerImg.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];

}




/***************************************/
//         displayThumbnail()
/***************************************/
function displayThumbnail()
{ 
	var frm = currForm;
  var ary = frm.name.split('_');
  var num = ary[1];
  
  var imgWidth  = objImg.width;
  var imgHeight = objImg.height;

  var imgName = 'thumb_' + num;

  if(document.images[imgName])
  { var thumb_scale;
    
    if(imgWidth <= maxSize && imgHeight <= maxSize)
    { document.images[imgName].width  = imgWidth;
      document.images[imgName].height = imgHeight;
      thumb_scale = 100;
    }
    else if(imgWidth > imgHeight)
    { document.images[imgName].width  = maxSize;
      document.images[imgName].height = Math.round(imgHeight*maxSize/imgWidth);
      thumb_scale = Math.round(maxSize/imgWidth*100);
    }
    else
    { document.images[imgName].height = maxSize;
      document.images[imgName].width  = Math.round(imgWidth*maxSize/imgHeight);
      thumb_scale = Math.round(maxSize/imgHeight*100);
    }
    document.images[imgName].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   = '';
    frm.thumb_scale.value = '';
  }
  else
  { frm.image_size.value  = formatInt(imgWidth)+' x '+formatInt(imgHeight)+' pixels';
    frm.file_size.value   = strFilesize;
  }
  
}


/***************************************/
//         displayBanner()
/***************************************/
function displayBanner()
{ 
	var frm = currForm;
  var ary = frm.name.split('_');
  var num = ary[1];
  
  var imgWidth  = bannerImg.width;
  var imgHeight = bannerImg.height;

  var imgName = 'banner_' + num;

  if(document.images[imgName])
  { var thumb_scale;
    
    if(imgWidth <= maxSize2 && imgHeight <= maxSize2)
    { document.images[imgName].width  = imgWidth;
      document.images[imgName].height = imgHeight;
      thumb_scale = 100;
    }
    else if(imgWidth > imgHeight)
    { document.images[imgName].width  = maxSize2;
      document.images[imgName].height = Math.round(imgHeight*maxSize2/imgWidth);
      thumb_scale = Math.round(maxSize2/imgWidth*100);
    }
    else
    { document.images[imgName].height = maxSize2;
      document.images[imgName].width  = Math.round(imgWidth*maxSize2/imgHeight);
      thumb_scale = Math.round(maxSize2/imgHeight*100);
    }
    document.images[imgName].src = bannerImg.src;
  }


  var imgFilesize = bannerImg.fileSize;
  var strFilesize = '';

  if(!imgFilesize || imgFilesize <= 0)
    strFilesize = "";
  else if(imgFilesize < 1024)
    strFilesize = imgFilesize + " bytes";
  else
    strFilesize = formatInt(Math.round(imgFilesize / 1024)) + " KB";

  if(bannerImg.src == blkBannerImg.src)
  { frm.banner_size.value  = '';
    frm.banner_file_size.value   = '';
    frm.banner_thumb_scale.value = '';
  }
  else
  { frm.banner_size.value  = formatInt(imgWidth)+' x '+formatInt(imgHeight)+' pixels';
    frm.banner_file_size.value   = strFilesize;
  }
  
}




/***************************************/
//         getFileExtension()
/***************************************/
function getFileExtension(path)
{ if(path.indexOf(".") > -1)
  { var ary = path.split('.');
    return (ary[ary.length -1]).toUpperCase();
  }
  else
    return '';
}

/***************************************/
//           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;
}


/***************************************/
//            onFocusForm()
/***************************************/
function onFocusForm(frm)
{   currForm = frm;
}

/***************************************/
//            loadDefaultImage()
/***************************************/
function loadDefaultImage(chkBox, dir)
{
  var frm = currForm;
  var ary = frm.name.split('_');
  var num = ary[1];
  var thumbnail = 'thumb_' + num;

  var imgWidth  = objImg.width;
  var imgHeight = objImg.height;

	switch(dir){
		
		case 1:	var imgSrc = rootpath + 'lib/images/properties/properties.gif';
						var orgImg = rootpath + 'lib/images/properties/' + frm.curr_image.value;	

									if(chkBox.checked)
										fileSelectChanged(frm, imgSrc)		
									else
										fileSelectChanged(frm, orgImg)		
									break;
		case 2:	var imgSrc = rootpath + 'lib/images/properties/rooms/rooms.jpg';
						var orgImg = rootpath + 'lib/images/properties/rooms/' + frm.curr_image.value;	

									if(chkBox.checked)
										fileSelectChanged(frm, imgSrc)		
									else
										fileSelectChanged(frm, orgImg)		
									break;
		case 3:	var imgSrc = rootpath + 'lib/images/packages/packages.jpg';
						var orgImg = rootpath + 'lib/images/packages/' + frm.curr_image.value;	

									if(chkBox.checked)
										fileSelectChanged(frm, imgSrc)		
									else
										fileSelectChanged(frm, orgImg)		
									break;
		case 4:	var imgSrc = rootpath + 'lib/images/searchengines/searchengines.gif';
						var orgImg = rootpath + 'lib/images/searchengines/' + frm.curr_image.value;	

									if(chkBox.checked)
										fileSelectChanged(frm, imgSrc)		
									else
										fileSelectChanged(frm, orgImg)		
									break;
		case 5:	var imgSrc = rootpath + 'lib/images/affiliates/affiliates.gif';
						var orgImg = rootpath + 'lib/images/affiliates/' + frm.curr_image.value;	

									if(chkBox.checked)
										fileSelectChanged(frm, imgSrc)		
									else
										fileSelectChanged(frm, orgImg)		
									break;									
		case 6:	var imgSrc = rootpath + 'lib/images/tours/tours.jpg';
						var orgImg = rootpath + 'lib/images/tours/' + frm.curr_image.value;	

									if(chkBox.checked)
										fileSelectChanged(frm, imgSrc)		
									else
										fileSelectChanged(frm, orgImg)		
									break;									
	}//end switch
}


/*************************/
/*      validateDay      */
/*		Modified by Aqeel  */
/*************************/
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/topmenu_hide.gif";
    oImg.alt = "Hide Instructions";
    oDiv.style.display = "block";
  }
  else
  { oImg.src = rootpath + "images/topmenu_show.gif";
    oImg.alt = "Show Instructions";
    oDiv.style.display = "none";
  }
}
