﻿function weJvfToggleDirector(id, value) {
	// value means 
	var pnl = document.getElementById(id);
	if (pnl) {
		//pnl.style.display = value ? "none" : "";
		pnl.style.visibility = value ? "hidden" : "";
	}

	// disable directors validator
	var vAO = DES_FindAOById('ctl00_ctl05_jvf_project_valDirectors');
	if (vAO) {
		vAO.Enabled = !value;
	}
}

function weForumSerieCount(id, txtID, value) {
	// value means 
	var pnl = document.getElementById(id);
	if (pnl) {
		pnl.style.display = value ? "" : "none";
		//pnl.style.visibility = value ? "hidden" : "";
	}

	// set series to 1
	var txt = document.getElementById(txtID);
	txt.value = 1;
}

/* CONTACT POPUPS */
/* add */
function weAddContact(index, contactID, ctrlID, metaID, functionsID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditContact.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&metaid=' + metaID + '&functionsid=' + functionsID + '&userid=' + userID, 'Add Contact', 420, 600, 180);
}
function weAddContactPhone(index, contactID, ctrlID, metaID, functionsID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditContact.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&metaid=' + metaID + '&functionsid=' + functionsID + '&requiredphone=true&userid=' + userID, 'Add Contact', 420, 600, 180);
}
function weAddContactNoCompany(index, contactID, ctrlID, metaID, functionsID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditContactNoCompany.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&metaid=' + metaID + '&functionsid=' + functionsID + '&userid=' + userID, 'Add Contact', 420, 600, 180);
}
function weAddContactMini(index, contactID, ctrlID, metaID, functionsID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditContactMini.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&metaid=' + metaID + '&functionsid=' + functionsID + '&userid=' + userID, 'Add Contact', 420, 600, 180);
}
function weAddContactCredits(index, contactID, ctrlID, metaID, functionsID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditContactCredits.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&metaid=' + metaID + '&functionsid=' + functionsID + '&userid=' + userID, 'Add Contact', 420, 600, 180);
}
function weAddContactWithHistory(index, contactID, ctrlID, metaID, functionsID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditContactWithHistory.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&metaid=' + metaID + '&functionsid=' + functionsID + '&userid=' + userID, 'Add Contact', 420, 600, 180);
}
function weAddCompanyMini(index, contactID, ctrlID, metaID, functionsID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditCompanyMini.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&metaid=' + metaID + '&functionsid=' + functionsID + '&userid=' + userID, 'Add Contact', 420, 600, 180);
}
function weAddContactMiniWithCompany(index, contactID, ctrlID, metaID, functionsID, userID, companyRequired) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditContactMiniWithCompany.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&metaid=' + metaID + '&functionsid=' + functionsID + '&userid=' + userID + '&companyRequired=' + companyRequired, 'Add Contact', 420, 600, 180);
}
function weAddContactTiny(index, contactID, ctrlID, metaID, functionsID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditContactTiny.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&metaid=' + metaID + '&functionsid=' + functionsID + '&userid=' + userID, 'Add Contact', 420, 600, 180);
}
function weAddCompanyCountry(index, contactID, ctrlID, metaID, functionsID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditCompany.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&metaid=' + metaID + '&functionsid=' + functionsID + '&userid=' + userID, 'Add Contact', 420, 600, 180);
}
function weAddFinancer(index, contactID, ctrlID, metaID, functionsID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditFinancer.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&metaid=' + metaID + '&functionsid=' + functionsID + '&userid=' + userID, 'Add Contact', 420, 600, 180);
}
function weAddProducer(index, contactID, ctrlID, metaID, functionsID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditProducer.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&metaid=' + metaID + '&functionsid=' + functionsID + '&userid=' + userID, 'Add Contact', 420, 600, 180);
}
/* edit */
function weEditContact(index, contactID, ctrlID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditContact.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&userid=' + userID, 'Edit Contact', 420, 600, 180);
}
function weEditContactPhone(index, contactID, ctrlID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditContact.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&requiredphone=true&userid=' + userID, 'Edit Contact', 420, 600, 180);
}
function weEditContactNoCompany(index, contactID, ctrlID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditContactNoCompany.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&userid=' + userID, 'Edit Contact', 420, 600, 180);
}
function weEditContactMini(index, contactID, ctrlID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditContactMini.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&userid=' + userID, 'Edit Contact', 420, 600, 180);
}
function weEditContactTiny(index, contactID, ctrlID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditContactTiny.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&userid=' + userID, 'Edit Contact', 420, 600, 180);
}
function weEditContactMiniWithCompany(index, contactID, ctrlID, userID, companyRequired) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditContactMiniWithCompany.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&userid=' + userID + '&companyRequired=' + companyRequired, 'Edit Contact', 420, 600, 180);
}
function weEditContactCredits(index, contactID, ctrlID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditContactCredits.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&userid=' + userID, 'Edit Contact', 420, 600, 180);
}
function weEditContactWithHistory(index, contactID, ctrlID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditContactWithHistory.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&userid=' + userID, 'Edit Contact', 420, 600, 180);
}
function weEditCompanyCountry(index, contactID, ctrlID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditCompany.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&userid=' + userID, 'Edit Contact', 420, 600, 180);
}
function weEditCompanyMini(index, contactID, ctrlID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditCompanyMini.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&userid=' + userID, 'Edit Contact', 420, 600, 180);
}
function weEditFinancer(index, contactID, ctrlID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditFinancer.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&userid=' + userID, 'Edit Contact', 420, 600, 180);
}
function weEditProducer(index, contactID, ctrlID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/EditProducer.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID + '&userid=' + userID, 'Edit Contact', 420, 600, 180);
}
function weDeleteContact(index, contactID, ctrlID, userID) {
	openDefaultWindow('/sites/shared/popups/WebEntries/DeleteContact.aspx?index=' + index + '&contactid=' + contactID + '&ctrlid=' + ctrlID, 'Delete Contact', 420, 200, 360);
}

