// check that field got value
function mandatory(fieldobject,fieldname)
{
  var field = fieldobject.value;
  
  if (field != null)
  {
    if (field.replace(/ /g, "") == "") {
      alert("Please enter value for "+fieldname);
      fieldobject.focus();        
      return false;
    }
  }
  return true;
}

// check that field contain only numeric value
function validNumeric(fieldobject,fieldname, minlength){
  fieldvalue=fieldobject.value;
  fieldvalue = fieldvalue.replace(/,/g, '')
  if (minlength > 0){
    if (fieldvalue.length < minlength){
        alert("Min "+minlength+" characters for "+fieldname);
        fieldobject.focus();     
        return false;
    }
  }

  for(i=0; i<fieldvalue.length; i++){
    if (fieldvalue.charAt(i) < "0" || fieldvalue.charAt(i) > "9"){
      alert("Please correct "+fieldname+" format");
      fieldobject.focus();
      return false;
    }
  }
  return true;
}
// check that field is a valid email address
function validEmail(fieldobject){
  fieldvalue=fieldobject.value;
  if(fieldvalue.indexOf ('@',0) == -1){
    alert("Please enter a valid email address");
    fieldobject.focus();
    return false;
  }
  return true;
}

// confirmation alert
function confirmation(msg){
  var ok = confirm(msg);
  if(ok){
    return true;
  }else{
    return false;
  }
}

// mouse over effect for inactive tab
function inActiveMouseOver(name,bgcolor)
{
  var element = document.getElementsByName(name);
  for (i=0; i<element.length; i++)
  {
    element[i].style.backgroundColor = bgcolor;
    element[i].style.cursor = "pointer";
  }
}

// mouse out effect for inactive tab
function inActiveMouseOut(name)
{
  var element = document.getElementsByName(name);
  for (i=0; i<element.length; i++)
    element[i].style.backgroundColor = "white";
}

function mouseOver(name,bgcolor)
{
  var element = document.getElementById(name);
  element.style.backgroundColor = bgcolor;
  element.style.cursor = "pointer";  
}

function mouseOut(name)
{
  var element = document.getElementById(name);
  element.style.backgroundColor = "white";
}

// mouse over effect for row and check if checkbox is checked
function inActiveMouseOutCheckBox(name,theForm,theCheckBox)
{
  var element = document.getElementsByName(name);
  var theElements = document.forms[theForm].elements[theCheckBox];
  name = name.replace(/row_/g, "");

  if (theElements.length != null)
  {
    if(!theElements[name].checked)
    {
      for (i=0; i<element.length; i++)
        element[i].style.backgroundColor = "white";
    }
  }
  else
  {
    if(!theElements.checked)
    {
      for (i=0; i<element.length; i++)
        element[i].style.backgroundColor = "white";
    }
  }
}

function directURLOnClick(url)
{
  location.href = url;
}

// Check or uncheck checkboxes      
function checkToggle(theForm,theCheckbox,action) 
{
     
   var theElements = document.forms[theForm].elements[theCheckbox];
   if(!theElements)
     return;
    
   if (theElements.length != null)
   {
       for (var count = 0; count < theElements.length; count++) 
    {
      if (theElements[count].disabled != true)
              theElements[count].checked = action;
    }
   }
   else
     theElements.checked = action;
      
   if(action)
   {
     for (var i=0; theElements[i] != null; i++)
     {
      if (theElements[i].disabled != true)
        inActiveMouseOver('row_'+i,'khaki');
     }
   }
   else
   {
     for (var i=0; theElements[i] != null; i++)
      inActiveMouseOut('row_'+i);
   }

     return false;
}


// Toggle checkbox and highlight row when row is clicked
function setCheckBox(theForm,theCheckbox,count,name,bgcolor)
{
  if(!count)
      count = name.replace(/row_/g, "");
  
  var theElements = document.forms[theForm].elements[theCheckbox];
  if (theElements.length != null)
      theElements = theElements[count];

  if(theElements.checked){
    theElements.checked = false;
    inActiveMouseOut(name);
  }else{
    theElements.checked = true;
    inActiveMouseOver(name,bgcolor);
  }
}

// Highlight/unhighlight row when checkbox is checked / unchecked
function setRow(theForm,theCheckbox,count,name,bgcolor)
{
  var theElements = document.forms[theForm].elements[theCheckbox];

  if(theElements.length != null)
  {
    if(theElements[count].checked)
      inActiveMouseOver(name,bgcolor);
    else
      inActiveMouseOut(name);
  }
  else
  {
    if(theElements.checked)
      inActiveMouseOver(name,bgcolor);
    else
      inActiveMouseOut(name);
  }
}

