/* http://adtuk.v9.sitekit.net */

ValidateFireRegistration = function()
{
    if($("#SKFCE_5_UserInput").exists())
       $("#SKFCE_5_UserInput").addClass("validate[required,length[0,40]]");

    if($("#SKFCE_6_UserInput").exists())
       $("#SKFCE_6_UserInput").addClass("validate[required,length[0,40]]");

    if($("#SKFCE_16_UserInput").exists())
       $("#SKFCE_16_UserInput").addClass("validate[required,custom[email],length[0,80]]");

    if($("#SKFCE_17_UserInput").exists())
       $("#SKFCE_17_UserInput").addClass("validate[required,length[0,40]]");

    if($("#SKFCE_19_UserInput").exists())
       $("#SKFCE_19_UserInput").addClass("validate[required,length[0,40]]");

    if($("#SKFCE_22_UserInput").exists())
       $("#SKFCE_22_UserInput").addClass("validate[required,length[0,40]]");

    if($("#SKFCE_24_UserInput").exists())
       $("#SKFCE_24_UserInput").addClass("validate[required,length[0,40]]");

    $.validationEngineLanguage.newLang();

    $("#Sitekit_Form_406").validationEngine();

}


ValidateJobApplication = function()
{
    if($("#SKFCE_1_UserInput").exists())
       $("#SKFCE_1_UserInput").addClass("validate[required,length[0,40]]");

    if($("#SKFCE_2_UserInput").exists())
       $("#SKFCE_2_UserInput").addClass("validate[required,custom[email],length[0,80]]");

    if($("#SKFCE_3_UserInput").exists())
       $("#SKFCE_3_UserInput").addClass("validate[optional,length[0,40]]");

    if($("#SKFCE_11_UserInput").exists())
       $("#SKFCE_11_UserInput").addClass("validate[length[0,40]]");

    if($("#SKFCE_15_UserInput").exists())
       $("#SKFCE_15_UserInput").addClass("validate[length[0,40]]");

    if($("#SKFCE_19_UserInput").exists())
       $("#SKFCE_19_UserInput").addClass("validate[length[0,40]]");

    if($("#SKFCE_6_UserInput").exists())
       $("#SKFCE_6_UserInput").addClass("validate[required,length[0,40]]");

    if($("#SKFCE_7_UserInput").exists())
       $("#SKFCE_7_UserInput").addClass("validate[required,length[0,10]]");

    if($("#SKFCE_8_UserInput").exists())
       $("#SKFCE_8_UserInput").addClass("validate[required,length[0,40]]");

    if($("#SKFCE_9_UserInput").exists())
       $("#SKFCE_9_UserInput").addClass("validate[required,length[0,40]]");

    if($("#SKFCE_10_UserInput").exists())
       $("#SKFCE_10_UserInput").addClass("validate[required,length[0,40]]");

    if($("#SKFCE_14_UserInput").exists())
       $("#SKFCE_14_UserInput").addClass("validate[length[0,50]]");

    if($("#SKFCE_17_UserInput").exists())
       $("#SKFCE_17_UserInput").addClass("validate[length[0,255]]");

    if($("#SKFCE_18_UserInput").exists())
       $("#SKFCE_18_UserInput").addClass("validate[required,length[0,20]]");

    if($("#SKFCE_21_UserInput").exists())
       $("#SKFCE_21_UserInput").addClass("validate[length[0,255]]");

    if($("#SKFCE_22_UserInput").exists())
       $("#SKFCE_22_UserInput").addClass("validate[length[0,255]]");

/*alert(document.getElementById("SKFCE_1_UserInput").className);*/

    $.validationEngineLanguage.newLang();

    $("#Sitekit_Form_356").validationEngine();

}