function weRefreshContactInList(index, name, ctrlID) {
	var prevIndex = index - 1;
	var nextIndex = parseInt(index) + 1;

	var tr;
	var td_a;
	var td_b;
	var td_c;
	var td_d;
	var isNew = false;


	// update me
	tr = document.getElementById(ctrlID + '_TR' + index);
	if (tr) {
		td_a = document.getElementById(ctrlID + '_TD' + index + 'a');
		td_b = document.getElementById(ctrlID + '_TD' + index + 'b');
		td_c = document.getElementById(ctrlID + '_TD' + index + 'c');
		td_d = document.getElementById(ctrlID + '_TD' + index + 'd');
		// set name
		td_a.innerHTML = index + '. ' + name;

		// new contact?
		isNew = (td_b.style.visibility == "hidden") ? true : false;
		if (isNew) {
			td_a.style.fontWeight = "bold";
			td_b.style.visibility = "";
			td_c.style.visibility = "";
		}
	}

	// show next row if present
	if (isNew) {
		tr = document.getElementById(ctrlID + '_TR' + nextIndex);
		if (tr) {
			td_a = document.getElementById(ctrlID + '_TD' + nextIndex + 'a');
			td_b = document.getElementById(ctrlID + '_TD' + nextIndex + 'b');
			td_c = document.getElementById(ctrlID + '_TD' + nextIndex + 'c');
			td_d = document.getElementById(ctrlID + '_TD' + nextIndex + 'd');
			tr.style.display = "";
			td_a.style.visibility = "";
			td_a.style.fontWeight = "normal";
			td_a.innerHTML = td_d.innerHTML;
			td_b.style.visibility = "hidden";
			td_c.style.visibility = "hidden";
		}

		// hide delete button on previous row
		tr = document.getElementById(ctrlID + '_TR' + prevIndex);
		if (tr) {
			td_c = document.getElementById(ctrlID + '_TD' + prevIndex + 'c');
			td_c.style.visibility = "hidden";
		}

	}

	// check if function exists, if so then run
	if (typeof reCalc == 'function') {
		reCalc();
	}
}

