﻿  function InitializeSjs(control, regType)
  {
    var sjs = new Object();
    control.sjs = sjs;
    sjs.root = control;
    sjs.KeywordsEmpty = 'keywords';
    sjs.LocationEmpty = 'county, city or postcode';
    
    sjs.RegType = function(value)
    {
      if (value==undefined)
        if (sjs.regType==undefined || sjs.regType==null || sjs.regType=='')
          return 0;//undefined
        else
          return sjs.regType;
      else if (value==null || value=='')
        sjs.regType = 0;//undefined
      else
        sjs.regType = value;
    };
    sjs.RegType(regType);
    
    sjs.Email = function(value)
    {
      var control = sjs.root;
      if (!control || control==null)
        return;
      var targetElement = $('input.sjsEmail', control);
      if (!targetElement[0])
        return;
        
      if (value==undefined)
        return targetElement.val();
      else
        targetElement.value(value);
    };
  
    sjs.Keywords = function(value)
    {
      var control = sjs.root;
      if (!control || control==null)
        return;
      var targetElement = $('input.sjsKeywords', control);
      if (!targetElement[0])
        return;
        
      if (value==undefined)
      {
        var result = targetElement.val();
        if (result == sjs.KeywordsEmpty)
          result = '';
        return result;
      } 
      else if (value==null || value=='')
      {
        targetElement.val(sjs.KeywordsEmpty);
        targetElement[0].style.color="Gray";
      }  
      else
        targetElement.val(value);
    };
    
    sjs.Location = function(value)
    {
      var control = sjs.root;
      if (!control || control==null)
        return;
      var targetElement = $('input.sjsLocation', control);
      if (!targetElement[0])
        return;
        
      if (value==undefined)
      {
        var result = targetElement.val();
        if (result == sjs.LocationEmpty)
          result = '';
        return result;
      } 
      else if (value==null || value=='')
      {
        targetElement.val(sjs.LocationEmpty);
        targetElement[0].style.color="Gray";
        var distElem = $('select.sjsDistance', control)[0];
        if (distElem != null)
        {
          distElem.selectedIndex=0;
          distElem.disabled=true;
        } 
      }  
      else
        targetElement.val(value);
    };
    
    sjs.Category = function(value)
    {
      var control = sjs.root;
      if (!control || control==null)
        return;
      var targetElement = $('select.sjsCategory', control);
      if (!targetElement[0])
        return;
        
      if (value==undefined)
        return $(targetElement).selectedValues()[0];
      else if (value==null || value=='')
        $(targetElement).selectOptions(0, true);
      else
        $(targetElement).selectOptions(value, true);
    }
    
    sjs.Distance = function(value)
    {
      var control = sjs.root;
      if (!control || control==null)
        return;
      var targetElement = $('select.sjsDistance', control);
      if (!targetElement[0])
        return;
        
      if (value==undefined)
      {
        var result = $(targetElement).selectedValues()[0];
        if (result=='')
          result=0;
        return result;
      }  
      else if (value==null || value=='')
        $(targetElement).selectOptions(0, true);
      else
        $(targetElement).selectOptions(value, true);
    }
    
    sjs.DeliverDays = function(value)
    {
      var control = sjs.root;
      if (!control || control==null)
        return;
      var targetElement = $('span#daySelecter', control);
      if (!targetElement[0])
        return;
       
      if (value==undefined)
        return targetElement[0].daySelecter.SelectedDays();
      else
        targetElement[0].daySelecter.SelectedDays(value);
    };
        
    sjs.Validate = function()
    {
      var email=sjs.Email();
      if (email==undefined || email==null || email=='')
      {
        alert('You should to enter email address!');
        arguments.IsValid = false;
        return false;
      }
      var keywords = sjs.Keywords();
      var location = sjs.Location();
      var catElement = $('select.sjsCategory', sjs.root);
      if (keywords=='' && location=='' && catElement[0].selectedIndex==0)
      {
        alert('You should to fill any of the following fields - Keywords, Location or Category');
        arguments.IsValid = false;
        return false;
      }
      else
      {
        arguments.IsValid = true;
        return true;
      } 
    };
    
    sjs.SaveJobAlertCallback = function (res)
    {
      if(res.error != null)
        alert(res.error);
      else
        alert(res.value);
    }
        
    sjs.SaveJobAlert = function()
    {
      if (sjs.Validate())
        SJS.AjSaveJobAlert(sjs.Email(), sjs.Keywords(), sjs.Category(),
        sjs.Location(), sjs.Distance(), sjs.RegType(), sjs.DeliverDays(), sjs.SaveJobAlertCallback);
      return false;  
    }

    SetSjsHandlers(control);
    SetCurrentSjsParameters(control);
  }
  
  function SetSjsHandlers(control)
  {
    var sjs = control.sjs;
    var keywElem = $('input.sjsKeywords', control);
    keywElem.click(function()
    {
      sjsField_onclick(keywElem, sjs.KeywordsEmpty);
    });
    keywElem.select(keywElem.click);
    
    var locElem = $('input.sjsLocation', control);
    locElem.click(function()
    {
      sjsField_onclick(locElem, sjs.LocationEmpty);
    }
    );
    locElem.select(locElem.click);
    
    keywElem.blur(function()
    {
      if ($.trim(keywElem.val())=='')
        sjs.Keywords('');
      else
        keywElem[0].style.color="";
    });
    
    locElem.blur(function()
    {
      if ($.trim(locElem.val())=='')
        sjs.Location('');
      else
      {
        locElem[0].style.color="";
        var distElem = $('select.sjsDistance', control)[0];
        if (distElem != null)
          distElem.disabled=false;
      }  
    });
    
    var sjsBtnCancel = $('input#sjsBtnCancel', control);
    sjsBtnCancel.click(function()
    {
      var popupObj = $('span#sjspp', control)[0];
      if (popupObj.ppChildObj!=undefined)
        var disappearInterval = popupObj.ppChildObj.disappearInterval;
      else
        var disappearInterval = 400;
        
      if ($.browser.msie && disappearInterval>200)
        $(popupObj).slideUp(200);
      else
        $(popupObj).slideUp(disappearInterval);
      return false;//for prevent server side onclick event 
    });
  
    var jaValidator = $('.sjsValidator', control);
    jaValidator[0].clientvalidationfunction = sjs.Validate;
  }
  
  function sjsField_onclick(elem, isEmptyMarker)
  {
    elem[0].style.color=""; 
    if (elem.val()==isEmptyMarker)
      elem.val('');
  }
  
  function SetCurrentSjsParameters(control)
  {
    var sjs = control.sjs;
    sjs.Keywords(sjs.Keywords());
    sjs.Location(sjs.Location());
  }