//Show form according to verticals
    ShowQuickFillForm = function(branchId)
    {
        var frmGuid = jQuery.uuid();
        $("#instanceID").val(frmGuid);
        //alert(frmGuid )
        $.validationEngine.closePrompt('.formError',true)
        if(branchId == 1)
        {
            $(".module_bus").css("display","none");
            $(".module_res").css("display","");
            $("#SKFCE_0_UserInput").val("RESIDENTIAL");
            $("#module_title_resi").css("display","block");
            $("#module_qf_organisation").css("display","none");
            $("#module_title_non_resi").css("display","none");
            
            ValidateQuickfillEnquiryRes(); 
        }
        else
        {
            $(".module_res").css("display","none");
            $(".module_bus").css("display","");
            $("#SKFCE_0_UserInput").val("BUSINESS");
            $("#module_title_resi").css("display","none");
            $("#module_title_non_resi").css("display","block");

            ValidateQuickfillEnquiryBus();
        }
        if(branchId == 3){
            var options = {"":"I would like","A call back":"A call back","To arrange security/fire audit":"To arrange security/fire audit","More information":"More information"};
            AddOptions('SKFCE_7_UserInput', options);        
        }

        $(".formError").css("display","");

    }


//validate enquiry form for residential
ValidateQuickfillEnquiryRes = function()
{

    $("#SKFCE_9_UserInput").addClass("validate[required]")

    if($("#SKFCE_10_UserInput").exists()) 
        $("#SKFCE_2_UserInput").removeClass("validate[required,length[0,40]]");       
    // $("#SKFCE_10_UserInput").addClass("validate[required]")
        
    if($("#SKFCE_1_UserInput").exists()){
        //$("#SKFCE_1_UserInput").removeClass("validate[length[0,40]]")
        $("#SKFCE_1_UserInput").addClass("validate[required,length[0,40]]");
    }
    if($("#SKFCE_6_UserInput").exists())        
       // $("#SKFCE_6_UserInput").removeClass("validate[required,custom[email],length[0,80]]");
        $("#SKFCE_6_UserInput").addClass("validate[required,custom[email],length[0,80]]"); 

      if($("#SKFCE_2_UserInput").exists())
        $("#SKFCE_2_UserInput").addClass("validate[required,length[0,40]]");            
    
//if($("#SKFCE_11_UserInput").exists())
     //   $("#SKFCE_11_UserInput").addClass("validate[required,length[0,40]]");

    if($("#SKFCE_2_UserInput").exists())
        $("#SKFCE_2_UserInput").removeClass("validate[required,length[0,40]]");
         
    if($("#SKFCE_5_UserInput").exists())   
         $("#SKFCE_5_UserInput").addClass("validate[required,length[0,40]]");
    
    if($("#SKFCE_16_UserInput").exists())
        $("#SKFCE_16_UserInput").addClass("validate[required,length[0,20]]");

    if($("#SKFCE_17_UserInput").exists())        
        $("#SKFCE_17_UserInput").removeClass("validate[required]")
    
    if($("#SKFCE_19_UserInput").exists())        
        $("#SKFCE_19_UserInput").addClass("validate[required]");

    if($("#SKFCE_10_UserInput").exists())        
        $("#SKFCE_10_UserInput").addClass("validate[required]");

    if($("#SKFCE_7_UserInput").exists())        
           $("#SKFCE_7_UserInput").removeClass("validate[required]")    
           //    $("#SKFCE_18_UserInput").addClass("validate[required]");
    
       if($("#SKFCE_4_UserInput").exists())        
        $("#SKFCE_4_UserInput").addClass("validate[required]");

    $.validationEngineLanguage.newLang();
    if($("#Sitekit_Form_358").exists())
    {
       $(':input','#Sitekit_Form_358').not(':button, :submit, :reset, :hidden, :radio').val('').removeAttr('checked').removeAttr('selected'); 
       $("#Sitekit_Form_358").validationEngine();
    }
    if($("#Sitekit_Form_471").exists())
    {
       $(':input','#Sitekit_Form_471').not(':button, :submit, :reset, :hidden, :radio').val('').removeAttr('checked').removeAttr('selected'); 
       $("#Sitekit_Form_471").validationEngine();
    }
    if($("#Sitekit_Form_360").exists())
    {
       $(':input','#Sitekit_Form_360').not(':button, :submit, :reset, :hidden, :radio').val('').removeAttr('checked').removeAttr('selected'); 
       $("#Sitekit_Form_360").validationEngine();
    }
}