// Submit by drop down list operations
function dropDownSubmit(theForm,theCheckbox)
{
  var theElements = document.forms[theForm].elements[theCheckbox];
  var isValid;

  // Elements checked
  if (theElements.length != null)
  {
    for(var count = 0; count < theElements.length; count++)
    {
      if (theElements[count].checked)
        isValid = true;
    }  
  }
  else
  {
    if (theElements.checked)
      isValid = true;
  }
  
  if (isValid)
  {
    if (document.forms[theForm].withSelected.options[
      document.forms[theForm].withSelected.options.selectedIndex].value == "Move")
    {
      if (!moveRecords(theForm,theCheckbox,theElements.length))
        return;
    }
    if (document.forms[theForm].withSelected.options[
      document.forms[theForm].withSelected.options.selectedIndex].value == "Delete")
    {
      if (!deleteRecords(theForm,theCheckbox,theElements.length))
        return;
    }
    if (document.forms[theForm].withSelected.options[
      document.forms[theForm].withSelected.options.selectedIndex].value == "Thumb")
    {
      if (!generateThumb(theForm,theCheckbox,theElements.length))
        return;
    }
     
    document.forms[theForm].submit();
  }
  
  // Reset to the first option if nothing is selected
  document.forms[theForm].withSelected.options[0].selected = true;
}

function adropDownSubmit(theForm,theCheckbox)
{
  var theElements = document.forms[theForm].elements[theCheckbox];
  var isValid;

  // Elements checked
  if (theElements.length != null)
  {
    for(var count = 0; count < theElements.length; count++)
    {
      if (theElements[count].checked)
        isValid = true;
    }
  }
  else
  {
    if (theElements.checked)
      isValid = true;
  }

  if (isValid)
  {
    if (document.forms[theForm].withSelected.options[
      document.forms[theForm].withSelected.options.selectedIndex].value == "Move")
    {
      if (!moveRecords(theForm,theCheckbox,theElements.length))
        return;
    }
    if (document.forms[theForm].withSelected.options[
      document.forms[theForm].withSelected.options.selectedIndex].value == "aDelete")
    {
      if (!adeleteRecords(theForm,theCheckbox,theElements.length))
        return;
    }
    if (document.forms[theForm].withSelected.options[
      document.forms[theForm].withSelected.options.selectedIndex].value == "Thumb")
    {
      if (!generateThumb(theForm,theCheckbox,theElements.length))
        return;
    }
    if (document.forms[theForm].withSelected.options[
      document.forms[theForm].withSelected.options.selectedIndex].value == "Sync")
    {
      if (confirmation("Are you sure you want to synchronize the image?"))
        document.forms[theForm].operation.value = "Sync";
      else
      {
        document.forms[theForm].withSelected.options[0].selected = true;
        return;
      }
    }
    if (document.forms[theForm].withSelected.options[
      document.forms[theForm].withSelected.options.selectedIndex].value == "Image_Permission")
    {
      document.forms[theForm].operation.value = "MultipleImagePermission";
    }


    document.forms[theForm].submit();
  }

  // Reset to the first option if nothing is selected
  document.forms[theForm].withSelected.options[0].selected = true;
}
function restore(theForm,theCheckbox)
{
    var theElements = document.forms[theForm].elements[theCheckbox];
  var isValid;
    var records= "";
  // Elements checked
  if (theElements.length != null)
  {
    for(var count = 0; count < theElements.length; count++)
    {
      if (theElements[count].checked)
      {   records += "\n" + document.forms[theForm].elements["hiddenName_"+count].value;
        isValid = true;
      }
    }
  }
  else
  {   if (theElements.checked)
        {   records += "\n" + document.forms[theForm].elements["hiddenName_0"].value;
      isValid = true;
    }
    }
     if (isValid)
  {  if (confirmation("Are you sure you want to retore the following:" + records + " ?"))
    {
            document.forms[theForm].operation.value ="Restore";
            document.forms[theForm].submit();
        }
    }
    else
    {
        alert("You need to check at least one folder to restore");
    }

}

