﻿var GmessageControlId = "message";
var GmessageContainerControlId = "messageContainer";
var GdateFormatString = "mm/dd/yyyy";



function showMessage(msg) {
  if ( msg == null || msg == "" )
    return;

  var ctrlMsgContainer = document.getElementById(GmessageContainerControlId);
  if ( ctrlMsgContainer == null ) {
    alert("There no no control " + GmessageContainerControlId);
    return;
  }
  
  var ctrlMsg = document.getElementById(GmessageControlId);
  if ( ctrlMsg == null ) {
    alert("There no no control " + GmessageControlId);
    return;
  }
  
  ctrlMsg.innerHTML = msg;
  ctrlMsgContainer.style.visibility = "visible";
}
function hideMessage() {
  var ctrlMsgContainer = document.getElementById(GmessageContainerControlId);
  if ( ctrlMsgContainer == null )
    return;
  ctrlMsgContainer.style.visibility = "hidden";
}
function clearMessage() {
  var ctrlMsg = document.getElementById(GmessageControlId);
  if ( ctrlMsg == null ) {
    alert("There no no control " + GmessageControlId);
    return;
  }
  
  if (ctrlMsg.innerHTML.length > 0)
    ctrlMsg.innerHTML = '';

  hideMessage();
}
/* pop up window */
function popup(link, windowName, width, height) {
  var property = 'width=' + width + ',height=' + height + ',status=no';
  window.open(link, windowName, property);  
}

/* Center windows */
function centerScreen() {
  if ( navigator.userAgent.indexOf("Gecko") > 0 ) {
    centerScreenGecko();
  } else {
    centerScreenIE();
  }
}
function centerScreenIE() {
  var w = window.document.body.offsetWidth + 6;
  var h = window.document.body.offsetHeight + 25;
  var W = screen.availWidth;
  var H = screen.availHeight - 20;
  window.moveTo((W - w) / 2, (H - h) / 2);
}
function centerScreenGecko() {
  var w = window.outerWidth;
  var h = window.outerHeight;
  var W = screen.availWidth;
  var H = screen.availHeight - 20;
  window.moveTo((W - w) / 2, (H - h) / 2);
}

function isInteger(s){
	var i;
  for (i = 0; i < s.length; i++){   
    var c = s.charAt(i);
    if (((c < "0") || (c > "9"))) return false;
  }
  return true;
}
function trim(str) {
  return str.replace(/^\s*|\s*$/g,"");
}

function showConfirm(message) {
	return confirm(message)
}

function confirmAction (event, message) {
	if (confirm(message)) {
		return;
	} else {
		if (event.preventDefault) {
			event.preventDefault();
		} else {
			event.returnValue = false;	
		}
	}
}

