$(document).ready(function() {

  if ($('#casestudy').length && $('#centerSlideshow').length) {
    $('#casestudy').cycle({
      fx: 'fade',
      speed: 300,
      timeout: 5000,
      cleartypeNoBg: 1,
      cleartype: 0
    });
    $('#casestudy').cycle('pause');
  
    var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
    var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

    // Supersleight PNG fix only works with Cycle on IE6 if clearTypeNoBg and clearType are set to 1
    var clearType = 0;
    if (jQuery.browser.msie && (ie55 || ie6))
    {
      clearType = 1;
    }
    
    $('#centerSlideshow').cycle({
      fx: 'fade',
      speed: 500,
      timeout: 10000,
      pause: 1,
      cleartypeNoBg: clearType,
      cleartype: clearType
    });
  }
});





var formatDate = function (formatDate, formatString) {
    if(formatDate instanceof Date) {
        var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
        var yyyy = formatDate.getFullYear();
        var yy = yyyy.toString().substring(2);
        var m = formatDate.getMonth() + 1;
        var mm = m < 10 ? "0" + m : m;
        var mmm = months[m];
        var d = formatDate.getDate();
        var dd = d < 10 ? "0" + d : d;
 
        var h = formatDate.getHours();
        var hh = h < 10 ? "0" + h : h;
        var n = formatDate.getMinutes();
        var nn = n < 10 ? "0" + n : n;
        var s = formatDate.getSeconds();
        var ss = s < 10 ? "0" + s : s;
 
        formatString = formatString.replace(/yyyy/i, yyyy);
        formatString = formatString.replace(/yy/i, yy);
        formatString = formatString.replace(/mmm/i, mmm);
        formatString = formatString.replace(/mm/i, mm);
        formatString = formatString.replace(/m/i, m);
        formatString = formatString.replace(/dd/i, dd);
        formatString = formatString.replace(/d/i, d);
        formatString = formatString.replace(/hh/i, hh);
        formatString = formatString.replace(/h/i, h);
        formatString = formatString.replace(/nn/i, nn);
        formatString = formatString.replace(/n/i, n);
        formatString = formatString.replace(/ss/i, ss);
        formatString = formatString.replace(/s/i, s);
 
        return formatString;
    } else {
        return "";
    }
}







// AJAX form posting
var http_request = false;
function makeGETRequest(url, parameters) 
{
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
     http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType) {
        http_request.overrideMimeType('text/html');
     }
  } else if (window.ActiveXObject) { // IE
     try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
        try {
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
     }
  }
  if (!http_request) 
  {
     alert('Cannot create XMLHTTP instance');
     return false;
  }

  http_request.open('GET', url + parameters, true);
  http_request.send(null);
}







var popupForm = null;
var popupFader = null;
function disclaimerLinkPopupShow(popupName)
{
  popupFader = document.getElementById(popupName + 'Fader');
  popupForm = document.getElementById(popupName + 'Box');
  popupForm.style.display = "none";
  popupFader.style.display = "none";
  
  
  var disclaimerFader = document.getElementById('disclaimerFader');
  var disclaimerBox = document.getElementById('disclaimerBox');
  disclaimerFader.style.display = "block";
  disclaimerBox.style.display = "block";
}


function onDisclaimerClose()
{
  if (popupForm != null && popupFader != null)
  {
    popupFader.style.display = "block";
    popupForm.style.display = "block";
  }
}



/* Function to open a pre-populated email message in the user's email client */
function requestInformationEmail()
{
  var theReferrer = document.referrer;
  var email = "sales@geomatic.com.au";
  var subject = "Enquiry regarding";
  var body_message = "Hi,%0D%0DI am interested in _______ and would like further information.%0D%0DPlease contact me on __________.";
  
  var mailto_link = 'mailto:'+email+'?subject='+subject+'&body='+body_message;
  
  window.location = mailto_link;
}



function breadcrumbEmail()
{
  var theReferrer = location.href;
  var subject = "Geomatic Technologies";
  var body_message = "Hi _______,%0D%0DI thought you might be interested in the following link:%0D%0D" + theReferrer + "%0D%0D";
  
  var mailto_link = 'mailto:?subject='+subject+'&body='+body_message;
  
  window.location = mailto_link;
}




/* Validation for search button click */
function searchSubmit()
{
    var searchBox = this.search;
    this.search.value = searchBox.value.replace(/^\s+|\s+$/g, '');
    if (searchBox.value != 'Search')
    {
      this.submit();
    }
}


/* Get date/time four hours from now */
function getNewExpiryDate()
{
  var expiryDate = new Date();
  var hours = expiryDate.getHours();
  hours += 4;
  expiryDate.setHours(hours);
  return expiryDate;
}





/* Check if we've entered the shortcut value in this field */
function validateShortcut(fld)
{
  if (fld.value == "gt64")
  {
    return true;
  }
  return false;
}
 
