function emailValid(iEmail) {
  var result = true;
  var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
  if (!filter.test(iEmail)) {
    result = false;
  }
  return result;
} // checkEmail

function checkInsComment(iLn, iForm) {
  var result = true;

	if (iForm.name.value == "" || iForm.comment.value == "") {
    result = false;
    alert("Name and comment are required to submit a comment.");
	} 
  if (iForm.email.value != "") { 
    if(!emailValid(iForm.email.value)) {
      result = false;	  
      alert("This does not seem to be a valid e-mail address.");	     
	  }
	}

	return result;
} // checkInsComment

// Anti-spam E-mail function
function EMail(iName, iDomain, iSuffix, iText) {
  var address = iName + "\u0040" + iDomain + "." + iSuffix;
  var url = "mailto:" + address;

  if( ! iText )
    text = address;
  else
    text = iText;

  document.write("<a href=\"" + url + "\">" + text + "</a>");
} // EMail

function EMailMe(iText) {
  EMail('webmaster', 'interestingillusions', 'com', iText);
} // EMailMe

function lookup(inputString) {
   if(inputString.length < 3) { // Type minimum 3 chars for the search box 
      $('#suggestions').fadeOut(); // Hide the suggestions box
   } else {
      $.post("/search_ajax.php", {queryString: ""+inputString+""}, function(data) { // Do an AJAX call
         $('#suggestions').fadeIn(); // Show the suggestions box
         $('#suggestions').html(data); // Fill the suggestions box
      });
   }
} // lookup

function setCookie(iName, iValue) {
  document.cookie = iName + '=' + escape(iValue) + "; path=/; expires=Wed, 31 Dec 2070 23:00:00 GMT";
} // setCookie

function getCookie(iName) {
  var ck = document.cookie;
  var vFld,tFld;
  if (ck.length<=0) return "";
  if ((vFld = ck.indexOf(iName + "=")) == -1) return "";
  if ((tFld = ck.indexOf(";",vFld)) == -1) tFld = ck.length;
  
  return unescape(ck.substring(vFld+iName.length+1,tFld));
} // getCookie

function swapImg () {
  tImage = document.swapimg.src;
  document.swapimg.src = document.swapimg.lowsrc;
  document.swapimg.lowsrc = tImage;
} // swapImg

/* Rating Begin */

function dataServer() {
};

dataServer.prototype.initialize = function() {
  try {
    // Mozilla / Safari
    this._xh = new XMLHttpRequest();
  } catch (e) {
    // Explorer
    var _ieModel = new Array(
    'MSXML2.XMLHTTP.5.0',
    'MSXML2.XMLHTTP.4.0',
    'MSXML2.XMLHTTP.3.0',
    'MSXML2.XMLHTTP',
    'Microsoft.XMLHTTP'
    );
    var success = false;
    for (var i=0;i < _ieModel.length && !success; i++) {
      try {
        this._xh = new ActiveXObject(_ieModel[i]);
        success = true;
      } catch (e) {
        // Implement exceptions
      }
    }
    if ( !success ) {
      // Implement exceptions
      return false;
    }
    return true;
  }
}

dataServer.prototype.occupied = function() {
  actualState = this._xh.readyState;
  return (actualState && (actualState < 4));
}

dataServer.prototype.process = function() {
  if (this._xh.readyState == 4 && this._xh.status == 200) {
    this.processed = true;
  }
}

dataServer.prototype.send = function(urlget,datos) {
  if (!this._xh) {
    this.initialize();
  }
  if (!this.occupied()) {
    this._xh.open("GET",urlget,false);
    this._xh.send(datos);
    if (this._xh.readyState == 4 && this._xh.status == 200) {
      return this._xh.responseText;
    }
    
  }
  return false;
}

function _gr(reqseccion,divcont) {
  remote = new dataServer;
  nt = remote.send(reqseccion,"");
  document.getElementById(divcont).innerHTML = nt;
}

function rateItem(iItem, iRating)  {
  if(getCookie(iItem)) {
    alert('You already voted for this image.');
  }
  else {  
    remote = new dataServer;
    nt = remote.send('/upd_item_rating.php?item='+iItem+'&rating='+iRating);

    switch(iRating) {
      case 1:
        ratingClass = "onestar";
        break;
      case 2:
        ratingClass = "twostar";
        break;
      case 3:
        ratingClass = "threestar";
        break;
      case 4:
        ratingClass = "fourstar";
        break;
      case 5:
        ratingClass = "fivestar";
        break;
    }

    document.getElementById('rating'+iItem).className = "rating "+ratingClass;
    document.getElementById('doVote').style.color = 'Silver';    
    document.getElementById('voteDone').style.visibility = 'visible';    
    setCookie(iItem, iRating);
  }
}

/* Rating End */
