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

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[required,length[0,40]]");

    if($("#SKFCE_11_UserInput").exists())
       $("#SKFCE_11_UserInput").addClass("validate[optional,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_6_UserInput").exists())
       $("#SKFCE_6_UserInput").addClass("validate[optional,length[0,255]]");

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

    if($("#SKFCE_19_UserInput").exists())
       $("#SKFCE_19_UserInput").addClass("validate[optional,length[0,255]]");
*/
    BindFormValidation('Sitekit_Form_415');
}


//Show form according to verticals
ShowQuickFillForm = function(branchId)
{
    $.validationEngine.closePrompt('.formError',true)
    
    if($("#frm-SalesForce").exists())
    {
        $('#recordType').val('012200000000RlD');
        QuickfillForm_AddValidationToAllFields_SF();
        QuickfillForm_RemoveValidationFromFields_SF(branchId);
        BindFormValidation('frmSalesforce');
        $("#frm-SalesForce").css('display',"");
    }

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

//Add validation class to Quick Fill form
QuickfillForm_AddValidationToAllFields_SF = function() //SALESFORCE
{
    if($("#first_name").exists())
        $("#first_name").addClass("validate[required,length[0,40]]");

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

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

    //if($("#company1").exists())
    //    $("#company1").addClass("validate[required,length[0,100]]");

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

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

    if($("#zip").exists())   
         $("#zip").addClass("validate[required,length[0,40]]");
    
    if($("#00N200000026841").exists())        
        $("#00N200000026841").addClass("validate[required]");
}

//Remove Or Change validation class to quick fill form 
QuickfillForm_RemoveValidationFromFields_SF = function(branchId) //SALESFORCE
{
    $('#dv_company').css('display','');
    $("#company").removeClass("validate[optional,length[0,100]]");
    switch(branchId)
    {
    //case 1:
    case 6:
        $('#recordType').val('012200000000ScX');
        $('#dv_company').css('display','none');
        if($("#company").exists())
        {
            $("#company").removeClass("validate[required,length[0,100]]");      
            $("#company").addClass("validate[optional,length[0,100]]");            
        }
      break;
    }
}


//binding validation to defined form
BindFormValidation = function(formId)
{
    $.validationEngineLanguage.newLang();
    if($("#" + formId).exists())
    {
       $("#" + formId).validationEngine();
    }
}

// reset the defined form
ResetForm = function(formId)
{
    $('textarea','#' + formId).text('');
    $(':input','#' + formId).not(':button, :submit, :reset, :hidden, :radio, :checkbox').val('').removeAttr('checked').removeAttr('selected'); 
}


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);  
    }); 
}


//Show Main form according to options
ShowMainForm_SK = function(branchid)
{
    var frmGuid = jQuery.uuid();
    $("#instanceID").val(frmGuid);
    $.validationEngine.closePrompt('.formError',true)
    var options;

    if(branchid == 1)
        options = {"":"Wählen Sie…","Produktinformationen":"Produktinformationen","Ich wünsche ein Angebot":"Ich wünsche ein Angebot","Technische Hilfe":"Technische Hilfe","Andere":"Andere"};
    else
        options = {"":"Wählen Sie…","Produktinformationen":"Produktinformationen","Ich wünsche ein Angebot":"Ich wünsche ein Angebot","Technische Hilfe":"Technische Hilfe","Andere":"Andere"};

    AddOptions('SKFCE_10_UserInput', options);
    MainForm_AddValidationToAllFields_SK();
    MainForm_RemoveValidationFromFields_SK(branchid);
    BindFormValidation('Sitekit_Form_470');
}

