// JScript File
function popCodeWindow(url)
{
    var newwindow=window.open(url,'code_popup','height=380,width=350');    
    if (window.focus) {newwindow.focus()}
}

function popRAFWindow(url)
{
    var newwindow=window.open(url,'code_popup','height=550,width=350');    
    if (window.focus) {newwindow.focus()}
}

function CloseNoConfirm()
{
    //win = top;
    //alert("hi");
    window.opener = 'top';
    window.close();
}

function validateUseCode(){
	var code = document.getElementById('code').value;
	
	if(code=='') 
		{
			document.getElementById('errorMsgDiv').innerHTML='ALL FIELDS REQUIRED';
			return false;
		}
	else
		{
			document.getElementById('errorMsgDiv').innerHTML='';
			
			Tremor.Util.SetCookie('programcode', code);			
			return true;
		}
}

function validateRequestCode(){
	var code = document.getElementById('email').value;
	
	if(code=='') 
		{
			document.getElementById('errorMsgDiv').innerHTML='ALL FIELDS REQUIRED';
			return false;
		}
	else
		{
			document.getElementById('errorMsgDiv').innerHTML='';
			
			return true;
		}
}

function validateSendSurprise(){
	var senderLastName = document.getElementById('senderLastName').value;
	var senderEmailAddress = document.getElementById('senderEmailAddress').value;
	var friendName = document.getElementById('friendName').value;
	var friendEmailAddress = document.getElementById('friendEmailAddress').value;
	var comment = document.getElementById('comment').value;
	
	if(senderEmailAddress=='' || friendEmailAddress=='' || senderLastName=='' || friendName=='') 
		{
			document.getElementById('errorMsgDiv').innerHTML="ALL FIELDS ARE REQUIRED";
			return false;
		}
	else
		{
			document.getElementById('errorMsgDiv').innerHTML='';
			return true;
		}
}

function getCouponIncCouponUrl(code)
{
    var checkCode = "CO";
	var pinCode = code;
	var shortKey="ezmtw2lf7j";
	var longKey = "sYdL7AUe3BFlRHmknWNcX6Gg2vjtEO1QTyboiu8rJqz9ZxIKSCaVP5hMDf4pw";
	var offerCode=53031;

    //alert("in getCouponIncCouponUrl. received code="+code);
    //vString, int vOffer, string vKey, string encodeX
    //string pinCode, int offerCode, string shortKey, string longKey
    var encodedCPT = Tremor.Ajax.Call('CouponCodeGenerator','CouponsIncEncodeCPT',
	    { vString:pinCode, vOffer:offerCode, vKey:shortKey, encodeX:longKey },{sync:true});

    //alert("in getCouponIncCouponUrl. encodedCPT="+encodedCPT);

	var couponUrl = "http://bricks.coupons.com/enable.asp?eb=1&o="+ offerCode + "&c=" + checkCode + "&p=" + pinCode + "&cpt=" + encodedCPT;
    //http://bricks.coupons.com/enable.asp?eb=1&o=53031&c=CO&p=F0KA22CZ&cpt=KbqVo1tb9Y3pGb7PY4Cf
    return couponUrl;
}

function useCode(code){
	//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)
			//var newwin = window.open("getcoupon.html", "mywindow");
		    	//newwin.focus();
		    	
			//close the popup
			//window.close();

		    //CloseNoConfirm();
		    
         //return true;
         
       }
       else
       {
            //document.getElementById('errorMsgDiv').innerHTML = resp.Notice;   
		window.location = "usecode_sorry.html?code="+code;
       }
			
	//}
}

function sendSurpriseFinder() {
	var isValidData = validateSendSurprise();

	if(isValidData){
        //var fileName="email/email_shareSurprise.html";
	    var fileName = "email/surprisesEmail.html";
	    var senderLastName = document.getElementById('senderLastName').value;
	    var senderFirstName = ''; //senderLastName ;
	    var senderEmailAddress = document.getElementById('senderEmailAddress').value;
	    var friendName = document.getElementById('friendName').value;
	    var friendEmailAddress = document.getElementById('friendEmailAddress').value;
	    var comment = document.getElementById('comment').value;
	    var subject = "Share Surprise with your friend";
        

        var resp = Tremor.Ajax.Call('email','sendfile',
        { fileName:fileName, toEmailAddress:friendEmailAddress, 
          senderFirstName:senderFirstName, senderLastName:senderLastName,
          senderEmailAddress:senderEmailAddress, subject:subject, comment:comment, friendName:friendName
        },{sync:true});

	  //alert(resp);
	  //alert(resp.Type);

        if (resp != null && resp.Type=='success')
        {
	        window.location="shareSurpriseFinder_thanks.html";
        }
        else
        {
	        document.getElementById('errorMsgDiv').innerHTML='error sending email';
        }
    }
}

function requestCode(limitCodeByEmail){
	var isValidData = validateRequestCode();

	if(isValidData){
		
		var friendEmailAddress = document.getElementById('email').value;
		var subject = "Your Vocalpoint Coke Zero Code";
		var programName = "CokeZero_VP_TP8";
		var usesLeft = 1;
		//var limitCodeByEmail = false;
		
        var firstName = null;
        var lastName=null;
        //var email="";
        var addr1=null;
        var addr2=null;
        var city=null;
        var state=null;
        var zip=null;
                                    		
        document.getElementById('errorMsgDiv').innerHTML='';

		var code = Tremor.Ajax.Call('ProgramCode','issueProgramCode',
			{ programName:programName, usesLeft:usesLeft, limitCodeByEmail:limitCodeByEmail,
			  firstName:firstName, lastName:lastName,email:friendEmailAddress,addr1:addr1, addr2:addr2, city:city, state:state, zip:zip },{sync:true});
        
        //alert(code);
        
       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
       {
            //document.getElementById('errorMsgDiv').innerHTML = "error issuing code";   
		window.location = "getcode_sorry.html?code="+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;
    }
}
