﻿  
  //Global Cookie names
  OfferCookieName = "OfferId=";
  PubCookieName = "PubKey=";
  
   //Set Publisher cookie & offer_id cookie
   //If there is a query string value, then overwrite the existing publisher cookie & offer_id cookie
   //If no query string, then check if a cookie alredy exists, and if NO, then set it with a bogus value, if YES then do nothing
   function SetCookies(offerId){
    //if a new publisher id is set, then a new offer_id need to be set
    if(true == SetPublisherCookie()){
        SetOfferCookie(offerId);
    }
    //Else: no publisher id in query string, and its user navigating inside the site
   }
   
    function SetPublisherCookie() {  
        var value ="pub=99999";
        var ca = document.cookie;
        
        query = '' + this.location; 
        if(query.indexOf('?') >-1){
            value = query.substring(query.indexOf('?')+1);           
        }        
        else if(GetPublisherCookieValue()){            
            return false;
        }
        
        var date = new Date();
        date.setTime(date.getTime()+(30*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();            
                              	
        document.cookie = PubCookieName+value+expires+"; path=/";  
        return true;     
    }
        
    //Get the Value from the publisher cookie
    function GetPublisherCookieValue(){       
        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(PubCookieName) == 0) return(c.substring(PubCookieName.length,c.length));
        }
    }
    
    
    //if the submission is coming from an external publisher get their id
    function setExternalPub() {
	currentUrl = '' + this.location; 
        if(currentUrl.indexOf('expub') >-1){
            expubId = query.substring(query.indexOf('?expub=')+1);
	    return expubId;
        } else {
	    return false
	}
	
    }
    //Validate the submitted form
    //If valid, the get the cookie value and set as a query string to the redirected url
    function ValidateAndSetSubmitUrl(form){
	
	if(true == checkForm(form)){
	    
            var submitBtn = document.getElementById("SubmitButton")
            if(submitBtn){
                submitBtn.disabled = true; 
            }
	    
            var publisherData = GetPublisherCookieValue();   
            form.action ="utSubmit.aspx?" + publisherData ;
	    
            return true;
        }
        else {
            return false;
        }          
    }
    
    //Set the value for the offer id in a cookie, if already exists , then do nothing
    function SetOfferCookie(offerId){
        if(!offerId) return;        
        var value = offerId;
        var date = new Date();
        date.setTime(date.getTime()+(30*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();            
                              	
        document.cookie = OfferCookieName+value+expires+"; path=/";
    }
    
    
    //If offer id = 2 then redirect to the offer page
    function ReDirectIfOfferPage(){
        var offerId = GetOfferCookieValue();
        if(!offerId) return;
        if(2 == offerId) window.location = 'insidersguideformFree.html';
    }
    
    //Get the offer id value from the cookie
    function GetOfferCookieValue(){        
        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(OfferCookieName) == 0) return(c.substring(OfferCookieName.length,c.length));
        }
    }
    