/***********************************************

    CODE BLOBS
    
***********************************************/

// Evaluates a code blob, replacing the tags with the arguments passed in (the additional
//  arguments are name/value pairs to replace the tags in the blob or objects to
//  evaluate).
function EvaluateCodeBlob(codeBlobName) {

      // Gets block of code from function name passed in
      var codeTag = 'code'
      var textRegion = new String(codeBlobName);
      
      var start = textRegion.toLowerCase().indexOf('<' + codeTag + '>');
      var end = textRegion.toLowerCase().indexOf('</' + codeTag + '>');
      var code = textRegion.substring(start + codeTag.length + 2, end);

      // Evaluates code blob based on arguments passed.
      // We build up an object of the items passed in
      var tags = new Object();
      
      // We need to decide if this is objects being evaluated or name/value pairs.
      var evalObjects = false;
      var iteration = 2;
      for (var i = 1; i < EvaluateCodeBlob.arguments.length; i++) {
          if (typeof(EvaluateCodeBlob.arguments[i]) == 'object') {
              evalObjects = true;
              iteration = 1;
              break;
          } else {
              break;
          }
      }
          
      // Now we need to evaluate the tags
      for (var i = 1; i < EvaluateCodeBlob.arguments.length; i+=iteration) {
          if (evalObjects) {
              // Assign the name/value pairs from the object
              var o = EvaluateCodeBlob.arguments[i];
              for (var key in o) {
                  tags[key.toLowerCase()] = o[key];
              }
          } else {
              tags[EvaluateCodeBlob.arguments[i].toLowerCase()] = EvaluateCodeBlob.arguments[i+1];
          }
      }
    
    // Pattern to match code blob tags - \x27 matches the ' character
    var tagPattern = /\[((\w+)|\x27([^\x27]+)\x27):{0,1}(\w*)\]/g;
    
    // Replace the tags
    return (code.replace(tagPattern,
            
            // $0 matches the whole string, 
            // $2 matches the first part of the tag if not quoted (field name) (or all of the tag if no ':' )
            // $3 matches the first part of the tag if quoted (hard-coded value) (or all of the tag if no ':' )
            // $4 matches the second part of the tag (function name) 
            function($0,$1,$2,$3,$4) {

                var value; 
                
                if ($2 != null) {
                    // Get the value of the tag passed in
                    value = eval('tags.' + $1.toLowerCase());
                } else {
                    // A hard coded value was supplied
                    value = $3;
                }
                
                // If no value for the tag was passed in, return the tag unchanged
                if (value == null)
                    return $0;
                
                if ($4.length > 0) {
                    // Function name was supplied, call the function
                    return eval($4 + '(value)');
                } else
                    // No function was supplied, just return the value
                    return value;
           }
           
    ));
}

function SetAsPrimary(id, url)
{
    var cell = document.getElementById('PrimaryImage_' + id);
    cell.innerHTML = '<img src="'+url+'" style="border-width: 0"/>';
}

function SetImageUrl(imageID, imageHoverUrl)
{
    var image = document.getElementById(imageID);
    
    image.src = imageHoverUrl;
 
}

var previousActionFormCellHtml = null;
function ExpandEnquiryForm(actionFormCell, horseID, horseName)
{
    previousActionFormCellHtml = document.getElementById(actionFormCell).innerHTML; 
    

    document.getElementById(actionFormCell).innerHTML = EvaluateCodeBlob(ExpandEnquiryForm_CodeBlob,
        'HorseID', horseID, 'HorseName', horseName.replace('|', '\''), 'ActionFormCell', actionFormCell);
        
    document.getElementById("NameInput_" + horseID).focus();
    document.getElementById("NameInput_" + horseID).select();
    
  
}

function ExpandEnquiryForm_CodeBlob()
{
    /*
    <Code>
    
    <table cellpadding="0px" cellspacing="0px" style="width: 100%">
                <tr>
                    <td class="Name" colspan="2">
                        Enquiry about [HorseName]
                    </td>
                </tr>
                <tr>
                    <td class="Field">
                        Your name:</td>
                    <td class="FieldValue">
                        <input id="NameInput_[HorseID]" type="text" style="width: 93%" /></td>
                </tr>
                <tr>
                    <td class="Field">
                        Phone/Mobile:</td>
                    <td class="FieldValue">
                        <input type="text" style="width: 93%" /></td>
                </tr>
                <tr>
                    <td class="Field">
                        Email:</td>
                    <td class="FieldValue">
                        <input type="text" style="width: 93%" /></td>
                </tr>
                <tr>
                    <td class="Field" style="padding-top: 5px;" colspan="2">
                        Comments
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <textarea rows="8" style="width: 95%"></textarea></td>
                </tr>
                <tr>
                    <td style="padding-bottom: 3px; padding-top: 20px;" align="center" colspan="2">
                        <span class="TextButton"><a href="#">Submit</a>
                        <a href="javascript: CancelAction('[ActionFormCell]')">Cancel</a>
                        </span></td>
                </tr>
            </table>
    </Code>
    */
}

function ExpandSubscribeNewsForm(actionFormCell, horseID, horseName)
{
    previousActionFormCellHtml = document.getElementById(actionFormCell).innerHTML; 

    document.getElementById(actionFormCell).innerHTML = EvaluateCodeBlob(ExpandSubscribeNewsForm_CodeBlob,
        'HorseID', horseID, 'HorseName', horseName.replace('|', '\''), 'ActionFormCell', actionFormCell);
        
    document.getElementById("NameInput_" + horseID).focus();
    document.getElementById("NameInput_" + horseID).select();

}

function ExpandSubscribeNewsForm_CodeBlob()
{
    /*
    <Code>
    
    <table cellpadding="0px" cellspacing="0px" style="width: 100%">
                <tr>
                    <td class="Name" colspan="2">
                        Subscribe to news on [HorseName]
                    </td>
                </tr>
                <tr>
                    <td class="Field">
                        Name:</td>
                    <td class="FieldValue">
                        <input id="NameInput_[HorseID]" type="text" style="width: 93%" /></td>
                </tr>
                <tr>
                    <td class="Field">
                        Email:</td>
                    <td class="FieldValue">
                        <input type="text" style="width: 93%" /></td>
                </tr>
                <tr>
                    <td style="padding-bottom: 3px; padding-top: 20px;" align="center" colspan="2">
                        <span class="TextButton"><a href="#">Submit</a>
                        <a href="javascript: CancelAction('[ActionFormCell]')">Cancel</a>
                        </span></td>
                </tr>
            </table>
    </Code>
    */
}

function CancelAction(actionFormCell)
{
    document.getElementById(actionFormCell).innerHTML = "";
}