function handleCheckOneOnclick(form, checkBoxAll, checkBoxName) {
    var numberOfCheckBoxesInForm = 0;
    var numberOfCheckedCheckBoxes = 0;
    for (var i = 0; i < form.elements.length; i++) {
		var element = form.elements[i];
		if (element.type == "checkbox" && element.name == checkBoxName) {
			numberOfCheckBoxesInForm++;
			if (element.checked == true) {
			    numberOfCheckedCheckBoxes++;
			}
		}
	}
	var cb = document.getElementById(checkBoxAll);
	if (numberOfCheckedCheckBoxes == numberOfCheckBoxesInForm) {
	    cb.checked = true;
	} else {
	    cb.checked = false;
	}
}
function checkAll(form, checkBoxName) {
    for (var i = 0; i < form.elements.length; i++) {
        if (form.elements[i].type == "checkbox" && form.elements[i].name == checkBoxName) {
            form.elements[i].checked = true;
        }
    }
}
function uncheckAll(form, checkBoxName) {
    for (var i = 0; i < form.elements.length; i++) {
        if (form.elements[i].type == "checkbox" && form.elements[i].name == checkBoxName) {
            form.elements[i].checked = false;
        }
    }
}
function handleCheckAllOnclick(checkBoxAllId, form, checkBoxId) {
    var cb = document.getElementById(checkBoxAllId);
    if (cb.checked == true) {
        checkAll(form, checkBoxId);
    }
    else {
        uncheckAll(form, checkBoxId);
    }
}
function confirmActionOnItemsChecked(form, checkBoxName, actionName) {
    var isAnyChecked = false;
    for (var i=0; i<form.elements.length; i++)
        if ( form.elements[i].type == "checkbox" && form.elements[i].name == checkBoxName && form.elements[i].checked == true) {
            isAnyChecked = true;
        }
    if (isAnyChecked == false) {
        alert("Please choose item(s).");
        return false;
    }
    else {
        return confirm("Are you sure to " + actionName + " selected item(s) ?");
    }                
}
function confirmActiveProduct(form, checkBoxName) {
    var isAnyChecked = false;
    var count = 0;
    for (var i = 0; i < form.elements.length; i++)
        if (form.elements[i].type == "checkbox" && form.elements[i].name == checkBoxName && form.elements[i].checked == true) {
            count++;
            isAnyChecked = true;
        }
    if (isAnyChecked == false) {
        alert("Please choose a product to set active.");
        return false;
    }
    else if (count > 1) {
        alert("You can choose only one product to set active.");
        return false;
    }
    else {
        return confirm("Are you sure to set active selected product?");
    }                
}
function handleTextareaMaxlength(field, maxlimit) {
    if (field.value.length > maxlimit) field.value = field.value.substring(0, maxlimit);
}
/*******************************************************************************************************/
function closeWindow() {
    window.close();
}

function checkFreeTextBoxContent(event, ftbId, message){
    var ftbControl = document.getElementById(ftbId);
    if (ftbControl.value == null || trim(FTB_API[ftbId].GetHtml()) == "") {
        alert(message);
        preventEvent(event);
    } else {
        return true;
    }
}

function checkTextBoxContent(event, txtId, message) {
    var txtControl = document.getElementById(txtId);
    if (txtControl.value == null || trim(txtControl.value) == "") {
        alert(message);
        return false;
    } else {
        return true;
    }
}

function checkReminderEmail(event, txtId, ftbId, message1, message2) {
    var checkSubject = checkTextBoxContent(event, txtId, message1);
    if (checkSubject) {
        return checkFreeTextBoxContent(event, ftbId, message2);
    }
}

function preventEvent(event) {
    if (event.preventDefault) {
		event.preventDefault();
	} else {
		event.returnValue = false;	
	}
}

function checkCheckAllCheckBox(checkboxClass, checkboxAllId) {
    var form = document.forms[0];
    var checkboxAll = document.getElementById(checkboxAllId);
    var countNumberOfCheckBoxes = 0;
    var countCheckedCheckBoxes = 0;
    for (var i = 0; i < form.elements.length; i++) {
		var element = form.elements[i];
		if (element.type == "checkbox" && element.id && !element.disabled 
			&& element.className == checkboxClass) {
			countNumberOfCheckBoxes++;
		}
	}
	
	for (var i = 0; i < form.elements.length; i++) {
		var element = form.elements[i];
		if (element.type == "checkbox" && element.id && !element.disabled 
			&& element.className == checkboxClass && element.checked == true) {
			    countCheckedCheckBoxes++;
		}
	}
	if (countCheckedCheckBoxes == countNumberOfCheckBoxes) {
	    checkboxAll.checked = true;
	} else {
	    checkboxAll.checked = false;
	}
}
/*********************************/
function checkAllCheckBox(mainSelectBox)
{	
	var form = document.forms[0];
	var elements = form.elements;
	form.hdCount.value = 0;
	for (i = 0; i < elements.length; i++) 
	{		
		if (elements[i].type == 'checkbox' && !elements[i].disabled)
		{			
			elements[i].checked = mainSelectBox.checked;
			form.hdCount.value ++;
		}
	}
	form.hdCount.value --;
	
	if (!mainSelectBox.checked)
		form.hdCount.value = 0;
}

