/*
	javascript development
	============================
	website 	: 	Hestia
	date 		: 	20-7-2010		
	author 		: 	mayra metaxa / developer
	company		: 	mozaik creative business solutions
	url			:	http://www.mozaik.com

*/


function click_clear(id) 
{
	if (document.getElementById(id).value == document.getElementById(id).defaultValue)
		document.getElementById(id).value = "";
	else if (document.getElementById(id).value == "")
		document.getElementById(id).value = document.getElementById(id).defaultValue;
}


/* ========= FORM functions ========= */


function show_error(id)
{
	document.getElementById(id).style.background="#FEFF8F";
}


function clear_errors(formid)
{
	var form = document.getElementById(formid);
	
	for (i=0;i<form.elements.length;i++)
	{
		if ( (form.elements[i].type != "button") && (form.elements[i].type != "reset") )
			form.elements[i].style.background="#F5F5EF";
	}
}


function checkEmail(elem)
{
   var field = document.getElementById(elem);
   var val = field.value.toLowerCase();
   //field.value = val;
   
   var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z0-9]{2,4}$/;
   if (val.length ==0)
		return false;

   if(val.match(emailExp))
		return true;
   else 
		return false;
   
}

function file_upload(file_id)	
{
	if (document.getElementById(file_id))
	{
		var file = document.getElementById(file_id);
		if (file.value == "")
		{
			//document.getElementById(file_id).focus();
			alert("You have to attach a file!");
			return false;
		}
		
		var file1 = file.value.split(".");
		
		if (file1.length < 2)
		{
			//document.getElementById(file_id).focus();
			alert("Invalid file path!");
			return false;
		}
		if ( (file1[file1.length-1] != "txt") && (file1[file1.length-1] != "doc") && (file1[file1.length-1] != "docx") && (file1[file1.length-1] != "rtf") && (file1[file1.length-1] != "pdf") && (file1[file1.length-1] != "zip") && (file1[file1.length-1] != "xls") && (file1[file1.length-1] != "xlsx") && (file1[file1.length-1] != "jpg") && (file1[file1.length-1] != "jpeg") && (file1[file1.length-1] != "png") && (file1[file1.length-1] != "gif") )
		{
			//document.getElementById(file_id).focus();
			alert("Invalid file type! Accepted file types: .txt, .doc, .rtf, .pdf, .docx, .zip, .xls, .xlsx, .jpg, .jpeg, .png, .gif");
			return false;
		}
	}

	return true;
		
}


