var delimiter = ":";


function MoveDept (MoveFrom, MoveTo, ToDo)
{
  var SelectFrom = eval('document.main_form.'+MoveFrom);
  var SelectTo = eval('document.main_form.'+MoveTo);
  var SelectedIndex = SelectFrom.options.selectedIndex;
  var container;
  if (ToDo=='Add') {
    container=eval('document.main_form.' + ToDo + MoveTo);
  }
  if (ToDo=='Remove') {
    container=eval('document.main_form.' + ToDo + MoveFrom);
  }
  if (SelectedIndex == -1) {
    alert("Please select a department(s) to move.");
  } else {
    for (i=0; i<SelectFrom.options.length; i ++) {
      if (SelectFrom.options[i].selected) {
        var name = SelectFrom.options[i].text;
        var ID = SelectFrom.options[i].value;
        SelectFrom.options[i] = null;
        SelectTo.options[SelectTo.options.length]= new Option (name,ID);
        i=i-1;
        if (ToDo=='Add'||ToDo=='Remove') {
          container.value=container.value+name+'^' + ID + delimiter;
        }
      }
    }
  }
}

function MoveGroup (MoveFrom, MoveTo, ToDo)
{
  var SelectFrom = eval('document.main_form.'+MoveFrom);
  var SelectTo = eval('document.main_form.'+MoveTo);
  var SelectedIndex = SelectFrom.options.selectedIndex;
  var container;
  if (ToDo=='Add') {
    container=eval('document.main_form.' + ToDo + MoveTo);
  }
  if (ToDo=='Remove') {
    container=eval('document.main_form.' + ToDo + MoveFrom);
  }
  if (SelectedIndex == -1) {
    alert("Please select a group(s) to move.");
  } else {
    for (i=0; i<SelectFrom.options.length; i ++) {
      if (SelectFrom.options[i].selected) {
        var name = SelectFrom.options[i].text;
        var ID = SelectFrom.options[i].value;
        SelectFrom.options[i] = null;
        SelectTo.options[SelectTo.options.length]= new Option (name,ID);
        i=i-1;
        if (ToDo=='Add'||ToDo=='Remove') {
          container.value=container.value+name+'^' + ID + delimiter;
        }
      }
    }
  }
}



function lockButtons(whichform) 
{
    ua = new String(navigator.userAgent);
    if (ua.match(/IE/g)) {
        for (i=1; i<whichform.elements.length; i++) {
            if ((whichform.elements[i].type == 'submit') || (whichform.elements[i].type == 'button'))
                whichform.elements[i].disabled = true;
        }
    }
    whichform.submit();
}

function openWindow()
{
    var newWin = null;
    var url = openWindow.arguments[0];
    nArgs = openWindow.arguments.length;
    var width = openWindow.arguments[1];
    var height = openWindow.arguments[2];

    //  if dynamic window size args are passed
    if (nArgs > 1)
        newWin =  window.open ("","newWindow","toolbar=no,width=" + width + ",height=" + height + ",directories=no,status=no,scrollbars=yes,resizable=no,menubar=no");
    else 
         newWin =  window.open ("","newWindow","toolbar=no,width=" + SGL_JS_WINWIDTH + ",height=" + SGL_JS_WINHEIGHT + ",directories=no,status=no,scrollbars=yes,resizable=no,menubar=no");
    newWin.location.href = url;
}

function confirmSubmit(item, formName)
{
    var evalFormName = eval('document.' + formName)
    var flag = false
    for (var count = 0; count < evalFormName.elements.length; count++) {
        var tipo = evalFormName.elements[count].type
        if (tipo == 'checkbox' && evalFormName.elements[count].checked == true && evalFormName.elements[count].name != '')
            flag = true 
    }
    if (flag == false) {
        alert('You must select an element to delete')
        return false
    }
    var agree = confirm("Are you sure you want to delete this " + item + "?");
    if (agree)
        return true;
    else
        return false;
}

function confirmSave(formName)
{
    var evalFormName = eval('document.' + formName)
    var flag = false
    for (var count = 0; count < evalFormName.elements.length; count++) {
        var tipo = evalFormName.elements[count].type
        if (tipo == 'checkbox' && evalFormName.elements[count].checked == true && evalFormName.elements[count].name != '')
            flag = true 
    }
    if (flag == false) {
        alert('You must select an element to save')
        return false
    }
}

