
function rate(imgObj,eEvent,iCurrentState) {
  var theRadioID = imgObj.id.split("_")[1];
  if (eEvent) {

    // preview the number of stars they would have if they clicked there
    var regionSize = (imgObj.offsetWidth/10)
    if (document.all) {
      var selectedRegion = parseInt(eEvent.offsetX/regionSize,10);
    } else {
      var selectedRegion = parseInt(eEvent.layerX/regionSize,10);
    }
    var numberOfStars = parseFloat(selectedRegion/2) + 0.5;
    var buffArr = imgObj.src.split("_stars_");
    var sImgSize = buffArr[buffArr.length-1].split(".gif")[0];
    imgObj.src = "images/" + numberOfStars + (numberOfStars==parseInt(numberOfStars,10) ? ".0" : "") + "_stars_" + sImgSize + ".gif";
    imgObj.nextSibling.innerHTML = document.getElementById(theRadioID).options[selectedRegion+1].text + " - " + numberOfStars + " stars";

    // save the number of stars to the radio buttons
    if (iCurrentState==1) {
      document.getElementById(theRadioID).selectedIndex = selectedRegion+1;
    }

  } else {

    // get the old selected number of stars from the radio buttons
    var numberOfStars = document.getElementById(theRadioID).options[document.getElementById(theRadioID).selectedIndex].value;
    var buffArr = imgObj.src.split("_stars_");
    var sImgSize = buffArr[buffArr.length-1].split(".gif")[0];
    imgObj.nextSibling.innerHTML = (numberOfStars=="none" ? "No Rating Specified" : document.getElementById(theRadioID).options[document.getElementById(theRadioID).selectedIndex].text+" - "+numberOfStars+" stars");
    if (numberOfStars=="none") numberOfStars=0;
    imgObj.src = "images/" + numberOfStars + (numberOfStars==parseInt(numberOfStars,10) ? ".0" : "") + "_stars_" + sImgSize + ".gif";

  }
}

function isEmailValid(str) {
  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  if (str.indexOf(at)==-1){
     return false
  }

  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
     return false
  }

  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
      return false
  }

   if (str.indexOf(at,(lat+1))!=-1){
      return false
   }

   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
      return false
   }

   if (str.indexOf(dot,(lat+2))==-1){
      return false
   }

   if (str.indexOf(" ")!=-1){
      return false
   }

   return true          
}

function submitStandardizedInvitation(myform, errorDivId, myEmail)
{    
  if(isInvitesValid(myform, errorDivId, false, myEmail) == false)
  {
    return;
  }

  myform.action='send_friend_invitation.htm';
  myform.submit();      
}

function submitCustomizedInvitation(myform, errorDivId, myEmail)
{    
  if(isInvitesValid(myform, errorDivId, false, myEmail) == false)
  {
    return;
  }

  myform.action='customize_invitation.htm';
  myform.submit();      
}

function submitStandardizedSendReview(myform, errorDivId, myEmail)
{    
  if(isInvitesValid(myform, errorDivId, false, myEmail) == false)
  {
    return;
  }

  myform.action='send_review.htm';
  myform.submit();      
}

function submitCustomizedSendReview(myform, errorDivId, myEmail)
{    
  if(isInvitesValid(myform, errorDivId, false, myEmail) == false)
  {
    return;
  }

  myform.action='customize_send_review.htm';
  myform.submit();      
}

function isInvitesValid(myform, errorDivId, allowEmpty, myEmail)
{
  var errorDiv = document.getElementById(errorDivId);
  errorDiv.innerHTML = "";
  var emailInvites = myform.invites.value;
  if((emailInvites.length <= 0) && (allowEmpty == false) )
  {
    errorDiv.innerHTML = "Emails can not be empty.";
    return false;
  }
  else
  {
    var tmpEmailInvites = "";
    var emailArray = emailInvites.split(/[,\r\n]/);
    var validEmailCount = 0;
    for(var index = 0; index<emailArray.length; index ++)
    {
      var tmpStr = trim(emailArray[index]);   
      if(tmpStr.length > 0)
      {
        if(isEmailValid(tmpStr) == false)
        {
          errorDiv.innerHTML = "Email(s) not valid.";
          return false;
        }
        
        if(myEmail != null)
        {
          if(tmpStr.toLowerCase() == myEmail.toLowerCase())
          {
            errorDiv.innerHTML = "You can not invite yourself.";
            return false;
          }
        }
        
        validEmailCount ++;
        if(index == 0) tmpEmailInvites = tmpStr;
        else tmpEmailInvites += ',' + tmpStr;        
      }
      
      if((validEmailCount <= 0) && (allowEmpty == false) )
      {
        errorDiv.innerHTML = "Emails can not be empty.";
        return false;
      }
      
      myform.invites.value = tmpEmailInvites;
    }
  }

  return true;
}

function trim(stringToTrim) {
  return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
  return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
  return stringToTrim.replace(/\s+$/,"");
}

function trimFields(myForm)
{
  for(var i=0; i<myForm.elements.length; i++)
  {
    var formField = myForm.elements[i];

    if(formField.type == 'text' || formField.type == 'textarea')
    {
      myForm.elements[i].value = trim(formField.value);
    }
  }
}
