// JScript File

function handleBadCode()
{
	Tremor.Ajax.Call("tracking","track",{tremorPageName:'Fruitnaturals_userEnteredInvalidCode'});
	urchinTracker('/fruitnaturals/userEnteredInvalidCode');
	$("#msg").html("We're sorry, the code was not entered correctly.<br />Please check your numbers and try again.");
}

function handleGoodCode()
{
	var code = document.getElementById('code').value;
	Tremor.Util.SetCookie("code", code);
	//Tremor.Session.SetData("code", code);
	//alert("Tremor.Util.GetCookie="+Tremor.Util.GetCookie("code"));
	 window.location = "login.html?code="+code;

}

function handleIssueCodeSuccess()
{
	//alert("issue code success");
	window.location = "thankyou.html";
}

function handleIssueCodeFailure()
{
	//alert("issue code failure");
	window.location = "sorry.html";
}

function validateCode(code){
		if (code=='')
		{
		    code = document.getElementById('code').value;
		}
    	if(code=='') 
		{
			document.getElementById('errorMsgDiv').innerHTML='ALL FIELDS REQUIRED';
			return false;
		}
    
        //alert(code);
		    
		var programName = "FruitNaturals_VP_TP4";
		
	    var resp = Tremor.Ajax.Call('ProgramCode','validateProgramCode', { programName:programName, code:code },{sync:true});
        
        //alert(resp.ResultCode);
        //alert(resp.Notice);
        
/*
       if (resp.ResultCode==0 || resp.Notice=="Success")
       {
            Tremor.Util.SetCookie('code', code);	       
	    window.location = "requestEntry_login.html";
       }
*/
	alert(resp.Type.value);
    if (resp != null && resp.Type=='success')
    {
	 window.location = "requestEntry_login.html";
    }

       else
       {
		
            //document.getElementById('errorMsgDiv').innerHTML = resp.Notice;   
			window.location = "invalidcode.html?code="+code;
       }
			
	//}
}

function issueCode()
{
	var code = Tremor.Util.GetCookie("code");
	document.getElementById('code').value=code;
	var isValidData = validateIssueCode();

	//alert('issuecode. code='+code);

	if(isValidData){
	    var code = document.getElementById('code').value;
	    var firstname = document.getElementById('firstname').value;
	    var lastname = document.getElementById('lastname').value;
	    var address1 = document.getElementById('address1').value;
	    var city = document.getElementById('city').value;
	    var state = document.getElementById('state').value;
	    var zip = document.getElementById('zip').value;
	    var primaryEmail = document.getElementById('email').value;
	    var limitCodeByEmail=true;
		
	    var programName = "FruitNaturals_VP_TP4";
	    var usesLeft = 0;
		
alert('code='+code +', firstName='+firstname +', lastName='+lastname +', address1='+address1 +', city='+city + ', state='+state +', email='+primaryEmail);


		//var limitCodeByEmail = true;
		
		var code = Tremor.Ajax.Call('ProgramCode','issueProgramCode',
			{ programName:programName, usesLeft:usesLeft, code:code, limitCodeByEmail:limitCodeByEmail,
			  firstName:firstname, lastName:lastname,email:primaryEmail,address1:address1, city:city, state:state, zip:zip },
			  {
				onSuccess:function(){
					//alert("success");
				},
				onFail:function(){
					//alert("error");
				}
			  },
			  {sync:true});
        
      
       //alert(code);
        return false;

	
       if (code != "" && code != null)
       {
            var emailFile="email/email_code.html";
            var senderFirstName = null;
            var senderLastName =null;
            
            var ret = sendEmail(emailFile, friendEmailAddress, senderFirstName, senderLastName, friendEmailAddress, subject, code);
            if (ret==true)
		        window.location="getcode_thanks.html";
		    else
		        document.getElementById('errorMsgDiv').innerHTML='error sending email';
         //return true;
         
       }
       else
       {
            window.location="code_sorry.html";
	    //document.getElementById('errorMsgDiv').innerHTML = "error issuing code";   
       }
			
	}
}

function sendEmail(fileName, friendEmailAddress, senderFirstName, senderLastName, senderEmailAddress, subject, code)
{
    
    var resp = Tremor.Ajax.Call('email','sendfile',
    { fileName:fileName, toEmailAddress:friendEmailAddress, 
      senderFirstName:senderFirstName, senderLastName:senderLastName,
      senderEmailAddress:senderEmailAddress, subject:subject, code:code
    },{sync:true});
    if (resp != null && resp.Type=='success')
    {
        return true;
    }
    else
    {
        return false;
    }
}


function validateIssueCode()
{
	var code = document.getElementById('code').value;
	var firstname = document.getElementById('firstname').value;
	var lastname = document.getElementById('lastname').value;
	var address1 = document.getElementById('address1').value;
	var city = document.getElementById('city').value;
	var state = document.getElementById('state').value;
	var zip = document.getElementById('zip').value;
	var primaryEmail = document.getElementById('email').value;
	
	if(code =='' || firstname =='' || lastname =='' || address1 =='' || city =='' || state =='' || zip=='' || primaryEmail =='') 
	{
		document.getElementById('errorMsgDiv').style.visibility = 'visible'; 
		return false;
	}
	else
	{
		document.getElementById('errorMsgDiv').style.visibility = 'hidden';
		return true;
	}
}




function useCode(code, popNewWindow){
	//var isValidData = validateUseCode();

	//if(isValidData){
		
		if (code=='')
		{
		    code = document.getElementById('code').value;
		}
    
        //alert(code);
		    
		var programName = "CokeZero_VP_TP8";
		
		//alert(programName);
        //document.getElementById('errorMsgDiv').innerHTML='';
		
	var resp = Tremor.Ajax.Call('ProgramCode','useProgramCode', { programName:programName, code:code },{sync:true});
        
        //alert(resp.ResultCode);
        //alert(resp.Notice);
        
       if (resp.ResultCode==0 || resp.Notice=="Success")
       {
		    //alert("before calling getCouponIncCouponUrl");
		    var couponUrl = getCouponIncCouponUrl(code);
		    //
		    //window.open (couponUrl, "mywindow");
		    Tremor.Util.SetCookie('couponUrl', couponUrl);
		    Tremor.Ajax.Call("tracking","track",{tremorPageName:'CokeZero_GotoCoupon'});
			//window.location = couponUrl;
						
			window.location = couponUrl;
			window.resizeTo(800,800)         
       }
       else
       {
            //document.getElementById('errorMsgDiv').innerHTML = resp.Notice;   
            if (popNewWindow)
		{
			//this.close();
			//closeNoConfirm();
			popCodeWindow("usecode_sorry.html?code="+code);
		}
		else
		{
			window.location = "usecode_sorry.html?code="+code;
		}
       }
			
	//}
}