// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


function AjaxModelSearch(event)
{
    
      var objform = document.forms[0];
      Element.extend(objform);
      var params = objform.serialize();
      //objform.disabled = true;
      
      
      document.body.style.cursor = 'wait';
    
      objform.motolistings_motomodel_id.style.backgroundColor = 'pink';
      objform.motolistings_motomodel_id.style.color = 'pink';
      new Ajax.Request('/ajaxquery/get_moto_model' , {
      method: 'post',
      parameters: params,
      onFailure:function(response)
      {
              document.body.style.cursor = 'default';
              objform.motolistings_motomodel_id.style.display = 'inline';
              alert("An error occured while processing your request, please contact the site owners and report Error: 'AMS RESPONSE FAILED'");
      },
      onSuccess:function(response)
      {
              document.body.style.cursor = 'default';
              //objform.disabled = false;
              objform.motolistings_motomodel_id.style.display = 'none';
               var isdisabled =  $("motolistings_motomodel_id").disabled;
               Event.stopObserving('motolistings_motomodel_id', 'change', AjaxSearchResultCount);
                $("motomodel").innerHTML = response.responseText;
                $("motolistings_motomodel_id").disabled = isdisabled;
                Event.observe('motolistings_motomodel_id', 'change', AjaxSearchResultCount);
      }
});
}

function AjaxModelCreate(event)
{
    
      var objform = document.forms[0];
      Element.extend(objform);
      var params = objform.serialize();

      $("motolistings_motomodel_id").disabled = true;
      $("motolisting_year").disabled = true;
      $("motolistings_modeltype_id").disabled = true;
      $("motolistings_make_id").disabled = true;
                     

      new Ajax.Request('/ajaxquery/get_moto_model' , {
      method: 'post',
      parameters: params,
      onFailure:function(response)
      {
               alert("An error occured while processing your request, please contact the site owners and report Error: 'AMC RESPONSE FAILED'");
              //alert(response.responseText);
              $("motolistings_motomodel_id").disabled = false;
              $("motolistings_modeltype_id").disabled = false;
              $("motolisting_year").disabled = false;
              $("motolistings_make_id").disabled = false;
      },
      onSuccess:function(response)
      {

                $("motomodel").innerHTML = response.responseText;
                if($("motolistings_motomodel_id").value == 0)
                  {$("motolistings_motomodel_id").disabled = true;}
                $("motolistings_modeltype_id").disabled = false;
                $("motolisting_year").disabled = false;
                $("motolistings_make_id").disabled = false;
      }
});
}

function AjaxModelEdit(event)
{
      var objform = document.forms[0];
      Element.extend(objform);
      var params = objform.serialize();


      new Ajax.Request('/ajaxquery/get_moto_model' , {
      method: 'post',
      parameters: params,
      onFailure:function(response)
      {
               alert("An error occured while processing your request, please contact the site owners and report Error: 'AME RESPONSE FAILED'");
      },
      onSuccess:function(response)
      {
                var idval = $("motolistings_motomodel_id").value;
                $("motomodel").innerHTML = response.responseText;
                for(i=0; i <  $("motolistings_motomodel_id").options.length; i++)
                {
                  if($("motolistings_motomodel_id").options[i].value == idval)
                      $("motolistings_motomodel_id").options[i].selected = true;
                }
                $("motolistings_motomodel_id").selected = idval; 
                if($("motolistings_motomodel_id").value == 0)
                  $("motolistings_motomodel_id").disabled = true;
      }     
});
}



function AjaxSearchResultCount(event)
{

  
  searching = true;
  var objform = document.forms[0];
  Element.extend(objform);
  var params = objform.serialize();

  $("searchnumber").innerHTML = '<img src="/images/ajaxsearch.gif"/>';
  
   new Ajax.Request('/ajaxquery/listing_sql_search' , {
      method: 'post',
      parameters: params,
      onFailure:function(response)
      {
            alert("An error occured while processing your request, please contact the site owners and report Error: 'ASRC RESPONSE FAILED'");
      },
      onSuccess:function(response)
      {
            $("searchnumber").innerHTML = response.responseText;
      }
});

}


function ReloadPhoto(imgid, target)
{
    $(target).getElementsBySelector('img')[0].src = "/motofront/render_img_by_id/" +imgid +"?width=100&height=75"
    $(target).getElementsBySelector('img')[0].alt = imgid;
    $("edit_"+target).innerHTML = "Remove";
}




function OkAddDialog(dlgid, target)
{
    $('dlg_txt').innerHTML = "Trying to upload the file, please wait.";
    $('btn_cancel').style.display = "none";
    $('btn_ok').style.display = "none";
    $('dlg_frame').contentWindow.document.getElementById('motoform').submit();
    $('dlg_frame').style.display = "none";
    $('dlg_loadgif').style.display = "block";
}