function confirmSend(formName)
{
    var evalFormName = eval('document.' + formName)
    var flag = false
    for (var count = 0; count < evalFormName.elements.length; count++) {
        var tipo = evalFormName.elements[count].type
        if (tipo == 'checkbox' && evalFormName.elements[count].checked == true && evalFormName.elements[count].name != '')
            flag = true 
    }
    if (flag == false) {
        alert('You must select at least one recipient')
        return false
    }
}

function confirmCategoryDelete(item) 
{
    var agree = confirm("Are you sure you want to delete this " + item + "?");
    if (agree)
        return true;
    else
        return false;
}

function verifySelectionMade() 
{
    var moveForm = document.moveCategory.frmNewCatParentID
    var selectedCat = moveForm.value
    if (selectedCat == '') {
        alert('Please select a new parent category')
        return false;
    } else
        return true;
}

function checkInput(formName, fieldName)
{
    var f = eval('document.' + formName + '.' + fieldName)
    if (f.value == '') {
        alert('Please enter a value in the field before submitting');
        return false;
    } else
        return true;
}

function getSelectedValue(selectObj)
{
    return (selectObj.options[selectObj.selectedIndex].value);
}


function toggleDisplay(myElement)
{	
	boxElement = document.getElementById(myElement);

	if (boxElement.style.display == 'none') {
		boxElement.style.display = 'block';
	} else {
    	// ... otherwise collapse box
		boxElement.style.display = 'none';
	}
}

function confirmCustom(alertText, confirmText, formName)
{
    var evalFormName = eval('document.' + formName)
    var flag = false
    for (var count = 0; count < evalFormName.elements.length; count++) {
        var tipo = evalFormName.elements[count].type
        if (tipo == 'checkbox' && evalFormName.elements[count].checked == true && evalFormName.elements[count].name != '')
            flag = true 
    }
    if (flag == false) {
        alert(alertText)
        return false
    }
    var agree = confirm(confirmText);
    if (agree)
        return true;
    else
        return false;
}
function globalAlert(t,h,f){
   if(h == '' || h == undefined) var h = 'Information Alert';
   var alertTable = document.getElementById('global_alert_table');
   alertTable.tHead.rows[0].cells[0].innerHTML = h;
   if(document.getElementById('global_alert_continue') != null){
	   document.getElementById('global_alert_continue').onclick = function(){
	       document.getElementById('global_alert_shade').style.display = 'none';
		   document.getElementById('global_alert_popup').style.display = 'none';
		   if(f != undefined) f();
	   };
   }
   alertTable.tBodies[0].rows[0].cells[0].innerHTML = t;
   document.getElementById('global_alert_popup').style.display = 'block';
   document.getElementById('global_alert_shade').style.display = 'block';
}
function newuser(){
	var t = "<form name='regType'><table><tr><td>Select user account type: <td><select name='rtype' STYLE='WIDTH: 200PX;' ><option value=''>--Select--<option value='C'>Corporate</option><option value='I'>Individual</option></select><tr><td>Enter the number in the image below:</td><td><input id='cimgv' STYLE='WIDTH: 200PX;'/></td></table></form><hr /><img alt='Security Code' src='"+SGL_JS_WEBROOT+"/captcha/captcha.php?token="+Math.random()+"'/>";
	var h = "Select user account type";
	var f = function(){
	    var cimgv = document.getElementById('cimgv').value;
	    var rtype = document.forms['regType'].elements['rtype'].value;
		

		
		if(rtype == '' || Number(cimgv.length) < 1) {		
			var alertTable = document.getElementById('global_alert_table');
			alertTable.tHead.rows[0].cells[0].innerHTML = '';
			return alert('You need to select user account type, and enter the valid security number.');
		}

		if(rtype == 'C') window.location = SGL_JS_WEBROOT +'/index.php/newuser/newuser/c/'+cimgv;
		else if(rtype == 'I')  window.location = SGL_JS_WEBROOT +'/index.php/newinduser/newinduser/c/'+cimgv;
		
	}
	globalAlert(t,h,f);
}