function check_form_prefilled()
{
	var formid='apfrm';
	
	var mandatory_fields='mailing_address,area,postal_code,country,telephone_country_code,telephone_area_code,telephone_no,email,project_title,project_from_month,project_from_year,project_to,project_aim,activities_realized,results,wider_impact,project_importance,project_innovative_approach,project_impact,project_influence,first_reference_name,first_reference_surname,first_reference_position,first_reference_email,first_reference_tel_country_code,first_reference_tel_area_code,first_reference_tel_no,sec_reference_name,sec_reference_surname,sec_reference_position,sec_reference_email,sec_reference_tel_country_code,sec_reference_tel_area_code,sec_reference_tel_no,testimonial,seqcode';
	//or:
	var mandatory_fields_1='organization_title,year_established,contact_name,contact_surname,position_in_organization';
	var mandatory_fields_2='individual_name,individual_surname';
	
	var prefilled_fields='telephone_country_code,telephone_area_code,telephone_no,mobile_country_code,mobile_area_code,mobile_no,fax_country_code,fax_area_code,fax_no,project_from_month,project_from_year,first_reference_tel_country_code,first_reference_tel_area_code,first_reference_tel_no,sec_reference_tel_country_code,sec_reference_tel_area_code,sec_reference_tel_no';
	
	var numericfields='seqcode';
	//var emailids='email,first_reference_email,sec_reference_email';
	var emailids='email';
	
	clear_errors(formid);
	
	var filled_man_1 = 0;
	var filled_man_2 = 0;
	
	var man1 = mandatory_fields_1.split(",");
	for (i=0;i<man1.length;i++)
	{	
		if (document.getElementById(man1[i]).value != "")
			filled_man_1 = 1;
	}
	
	var man2 = mandatory_fields_2.split(",");
	for (i=0;i<man2.length;i++)
	{	
		if (document.getElementById(man2[i]).value != "")
			filled_man_2 = 1;
	}
	
	if ( ( (filled_man_1==0) && (filled_man_2==0) ) || ( (filled_man_1==1) && (filled_man_2==1) ) )
	{
		for (i=0;i<man1.length;i++)
			show_error(man1[i]);
		for (i=0;i<man2.length;i++)
			show_error(man2[i]);
			
		//document.getElementById('contact_name').focus();
		alert ("You can only fill ONE of the mandatory blocks (*)");
		
		return false;
	}
	else if (filled_man_1==1)
		mandatory_fields = mandatory_fields_1+","+mandatory_fields;		//block 1 filled
	else if (filled_man_2==1)
		mandatory_fields = mandatory_fields_2+","+mandatory_fields;		//block 2 filled
	
	
	
	var man = mandatory_fields.split(",");
	for (i=0;i<man.length;i++)
	{	
		if ( (document.getElementById(man[i]).type) && (document.getElementById(man[i]).type == "select-one") )
		{
			if (document.getElementById(man[i]).selectedIndex == 0)
			{
				show_error(man[i]);
				//document.getElementById(man[i]).focus();
				alert ("You must fill all the mandatory fields (*)");
				return false;
			}
		}
		else
		{
			if ( (document.getElementById(man[i]).value == "") || (document.getElementById(man[i]).value == document.getElementById(man[i]).defaultValue) )
			{
				show_error(man[i]);
				//document.getElementById(man[i]).focus();
				alert ("You must fill all the mandatory fields (*)");
				return false;
			}
		}
	}
	
	/*//check emails*/
	if (emailids != "")
	{
		var email = emailids.split(",");
		for (i=0;i<email.length;i++)
		{
			if (document.getElementById(email[i]))
			{
				if (!checkEmail(email[i]))
				{
					show_error(email[i]);
					//document.getElementById(email[i]).focus();
					alert ("The email address is not valid.");
					return false;
				}
			}
		}
	}
	
	
	//check numeric fields
	if (numericfields != "")
	{
		var numeric = numericfields.split(",");
		for (i=0;i<numeric.length;i++)
		{
			document.getElementById(numeric[i]).value = document.getElementById(numeric[i]).value.replace(/,/,".");
			
			if (isNaN (document.getElementById(numeric[i]).value))
			{
				show_error(numeric[i]);
				alert (numeric[i]+": should be a number. Please check and try again!");
				return false;
			}
		}
	}
	
	//check files
	var filesnum = document.getElementById('attachments_added').value;
	for (i=0;i<filesnum;i++)
	{
		if (!file_upload('file_'+i))
		{
			show_error('file_'+i);
			return false;
		}
	}
	
	//check cv
	var cvnum = document.getElementById('cv_added').value;
	for (i=0;i<cvnum;i++)
	{
		if (!file_upload('cv_'+i))
		{
			show_error('cv_'+i);
			return false;
		}
	}
	
	
	if (prefilled_fields!="")
	{
		var pref_f = prefilled_fields.split(",");
		for (i=0;i<pref_f.length;i++)
		{
			if (document.getElementById(pref_f[i]))
			{
				if ( (document.getElementById(pref_f[i]).type == "text") || (document.getElementById(pref_f[i]).type == "textarea") )
				{
					if (document.getElementById(pref_f[i]).value == document.getElementById(pref_f[i]).defaultValue)
						document.getElementById(pref_f[i]).value = "";
				}
			}
		}
	}
	
	//funding:
	var added_funding = document.getElementById('added_funding').value;
	for (k=1; k<=added_funding; k++)
	{
		var fundinfields = "timefrom_month,timefrom_year";
		var pref_f = fundinfields.split(",");
		for (i=0;i<pref_f.length;i++)
		{
			if (document.getElementById(pref_f[i]+k))
			{
				if ( (document.getElementById(pref_f[i]+k).type == "text") || (document.getElementById(pref_f[i]+k).type == "textarea") )
				{
					if (document.getElementById(pref_f[i]+k).value == document.getElementById(pref_f[i]+k).defaultValue)
						document.getElementById(pref_f[i]+k).value = "";
				}
			}
		}
	}
	
	//future funding:
	var added_future_funding = document.getElementById('added_future_funding').value;
	for (k=1; k<=added_future_funding; k++)
	{
		var fundinfields = "futtimefrom_month,futtimefrom_year";
		var pref_f = fundinfields.split(",");
		for (i=0;i<pref_f.length;i++)
		{
			if (document.getElementById(pref_f[i]+k))
			{
				if ( (document.getElementById(pref_f[i]+k).type == "text") || (document.getElementById(pref_f[i]+k).type == "textarea") )
				{
					if (document.getElementById(pref_f[i]+k).value == document.getElementById(pref_f[i]+k).defaultValue)
						document.getElementById(pref_f[i]+k).value = "";
				}
			}
		}
	}

	document.getElementById(formid).submit();
}