/* Check if this field is empty */
function validateEmpty(fld)
{
  /*if (fld.value.length == 0)*/
  if (fld.value == "" || /^\s+$/.test(fld.value))
  {
    fld.style.background = 'MistyRose';
    return false;
  }
  else
  {
    fld.style.background = 'White';
  }
  return true;
}
  
/* Check if this checkbox is checked. */
function validateChecked(fld)
{
  if (fld.checked == false)
  {
    return false;
  }
  return true;
}
  





/* Download center form submit actions */
function downloadCenterFormSubmit(theForm, downloadUrl)
{
  if(validateDownloadForm(theForm))
  {
    /* Create a cookie so we don't have to do this again for X hours */
    saveDownloadCookie(theForm);
  
    /* Let them download it */
    downloadLinkClick(downloadUrl);
    
    return true;
  }
  return false;
}


/* Download center form validation */
function validateDownloadForm(theForm)
{
  if(!validateShortcut(theForm.FirstName))
  {
    var reason = "";
  
    if(!validateEmpty(theForm.FirstName))
    {
      reason += "Please enter a value for First Name.\n";
    }
    if(!validateEmpty(theForm.LastName))
    {
      reason += "Please enter a value for Last Name.\n";
    }
    if(!validateEmpty(theForm.CompanyName))
    {
      reason += "Please enter a value for Company Name.\n";
    }
    if(!validateEmpty(theForm.ContactNo))
    {
      reason += "Please enter a value for Contact No.\n";
    }
    if(!validateEmpty(theForm.EmailAddress))
    {
      reason += "Please enter a value for Email Address.\n";
    }
    if(!validateEmpty(theForm.Country))
    {
      reason += "Please enter a value for Country.\n";
    }
    if(!validateChecked(theForm.DisclaimerAgree))
    {
      reason += "You must agree to the disclaimer before downloading.";
    }
        
    if (reason != "")
    {
      alert("Please enter all required fields:\n\n" + reason, "Details required");
      return false;
    }
  }
  return true;
}
 


function saveDownloadCookie(theForm)
{
  /* Save a cookie with an expiry date window for downloading files from the site */
  //document.cookie = "expires=" + getNewExpiryDate().toUTCString() + "; path=/;";
  var expiryDate = getNewExpiryDate();
  var expiryDateStr = formatDate(expiryDate, "yyyy/MM/dd hh:m:ss");
  
  document.cookie = "expirydate=" + expiryDateStr + "; path=/;";
  
  
  // Save user details
  document.cookie = "firstname=" + theForm.FirstName.value + "; path=/;";
  document.cookie = "lastname=" + theForm.LastName.value + "; path=/;";
  document.cookie = "email=" + theForm.EmailAddress.value + "; path=/;";
  document.cookie = "company=" + theForm.CompanyName.value + "; path=/;";
  document.cookie = "country=" + theForm.Country.value + "; path=/;";
  document.cookie = "comments=" + theForm.Comments.value + "; path=/;";
  document.cookie = "contactNo=" + theForm.ContactNo.value + "; path=/;";
}




function downloadLinkClick(downloadUrl)
{
  if (isDownloadCookieValid())
  {
    // Read cookie values and email them to sales for this download
    emailPDFDetails(downloadUrl);
    
    var downloadCenterFader = document.getElementById('downloadCenterFader');
    var downloadCenterBox = document.getElementById('downloadCenterBox');
    downloadCenterFader.style.display = "none";
    downloadCenterBox.style.display = "none";
    
    window.open(downloadUrl);

    return true;
  }
  else
  {
    var downloadCenterFader = document.getElementById('downloadCenterFader');
    var downloadCenterBox = document.getElementById('downloadCenterBox');
    downloadCenterFader.style.display = "block";
    downloadCenterBox.style.display = "block";
  }
  return false;
}


function isDownloadCookieValid()
{
  /* Check for cookie */
  var cookieOK = false;
  var expiryStr = getCookie('expirydate');
  
  var nowDate = new Date();
  var expiryDate = new Date(Date.parse(expiryStr));

  if (expiryStr != "")
  {
    if (nowDate < expiryDate) {
      cookieOK = true;
    }
  }
  return cookieOK;
}


function getCookie(cookieName)
{
  if (document.cookie.length != 0)
  {
    var cookieStart=document.cookie.indexOf(cookieName + "=");
    if (cookieStart!=-1)
    {
      cookieStart=cookieStart + cookieName.length+1;
      var cookieEnd=document.cookie.indexOf(";",cookieStart);
      if (cookieEnd==-1)
        cookieEnd=document.cookie.length;
      return unescape(document.cookie.substring(cookieStart,cookieEnd));
    }
  }
  return "";
}


