//global variable
var sngTimePerJasmatuphDefaultImages = 0.25;

//AJAX code
function refreshDataUsingAjax(aGetId,aObjectToUpdate,aObjectType)
{
 var xmlHttp;
 
 try
   {
   // Firefox, Opera 8.0+, Safari
   xmlHttp=new XMLHttpRequest();
   }
 catch (e)
   {
   // Internet Explorer
   try
     {
     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
     }
   catch (e)
     {
     try
       {
       xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
       }
     catch (e)
       {
       alert("Your browser does not support AJAX!");
       return false;
       }
     }
   }
   xmlHttp.onreadystatechange=function()
     {
     if(xmlHttp.readyState==4)
       {
          if(xmlHttp.status==200)
          {
             updateDivContent(aObjectToUpdate,xmlHttp.responseText,aObjectType);
             return true;
          }
       }
     }
   xmlHttp.open("GET",constructAjaxURLToPass(aGetId),true);
   xmlHttp.send(null);
}

function refreshDataUsingAjax_WithProcessing(aGetId,aObjectToUpdate,aObjectType)
{
 var xmlHttp;
 
 try
   {
   // Firefox, Opera 8.0+, Safari
   xmlHttp=new XMLHttpRequest();
   }
 catch (e)
   {
   // Internet Explorer
   try
     {
     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
     }
   catch (e)
     {
     try
       {
       xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
       }
     catch (e)
       {
       alert("Your browser does not support AJAX!");
       return false;
       }
     }
   }
   xmlHttp.onreadystatechange=function()
     {
     if(xmlHttp.readyState==4)
       {
          var objectToLoad = document.getElementById(aObjectToUpdate);
          
          if ((xmlHttp.status==200) && (objectToLoad != null))
          {
             updateDivContent(aObjectToUpdate,xmlHttp.responseText,aObjectType);
             setTimeout("fadeInImagesList('" + aObjectToUpdate + "','" + aObjectToUpdate.replace("_data","_processing") + "')",sngTimePerJasmatuphDefaultImages*returnImageCountInDiv(aObjectToUpdate)*1000);
             return true;
          }
       }
     }
   xmlHttp.open("GET",constructAjaxURLToPass(aGetId),true);
   xmlHttp.send(null);
}


function returnValueFromAjax(aGetId,aFieldName,aDialogBoxNumber)
{
 var xmlHttp2;
 
 try
   {
   // Firefox, Opera 8.0+, Safari
   xmlHttp2=new XMLHttpRequest();
   }
 catch (e)
   {
   // Internet Explorer
   try
     {
     xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");
     }
   catch (e)
     {
     try
       {
       xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
       }
     catch (e)
       {
       alert("Your browser does not support AJAX!");
       return false;
       }
     }
   }
   xmlHttp2.onreadystatechange=function()
     {
     if(xmlHttp2.readyState==4)
       {
          if(xmlHttp2.status==200)
          {
             updateFormFieldValue(aFieldName,xmlHttp2.responseText);
             refreshHintBox(aDialogBoxNumber);
             return true;
          }
       }
     }
   xmlHttp2.open("GET",constructAjaxURLToPass(aGetId),true);
   xmlHttp2.send(null);
}

