﻿var varContentType;
var varDataType;
var varProcessData;

function ServiceSucceeded(result) {

    if ( result.SendEmailResult == true ) {
        $('#result').html('Thanks for contacting us!');
        $('#result').fadeIn('slow').delay(5000).fadeOut('slow');

        //if successful, empty the form!
        $(':input', '#commentForm')
        .not(':button, :submit, :reset, :hidden')
        .val('')
        .removeAttr('checked')
        .removeAttr('selected');
    }
    else {
        $('#result').html('Failed, please email <a href="mailto:info@eikospartners.com">info@eikospartners.com</a>');
    }
}

function ServiceFailed(result) {
    /*
    var ProvinceDDL = document.getElementById("ddlProvince");
    for (j = ProvinceDDL.options.length - 1; j >= 0; j--) { ProvinceDDL.remove(j); }
    for (i = 0; i < result.length; i++) {
    var opt = document.createElement("option"); opt.text = result[i];
    ProvinceDDL.options.add(opt)
    }
    */
    $('#result').html('Invalid information please try again ' + varUrl );
}

function ServiceSucceededRegister(result) {

    if ( result.SendEmailResult == true ) {
        $('#registrationResult').html('Thanks for contacting us!');
        $('#registrationResult').fadeIn('slow').delay(5000).fadeOut('slow');

        //if successful, empty the form!
        $(':input', '#registrationForm')
        .not(':button, :submit, :reset, :hidden')
        .val('')
        .removeAttr('checked')
        .removeAttr('selected');
    }
    else {
        $('#registrationResult').html('Failed, please email <a href="mailto:info@eikospartners.com">info@eikospartners.com</a>');
    }
}

function ServiceFailedRegister(result) {
    /*
    var ProvinceDDL = document.getElementById("ddlProvince");
    for (j = ProvinceDDL.options.length - 1; j >= 0; j--) { ProvinceDDL.remove(j); }
    for (i = 0; i < result.length; i++) {
    var opt = document.createElement("option"); opt.text = result[i];
    ProvinceDDL.options.add(opt)
    }
    */
    $('#registrationResult').html('Invalid information please try again ' + varUrl );
}

function CallService(type, url, data, success, failed) {
    $.ajax({
        type: type, //GET or POST or PUT or DELETE verb
        url: url, // Location of the service
        data: data, //Data sent to server
        contentType: varContentType, // content type sent to server
        dataType: varDataType, //Expected data format from server
        processdata: varProcessData, //True or False
        success: success, // When the Service Succeeds
        error: failed// When Service call fails
    });
}

function CallJSONService(type, url, data, success, failed) {
    $.ajax({
        type: type,                 //GET or POST or PUT or DELETE verb
        url: url,                   // Location of the service
        data: data,              //Data sent to server
        contentType: "application/json; charset=utf-8", // content type sent to server
        dataType: "json",           //Expected data format from server
        processdata: varProcessData, //True or False
        success: success,           // When the Service Succeeds
        error: failed               // When Service call fails
    });
}

/*
Uses post to get data submitted to the client
*/
function CallPostJsonService(url, data, success, failed) {
    $.ajax({
        type: "POST",               //GET or POST or PUT or DELETE verb
        url: url,                   // Location of the service
        data: data,              //Data sent to server
        contentType: "application/json; charset=utf-8", // content type sent to server
        dataType: "json",           //Expected data format from server
        processdata: true,          //True or False
        success: success,           // When the Service Succeeds
        error: failed,               // When Service call fails
         beforeSend: function(xhrObj){
            xhrObj.setRequestHeader("loginToken","SomeValue");
            // could use contentType for the above but setting it this way to illustrate the example
            xhrObj.setRequestHeader("signatureToken","SomeValue");
    }

    });
}

function packageParam(parameter, name) {
return '"' + parameter + '": "' + $(name).val() + '"';
}

function packageParamValue(parameter, name) {
return '"' + parameter + '": "' + name + '"';
}

function ValidateForm() {
    var result = $("#commentForm").valid();

    if (result == true)
        SendEmail("#commentForm", 'contacts');

    return result;
}

function ValidateRegistrationForm() {
    var result = $("#registrationForm").valid();

    if (result == true)
        SendEmail("#registrationForm", 'register');

    return result;
}


function SendEmail(form, func) {
var url = "Service/SiteService.svc/SendEmail";

var data = '{' + packageParamValue("function", func);
data = data + ',' + packageParam("name", form + ' [name="name"]');
data = data + ',' + packageParam("email", form + ' [name="email"]');
data = data + ',' + packageParam("phone", form + ' [name="phone"]');
data = data + ',' + packageParam("message", form + ' [name="message"]');
data = data + '}';

if ( func == "register" )
	CallPostJsonService(url, data, ServiceSucceededRegister, ServiceFailedRegister);
else
	CallPostJsonService(url, data, ServiceSucceeded, ServiceFailed);
}

function processContacts(result) {
var ddl = $('#contacts');
ddl.html("");

var html = "";

var data = result.ContactsResult;

for (i = 0; i < data.length; i++) {
var name = data[i].Name;

html = html + "<option value='{0}'>{0}</option>".replace("{0}", name).replace("{0}", name);
}

ddl.html(html);
}

function processContactsTemplate(result) {
    $('#template').tmpl(result.ContactsResult).appendTo("#contacts");
    $('#template2').tmpl(result.ContactsResult).appendTo("#graphic");
    }

/*
Get a list of Contacts from the back end
*/
function Contacts() {
    var url = "Service/SiteService.svc/Contacts";

    CallPostJsonService(url, null, processContactsTemplate, ServiceFailed);
}