function checkAllCheckBox(mainSelectBox, checkBoxName)
{	
	var form = document.forms[0];
	var elements = form.elements;
	form.hdCount.value = 0;
	for (i = 0; i < elements.length; i++) 
	{		
		if (elements[i].type == 'checkbox' && elements[i].name == checkBoxName && !elements[i].disabled)
		{			
			elements[i].checked = mainSelectBox.checked;
			form.hdCount.value ++;
		}
	}
	
	if (!mainSelectBox.checked)
		form.hdCount.value = 0;
}

function checkCheckBox(mainSelectBox,selectBox)
{
	var form = document.forms[0];
	if (!selectBox.checked)
	{
		mainSelectBox.checked = false;
		form.hdCount.value --;
	}
	else
		form.hdCount.value ++;
}



/**############################################3/
/*script using in list page*/


function parentCheckBoxOnclick(parentForm, status, subCheckBoxName)
{                  
    for (var i=0; i<parentForm.elements.length; i++) {            
        if(parentForm.elements[i].type == "checkbox" && parentForm.elements[i].name == subCheckBoxName) {
            parentForm.elements[i].checked = status;
        }
    }
}

function subCheckBoxOnClick(status, parentCheckBox)
{
    if (status == false)
        parentCheckBox.checked = false;
}

function subCheckBoxOnclick(parentForm, parentCheckBoxId, subCheckBoxName) {
    var totalSubCheckBox = 0;
    var totalCheckedSubCheckBox = 0;
    
    for (var i=0; i<parentForm.elements.length; i++) {            
        if(parentForm.elements[i].type == "checkbox" && parentForm.elements[i].name == subCheckBoxName) {
            totalSubCheckBox++;
            var subCheckBox = parentForm.elements[i];
            if (subCheckBox.checked) {
                totalCheckedSubCheckBox++;
            }
        }
    }
    
    var parentCheckBox = document.getElementById(parentCheckBoxId);
    parentCheckBox.checked = (totalCheckedSubCheckBox == totalSubCheckBox);
}

function checkSubCheckBoxIsChecked(form, subCheckBoxName, actionName)
{
    
    var isAnyChecked = false;
    for(var i=0; i<form.elements.length; i++)
        if( form.elements[i].type == "checkbox" && 
            form.elements[i].name == subCheckBoxName   &&
            form.elements[i].checked == true)
            {
                isAnyChecked = true;
            }
    if (isAnyChecked == false)
    {
        alert("Please choose item(s).");
        return false;
    }
    else
    {        
        return confirm("Are you sure you want to " + actionName + " selected item(s)?");        
    }                           
}

function checkSubCheckBoxIsCheckedForDelete(form, subCheckBoxName)
{
    
    var isAnyChecked = false;
    for(var i=0; i<form.elements.length; i++)
        if( form.elements[i].type == "checkbox" && 
            form.elements[i].name == subCheckBoxName   &&
            form.elements[i].checked == true)
            {
                isAnyChecked = true;
            }
    if (isAnyChecked == false)
    {
        alert("Veuillez choisir un élément.");
        return false;
    }
    else
    {        
        return confirm("Etes-vous certain(e) de vouloir supprimer le(s) élément(s) sélectionné(s) ?");        
    }                           
}

function checkPostQuestionsToExpert(form, subCheckBoxName)
{
    
    var isAnyChecked = false;
    for(var i=0; i<form.elements.length; i++)
        if( form.elements[i].type == "checkbox" && 
            form.elements[i].name == subCheckBoxName   &&
            form.elements[i].checked == true)
            {
                isAnyChecked = true;
            }
    if (isAnyChecked == false)
    {
        alert("Please choose item(s).");
        return false;
    }
    return true;
}



// code for popup image

// Script Source: CodeLifter.com
// Copyright 2003
// Do not remove this notice.