// Submit by clicking hyperlinks in Action column
function actionSubmit(theForm,theCheckbox,index,operation)
{  
  var theElements = document.forms[theForm].elements[theCheckbox];

  checkToggle(theForm,theCheckbox,false);

  if (theElements.length != null)
    theElements[index].checked = true;  
  else
    theElements.checked = true;  
  
  inActiveMouseOver("row_"+index,"khaki");
  
  if (operation == "Edit")
    document.forms[theForm].operation.value = "Edit";
        if (operation == "aEdit")
    document.forms[theForm].operation.value = "aEdit";
  if (operation == "Delete")
  {
    if (!deleteRecords(theForm,theCheckbox))
      return;
  }
  if (operation == "aDelete")
  {
    if (!adeleteRecords(theForm,theCheckbox))
      return;
  }
  if (operation == "Thumb")
  {
    if (!generateThumb(theForm,theCheckbox))
      return;
  }
  if (operation == "Reset")
  {
    if (confirmation("An email will be sent to the user. Are you sure you want to reset the user?"))
      document.forms[theForm].operation.value = "Reset";    
    else
      return;
  }
  if (operation == "Sync")
  {
    if (confirmation("Are you sure you want to synchronize the image?"))
      document.forms[theForm].operation.value = "Sync";    
    else
      return;
  }
  if (operation == "Move")
  {
    if (!moveRecords(theForm,theCheckbox,theElements.length))
        return;
  }
  if (operation == "aMove")
  {
    if (!amoveRecords(theForm,theCheckbox,theElements.length))
        return;
  }
  
  document.forms[theForm].submit();  
}

function actionSubmitNoCheckBox(theForm,index,operation,operationIndex,indexName,string)
{  
  inActiveMouseOver("row_"+index,"khaki");
  
  if (operation == "Edit")
  {
    document.forms[theForm].operation.value = "Edit";    
    document.forms[theForm].operationIndex.value = operationIndex;    
  }
  if (operation == "Delete")
  {
    if (confirmation("Are you sure you want to delete " + string + ": " + indexName + " ?"))
    {
      document.forms[theForm].operation.value = "Delete";  
      document.forms[theForm].operationIndex.value = operationIndex;    
    }
    else
      return false;
  }
    if (operation == "aDelete")
  {
    if (confirmation("Are you sure you want to delete " + string + ": " + indexName + " ?"))
    {
      document.forms[theForm].operation.value = "aDelete";
      document.forms[theForm].operationIndex.value = operationIndex;
    }
    else
      return false;
  }
  document.forms[theForm].submit();  
}



function folderDelete(theForm,folderName)
{
  if (confirmation("All the sub folders and its contents will be deleted! Are you sure you want to delete "+folderName+"?"))
  {
    document.forms[theForm].operation.value = "Delete";    
    document.forms[theForm].submit();
  }
  else
  {
    document.forms[theForm].withSelected.options[0].selected = true;
    return false;    
  }
}

function afolderDelete(theForm,folderName)
{
  if (confirmation("All the sub folders and its contents will be deleted! Are you sure you want to delete "+folderName+"?"))
  {
    document.forms[theForm].operation.value = "aDelete";
    document.forms[theForm].submit();
  }
  else
  {
    document.forms[theForm].withSelected.options[0].selected = true;
    return false;
  }
}

function deleteRecords(theForm,theCheckbox)
{
  var records = "";
  var theElements = document.forms[theForm].elements[theCheckbox];

  if (theElements.length != null)
  {
    for (var i=0; i<theElements.length; i++)
    {
      if (theElements[i].checked)
      {
        records += "\n" + document.forms[theForm].elements["hiddenName_"+i].value;
      }
    }
  }
  else
    records += "\n" + document.forms[theForm].elements["hiddenName_0"].value;
  
  // If folderMgmt (only for folder Mgmt)
  if (document.forms[theForm].dirPath != null)  
  {
    folderDelete(theForm,records);
    return false;
  }
  else
  {
    if (confirmation("Are you sure you want to delete the following:" + records + " ?"))
    {
      document.forms[theForm].operation.value = "Delete";
      return true;
    }
    else
    {
      document.forms[theForm].withSelected.options[0].selected = true;
      return false;
    }
  }
}

function adeleteRecords(theForm,theCheckbox)
{
  var records = "";
  var theElements = document.forms[theForm].elements[theCheckbox];

  if (theElements.length != null)
  {
    for (var i=0; i<theElements.length; i++)
    {
      if (theElements[i].checked)
      {
        records += "\n" + document.forms[theForm].elements["hiddenName_"+i].value;
      }
    }
  }
  else
    records += "\n" + document.forms[theForm].elements["hiddenName_0"].value;

  // If folderMgmt (only for folder Mgmt)
  if (document.forms[theForm].dirPath != null)
  {
    afolderDelete(theForm,records);
    return false;
  }
  else
  {
    if (confirmation("Are you sure you want to delete the following:" + records + " ?"))
    {
      document.forms[theForm].operation.value = "aDelete";
      return true;
    }
    else
    {
      document.forms[theForm].withSelected.options[0].selected = true;
      return false;
    }
  }
}