function OkDelDialog(dlgid, target)
{
    CancelDialog(dlgid);
    ajax_request_dest = "/ajaxquery/delete_motoimg";
    params = "?id=" + $(target).getElementsBySelector('img')[0].alt;
    params += "&pos=" + String($(target).getElementsBySelector('div')[0].id).slice(-1);

  
   new Ajax.Request(ajax_request_dest , {
      method: 'post',
      parameters: params,
      onFailure:function(response)
      {
               alert("An error occured while processing your request, please contact the site owners and report Error: 'ODD RESPONSE FAILED'");
      },
      onSuccess:function(response)
      {
          $("edit_"+target).innerHTML = "Add";
          $(target).getElementsBySelector('img')[0].src = "/images/thumbnail.png";
      }});  
}

function CancelDialog(dlgid)
{
    $(dlgid).remove();
    dlg_isopen = false;
}

var dlg_isopen = false;
function AjaxEditPhoto(event)
{

    var source_element = Event.element(event)
    var target_element =  $("image_" + String(source_element.id).slice(-1) );
    var ajax_request_dest;


      if(dlg_isopen)
         return;
    
    
      if(source_element.innerHTML == "Remove")
      {
            ajax_request_dest = "/ajaxquery/yes_no_dlg"
            params = "target=image_" + String(source_element.id).slice(-1);
      }
      
    
      if(source_element.innerHTML == "Add")
      {
              ajax_request_dest = "/ajaxquery/add_dlg?target=image_" + String(source_element.id).slice(-1) ;
              params = "target=image_" + String(source_element.id).slice(-1);
              params += "&motolistingid=" + $('motolisting_id').value; 
    
      }
    
        dlg_isopen = true;
        new Ajax.Request(ajax_request_dest , {
        method: 'post',
        parameters: params,
        onFailure:function(response)
      {
              dlg_isopen = false;
               alert("An error occured while processing your request, please contact the site owners and report Error: 'AEP RESPONSE FAILED'");
      },
        onSuccess:function(response)
        {
              new Insertion.After('middle', response.responseText);
              CenterElemObj($('dlg_dialog'));
              var newDlg = $('dlg_bgmask');
              newDlg.style.left = "0px";
              newDlg.style.top = "0px";
              newDlg.style.display ="block";
              newDlg.style.height = document.body.clientHeight + "px";
              newDlg.style.width = document.body.clientWidth + "px";
          }});     
    
    




}


function EnableToggle(event)
{
   var objid =  String(Event.element(event).id).slice(0,-6) ;
   $(objid).disabled = !Event.element(event).checked;

}




function CenterElemObj(ctnObj)
{
	
	swidth = parseInt(document.body.scrollWidth);
  sheight =parseInt(document.body.scrollHeight);
	vwidth = parseInt(ctnObj.clientWidth);
	vheight = parseInt(ctnObj.clientHeight);
	
	
	if( (newleft = ((swidth - vwidth)/2) ) > 0)
		ctnObj.style.left = newleft + 'px';
	else 
		ctnObj.style.left = '0px';
		
	if( (newheight =  ((sheight - vheight)/2)) > 0)	
		ctnObj.style.top = newheight + 'px';
	else
		ctnObj.style.top =  '0px';
	
}

function EmailCheckSend(theForm)
{
    var val_form = $(theForm);
    var regexadd =  /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i;
    
    val_form.contact_name.style.backgroundColor = "#ffffff";
    val_form.contact_email.style.backgroundColor = "#ffffff";
    val_form.contact_message.style.backgroundColor = "#ffffff";
    
    if (val_form.contact_name && val_form.contact_name.value == "")
    {
        val_form.contact_name.style.backgroundColor = "#ff0000";
        alert("You must provide your name");
        return;
    }
    
    if (val_form.contact_email && val_form.contact_email.value == "")
    {
        val_form.contact_email.style.backgroundColor = "#ff0000";
        alert("You must provide a valid email address.");
        return;
    }
    
    if (val_form.contact_email && String(val_form.contact_email.value).match(regexadd) == null)
    {
        val_form.contact_email.style.backgroundColor = "#ff0000";
        alert("[ " + val_form.contact_email.value + "] is not a valid email address. ");
        return;
    }
    
    
    if (val_form.contact_f_email && val_form.contact_f_email.value == "")
    {
        val_form.contact_f_email.style.backgroundColor = "#ff0000";
        alert("You must provide a valid email address.");
        return;
    }
    
    if (val_form.contact_f_email && String(val_form.contact_f_email.value).match(regexadd) == null)
    {
        val_form.contact_f_email.style.backgroundColor = "#ff0000";
        alert("[ " + val_form.contact_f_email.value + "] is not a valid email address. ");
        return;
    }
   
    
      if (val_form.contact_message && val_form.contact_message.value == "")
    {
        val_form.contact_message.style.backgroundColor = "#ff0000";
        alert("You must inlcude a message.");
        return;
    }
    
    val_form.submit();
      

}