// SETUPS:
// ===============================

// Set the horizontal and vertical position for the popup

PositionX = 100;
PositionY = 100;

// Set these value approximately 20 pixels greater than the
// size of the largest image to be used (needed for Netscape)

defaultWidth  = 300;
defaultHeight = 300;

// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows

var AutoClose = true;

// Do not edit below this line...
// ================================
if (parseInt(navigator.appVersion.charAt(0))>=4){
var isNN=(navigator.appName=="Netscape")?1:0;
var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
var optNN='scrollbars=yes,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=yes,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
function popImage(imageURL,imageTitle){

if (isNN){imgWin=window.open('about:blank','',optNN);}
if (isIE){imgWin=window.open('about:blank','',optIE);}

with (imgWin.document){
writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(100,100);');
writeln('width=100-(document.body.clientWidth-document.images[0].width);');
writeln('height=100-(document.body.clientHeight-document.images[0].height);');
writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
writeln('function doTitle(){document.title="'+imageTitle+'";}');

writeln('function centerScreen() {');
writeln('  if ( navigator.userAgent.indexOf("Gecko") > 0 ) {');
writeln('    centerScreenGecko();');
writeln('  } else {');
writeln('    centerScreenIE();');
writeln('  }');
writeln('}');
writeln('function centerScreenIE() {');
writeln('  var w = window.document.body.offsetWidth + 6;');
writeln('  var h = window.document.body.offsetHeight + 25;');
writeln('  var W = screen.availWidth;');
writeln('  var H = screen.availHeight - 20;');
writeln('  window.moveTo((W - w) / 2, (H - h) / 2);');
writeln('}');
writeln('function centerScreenGecko() {');
writeln('  var w = window.outerWidth;');
writeln('  var h = window.outerHeight;');
writeln('  var W = screen.availWidth;');
writeln('  var H = screen.availHeight - 20;');
writeln('  window.moveTo((W - w) / 2, (H - h) / 2);');
writeln('}');

writeln('</sc'+'ript>');
if (!AutoClose)  writeln('</head><body bgcolor=000000 scroll="yes" onload="reSizeToImage();doTitle();centerScreen();self.focus()">')
else writeln('</head><body bgcolor=000000 scroll="yes" onload="reSizeToImage();doTitle();centerScreen();self.focus()" onblur="">');
writeln('<img name="George" src="'+imageURL+'" style="display:block"></body></html>');
close();		
}}


// end of code popupImage

function confirmDelete(itemName) {
	var form = document.forms[0];	
	if (form.hdCount.value <= 0) {
		alert("Please select the " + itemName + " to delete.");
		return false;
	}
	else {
		return confirm("Are you sure you want to delete the selected " + itemName + "?");
	}
}

function confirmUpdateUserStatus() {		
	var count = 0;
	var checkList = document.getElementsByName("chkDisableUser");
	for (i=0; i<checkList.length; i++) {
		if (checkList[i].checked) { count++; }
	}
	if (count > 0) {
		return confirmUpdateUserStatusMessage();
	}
	checkList = document.getElementsByName("chkActivateUser");
	for (i=0; i<checkList.length; i++) {
		if (checkList[i].checked) { count++; }
	}
	if (count > 0) {
		return confirmUpdateUserStatusMessage();
	}
	alert("Please select user(s) to update status.");
	return false;
}

function confirmUpdateUserStatusMessage() {
	return confirm("Are you sure you want to update status the selected user(s)?");
}

function handleTextareaChangedEvent(field, maxlimit) {
    if (field.value.length > maxlimit) field.value = field.value.substring(0, maxlimit);
}

function encodeInputData(inputName) {
	var inputs = document.getElementsByName(inputName);
	for (var i = 0; i < inputs.length; i++) {
		inputs[i].value = encodeURIComponent(inputs[i].value);
	}
}

function handleButtonClickEvent(action, itemname) {
    return confirm("Are you sure to " + action + " this " + itemname + "?");
}