function moveRecords(theForm,theCheckbox)
{
  var records = "";
  var recordsID = "";
  var windowObject;
        var f = document.forms[theForm];
  var theElements = f.elements[theCheckbox];

  if (theElements.length != null)
  {
    for (var i=0; i<theElements.length; i++)
    {
      if (theElements[i].checked)
      {
        records += "\n" + f.elements["hiddenName_"+i].value;
        recordsID +=  f.elements["hidden_"+i].value + " ";
      }
    }
  }
  else
  {
    records += "\n" + f.elements["hiddenName_0"].value;
    recordsID += f.elements["hidden_0"].value;
  }
  // records is the name of folder to be moved
  
  if (confirmation("Are you sure you want to move the following:" + records + " ?"))
  {
    //windowObject = window.open("../folder/folderSelection.jsp?id="+f.parent.value+"&ids="+recordsID,"NewWindow","HEIGHT=400,WIDTH=600,TOP=200,LEFT=200,DEPENDENT=YES,SCROLLBARS=YES");
    windowObject = dhtmlwindow.open("folderSelection", "iframe", "../folder/folderSelection.jsp?id="+f.parent.value+"&ids="+recordsID,
        "Folder Selection","height=400,width=600,resize=1,scrolling=1,center=1", "recal");
    f.operation.value = "Move";
    f.withSelected.options[0].selected = true;
    return false;
  }
  else
  {
    f.withSelected.options[0].selected = true;
    return false;
  }

}

function amoveRecords(theForm,theCheckbox)
{
  var records = "";
  var recordsID = "";
  var windowObject;
  var theElements = document.forms[theForm].elements[theCheckbox];

  if (theElements.length != null)
  {
    for (var i=0; i<theElements.length; i++)
    {
      if (theElements[i].checked)
      {
        records += "\n" + document.forms[theForm].elements["hiddenName_"+i].value;
        recordsID +=  document.forms[theForm].elements["hidden_"+i].value + " ";
      }
    }
  }
  else
  {
    records += "\n" + document.forms[theForm].elements["hiddenName_0"].value;
    recordsID += document.forms[theForm].elements["hidden_0"].value;
  }
  // records is the name of folder to be moved

  if (confirmation("Are you sure you want to move the following:" + records + " ?"))
  {
    windowObject = window.open("archiveFolderMove.php?f="+theForm+"&fName="+records+"&fID="+recordsID,"NewWindow","HEIGHT=400,WIDTH=600,TOP=200,LEFT=200,DEPENDENT=YES,SCROLLBARS=YES");
    document.forms[theForm].operation.value = "aMove";
    document.forms[theForm].withSelected.options[0].selected = true;
    return false;
  }
  else
  {
    document.forms[theForm].withSelected.options[0].selected = true;
    return false;
  }

}

function generateThumb(theForm,theCheckbox)
{
  var records = "";
  var theElements = document.forms[theForm].elements[theCheckbox];

  if (theElements.length != null)
  {
    for (var i=0; i<theElements.length; i++)
    {
      if (theElements[i].checked)
      {
        records += "\n" + document.forms[theForm].elements["hiddenName_"+i].value;
      }
    }
  }
  else
    records += "\n" + document.forms[theForm].elements["hiddenName_0"].value;
  
  if (confirmation("Are you sure you want to generate thumbnails for the following:" + records + " ?"))
  {
    document.forms[theForm].operation.value = "Thumb";    
    return true;
  }
  else
  {
    document.forms[theForm].withSelected.options[0].selected = true;
    return false;
  }

}

function trim(s) {
  while (s.substring(0,1) == ' ')
    s = s.substring(1,s.length);
  while (s.substring(s.length-1,s.length) == ' ') 
    s = s.substring(0,s.length-1);
  return s;
}

function termsAndCondition(frm)
{      
    window.open("tnc.html","NewWindow","HEIGHT=600,WIDTH=820,SCROLLBARS=YES");
}
  
function privacypolicy(frm)
{      
    window.open("privacy_policy.html","NewWindow","HEIGHT=600,WIDTH=820,SCROLLBARS=YES");
}

function fSelectOption(value1,value2)
{
        If (value1 == value2)
      return "selected"; 
}

  
function explode( delim, string ) 
{
  var piecesArray = string.split( " " );
    var str ="";
    for ( var i = 0; i < piecesArray.length; i++ ) 
  {
      str[i] = arr[i];
    }
    
    return str;
} 

   
  
