// javascript functions
var win1 = null;
function ShowLargeWin(pageparms, mwidth, mheight) {
	var mleft = (document.body.clientWidth - mwidth)/2;
	var mtop = (document.body.clientHeight - mheight)/2;
	var mscale = 'top='+mtop+', left='+mleft+', scrollbars=yes, width=';
	
	if(mwidth !=0)
		mscale = mscale + mwidth + ', height=';
	else
		mscale = mscale + '500, height=';
	if(mheight !=0)
		mscale = mscale + mheight;
	else
		mscale = mscale + '400';
	
	win1 = window.open(pageparms,'largewin', mscale);
	win1.focus();
}

function CloseLargeWin() {
	win1 = window.open('','largewin');
	win1.close();
}
	
function RemoveFormatString(TargetElement, FormatString) {
  if (TargetElement.value == FormatString)
    TargetElement.value = "";
    TargetElement.select();
}

function CheckDateRange(from, to) {
  if (Date.parse(from.value) <= Date.parse(to.value))
    return true;
  else
    return false;
}

function IsValidDate(DateToCheck, FormatString) {
  var strDateToCheck;
  var strDateToCheckArray;
  var strFormatArray;
  var strFormatString;
  var strDay;
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var intDateSeparatorIdx = -1;
  var intFormatSeparatorIdx = -1;
  var strSeparatorArray = new Array("-"," ","/",".");
  var strMonthArray = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
  var intDaysArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  strDateToCheck = DateToCheck.toLowerCase();
  strFormatString = FormatString.toLowerCase();
  
  if (strDateToCheck.length != strFormatString.length) {
    return false;
  }

  for (i = 0; i < strSeparatorArray.length; i++) {
    if (strFormatString.indexOf(strSeparatorArray[i]) != -1) {
      intFormatSeparatorIdx = i;
      break;
    }
  }

  for (i = 0; i < strSeparatorArray.length; i++) {
    if (strDateToCheck.indexOf(strSeparatorArray[i]) != -1) {
      intDateSeparatorIdx = i;
      break;
    }
  }
  
  if (intDateSeparatorIdx != intFormatSeparatorIdx)
    return false;

  if (intDateSeparatorIdx != -1) {
    strFormatArray = strFormatString.split(strSeparatorArray[intFormatSeparatorIdx]);
    if (strFormatArray.length != 3) {
      return false;
    }

    strDateToCheckArray = strDateToCheck.split(strSeparatorArray[intDateSeparatorIdx]);
    if (strDateToCheckArray.length != 3) {
      return false;
    }

    for (i = 0; i < strFormatArray.length; i++) {
      if (strFormatArray[i] == 'mm' || strFormatArray[i] == 'mmm')
        strMonth = strDateToCheckArray[i];
      if (strFormatArray[i] == 'dd')
        strDay = strDateToCheckArray[i];
      if (strFormatArray[i] == 'yyyy')
        strYear = strDateToCheckArray[i];
    }
  }
  else {
    if (FormatString.length>7) {
      if (strFormatString.indexOf('mmm') == -1)
        strMonth = strDateToCheck.substring( strFormatString.indexOf('mm'), 2 );
      else
        strMonth = strDateToCheck.substring( strFormatString.indexOf('mmm'), 3 );
      strDay = strDateToCheck.substring( strFormatString.indexOf('dd'), 2 );
      strYear = strDateToCheck.substring( strFormatString.indexOf('yyyy'), 2 );
    }
    else {
      return false;
    }
  }

  if (strYear.length != 4) {
    return false;
  }
  intday = parseInt(strDay, 10);
  if (isNaN(intday)) {
    return false;
  }
  if (intday < 1) {
    return false;
  }

  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) {
    for (i = 0;i < strMonthArray.length; i++) {
      if (strMonth == strMonthArray[i]) {
        intMonth = i+1;
        break;
      }
    }
    if (isNaN(intMonth)) {
      return false;
    }
  }
  if (intMonth>12 || intMonth<1) {
    return false;
  }

  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) {
    return false;
  }
  if (IsLeapYear(intYear) == true) {
    intDaysArray[1] = 29;
  }

  if (intday > intDaysArray[intMonth - 1]) {
    return false;
  }
  
  return true;
}

function IsLeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0)
      return true;
  }
  else {
    if ((intYear % 4) == 0)
      return true;
  }

  return false;
}

function VgoDeleteConfirm() {
	msg = "Are you sure you wanna delete it and its derivatives?";
	return confirm(msg);
}

function clearText(thefield){
	if (thefield.defaultValue==thefield.value)
		thefield.value = ""
}

function resetText(thefield){
	if (thefield.value == "")
		thefield.value = thefield.defaultValue
}

// ===================================================================
// Customized VGO SelectBox 
// ===================================================================
var selBoxTimeout;

function VgoSelectBox_click(mSpanID) {
	if(document.getElementById(mSpanID).style.display == 'block') {
		document.getElementById(mSpanID).style.display='none';
	}else {
		document.getElementById(mSpanID).style.display='block';
	}
}

function VgoSelectBox_disable(mSpanID) {
	document.getElementById(mSpanID).style.display='none';
}

function VgoSelectBox_outfocus(mSpanID) {
	selBoxTimeout=setTimeout("VgoSelectBox_disable('"+mSpanID+"');", 3000);
}

function VgoSelectBox_infocus() {
	clearTimeout(selBoxTimeout);
}

// ===================================================================
// Ajax animation rollover on mouseover event
// ===================================================================
if (timeout_id) clearTimeout(timeout_id); 
var aniframes = new Array(10);
var frame = 0;          // The frame counter: keeps track of current frame
var timeout_id = null;  // Allows us to stop the animation with clearTimeout( )

// This function performs the animation. Call it once to start.
// Note that we refer to the onscreen image using its name attribute.
function animate(mImgID, countImg) {
	if (timeout_id) clearTimeout(timeout_id); 
	frame=0;
	timeout_id = null;
    timeout_id = setTimeout("animate_real('"+mImgID+"',"+countImg+")", 500);      // Display the next frame later
}

function animate_real(mImgID, countImg) {
	if(document.getElementById(mImgID).onmouseover) {
		document.getElementById(mImgID).src = aniframes[frame].src;    // Display the current frame
		frame = (frame + 1)%countImg;                           // Update the frame counter
		timeout_id = setTimeout("animate_real('"+mImgID+"',"+countImg+")", 1000);      // Display the next frame later
	}else {
		clearTimeout(timeout_id); 
		timeout_id=null;
		frame=0;
	}
}

function animate_restore(mImgID) {
	if (timeout_id) clearTimeout(timeout_id); 
	timeout_id=null;
	frame=0;
//    document.getElementById(mImgID).src = aniframes[0].src;    // Display the current frame
}