function constructAjaxURLToPass(aGetId)
{
   switch(aGetId)
   {
      case 1 : case 9 :
         //Contact Form 
	     aNewURLToPass = "jasmatuph_code.asp?GetId=" + aGetId;
	     aNewURLToPass = aNewURLToPass + "&ContactName=" + encodeURIComponent(document.getElementById("contact_name").value);
	     aNewURLToPass = aNewURLToPass + "&EmailAddress=" + encodeURIComponent(document.getElementById("contact_emailaddress").value);
	     aNewURLToPass = aNewURLToPass + "&PhoneNumber=" + encodeURIComponent(document.getElementById("contact_phonenumber").value);
	     aNewURLToPass = aNewURLToPass + "&QueryText=" + encodeURIComponent(document.getElementById("contact_querytext").value);
	     break;
	  case 2 : case 3 :
         //Projects Form
	     aNewURLToPass = "jasmatuph_code.asp?GetId=" + aGetId;
	     aNewURLToPass = aNewURLToPass + "&ProjectStartPosition=" + document.getElementById("projectstartposition").value;
	     aNewURLToPass = aNewURLToPass + "&ProjectIncrement=" + document.getElementById("projectincrement").value;
	     aNewURLToPass = aNewURLToPass + "&ProjectSearch=" + document.getElementById("project_search").value;
	     aNewURLToPass = aNewURLToPass + "&ProjectSearchCheckMSAccessVBA=" + document.getElementById("projects_search_msaccessvba").checked;
	     aNewURLToPass = aNewURLToPass + "&ProjectSearchCheckASPVBScript=" + document.getElementById("projects_search_aspvbscript").checked;
	     aNewURLToPass = aNewURLToPass + "&ProjectSearchCheckRealBasic=" + document.getElementById("projects_search_realbasic").checked;
	     aNewURLToPass = aNewURLToPass + "&ProjectSearchCheckDVD3d=" + document.getElementById("projects_search_dvd3d").checked;
	     break;
	  case 5 : case 6 :
	     //Recommendations Form
	     aNewURLToPass = "jasmatuph_code.asp?GetId=" + aGetId;
	     aNewURLToPass = aNewURLToPass + "&RecommendationId=" + document.getElementById("recommendationId").value;
	     break;
	  case 7 : case 8 :
	     //Downloads form
	     aNewURLToPass = "jasmatuph_code.asp?GetId=" + aGetId;
	     aNewURLToPass = aNewURLToPass + "&DownloadId=" + document.getElementById("downloadId").value;
	     break;
	  case 10 : case 12 : case 13: case 14 :
	     aNewURLToPass = "jasmatuph_code.asp?GetId=" + aGetId;
	     aNewURLToPass = aNewURLToPass + "&ProjectId=" + document.getElementById("projectId").value;
	     break;
      case 11: 
	     aNewURLToPass = "jasmatuph_code.asp?GetId=" + aGetId;
	     aNewURLToPass = aNewURLToPass + "&ProjectId=" + document.getElementById("projectId").value;
	     aNewURLToPass = aNewURLToPass + "&CurrentImageNumber=" + document.getElementById("project_currentImageNumber").value;
	     break;
      case 15 :
	     //Project Image navigation
	     aNewURLToPass = "jasmatuph_code.asp?GetId=" + aGetId;
	     aNewURLToPass = aNewURLToPass + "&ProjectId=" + document.getElementById("projectId").value;
	     aNewURLToPass = aNewURLToPass + "&CurrentImageNumber=" + document.getElementById("project_currentImageNumber").value;
	     break;
      default:
   }
   
   //alert(aNewURLToPass);
   return aNewURLToPass;
}

function updateDivContent(aFormFieldName,aNewHTML,aObjectType)
{
   //window.alert(aFormFieldName + "," + aNewHTML + "," + aObjectType);
   switch (aObjectType)
   {
      case "iframe":
         //iframe
          var aObjectBody = document.getElementById(aFormFieldName).contentWindow.document.body;
          aObjectBody.innerHTML = aNewHTML;
          break;
      case "input":
         //input
         var aObjectBody = document.getElementById(aFormFieldName);
         aObjectBody.value = aNewHTML;
      default:
         //div
         var aObjectBody = document.getElementById(aFormFieldName);
         aObjectBody.innerHTML = aNewHTML;
   }

}

function showElement(objName,bolValue)
{
   if (bolValue)
   {
      objName.style.opacity = '1';
      objName.style.filter = 'alpha(opacity = 100)';
      objName.style.zoom = 1;
      objName.style.display = 'block';
      objName.style.visibility = 'visible';
   }
   else
   {
      objName.style.opacity = '0';
      objName.style.filter = 'alpha(opacity = 0)';
      objName.style.zoom = 1;
      objName.style.display = 'none';
      objName.style.visibility = 'hidden';
   }
}
   
