/*** ajax functions ***/
// var xmlHttp;

function addWorkToPalette( accountId, projectId, paletteId, workId )
{
   xmlHttp = GetXmlHttpObject();
   
   if( xmlHttp == null )
   {
      alert( "Your browser does not support AJAX!" );
      return;
   }
   
   var url = "common/addworktopalette.php?sid=" + Math.random() +
                                        "&accountId=" + accountId +
                                        "&projectId=" + projectId +
                                        "&paletteId=" + paletteId +
                                        "&workId=" + workId;
   
   xmlHttp.onreadystatechange = function() 
   {
      if( xmlHttp.readyState == 4 )
      {
         // document.getElementById( "selectedImages" ).innerHTML = xmlHttp.responseText;
         loadProjectsWorks( accountId, projectId )
      }
   };
   
   xmlHttp.open( "GET", url, true );
   xmlHttp.send( null );
}

function addWorkToProject( accountId, projectId, pageId, workId )
{
   xmlHttp = GetXmlHttpObject();
   
   if( xmlHttp == null )
   {
      alert( "Your browser does not support AJAX!" );
      return;
   }
   
   var url = "common/addworktoproject.php?sid=" + Math.random() +
                                        "&accountId=" + accountId +
                                        "&projectId=" + projectId +
                                        "&pageId=" + pageId +
                                        "&workId=" + workId;
   
   xmlHttp.onreadystatechange = function() 
   {
      if( xmlHttp.readyState == 4 )
      {
         // document.getElementById( "selectedImages" ).innerHTML = xmlHttp.responseText;
         loadProjectsWorks( accountId, projectId );
      }
   };
   
   xmlHttp.open( "GET", url, true );
   xmlHttp.send( null );
}

function loadPaletteSelectedWorks( accountId, projectId, paletteId )
{
   xmlHttp = GetXmlHttpObject();
   
   if( xmlHttp == null )
   {
      alert( "Your browser does not support AJAX!" );
      return;
   }
   
   var url = "common/getpalettesselectedworks.php?sid=" + Math.random() +
                                                "&accountId=" + accountId +
                                                "&projectId=" + projectId +
                                                "&paletteId=" + paletteId;
   
   xmlHttp.onreadystatechange = function() 
   {
      if( xmlHttp.readyState == 4 )
      {
         // document.getElementById( "selectedImages" ).innerHTML = xmlHttp.responseText;
      }
   };
   
   xmlHttp.open( "GET", url, true );
   xmlHttp.send( null );
}

function loadProjectSelectedWorks( accountId, projectId, pageId )
{
   xmlHttp = GetXmlHttpObject();
   
   if( xmlHttp == null )
   {
      alert( "Your browser does not support AJAX!" );
      return;
   }
   
   var url = "common/getprojectsselectedworks.php?sid=" + Math.random() +
                                                "&accountId=" + accountId +
                                                "&projectId=" + projectId +
                                                "&pageId=" + pageId;
   
   xmlHttp.onreadystatechange = function() 
   {
      if( xmlHttp.readyState == 4 )
      {
         // document.getElementById( "selectedImages" ).innerHTML = xmlHttp.responseText;
      }
   };
   
   xmlHttp.open( "GET", url, true );
   xmlHttp.send( null );
}

function loadProjectsWorks( accountId, projectId )
{
   xmlHttpLPW = GetXmlHttpObject();
   
   if( xmlHttpLPW == null )
   {
      alert( "Your browser does not support AJAX!" );
      return;
   }
   
   var url = "common/getprojectsworks.php?sid=" + Math.random() +
                                         "&accountId=" + accountId +
                                         "&projectId=" + projectId;
   
   xmlHttpLPW.onreadystatechange = function() 
   {
      if( xmlHttpLPW.readyState == 4 )
      {
         document.getElementById( "projectsWorks" ).innerHTML = xmlHttpLPW.responseText;
      }
   };
   
   xmlHttpLPW.open( "GET", url, true );
   xmlHttpLPW.send( null );
}

function loadViewProject( accountId, projectId, divId )
{
   var xmlHttpVP = GetXmlHttpObject();
   
   if( xmlHttpVP == null )
   {
      alert( "Your browser does not support AJAX!" );
      return;
   }
   
   var url = "common/getprojectsworks.php?sid=" + Math.random() +
                                         "&accountId=" + accountId +
                                         "&projectId=" + projectId +
                                         "&dblClick=no";
   
   xmlHttpVP.onreadystatechange = function() 
   {
      if( xmlHttpVP.readyState == 4 )
      {
         document.getElementById( "projectsWorks" + divId ).innerHTML = xmlHttpVP.responseText;
      }
   };
   
   xmlHttpVP.open( "GET", url, true );
   xmlHttpVP.send( null );
}

function removeWorkFromPalette( accountId, projectId, paletteId, workId )
{
   xmlHttp = GetXmlHttpObject();
   
   if( xmlHttp == null )
   {
      alert( "Your browser does not support AJAX!" );
      return;
   }
   
   var url = "common/removeworkfrompalette.php?sid=" + Math.random() +
                                             "&accountId=" + accountId +
                                             "&projectId=" + projectId +
                                             "&paletteId=" + paletteId +
                                             "&workId=" + workId;
   
   xmlHttp.onreadystatechange = function() 
   {
      if( xmlHttp.readyState == 4 )
      {
         // document.getElementById( "selectedImages" ).innerHTML = xmlHttp.responseText;
         loadProjectsWorks( accountId, projectId )
      }
   };
   
   xmlHttp.open( "GET", url, true );
   xmlHttp.send( null );
}

