﻿function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


window.onload=function()
{
initialize();
}




function initialize()
{


    if (readCookie("CampaignPopup") == null || readCookie("CampaignPopup") == "")
    {
    document.getElementById("divBackLayer").style.display= "block";
    document.getElementById("divCampaignHolder").style.display= "block";
    document.getElementById("divCampaignLayer1").style.display= "block";
    document.getElementById("divCampaignLayer2").style.display= "none";
    document.getElementById("divCampaignLayer3").style.display= "none";
    resetForm();
    }
    else
    {
    document.getElementById("divBackLayer").style.display= "none";
    document.getElementById("divCampaignHolder").style.display= "none";
    document.getElementById("divCampaignLayer1").style.display= "none";
    document.getElementById("divCampaignLayer2").style.display= "none";
    document.getElementById("divCampaignLayer3").style.display= "none";
    }
    
}

function openLayer(layer)
{


document.getElementById("divCampaignLayer1").style.display= "none";
document.getElementById("divCampaignLayer2").style.display= "none";
document.getElementById("divCampaignLayer3").style.display= "none";

document.getElementById("divCampaignLayer"+layer).style.display= "block";

    if (layer==2)
    {
    document.getElementById("txtCampaignName").focus();
    }

}


function resetForm()
{
 $('txtCampaignName').value = "";
 $('txtCampaignAddress').value = ""; 
 $('txtCampaignZip1').value = ""; 
 $('txtCampaignZip2').value = ""; 
 $('txtCampaignCity').value = ""; 
 $('txtCampaignTelephone').value = ""; 
 $('txtCampaignMobile').value = ""; 
 $('txtCampaignEmail').value = ""; 
 $('txtCampaignYear').value = ""; 
 $('txtCampaignMonth').value = "";
 $('txtCampaignDay').value = ""; 
 $("divErrorMsgName").className = "campaignFieldHeadlines"; 
 $("divErrorMsgAddress").className = "campaignFieldHeadlines"; 
 $("divErrorMsgZip").className = "campaignFieldHeadlines"; 
 $("divErrorMsgCity").className = "campaignFieldHeadlines"; 
 $("divErrorMsgPhone").className = "campaignFieldHeadlines"; 
 $("divErrorMsgEmail").className = "campaignFieldHeadlines"; 
 $("divErrorMsgBirthYear").className = "campaignFieldHeadlines";
 $("divErrorMsg").style.display = "none";
 $("divErrorMsg").update("<img src='images/sign_warning.png' style='margin-right:3px;' align='top' alt=''/>Faltam preencher campos obrigatórios");
 
 
}

function resetCampaign()
{
createCookie("CampaignPopup","",30);
}

function closeLayers()
{
document.getElementById("divBackLayer").style.display= "none";
document.getElementById("divCampaignHolder").style.display= "none";
createCookie("CampaignPopup","true",30);
}


function SetNumeric(sText,type)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 var strNewString = "";
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
          Char = sText.charAt(i); 
      
          if (type==0)
          {
              if (ValidChars.indexOf(Char) == -1) 
                 {
                 IsNumber = false;
                 }
              else
                 {
                 strNewString+= Char;
                 }
              
          }
          if (type==1) //ZIP
          {
              if (i==4 && ValidChars.indexOf("-") == -1)
              {
              strNewString+= "-" 
              }
                if (ValidChars.indexOf(Char) >= 0) 
                 {
                   strNewString+= Char;
                 }
              
          }
          if (type==2) //Phone
          {
              if (Char == " ")
              {
              strNewString+= Char;
              }
              else
              {
              if (ValidChars.indexOf(Char) == -1) 
                 {
                 IsNumber = false;
                 }
              else
                 {
                 strNewString+= Char;
                 }
              }
          
          
          }
      }
      
   return strNewString;
   
   }



function validatePhone(obj)
{
var phoneValue = SetNumeric(obj.value,2);
obj.value = phoneValue;
}



function validateYear(obj)
{
var yearValue = SetNumeric(obj.value,0);
obj.value = yearValue;


//alert(yearValue);

var currentDate = new Date();

iCurrentYear = currentDate.getFullYear();

if (yearValue.length == 4)
{
   if (yearValue <1900 || yearValue>iCurrentYear)
   {
        obj.value = "";
   }
}

}

function validateMonth(obj)
{

var monthValue = SetNumeric(obj.value,0);
obj.value = monthValue;


if (monthValue < 0 || monthValue > 12)
   {
        obj.value = "";
   }


}