//validate enquiry form for business
ValidateQuickfillEnquiryBus = function()
{
    $("#SKFCE_9_UserInput").addClass("validate[required]")

    if($("#SKFCE_10_UserInput").exists())        
        $("#SKFCE_10_UserInput").removeClass("validate[required]")
        
    if($("#SKFCE_1_UserInput").exists()){
        $("#SKFCE_1_UserInput").addClass("validate[required,length[0,40]]")
        //$("#SKFCE_1_UserInput").addClass("validate[length[0,40]]");
    }

    if($("#SKFCE_6_UserInput").exists())
        $("#SKFCE_6_UserInput").addClass("validate[required,custom[email],length[0,80]]");

    if($("#SKFCE_11_UserInput").exists())
        $("#SKFCE_11_UserInput").removeClass("validate[required,length[0,40]]");
                
    if($("#SKFCE_2_UserInput").exists())
        $("#SKFCE_2_UserInput").addClass("validate[required,length[0,40]]");

    if($("#SKFCE_5_UserInput").exists())
        $("#SKFCE_5_UserInput").addClass("validate[required,length[0,40]]");
    
    if($("#SKFCE_16_UserInput").exists())
        $("#SKFCE_16_UserInput").addClass("validate[required,length[0,20]]");
    
    if($("#SKFCE_17_UserInput").exists())        
        $("#SKFCE_17_UserInput").addClass("validate[required]");

    if($("#SKFCE_19_UserInput").exists())        
        $("#SKFCE_19_UserInput").removeClass("validate[required]")

    if($("#SKFCE_7_UserInput").exists())        
        $("#SKFCE_7_UserInput").addClass("validate[required]");
      
     if($("#SKFCE_4_UserInput").exists())        
        $("#SKFCE_4_UserInput").removeClass("validate[required]")

    if($("#SKFCE_10_UserInput").exists())        
        $("#SKFCE_10_UserInput").addClass("validate[required]");

    $.validationEngineLanguage.newLang();
    if($("#Sitekit_Form_358").exists())
    {
       $(':input','#Sitekit_Form_358').not(':button, :submit, :reset, :hidden, :radio').val('').removeAttr('checked').removeAttr('selected'); 
       $("#Sitekit_Form_358").validationEngine();
    }
    if($("#Sitekit_Form_471").exists())
    {
       $(':input','#Sitekit_Form_471').not(':button, :submit, :reset, :hidden, :radio').val('').removeAttr('checked').removeAttr('selected'); 
       $("#Sitekit_Form_471").validationEngine();
    }
    if($("#Sitekit_Form_360").exists())
    {
       $(':input','#Sitekit_Form_360').not(':button, :submit, :reset, :hidden, :radio').val('').removeAttr('checked').removeAttr('selected'); 
       $("#Sitekit_Form_360").validationEngine();
    }
}

function AddOptions(ctrlid, arrValue)
{
    $('option',"#" + ctrlid).remove();
    var select = $('#' + ctrlid);  
    var options = select.attr('options'); 
    $.each(arrValue, function(val, text) {  
        options[options.length] = new Option(text, val);  
    }); 
}

//checks for element exists
jQuery.fn.exists = function(){return jQuery(this).length>0;} 

/*
 * Usuage:
 * var frmGuid = jQuery.uuid();
 * $("#instanceID").val(frmGuid);
*/

/*
 Generate fragment of random numbers
*/

jQuery._uuid_default_prefix = '';
jQuery._uuidlet = function () {
	return(((1+Math.random())*0x10000)|0).toString(16).substring(1);
};

/*
 Generates random uuid
*/

jQuery.uuid = function (p) {
	if (typeof(p) == 'object' && typeof(p.prefix) == 'string') {
		jQuery._uuid_default_prefix = p.prefix;
	} else {
		p = p || jQuery._uuid_default_prefix || '';
		return(p+jQuery._uuidlet()+jQuery._uuidlet()+"-"+jQuery._uuidlet()+"-"+jQuery._uuidlet()+"-"+jQuery._uuidlet()+"-"+jQuery._uuidlet()+jQuery._uuidlet()+jQuery._uuidlet());
	};
};

/* Functions to implement omniture tracking starts here*/
function DoRedirect(){
    if(document.formPost){
        SEventForEnquiryForm();
        SEventForCareerForm();
        window.location.href = document.formPost.SubmitAction.value + '?sEvent=' + $("#SEvent").val();  
        //document.formPost.action = document.formPost.SubmitAction.value + '?sEvent=' + $("#SEvent").val();  
        //document.formPost.submit();
    }
}

