﻿var ANZipCodeWidget = new function() 
{
this.controlId;
this.getEmail;
this.converage;
this.conditionType;
this.dlgTxtBoxId;
this.dlgImage;
this.valEmailId;
this.oldEmailValue="";
this.btnSubmit = "";
this.callbackCtrlId = null;
this.isCallBack = false;
this.useClickPostBack = false;
this.zipcode = '';

this.img1Path = "banner_getaccess"; 
this.img2Path = "banner_nocoverage";
this.img3Path = "banner_nocov_access";
this.img4Path = "banner_nocov_tool";
this.img5Path = "banner_nocov_acc_tool";

this.enableCoverage = false;
this.enableEmail = false;
this.emailGate = false;
this.config = {};
this.siteEmail={}
}

ANZipCodeWidget.DownloadImage=function(src)
{
    var img = new Image();    
    if (img && src && src != "")
       img.className= src;       

}

ANZipCodeWidget.DownloadImages=function()
{
   this.DownloadImage(this.img1Path);
   this.DownloadImage(this.img2Path);
   this.DownloadImage(this.img3Path); 
   this.DownloadImage(this.img4Path);
   this.DownloadImage(this.img5Path);   
   this.addEvent(this.dlgTxtBoxId,'keydown',this.KeyDownHandler);  
}

ANZipCodeWidget.IsNoCoverageOn=function()
{
    return (!this.converage && this.enableCoverage);
}

ANZipCodeWidget.IsEmailOn=function()
{
    return (this.getEmail && this.enableEmail);
}


ANZipCodeWidget.GetRules = function(zip)
{    
    if (typeof(Microsoft.BuyOnline.Clients.Web.BuyOnline.VehicleFinancing.QuotationWS) != 'undefined')
    {        
        if ( (typeof(zip) == "undefined" || zip =="") && this.controlId != "")
        {            
            zip = "";
            var txtBox = $get(this.controlId);
            if (txtBox)            
                zip = txtBox.value;     
            
            if (zip == "")
                return;                             
        }
        this.zipcode = zip;
        Microsoft.BuyOnline.Clients.Web.BuyOnline.VehicleFinancing.QuotationWS.ZipCodeRules(zip, this.Execute);
    }        
}

ANZipCodeWidget.Execute = function(params)
{
    var cmd = "ANZipCodeWidget.RunSteps(" + params + ");";
    eval(cmd);
}

ANZipCodeWidget.RunSteps = function(jsonData)
{   
    if (jsonData && jsonData.Rule)
    {
        if (jsonData.Config)
            this.config = jsonData.Config;
            
        if (jsonData.SiteEmail)
            this.siteEmail = jsonData.SiteEmail;
        
        if (jsonData.Rule.valid == 1)
        {
           this.getEmail = jsonData.Rule.email;
	       this.converage = jsonData.Rule.coverage; 	       
                      
           if (this.config && this.siteEmail)
           {
                this.enableCoverage =(this.config.coverage == 1);                
                this.enableEmail = false;
                if (this.conditionType){
                    if (this.conditionType.toLowerCase() == 'new')
                        this.enableEmail = (this.siteEmail.typeNew == true && this.emailGate); 
                    else 
                        this.enableEmail = (this.siteEmail.typeUsed == true && this.emailGate);
                }
           }

           this.SetEmailBox();
                
           if (this.IsEmailOn() || this.IsNoCoverageOn())
                this.DisplayMessage(true);
           else
                this.DisplayMessage(false);
        }
        else
        {
            this.AlertBadZip();
        }        
    }
}

ANZipCodeWidget.GetImage= function(id)
{
    var img = "";
    
    switch(id)
    {
        case 1:              
        img =(this.config && this.config.img1 && this.config.img1 != "") ? this.config.img1 : this.img1Path;                    
        break;
        
        case 2:
        img =(this.config && this.config.img2 && this.config.img2 != "") ? this.config.img2 : this.img2Path;
        break;
        
        case 3:
        img =(this.config && this.config.img3 && this.config.img3 != "") ? this.config.img3 : this.img3Path;        
        break;
        
        case 4:
        img =(this.config && this.config.img4 && this.config.img4 != "") ? this.config.img4 : this.img4Path;        
        break;
        
        case 5:
        img =(this.config && this.config.img5 && this.config.img5 != "") ? this.config.img5 : this.img5Path;        
        break;
    }
    this.DownloadImage(img);  
    
    return img;
}

ANZipCodeWidget.DisplayToolsButton= function(){
    return (typeof(this.config.url) != 'undefined' && this.config.url != '');
}

ANZipCodeWidget.DisplayToolsLink = function(showPopUp) {
    var lnk = $get('lbExit');
    var display = 'none';
    var url = (this.DisplayToolsButton()) ? this.config.url : '';
    
    if (this.IsNoCoverageOn()){        
        if (url != ''){            
            display = 'block';
            url +='?zipcode=' + this.zipcode + '&returnUrl=' + location.href + '&mode=' +  location.href.substring(7,location.href.indexOf(".")).toLowerCase().replace('local','').replace('dev','');            
        }
   }   
   if (lnk){
        lnk.style.display = display;
        lnk.href = url;
   }   
}