function weRemoveContactFromList(index, ctrlID) {
	var prevIndex = index - 1;
	var nextIndex = parseInt(index) + 1;

	var tr;
	var td_a;
	var td_b;
	var td_c;
	var td_d;

	// update row
	tr = document.getElementById(ctrlID + '_TR' + index);
	if (tr) {
		td_a = document.getElementById(ctrlID + '_TD' + index + 'a');
		td_b = document.getElementById(ctrlID + '_TD' + index + 'b');
		td_c = document.getElementById(ctrlID + '_TD' + index + 'c');
		td_d = document.getElementById(ctrlID + '_TD' + index + 'd');

		td_a.style.visibility = "";
		td_a.style.fontWeight = "normal";
		td_a.innerHTML = td_d.innerHTML;
		td_b.style.visibility = "hidden";
		td_c.style.visibility = "hidden";
	}

	// show delete button on previous row
	tr = document.getElementById(ctrlID + '_TR' + prevIndex);
	if (tr) {
		td_c = document.getElementById(ctrlID + '_TD' + prevIndex + 'c');
		td_c.style.visibility = "visible";
	}

	// hide next row
	tr = document.getElementById(ctrlID + '_TR' + nextIndex);
	if (tr) {
		td_a = document.getElementById(ctrlID + '_TD' + nextIndex + 'a');
		td_b = document.getElementById(ctrlID + '_TD' + nextIndex + 'b');
		td_c = document.getElementById(ctrlID + '_TD' + nextIndex + 'c');

		tr.style.display = "none";
		td_a.style.fontWeight = "normal";
		td_a.innerHTML = "";
		td_b.style.visibility = "hidden";
		td_c.style.visibility = "hidden";
	}
}

function weValidateContact(cond) {
	var td;
	var retVal = 0;

	// check visibility of edit cell in first row
	td = document.getElementById(cond.IDToEval + '_TD1b');
	if (td && td.style.visibility != "hidden") {
		retVal = 1;
	}
	return retVal;
}

function weValidateWordCount(cond) {
	var retVal = 1;
	var count = 0;
	var maxwords = -1;
	var minwords = -1;
	var ta;

	ta = document.getElementById(cond.IDToEval);
	if (ta) {
		minwords = ta.attributes["MinWords"] ? ta.attributes["MinWords"].value : -1;
		maxwords = ta.attributes["MaxWords"] ? ta.attributes["MaxWords"].value : -1;
		for (i = 0; i < ta.value.length; i++) {
			if (ta.value.substr(i, 1) == ' ') {
				count++;
			}
		}
	}


	if ((maxwords > -1 && count > maxwords) || (minwords > -1 && count < minwords)) {
		retVal = 0;
	}

	return retVal;
}

function weValidateMSDL(cond) {
	var retVal = 1;
	var ddl = DES_GetById(cond.IDToEval + "1");
	if (ddl.selectedIndex == 0) {
		retVal = 0
	}
	return retVal;
}

function addMore(name, number) {
	// get current selectbox
	var id = name + number;
	obj = document.getElementById(id);

	// create new element	
	newObj = document.createElement('select');
	for (var i = 0; i < obj.options.length; i++) {
		// fill options
		newObj.options[i] = new Option(obj.options[i].text, obj.options[i].value);
	}
	newObj.name = name + "[]";
	newNumber = parseInt(number) + 1;
	newObj.id = name + newNumber;

	newObj.style['width'] = obj.style.width;
	newObj.className = obj.className;

	div = document.getElementById(name + "div"); // get div
	knop = document.getElementById(name + "more"); // get button
	br = document.createElement('br');
	div.insertBefore(br, knop); // first add br
	div.insertBefore(newObj, knop); // then add select
	div.insertBefore(br, knop); // finally add button 
}

function setNextText(submit, save) {
	// this function doesn't seem to work, but at least it doesn't throw an error
	if ($(this).is(':checked')) {
		$(".weNext").innerHTML = submit;
	} else {
		$(".weNext").innerHTML = save;
	}
}

function noSubmitOnCancel() {
	try {
		$("input:checkbox").each(function () {
			$(this).attr('checked', false);
			$(this).attr('disabled', 'true');
		});

		return true;
	}
	catch (err) {
		alert(err);
		return false;
	}
}