// Querystring functions
function PageQuery(q) 
{
  if(q.length > 1) 
    this.q = q.substring(1, q.length);
  else 
    this.q = null;

  this.keyValuePairs = new Array();
  if(q) 
  {
    for(var i=0; i < this.q.split("&").length; i++) 
    {
      this.keyValuePairs[i] = this.q.split("&")[i];
    }
  }
  this.getKeyValuePairs = function() { return this.keyValuePairs; }
  this.getValue = function(s) 
          {
            for(var j=0; j < this.keyValuePairs.length; j++) 
            {
              if(this.keyValuePairs[j].split("=")[0] == s)
                return this.keyValuePairs[j].split("=")[1];
            }
            return false;
          }
  this.getParameters = function() 
            {
              var a = new Array(this.getLength());
              for(var j=0; j < this.keyValuePairs.length; j++) 
              {
                a[j] = this.keyValuePairs[j].split("=")[0];
              }
              return a;
            }
  this.getLength = function() 
          { 
            return this.keyValuePairs.length; 
          } 
}

function queryString(key)
{
  var page = new PageQuery(window.location.search); 
  return unescape(page.getValue(key)); 
}

function displayItem(key)
{
  if(queryString(key)=='false') 
  {
    return ("you didn't enter a ?name=value querystring item.");
  }
  else
  {
    return (queryString(key));
  }
}
  
  
  
  
function popWin1(){
  win1=window.open("loading.htm","uploading","height=150,width=350,left=250,top=250,scrollbars=no,resizable=no");
}

function closeDep1() {
  if (win1) {
     if (win1.open && !win1.closed) {
        win1.close();
     }
  }
}

function check() {
  len = document.f1.elements.length;
  for(i=0; i<len; i++) {
    if ((document.f1.elements[i].name)=='userfile[]'){
      
      // check for invalid image name
      imgName = document.f1.elements[i].value;
      chk = /'|&|%/;
      if (chk.test(imgName) == true){
        alert("Image name contains invalid characters.\nPlease only use alphabets or numbers for image name");
         return false;
      }
      
      // check for invalid extension
      ext = document.f1.elements[i].value;
      ext = ext.substring(ext.length-3,ext.length);
      if (ext=='') continue;
      ext = ext.toLowerCase();
      
      if (ext != 'jpg' && ext != 'gif' && ext != 'avi' && ext != 'tif' && ext != 'iff' && ext != 'jvu' && ext!='mpg' && ext!='peg' && ext != 'wmv' && ext!='psd' && ext!='pdf'){
        alert('You have selected an invalid file. Please select a .jpg, .gif, .avi, .mpg, .mpeg, .wmv, .psd, .pdf .tiff or .djvu file');
          return false;
      }
    }
  }
  //popWin1();
  return true;
}

/**
 *  @param isAlias: if isAlias, it's less restrict, i.e. can contain space, ' etc. otherwise is 
 *                  it's more strict as it will used as file name in the file system.
 */
function validNameField(field, fieldName, isAlias, optional)
{
  field.value = trim(field.value);
  if (field.value == "" && !optional) {
    message= "Please enter value for "+ fieldName;
    alert(message);
    field.focus();
    return false;
  }

  var pattern = /\'|\"|&|%|#|@|=|<|>|`|:|;|,|\^|\[|\]|\{|\}|\\|\?|\!|\(|\)|\/|\+|\*|\'/;
  var message = fieldName + " contains invalid characters! Characters such as \' \" & % # @ = < > ` ' : ; ^ \ [ ] { } ? ! ( ) / + * are not allowed";
  if(isAlias){
    pattern = /\'|\"|%|=|<|>|:|;|\^|\[|\]|\{|\}|\\|\?|\!|\*/;
    message = fieldName + " contains invalid characters! Characters such as  \' \" % = < > : ; ^ \ [ ] { } ? ! * are not allowed";
  }else{
    if(field.value.indexOf(' ') >= 0){
      alert(" Spaces are not allowed for " + fieldName);
      field.focus();
      return false;
    }
  }

  if (pattern.test(trim(field.value)) == true){
    alert(message);
    field.focus();
    return false;
  }

  return true;
}

function validDescriptionField(field)
{
  field.value = trim(field.value);
  if (field.value == "") 
    return true;
  if(field.value.indexOf('"') >= 0){
    alert(" Double quotes are not allowed for description!");
    field.focus();
    return false;
  }
  return true;
}