ANZipCodeWidget.DisplayMessage = function(showPopUp) 
{ 
    
    if (showPopUp)
    {
        var mpeQuotation = $find('EMailGatePop');
        if(mpeQuotation != null)
        {
            var imgSrc =this.GetImage(1); //email on - coverage off
            
            if (this.IsEmailOn() && this.IsNoCoverageOn())
                imgSrc = (this.DisplayToolsButton()) ? this.GetImage(5) :this.GetImage(3);
            else if (!this.IsEmailOn() && this.IsNoCoverageOn())
                imgSrc = (this.DisplayToolsButton()) ? this.GetImage(4) : this.GetImage(2);
                        
            this.DisplayToolsLink();
            
            var imgRef = $get(this.dlgImage);
            if (imgRef)
                imgRef.className= imgSrc;
            
	       
        	ShowDiv('divEmailGate');
            mpeQuotation.show();	    
        
            if (this.IsEmailOn()) 
               this.SetFocus(this.dlgTxtBoxId);            
            
        }
    }
   else 
   {	
	    this.PostBack();
   }
} 

ANZipCodeWidget.SetFocus= function (id)
{
      var ctrl = $get(id);
      if (ctrl)
      {
         ctrl.focus();
      }      
}


ANZipCodeWidget.SetEmailBox = function() 
{ 
    var txtBox = $get(this.dlgTxtBoxId);
    var row = $get("emailcontrol");
    if (txtBox && row)
    {
	if (!this.IsEmailOn())
        {
           this.oldEmailValue = txtBox.value;
           txtBox.value = "";                      
           this.EnableValidator(this.valEmailId,false);
       	 
       	   if (typeof(row.style.display) != 'undefined')
       	       row.style.display= "none";
           else 
        	   row.display= "none";	
        }
	    else
	    {
       	       this.EnableValidator(this.valEmailId,true);
	       if (txtBox.value == "")
		       txtBox.value = this.oldEmailValue;
	       if (typeof(row.style.display) != 'undefined')
       	       row.style.display= "block";
           else 
        	   row.display= "block";		       
	    }
    }
}

ANZipCodeWidget.EnableValidator = function(id,cmd)
{
   var validator = $get(id);
   if (validator)
        ValidatorEnable(validator, cmd);

   if (cmd)
   {      
      validator.style.visibility = "hidden";
   }
} 

ANZipCodeWidget.AlertBadZip = function() 
{ 
    var ref = $get(this.controlId);
    if (ref)
    {        
	    alert("Invalid Zip Code");
        ref.focus();
        ref.select();
        return true;
    }
    
    return false;
} 

ANZipCodeWidget.addEvent= function (id, evType, fn)
{
    var obj = $get(id);

    if (obj && obj.addEventListener)
    {
        obj.addEventListener(evType, fn, false);
        return true;
    }
    else if (obj && obj.attachEvent)
    {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    }
    else
    {
        return false;
    }
}

ANZipCodeWidget.KeyDownHandler = function (e)
{
  var evn = (e) ? e : event;
  if (evn.keyCode == 13)
  {
    ANZipCodeWidget.ProcessClick(ANZipCodeWidget.btnSubmit);       
  }
}

ANZipCodeWidget.ProcessClick = function(id)
{
    if (id && id != "")
    {
        var link = $get(id);
        if (link)
        {	    
	        if (link.click)
	        {               
	           link.click();
	        }      
	        else 
	        {
	             var evt = document.createEvent("MouseEvents"); 
	             evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); 
	             link.dispatchEvent(evt); 
	        }
    	
	    } 
	}
}

ANZipCodeWidget.Validate = function (txtBoxId, callbackId)
{
 
  var rtn = true;
  
  if (this.isCallBack)
  {
     this.isCallBack = false;
     this.PostBack();
     rtn =  false;
  }
  else
  {
  
  this.controlId = txtBoxId;     
  this.callbackCtrlId = callbackId;
  this.addEvent(this.btnSubmit,"click",this.CloseDialog);

  if (Page_IsValid)
	this.GetRules(); 

   rtn = true;
  } 
	
  return rtn;	  
}

ANZipCodeWidget.PostBack = function()
{

	if (this.callbackCtrlId)
	{
	     var arg = this.callbackCtrlId.replaceAll("_","$");
	     
	     if (this.useClickPostBack)
	     {
	        var btn = $get(this.callbackCtrlId);
	        if (btn.click)
	        {
	            btn.click();
	            return;
	        }	            
	     }	     	     
	     __doPostBack(arg,null);	        
	     
	}
	else
	{
	   theForm.submit();	   
	}
}
    
ANZipCodeWidget.CloseDialog = function(e)
{
  var mpeDlg = $find('EMailGatePop');
  if(mpeDlg != null)
  {	      
     mpeDlg.hide();
  }
      
  if(typeof event != "undefined" && event)
    event.returnValue = false;           
  else if (e && e.preventDefault)            
    e.preventDefault();  
   	
   if (ANZipCodeWidget.callbackCtrlId)
   {   	
   	 //ANZipCodeWidget.isCallBack = true;
	 ANZipCodeWidget.PostBack();
   	 ANZipCodeWidget.callbackCtrlId = null;
   }   
     
  return false;  
}

String.prototype.replaceAll = function(stringToFind,stringToReplace){
    var temp = this;
    var index = temp.indexOf(stringToFind);
        while(index != -1){
            temp = temp.replace(stringToFind,stringToReplace);
            index = temp.indexOf(stringToFind);
        }
        return temp;

} 

function HideElement(id)
{
    var ctrl = $get(id);
    if (ctrl)
    {
       if (typeof(ctrl.style.display) != 'undefined')
           ctrl.style.display= "none";
       else 
    	   ctrl.display= "none";	
    }

    var status = $get("progress");
    if (status)
    {
	    if (typeof(status.style.display) != 'undefined')
           status.style.display= "block";
        else 
	       status.display= "block";

        var img=$get("progressImg");
        var img2=$get("progressImg2");
    	
        if (img && img2)
        {
            var src = img.src;
            img.src= img2.src;	  
            img.src =  src;	 
        }
    }
    
return true;
}