//Add validation class to all fields
MainForm_AddValidationToAllFields_SK = function()
{
    //if($("#SKFCE_3_UserInput").exists())//account number
     //   $("#SKFCE_3_UserInput").addClass("validate[required]");

    if($("#SKFCE_10_UserInput").exists())//nature of inquiry
        $("#SKFCE_10_UserInput").addClass("validate[required]");

    if($("#SKFCE_1_UserInput").exists())//salutation
        $("#SKFCE_1_UserInput").addClass("validate[required]");

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

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

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

    if($("#SKFCE_11_UserInput").exists())//phone
        $("#SKFCE_11_UserInput").addClass("validate[required]")

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

    if($("#SKFCE_16_UserInput").exists())  //company      
        $("#SKFCE_16_UserInput").addClass("validate[required,length[0,100]]"); 
    
    if($("#SKFCE_17_UserInput").exists())//town/city
        $("#SKFCE_17_UserInput").addClass("validate[required]");

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

    if($("#SKFCE_18_UserInput").exists())//comments
        $("#SKFCE_18_UserInput").addClass("validate[optional,length[0,400]]");
}

//Remove or change validation class of the fields
MainForm_RemoveValidationFromFields_SK = function(branchid)
{
    $('#tdCompany1,#tdCompany2').css('display','');
    $("#SKFCE_16_UserInput").removeClass("validate[optional,length[0,100]]");

    switch(branchid)
    {
    case 1:
 //   case 6:
        $('#tdCompany1,#tdCompany2').css('display','none');
        if($("#company").exists())
        {
            $("#SKFCE_16_UserInput").removeClass("validate[required,length[0,100]]");      
            $("#SKFCE_16_UserInput").addClass("validate[optional,length[0,100]]");       
        }
      break;
    }

}

//Show Main form according to options
ShowMainForm_SF = function(branchid) //Salesforce
{

    var options;
    $.validationEngine.closePrompt('.formError',true)

    if(branchid == 1)
        options = {"":"Wählen Sie…","Zutrittskontrolle":"Zutrittskontrolle","Alarmüberwachung":"Alarmüberwachung","Videoüberwachung":"Videoüberwachung","Brand- und Gasmeldeanlagen":"Brand- und Gasmeldeanlagen","Einbruchmeldesysteme":"Einbruchmeldesysteme","Andere":"Andere"};
    else
        options = {"":"Wählen Sie…","Zutrittskontrolle":"Zutrittskontrolle","Alarmüberwachung":"Alarmüberwachung","CCTV Überwachung":"CCTV Überwachung","Gebäudemanagement":"Gebäudemanagement","Business Intelligence":"Business Intelligence","Sicherheitsbeleuchtung und Fluchtwegsteuerung":"Sicherheitsbeleuchtung und Fluchtwegsteuerung","Brand- und Gasmeldeanlagen":"Brand- und Gasmeldeanlagen","Gaslöschung":"Gaslöschung","Einbruchmeldesysteme":"Einbruchmeldesysteme","Lichtruf- und Kommunikationssysteme":"Lichtruf- und Kommunikationssysteme","Retail Anti-Theft Systeme":"Retail Anti-Theft Systeme","RFID":"RFID","Sprinkleranlagen":"Sprinkleranlagen","Andere":"Andere"};
    AddOptions('00N200000026841', options);
    MainForm_AddValidationToAllFields_SF();
    MainForm_RemoveValidationFromFields_SF(branchid); 
    BindFormValidation('frmSalesforce');
}

//validate enquiry form for residential
MainForm_AddValidationToAllFields_SF = function()
{
    if($("#00N200000026841").exists())
        $("#00N200000026841").addClass("validate[required]");

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

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

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

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

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

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

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

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

    if($("#description").exists())
        $("#description").addClass("validate[optional,length[0,400]]");
}

//validate enquiry form for business
MainForm_RemoveValidationFromFields_SF = function(branchId)
{
    $('#tdCompany3,#tdCompany4').css('display','');
    $("#company").removeClass("validate[optional,length[0,100]]");

    switch(branchId)
    {
    case 1:
//    case 6:
        $('#tdCompany3,#tdCompany4').css('display','none');
        if($("#company").exists())
        {
            $("#company").removeClass("validate[required,length[0,100]]");      
            $("#company").addClass("validate[optional,length[0,100]]");       
        }
      break;
    }
   
}

//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();
    }
}