function returnPageFeedback(aFormFieldName,aObjectType)
{
   if (aObjectType == "iframe")
   {
      document.getElementById(aFormFieldName).contentWindow.document.body.innerHTML = "Loading ...";
   }
   else
   {
      document.getElementById(aFormFieldName).innerHTML = "Loading ...";
   }
}

function resetContactForm()
{
   //fadeElement("div_contactform_feedback",2,0.2,1,0);
   document.getElementById("contact_name").value = "";
   document.getElementById("contact_emailaddress").value = "";
   document.getElementById("contact_phonenumber").value = "";
   document.getElementById("contact_querytext").value = "";
   showElement(document.getElementById("div_contactform_feedback"),false);
}

function showContactForm()
{
   //fadeElement("div_contactform_feedback",2,0.2,1,0);
   showElement(document.getElementById("div_contactform_feedback"),false);
}

function processContactForm(aGetId,aObjectToUpdate,aObjectType)
{
   showElement(document.getElementById("div_contactform_feedback"),true);
   //updateDivContent("div_contactform_feedback","Processing ... please wait","div");
   updateDivContent("div_contactform_feedback",returnProcessingPleaseWait("Processing ... please wait","images/menu/processing.gif",195,610),"div");
   refreshDataUsingAjax(9,"div_contactform_feedback",aObjectType);
   setTimeout("processContactForm_Send()",2000);
}

function processContactForm_Send()
{
   var strInnerHTML = document.getElementById("div_contactform_feedback").innerHTML;
   
   if (strInnerHTML.indexOf("Success...Processing...Please wait!",0) > -1)
   {
      refreshDataUsingAjax(1,"div_contactform_data","div");
      fadeElement("div_contactform_feedback",2,0.2,1,0);
      //showElement(document.getElementById("div_contactform_feedback"),false);
   }
}

function refreshProjectsData(intProjectStartPosition,intProjectIncrement)
{
   showElement(document.getElementById("div_projects_processing"),true);
   updateDivContent("div_projects_processing",returnProcessingPleaseWait("Processing ... please wait","images/menu/processing.gif",370,948),"div")
   document.getElementById("projectstartposition").value = intProjectStartPosition;
   document.getElementById("projectincrement").value = intProjectIncrement;
   refreshDataUsingAjax_WithProcessing(2,"div_projects_data","div");
   refreshDataUsingAjax_WithProcessing(3,"div_projects_navigation","div");
}

function refreshRecommendationsData(intRecommendationId)
{
   showElement(document.getElementById("div_recommendations_processing"),true);
   updateDivContent("div_recommendations_processing",returnProcessingPleaseWait("Processing ... please wait","images/menu/processing.gif",200,948),"div")
   document.getElementById("recommendationId").value = intRecommendationId;
   refreshDataUsingAjax_WithProcessing(5,"div_recommendations_data","div");
   refreshDataUsingAjax_WithProcessing(6,"div_recommendations_navigation","div");
}

function refreshDownloadsData(intDownloadId)
{
   showElement(document.getElementById("div_downloads_processing"),true);
   updateDivContent("div_downloads_processing",returnProcessingPleaseWait("Processing ... please wait","images/menu/processing.gif",200,948),"div")
   document.getElementById("downloadId").value = intDownloadId;
   refreshDataUsingAjax_WithProcessing(7,"div_downloads_data","div");
   refreshDataUsingAjax_WithProcessing(8,"div_downloads_navigation","div");
}

function displaySelectedProject(intProjectId)
{
   showElement(document.getElementById("div_popup_data"),true);
   document.getElementById("projectId").value = intProjectId;
   refreshDataUsingAjax_WithProcessing(10,"div_popupproject_projectname","div");
   refreshDataUsingAjax_WithProcessing(11,"div_popupproject_image","div");
   refreshDataUsingAjax_WithProcessing(12,"div_popupproject_imagefromto","div");
   refreshDataUsingAjax_WithProcessing(13,"div_popupproject_technologiesused","div");
   refreshDataUsingAjax_WithProcessing(14,"div_popupproject_description","div");
   refreshDataUsingAjax_WithProcessing(15,"div_popupproject_navigation","div");
}