/* ===================================== DOM ===================================== */

/* attachments */

var attachments_added = 1;

function add_attachment()
{
	attachments_added = attachments_added + 1;	
	var numofadded = attachments_added;
	
	document.getElementById('attachments_added').value = attachments_added;

	var ni = document.getElementById('extra_attachments');

	var newdiv = document.createElement('div');
	var divIdName = 'attachment_div'+numofadded;
	newdiv.setAttribute('id',divIdName);
	
	newdiv.innerHTML = '<input type="file" name="attachment'+numofadded+'" class="file" id="file_'+numofadded+'" value="" /><img src="'+websiteURL+'images/minus.png" alt="" class="minus" title="Remove" onclick="remove_attachment('+numofadded+')" /><br clear="all" />';
	
	ni.appendChild(newdiv);	
}

function remove_attachment(num)
{		
	var d = document.getElementById('extra_attachments');
	var deldiv = document.getElementById('attachment_div'+num);
	d.removeChild(deldiv);	
}


/* cv attachments */

var cv_added = 1;

function add_cv()
{
	cv_added = cv_added + 1;	
	var numofadded = cv_added;
	
	document.getElementById('cv_added').value = cv_added;

	var ni = document.getElementById('extra_cv');

	var newdiv = document.createElement('div');
	var divIdName = 'cv_div'+numofadded;
	newdiv.setAttribute('id',divIdName);
	
	newdiv.innerHTML = '<input type="file" name="cv_attachment'+numofadded+'" id="cv_'+numofadded+'" class="file" value="" /><img src="'+websiteURL+'images/minus.png" title="Remove" alt="" class="minus" onclick="remove_cv('+numofadded+')" /><br clear="all" />';
	
	ni.appendChild(newdiv);	
}

function remove_cv(num)
{		
	var d = document.getElementById('extra_cv');
	var deldiv = document.getElementById('cv_div'+num);
	d.removeChild(deldiv);	
}


/* testimonials */

var testimonials_added = 1;

function add_testimonial()
{
	testimonials_added = testimonials_added + 1;	
	var numofadded = testimonials_added;
	
	document.getElementById('testimonials_added').value = testimonials_added;

	var ni = document.getElementById('extra_testimonials');

	var newdiv = document.createElement('div');
	var divIdName = 'testim_div'+numofadded;
	newdiv.setAttribute('id',divIdName);
	
	newdiv.innerHTML = '<label class="label">Testimonial '+numofadded+'<br/>(No more than 100 words)<br/><img src="'+websiteURL+'images/minus.png" alt="" title="Remove" class="minus_in_label" onclick="remove_testimonial('+numofadded+')" /></label><textarea name="testimonial'+numofadded+'" rows="" cols="" class="textarea"></textarea><br clear="all" />';
	
	ni.appendChild(newdiv);	
}

function remove_testimonial(num)
{		
	var d = document.getElementById('extra_testimonials');
	var deldiv = document.getElementById('testim_div'+num);
	d.removeChild(deldiv);	
}


/* funding */

var funding_added = 1;