function SetSEvent(value)
{
    $("#SEvent").val(value);
}

function SEventForCareerForm()
{
    if($('#FormID').val()=='356')
    {
       SetSEvent(9);
    }
}

function SEventForEnquiryForm()
{
    if($('#FormID').val()=='358' || $('#FormID').val()=='360' || $('#FormID').val()=='471')
    {
        switch($('#SKFCE_0_UserInput').val().toLowerCase())
        {
            case 'business':
                if($('#SKFCE_7_UserInput').val() == 'A call back..')
                    SetSEvent(2);
                else if($('#SKFCE_7_UserInput').val() == 'To arrange a survey')
                    SetSEvent(1);
                else if($('#SKFCE_7_UserInput').val() == 'More Information')
                    SetSEvent(3);
                break;    
            case 'residential':
                if($('#SKFCE_4_UserInput').val() == 'A quick quote')
                    SetSEvent(5);
                else if($('#SKFCE_4_UserInput').val() == 'To arrange a survey')
                    SetSEvent(4);
                else if($('#SKFCE_4_UserInput').val() == 'More Information')
                    SetSEvent(6);            
                break;
        }
    }
}

/* Functions to implement omniture tracking end here*/

/************************************************************************************/
// marketing referrer
/************************************************************************************/

function marketingReferrer(marketing_ref){

	// the marketing referrer cookie is destroyed when the browser is closed, so this
	// will only work for new browser sessions

	if($.cookie("Marketing_Referrer")){

		// if there is a cookie, then the user has already visited a previous page, so
		// do NOT update the cookie	
		
		console.log('Existing cookie present');
		console.log($.cookie("Marketing_Referrer"));

	}else{
		
		console.log('Exisiting cookie not present');
		console.log($.cookie("Marketing_Referrer"));

		// if there isn't a cookie, then this is the users first visit to the page, so create
		// a new cookie and store the referrer URL	
		// get the query string	

		var q = $.parseQuery();
		
		// get the PPC ID (if it is there)

		var ppcId = q.ppc;	

		if(typeof ppcId != 'undefined'){	

			// PPC

			// trim the string to obtain the 3 and 4 digits
			// used for determining the campaign source
			// then write it to the cookie
			
			var ppcId = ppcId.substring(3,5);		
			
			switch(ppcId)
			{
			case "C0":
			  $.cookie("Marketing_Referrer", "PPC Brand - Google",{path: '/', expires: 60});		
			  break;
			case "C1":
			  $.cookie("Marketing_Referrer", "PPC Consumer - Google",{path: '/', expires: 60});		
			  break;
			case "C2":
			  $.cookie("Marketing_Referrer", "PPC Business - Google",{path: '/', expires: 60});		
			  break;
			case "C20":
			  $.cookie("Marketing_Referrer", "PPC : Yahoo",{path: '/', expires: 60});		
			  break;
			case "C30":
			  $.cookie("Marketing_Referrer", "PPC : MSN",{path: '/', expires: 60});		
			  break;		  
			default:
			  $.cookie("Marketing_Referrer", "PPC : Other",{path: '/', expires: 60});	
			}		

		}else{			

			// Organic, Directory, Direct				

			// check to see if referrer is null, and default it to pickfords domain
			
			if(document.referrer == "") {
			
				$.cookie("Marketing_Referrer", "Direct Access",{path: '/', expires: 60});

			}else{
			
				$.cookie("Marketing_Referrer", "Natural Search: " + document.referrer,{path: '/', expires: 60});

				}
		
		}		

	}

}



$(window).load(function() {

	// get the URL referrer for marketing purposes
	var marketing_ref = document.referrer;

	// set a cookie to track the marketing referrer
	marketingReferrer(marketing_ref);
	
	// If the main Enquiry Form is detected ont he page, 
	// copy the referrer from the cookie into the hidden
	// field

	if($("#Sitekit_Form_358").length > 0){

		$("#Sitekit_Form_358 #SKFCE_8_UserInput").val($.cookie("Marketing_Referrer"));
                $(".honey_me").hide();	
	
	}

	if($("#Sitekit_Form_471").length > 0){

		$("#Sitekit_Form_471#SKFCE_8_UserInput").val($.cookie("Marketing_Referrer"));	
	
	}

});

/************************************************************************************/