function refreshImagesData(intNextImageNumber)
{
   document.getElementById("project_currentImageNumber").value = intNextImageNumber;
   refreshDataUsingAjax_WithProcessing(15,"div_popupproject_navigation","div");
   refreshDataUsingAjax_WithProcessing(11,"div_popupproject_image","div");
}

function hideSelectedProject()
{
   document.getElementById("project_currentImageNumber").value = 0;
   showElement(document.getElementById("div_popup_data"),false);
}


function returnProcessingPleaseWait(strText,strAnimatedImage,intHeight,intWidth)
{
   strProcessingPleaseWait = "<table cellpadding=0 cellspacing=0 height=" + intHeight + "px width=" + intWidth + "px><tr><td>"
   if ((strAnimatedImage.length) > 0)
   {
      strProcessingPleaseWait = strProcessingPleaseWait + "<p align=center><img src=" + strAnimatedImage + "><br>" + strText + "</p>";
   }
   else
   {
      strProcessingPleaseWait = strProcessingPleaseWait + "<p align=center>" + strText + "</p>";
   }
   
   strProcessingPleaseWait = strProcessingPleaseWait + "</td></tr></table>"
   	
   return strProcessingPleaseWait;
}

function isExisting(obj)
{
  return typeof(obj)!='undefined';
}

function convertToHTMLFormat(aString)
{
   newHTMLString3 = aString.replace(/[\r\n]+/g, "<br>");
   
   return newHTMLString3;
}

function returnImageCountInDiv(strDiv)
{
   var divImages = document.getElementById(strDiv).getElementsByTagName("img");
  
   return parseInt(divImages.length);
}

function fadeInImagesList(strDiv,strDivProcessing)
{
   if (strDiv.indexOf("_data",0) > -1)
   {
	   var divImages = document.getElementById(strDiv).getElementsByTagName("img");
	   var strImageList = "";
	   var img_index=0;
	   
	   for (img_index = 0; img_index < divImages.length; img_index++) 
	   {
 	      if (isImageLoaded(divImages[img_index]))
 	      {
 	         strImageList = strImageList + img_index + ":" + divImages[img_index].name + " (" + divImages[img_index].complete + "), ";
	      }
   	   }

	   fadeElement(strDivProcessing,2,0.2,1,0);
   }
}

function isImageLoaded(objImage)
{
   return objImage.complete;
}

function changeCursor(aStyleToChangeTo)
{
//default(arrow),pointer(hand)
   document.body.style.cursor=aStyleToChangeTo;
}

function overlayOn(aDivName,aOverlayClass)
{
	 var newDiv = document.createElement('div');
	 var mainDiv = document.getElementById(aDivName);
	 
	 newDiv.className = aOverlayClass;
	 newDiv.id = 'overlay';
	 
	 winH = document.body.offsetHeight;
	 totalHeight = document.getElementById(aDivName).offsetHeight;
	 if(winH > totalHeight) {
		newDiv.style.height = winH + "px";
	 } else {
		newDiv.style.height = (totalHeight + 15) + "px";
	 }
	
	 //newDiv.style.height = parseInt(document.body.offsetHeight + 15) +'px';
	 
	 mainDiv.appendChild(newDiv);
	 return true;
}

function overlayOff(aDivName)
{
   var mainDiv = document.getElementById(aDivName);
   
   divElement = document.getElementById('overlay');
   mainDiv.removeChild(divElement);
   
   return true;
}

function updateCurrentImageNumber(intValue)
{
   document.getElementById("project_currentImageNumber").value = intValue;
}

function clearSearchField()
{
   document.getElementById("project_search").value = "";
}

function imageRollover(aObjectId,aActionType) 
{ 
    if (aActionType == "onMouseOver")
    {
       aOriginalsrc = document.getElementById(aObjectId.id).src;
       aNewsrc = aOriginalsrc.replace(/_off/, "_on");
       document.getElementById(aObjectId.id).src = aNewsrc;
    }
    else
    {
       aOriginalsrc = document.getElementById(aObjectId.id).src;
       aNewsrc = aOriginalsrc.replace(/_on/, "_off");
       document.getElementById(aObjectId.id).src = aNewsrc;
    }    
}