function validateDay(obj)
{


var dayValue = SetNumeric(obj.value,0);
obj.value = dayValue;

if (dayValue < 0 || dayValue > 31)
   {
        obj.value = "";
   }


}


function validateZip(obj)
{

zipValue = SetNumeric(obj.value,0);
obj.value = zipValue;


}


function isValidateSubmit()
{

    var showError = false;
    if (document.getElementById("txtCampaignName").value.length > 0)
    {
    document.getElementById("divErrorMsgName").className = "campaignFieldHeadlines"; 
    }
    else
    {
    showError = true;
    document.getElementById("divErrorMsgName").className = "errorMsg"; 
    }

    if (document.getElementById("txtCampaignAddress").value.length > 0)
    {
    document.getElementById("divErrorMsgAddress").className = "campaignFieldHeadlines"; 
    }
    else
    {
    showError = true;
    document.getElementById("divErrorMsgAddress").className = "errorMsg"; 
    }


    if (document.getElementById("txtCampaignZip1").value.length == 4)
    {
    document.getElementById("divErrorMsgZip").className = "campaignFieldHeadlines"; 
    }
    else
    {
    showError = true;
    document.getElementById("divErrorMsgZip").className = "errorMsg"; 
    
    }


    if (document.getElementById("txtCampaignCity").value.length > 0)
    {
    document.getElementById("divErrorMsgCity").className = "campaignFieldHeadlines"; 
    }
    else
    {
    showError = true;
    document.getElementById("divErrorMsgCity").className = "errorMsg"; 
    }

    
    if (document.getElementById("txtCampaignTelephone").value.length > 0)
    {
    document.getElementById("divErrorMsgPhone").className = "campaignFieldHeadlines"; 
    }
    else
    {
    showError = true;
    document.getElementById("divErrorMsgPhone").className = "errorMsg"; 
    }
    
    
    if (document.getElementById("txtCampaignYear").value.length > 0 && document.getElementById("txtCampaignYear").value.length <4)
    {
    showError = true;
    document.getElementById("divErrorMsgBirthYear").className = "errorMsg"; 
    }
    else
    {
    document.getElementById("divErrorMsgBirthYear").className = "campaignFieldHeadlines"; 
    }
    
    
    
    

    if (showError == true)
    {
    document.getElementById("divErrorMsg").style.display = "block";
    }
    else
    {
    document.getElementById("divErrorMsg").style.display = "none";
    DoSubmit();
    }

}






function DoSubmit()
{


   var strName = $('txtCampaignName').value; 
   var strAddress = $('txtCampaignAddress').value; 
   var strZip1 = $('txtCampaignZip1').value; 
   var strZip2 = $('txtCampaignZip2').value; 
   var strCity = $('txtCampaignCity').value; 
   var strPhone = $('txtCampaignTelephone').value; 
   var strMobile = $('txtCampaignMobile').value; 
   var strEmail = $('txtCampaignEmail').value; 
   var strYear = $('txtCampaignYear').value; 
   var strMonth = $('txtCampaignMonth').value; 
   var strDay = $('txtCampaignDay').value; 

   

    var sAJAXrand = '' + parseInt(Math.random() * 99999999); //Used for ensuring that we don't get cached pages
        new Ajax.Request('ajaxsubmit.aspx?type=submitform&rnd='+sAJAXrand, { method:'post', parameters:'name='+encodeURIComponent(strName)+'&address='+encodeURIComponent(strAddress)+'&zip1='+strZip1+'&zip2='+strZip2+'&city='+strCity+'&phone='+strPhone+'&mobile='+strMobile+'&email='+encodeURIComponent(strEmail)+'&birthyear='+strYear+'&birthmonth='+strMonth+'&birthday='+strDay,onSuccess: function(transport) 
            {
            var strText = transport.responseText;
            var arrText = strText.split("$¤^");
            
            
             if (arrText.length>0)
            {

              if (arrText[0] =="success")
              {              
               
              resetForm();
               
              // $('divStatus').style.color = "green";
              // $('divStatus').update(arrText[1]);
              createCookie("CampaignPopup","true",30);
              openLayer(3);
               
            
              }
              else
              {
              $('divErrorMsg').update(arrText[1]);
              $("divErrorMsg").style.display = "block";
              
             // alert(arrText[1]);
              }
            }
            
            
            
            
            }
            ,Parameters:{evalScripts:true} });
}



function successfulSubmit()
{






}