function getCookieValue(fieldName)
{
  if (document.cookie.length>0)
  {
    c_start=document.cookie.indexOf(fieldName + "=");
    if (c_start!=-1)
    {
    c_start=c_start + fieldName.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}


function emailPDFDetails(downloadUrl)
{
  var downloadDetailsQueryString = getEmailDownloadSendString();
  
  var emailLink = downloadDetailsQueryString + "&PDF=" + escape(downloadUrl);
  

  makeGETRequest('/send-mail.aspx', emailLink);
}


function getEmailDownloadSendString()
{
  var emailDetailsString = "?FirstName=" + getCookieValue('firstname')
      + "&LastName=" + getCookieValue('lastname')
      + "&CompanyName=" + getCookieValue('company')
      + "&EmailAddress=" + getCookieValue('email')
      + "&Country=" + getCookieValue('country')
      + "&Comments=" + getCookieValue('comments')
      + "&ContactNo=" + getCookieValue('contactNo');

  return emailDetailsString;
}











/* Subscribe to GT News form */
function subscribeToGTNewsFormSubmit(theForm)
{
  if(validateSubscribeForm(theForm))
  {
    /* Create a cookie so we don't have to do this again for X hours */
    saveSubscribeCookie(theForm);
    
    emailGTNewsDetails();
    
    var newsSubscribePopupFader = document.getElementById('newsSubscribePopupFader');
    var newsSubscribePopupBox = document.getElementById('newsSubscribePopupBox');
    newsSubscribePopupFader.style.display = "none";
    newsSubscribePopupBox.style.display = "none";
    
    return true;
  }
  return false;
}



/* Subscribe to GT News form validation */
function validateSubscribeForm(theForm)
{
  if(!validateShortcut(theForm.SubscribeFirstName))
  {
    var reason = "";
  
    if(!validateEmpty(theForm.SubscribeFirstName))
    {
      reason += "Please enter a value for First Name.\n";
    }
    if(!validateEmpty(theForm.SubscribeLastName))
    {
      reason += "Please enter a value for Last Name.\n";
    }
    if(!validateEmpty(theForm.SubscribeCompanyName))
    {
      reason += "Please enter a value for Company Name.\n";
    }
    if(!validateEmpty(theForm.SubscribeContactNo))
    {
      reason += "Please enter a value for Contact No.\n";
    }
    if(!validateEmpty(theForm.SubscribeEmailAddress))
    {
      reason += "Please enter a value for Email Address.\n";
    }
    if(!validateEmpty(theForm.SubscribeCountry))
    {
      reason += "Please enter a value for Country.\n";
    }
    if(!validateChecked(theForm.SubscribeDisclaimerAgree))
    {
      reason += "You must agree to the disclaimer before subscribing.";
    }
        
    if (reason != "")
    {
      alert("Please enter all required fields:\n\n" + reason, "Details required");
      return false;
    }
  }
  return true;
}


function saveSubscribeCookie(theForm)
{
  /* Save a cookie with an expiry date window for subscribing to GT news */
  document.cookie = "expirydatesub=" + getNewExpiryDate().toUTCString() + "; path=/;";
  
  // Save user details
  document.cookie = "firstnamesub=" + theForm.SubscribeFirstName.value + "; path=/;";
  document.cookie = "lastnamesub=" + theForm.SubscribeLastName.value + "; path=/;";
  document.cookie = "emailsub=" + theForm.SubscribeEmailAddress.value + "; path=/;";
  document.cookie = "companysub=" + theForm.SubscribeCompanyName.value + "; path=/;";
  document.cookie = "countrysub=" + theForm.SubscribeCountry.value + "; path=/;";
  document.cookie = "commentssub=" + theForm.SubscribeComments.value + "; path=/;";
  document.cookie = "contactNoSub=" + theForm.SubscribeContactNo.value + "; path=/;";
}





function subscribeGTNewsClick()
{
  if (isSubscribeCookieValid())
  {
    alert("You have already subscribed to GT News.");

    return true;
  }
  else
  {
    var newsSubscribePopupFader = document.getElementById('newsSubscribePopupFader');
    var newsSubscribePopupBox = document.getElementById('newsSubscribePopupBox');
    newsSubscribePopupFader.style.display = "block";
    newsSubscribePopupBox.style.display = "block";
  }
  return false;
}


function isSubscribeCookieValid()
{
  /* Check for cookie */
  var cookieOK = false;
  var expiryStr = getCookie('expirydatesub');
  
  var nowDate = new Date();
  var expiryDate = new Date(expiryStr);
  
  if (expiryStr != "")
  {
    if (nowDate < expiryDate) {
      cookieOK = true;
    }
  }
  return cookieOK;
}


function emailGTNewsDetails()
{
  var detailsString = getEmailSubscribeSendString();
  
  var emailLink = detailsString + "&subscribe=true";

  makeGETRequest('/send-mail.aspx', emailLink);
}


function getEmailSubscribeSendString()
{
  var emailDetailsString = "?FirstName=" + getCookieValue('firstnamesub')
      + "&LastName=" + getCookieValue('lastnamesub')
      + "&CompanyName=" + getCookieValue('companysub')
      + "&EmailAddress=" + getCookieValue('emailsub')
      + "&Country=" + getCookieValue('countrysub')
      + "&Comments=" + getCookieValue('commentssub')
      + "&ContactNo=" + getCookieValue('contactNoSub');

  return emailDetailsString;
}


