function add_funding()
{
	funding_added = funding_added + 1;	
	var numofadded = funding_added;
	
	document.getElementById('added_funding').value = funding_added;

	var ni = document.getElementById('extra_funding');

	var newdiv = document.createElement('div');
	var divIdName = 'fund_div'+numofadded;
	newdiv.setAttribute('id',divIdName);
	
	newdiv.innerHTML = '<p class="bold_12">Funding Until Today #'+numofadded+' <img src="'+websiteURL+'images/minus_sm.png" alt="" title="Remove" class="minus_inline" onclick="remove_funding('+numofadded+')" /></p><label class="label">Funding organization</label><input type="text" name="funding_organization'+numofadded+'" class="input" value="" /><br clear="all" /><label class="label">Amount</label><input type="text" name="funding_amount'+numofadded+'" class="input" value="" /><br clear="all" /><label class="label">Currency</label><input type="text" name="funding_currency'+numofadded+'" class="input" value="" /><br clear="all" /><label class="label">Time Period</label><label class="label_sm">From:</label><input type="text" name="funding_time_period_from_month'+numofadded+'" class="input_sm_gap_w" value="month" id="timefrom_month'+numofadded+'" /><input type="text" name="funding_time_period_from_year'+numofadded+'" class="input_sm" value="year" id="timefrom_year'+numofadded+'" /><br clear="all" /><label class="label">&nbsp;</label><label class="label_sm">To:</label><input type="text" name="funding_time_period_to'+numofadded+'" class="input_med" value="" id="timeperiodto'+numofadded+'" /><br clear="all" /><label class="label">Name of project/activity funded</label><textarea name="project_name_funded'+numofadded+'" rows="" cols="" class="textarea"></textarea><br clear="all" />';
	
	ni.appendChild(newdiv);	
}

function remove_funding(num)
{		
	var d = document.getElementById('extra_funding');
	var deldiv = document.getElementById('fund_div'+num);
	d.removeChild(deldiv);	
}



/* future funding */

var future_funding_added = 1;

function add_future_funding()
{
	future_funding_added = future_funding_added + 1;	
	var numofadded = future_funding_added;
	
	document.getElementById('added_future_funding').value = future_funding_added;

	var ni = document.getElementById('extra_future_funding');

	var newdiv = document.createElement('div');
	var divIdName = 'futfund_div'+numofadded;
	newdiv.setAttribute('id',divIdName);
	
	newdiv.innerHTML = '<p class="bold_12">Future Funding #'+numofadded+' <img src="'+websiteURL+'images/minus_sm.png" alt="" title="Remove" class="minus_inline" onclick="remove_future_funding('+numofadded+')" /></p><label class="label">Funding organization</label><input type="text" name="future_funding_organization'+numofadded+'" class="input" value="" /><br clear="all" /><label class="label">Amount</label><input type="text" name="future_funding_amount'+numofadded+'" class="input" value="" /><br clear="all" /><label class="label">Currency</label><input type="text" name="future_funding_currency'+numofadded+'" class="input" value="" /><br clear="all" /><label class="label">Time Period</label><label class="label_sm">From:</label><input type="text" name="future_funding_time_period_from_month'+numofadded+'" class="input_sm_gap_w" value="month" id="futtimefrom_month'+numofadded+'" /><input type="text" name="future_funding_time_period_from_year'+numofadded+'" class="input_sm" value="year" id="futtimefrom_year'+numofadded+'" /><br clear="all" /><label class="label">&nbsp;</label><label class="label_sm">To:</label><input type="text" name="future_funding_time_period_to'+numofadded+'" class="input_med" value="" id="futtimeperiodto'+numofadded+'" /><br clear="all" /><label class="label">Name of project/activity funded</label><textarea name="future_funding_project_name'+numofadded+'" rows="" cols="" class="textarea"></textarea><br clear="all" />';
	
	ni.appendChild(newdiv);	
}

function remove_future_funding(num)
{		
	var d = document.getElementById('extra_future_funding');
	var deldiv = document.getElementById('futfund_div'+num);
	d.removeChild(deldiv);	
}


