uda / Hasadna Participation Extended

// ==UserScript==
// @name        Hasadna Participation Extended
// @namespace   http://www.hasadna.org.il/
// @version     0.1.1
// @description Submit your attendance at coding meetings automatically with configuration

// @match       https://docs.google.com/*forms/d/1pMXeSh8sMAUBWMdSoGAoG55MmJAm0QJuU1ZlxDVTZbc/*
// @match       https://docs.google.com/*forms/d/1qDbCSiuD4m71IbfknvgfS9QNrUeaEdVRWjM8tTAN0wk/*

// @require     https://raw.github.com/sizzlemctwizzle/GM_config/master/gm_config.js
// @require     https://openuserjs.org/libs/src/sizzle/GM_config.js
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_log

// @author      uda
// @license     MIT; http://opensource.org/licenses/MIT
// @copyright   2014, Yehuda Deutsch <yeh@uda.co.il>
// ==/UserScript==

var usw = (typeof this.unsafeWindow !== "undefined") ? this.unsafeWindow : window;
var doc = usw.document;

if (usw.location.pathname.indexOf("/viewform") > -1) {
    action = "edit";
} else if (usw.location.pathname.indexOf("/formResponse") > -1) {
	action = "response";
} else {}

var gmFields = {};
var generalMessageTitle = "";
var generalMessageId = "";
var submitButtonId = "ss-submit";
var cityCommunity = "";

if (usw.location.pathname.indexOf("1pMXeSh8sMAUBWMdSoGAoG55MmJAm0QJuU1ZlxDVTZbc") > -1) {
    cityCommunity = "tlv";
    gmFields = {
        "FirstName": {
            "label": "שם פרטי",
            "type": "text",
            "default": "",
            "hasadnaField": "entry_1519029560",
            "hasadnaRequired": true
        },
        "LastName": {
            "label": "שם משפחה",
            "type": "text",
            "default": "",
            "hasadnaField": "entry_1863337503",
            "hasadnaRequired": true
        },
        "ProjectName": {
            "label": "שם פרויקט",
            "type": "text",
            "default": "",
            "hasadnaField": "entry_515986865",
            "hasadnaRequired": true
        },
        "askForMessage": {
            "label": "בקש ממני הודעה",
            "type": "checkbox",
            "default": false,
            "hasadnaRequired": false
        },
        "autoCommit": {
            "label": "שלח מידע אוטומטית",
            "type": "checkbox",
            "default": false,
            "hasadnaRequired": false
        }
    };
    generalMessageTitle = "מסרים לרכז הקהילה (הומור יתקבל בברכה)";
    generalMessageId = "entry_246285601";
} else if (usw.location.pathname.indexOf("1qDbCSiuD4m71IbfknvgfS9QNrUeaEdVRWjM8tTAN0wk") > -1) {
    cityCommunity = "jer";
	gmFields = {
        "FullName": {
            "label": "שם מלא",
            "type": "text",
            "default": "",
            "hasadnaField": "entry_1376996228",
            "hasadnaRequired": true
        },
        "ProjectName": {
            "label": "שם פרויקט",
            "type": "text",
            "default": "",
            "hasadnaField": "entry_39187576",
            "hasadnaRequired": false
        },
        "askForMessage": {
            "label": "בקש ממני הודעה",
            "type": "checkbox",
            "default": false,
            "hasadnaRequired": false
        }
    };
    generalMessageTitle = "עוד הצעות או רעיונות להאב הפתוח?";
    generalMessageId = "entry_391667244";
} else {}

var frame = document.createElement("div");
document.body.appendChild(frame);
GM_config.init({
	id: cityCommunity + "Hasadna_GM_config",
    title: "הגדרות רישום",
	fields: gmFields,
    frame: frame,
    events: {
        save: function() {
            usw.location.reload();
        }
    }
});

var hasConfig = false;
var isFulfilled = true;

for (i in gmFields) {
    value = GM_config.get(i);
    if (value === "") {
        if ("hasadnaRequired" in gmFields[i] && gmFields[i].hasadnaRequired) {
        	isFulfilled = false;
        }
    } else {
        hasConfig = true;
        if (action === "edit" && "hasadnaField" in gmFields[i] && doc.getElementById(gmFields[i].hasadnaField)) {
        	doc.getElementById(gmFields[i].hasadnaField).value = value;
        }
    }
}

if (hasConfig && isFulfilled) {
    button = doc.createElement("button");
    button.type = "button";
    button.innerHTML = "לעריכת הנתונים השמורים";
    button.onclick = function() {
    	GM_config.open();
    };
    
    div = doc.createElement("div");
    div.setAttribute("style", "position:fixed;top:5px;right:5px;");
    
    div.appendChild(button);
    doc.body.appendChild(div);
    
    if (action === "edit") {
        askForMessage = GM_config.get("askForMessage");
        if (askForMessage) {
            message = usw.prompt(generalMessageTitle, "");
            doc.getElementById(generalMessageId).value = message;
        }
        
        if (cityCommunity === "tlv") {
    		autoCommit = GM_config.get("autoCommit");
            if (autoCommit) {
                doc.getElementById(submitButtonId).click();
                return;
            }
        }
        doc.getElementById(generalMessageId).focus();
    }
} else {
	GM_config.open();
}