function removeWorkFromProject( accountId, projectId, pageId, workId )
{
   xmlHttp = GetXmlHttpObject();
   
   if( xmlHttp == null )
   {
      alert( "Your browser does not support AJAX!" );
      return;
   }
   
   var url = "common/removeworkfromproject.php?sid=" + Math.random() +
                                             "&accountId=" + accountId +
                                             "&projectId=" + projectId +
                                             "&pageId=" + pageId +
                                             "&workId=" + workId;
   
   xmlHttp.onreadystatechange = function() 
   {
      if( xmlHttp.readyState == 4 )
      {
         // document.getElementById( "selectedImages" ).innerHTML = xmlHttp.responseText;
         loadProjectsWorks( accountId, projectId )
      }
   };
   
   xmlHttp.open( "GET", url, true );
   xmlHttp.send( null );
}

function sendToBlender( accountId, projectId )
{
   xmlHttp = GetXmlHttpObject();
   
   if( xmlHttp == null )
   {
      alert( "Your browser does not support AJAX!" );
      return;
   }
   
   var url = "common/emailproject.php?sid=" + Math.random() +
                                    "&accountId=" + accountId +
                                    "&projectId=" + projectId;
   
   xmlHttp.onreadystatechange = function() 
   {
      if( xmlHttp.readyState == 4 )
      {
         document.getElementById( "sendToBlenderMsg" ).innerHTML = "<em>" + xmlHttp.responseText + "</em><br><br>";
      }
   };
   
   xmlHttp.open( "GET", url, true );
   xmlHttp.send( null );
}

function GetXmlHttpObject()
{
   var xmlHttp = null;
   
   try
   {
      // Firefox, Opera 8.0+, Safari
      xmlHttp = new XMLHttpRequest();
   }
   catch( e )
   {
      // Internet Explorer
      try
      {
         xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" );
      }
      catch( e )
      {
         xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
      }
   }
   
   return xmlHttp;
}



/*** validation functions ***/
function validateCreatePalette( theForm )
{
   var theInputs = theForm.elements;
   var count, anyBlankFields;
   
   anyBlankFields = false;
   
   for( count=0; count<theInputs.length; count++ )
   {
      if( ( theInputs[count].name == "paletteName" ) &&
          ( theInputs[count].value == "" ) )
      {
         anyBlankFields = true;
      }
   }
   
   if( anyBlankFields )
   {
      document.getElementById( "createPaletteError" ).innerHTML = "A name for your new palette is required<br><br>";
      return false;
   }
   else
   {
      return true;
   }
}

function validateCreateProject( theForm )
{
   var theInputs = theForm.elements;
   var count, anyBlankFields;
   
   anyBlankFields = false;
   
   for( count=0; count<theInputs.length; count++ )
   {
      if( ( theInputs[count].name == "projectName" ) &&
          ( theInputs[count].value == "" ) )
      {
         anyBlankFields = true;
      }
   }
   
   if( anyBlankFields )
   {
      document.getElementById( "createProjectError" ).innerHTML = "A name for your new project is required<br><br>";
      return false;
   }
   else
   {
      return true;
   }
}

function validateModifyPalette( theForm )
{
   var theInputs = theForm.elements;
   var count, anyBlankFields;
   
   anyBlankFields = false;
   
   for( count=0; count<theInputs.length; count++ )
   {
      if( ( theInputs[count].name == "paletteName" ) &&
          ( theInputs[count].value == "" ) )
      {
         anyBlankFields = true;
      }
   }
   
   if( anyBlankFields )
   {
      document.getElementById( "modifyPaletteError" ).innerHTML = "A name for your palette is required<br><br>";
      return false;
   }
   else
   {
      return true;
   }
}

function validateModifyProject( theForm )
{
   var theInputs = theForm.elements;
   var count, anyBlankFields;
   
   anyBlankFields = false;
   
   for( count=0; count<theInputs.length; count++ )
   {
      if( ( theInputs[count].name == "projectName" ) &&
          ( theInputs[count].value == "" ) )
      {
         anyBlankFields = true;
      }
   }
   
   if( anyBlankFields )
   {
      document.getElementById( "modifyProjectError" ).innerHTML = "A name for your project is required<br><br>";
      return false;
   }
   else
   {
      return true;
   }
}


/*** edit palette ***/
function editPalette( accountId, projectId, paletteId )
{
   document.getElementById( "editPalette" ).innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="750" height="450" id="squares" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="common/squares.swf?accountId=' + accountId + '&projectId=' + projectId + '&paletteId=' + paletteId + '" /><param name="menu" value="false" /><param name="quality" value="high" /><param name="scale" value="noscale" /><param name="bgcolor" value="#ffffff" /><embed src="common/squares.swf?accountId=' + accountId + '&projectId=' + projectId + '&paletteId=' + paletteId + '" menu="false" quality="high" scale="noscale" bgcolor="#ffffff" width="750" height="450" name="squares" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
}

function launchWindow( theFileName, theHeight, theWidth )
{
   var theX = ( screen.availWidth - theWidth ) / 2;
   var theY = ( screen.availHeight - theHeight ) / 2;
   
   var theFeatures = "height=" + theHeight + "," +
                     "width=" + theWidth + "," +
                     "left=" + theX + "," +
                     "top=" + theY + "," +
                     "scrollbars=0,status=0," +
                     "menubar=0,toolbar=0," +
                     "resizable=0";
   
   var theWindow = window.open( theFileName, "theWindow", theFeatures );
   theWindow.focus();
}