// Javascript Functions
// Helios Design http://www.heliosdesign.ro/

function searchDefault(hasFocus) {
	var defaultValue = 'cauta';
	search_field = document.getElementById('q');
	var q = search_field.value;
	if (hasFocus == 'focus') {
		if (q == defaultValue) search_field.value = '';
	}
	else {
		if (q == '') search_field.value = defaultValue;
	}
}

function confirm_action(path, text) {
	if(text == '') {
		text = 'Confirmati ?';
	}
	var answer = confirm (text);
	if (answer) {
	  location.href = path;
	}
}
function open_popup(path, file_name) {


    var width = 500;
    var height = 400;

	var winleft = (screen.width - width) / 2;
	if (winleft < 0) winleft = 0;
	var wintop = (screen.height - height) / 2;
	if (wintop < 0) wintop = 0;
    //alert(winleft);
    var parameters = "width="+width+", height="+height+", left="+winleft+", top="+wintop+", toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes";
	//window.open(\'includes/open_popup.php?path=download/051124_214101_29.jpg\',\'name\',\'width=+width+, height=+height+, left=+winleft+, top=+wintop+, toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes\');

	var file_to_open = '/main/open_popup.php?directory='+path+'&file_name='+file_name;

	window.open(file_to_open, '', parameters);
}

//		FORM VALIDATION

function trim(sString) {

	while (sString.substring(0,1) == ' ')
		{
		sString = sString.substring(1, sString.length);
		}
	while (sString.substring(sString.length-1, sString.length) == ' ')
		{
		sString = sString.substring(0,sString.length-1);
		}
	return sString;
}

function empty_field(val) {

    if(trim(val) == ""){return false;}
	return true;
    }

function check_email(value) {

	var x = value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else return false;
}

function check_date(value) {
	//	minimal date check
	var x = value;
	var filter  = /^([0-3]{1})+([0-9]{1})+(\/)+([0-1]{1})+([0-9]{1})+(\/)+([0-9]{4})+$/;
	if (filter.test(x)) return true;
	else return false;
}

function check_accreditation_form() {                                                // verificare date formular   xxxxxxxxxxxxx

     if (empty_field(document.accreditation_form.last_name.value) == false) {
     		alert("Va rugam sa completati campul 'Nume' !");
        	return false;
    	}
     if (empty_field(document.accreditation_form.first_name.value) == false) {
     		alert("Va rugam sa completati campul 'Prenume' !");
        	return false;
    	}
     if (empty_field(document.accreditation_form.birth_date.value) == false) {
     		alert("Va rugam sa completati campul 'Data nasterii' !");
        	return false;
    	}

   	if (check_date(document.accreditation_form.birth_date.value) == false) {
     		alert("Format incorect. Data nasterii trebuie sa fie in format ZZ/LL/AAAA !");
        	return false;
    	}
    if (empty_field(document.accreditation_form.birth_place.value) == false) {
     		alert("Va rugam sa completati campul 'Locul nasterii' !");
        	return false;
    	}

    if (empty_field(document.accreditation_form.nationality.value) == false) {
     		alert("Va rugam sa completati campul 'Nationalitatea' !");
        	return false;
    	}

    if (empty_field(document.accreditation_form.identity_act_no.value) == false) {
     		alert("Va rugam sa completati campul 'Act de identitate: Serie / Numar' !");
        	return false;
    	}

    if (empty_field(document.accreditation_form.address_1_address.value) == false) {
     		alert("Va rugam sa completati campul 'Adresa redatiei: Adresa' !");
        	return false;
    	}
    if (empty_field(document.accreditation_form.address_1_email.value) == false) {
     		alert("Va rugam sa completati campul 'Adresa redatiei: Email' !");
        	return false;
    	}
    if (check_email(document.accreditation_form.address_1_email.value) == false) {
     		alert("Format invalid in campul 'Adresa redatiei: Email' !");
        	return false;
    	}
    if (empty_field(document.accreditation_form.address_1_phone.value) == false) {
     		alert("Va rugam sa completati campul 'Adresa redatiei: Telefon' !");
        	return false;
    	}
    if (empty_field(document.accreditation_form.address_1_fax.value) == false) {
     		alert("Va rugam sa completati campul 'Adresa redatiei: Fax' !");
        	return false;
    	}

//		LOCUL DE MUNCA

    if (empty_field(document.accreditation_form.work_institution.value) == false) {
     		alert("Va rugam sa completati campul 'Denumirea institutiei' !");
        	return false;
    	}
	if (empty_field(document.accreditation_form.work_contact_person.value) == false) {
     		alert("Va rugam sa completati campul 'Persoana de contact' !");
        	return false;
    	}

//		DROP LISTS

    if (document.accreditation_form.media_statute_type_id.value == ''
    	&& empty_field(document.accreditation_form.media_statute_type_other.value) == false) {
     		alert("Va rugam sa selectati sau completati, dupa caz, campul 'Statut / actionariat' !");
        	return false;
    	}
    if (document.accreditation_form.media_institution_type_id.value == ''
    	&& empty_field(document.accreditation_form.media_institution_type_other.value) == false) {
     		alert("Va rugam sa selectati sau completati, dupa caz, campul 'Tip institutie' !");
        	return false;
    	}
    if (document.accreditation_form.media_function_type_id.value == ''
    	&& empty_field(document.accreditation_form.media_function_type_other.value) == false) {
     		alert("Va rugam sa selectati sau completati, dupa caz, campul 'Functia' !");
        	return false;
    	}

	return true;
}

function check_contact_form() {                                                // verificare date formular CONTACT

     if (empty_field(document.contact.name.value) == false) {
     		alert("Va rugam sa completati campul 'Nume' !");
        	return false;
    	}
     if (empty_field(document.contact.email.value) == false) {
     		alert("Va rugam sa completati campul 'Email' !");
        	return false;
    	}
     if (check_email(document.contact.email.value) == false) {
     		alert("Adresa de email nu este valida!");
        	return false;
    	}
     if (empty_field(document.contact.msg.value) == false) {
     		alert("Va rugam sa completati campul 'Mesaj' !");
        	return false;
    	}

	return true;
}

function show_hide_div_content(div_id) {
		
	var div_less = div_id + '_less';
	var div_more = div_id + '_more';
	
	var link_less = div_id + '_link_less';
	var link_more = div_id + '_link_more';
		
	if (document.getElementById)
	{
		// this is the way the standards work
	
		if(document.getElementById(div_less).style.display == "block") {
			
			document.getElementById(div_less).style.display = "none";
			//document.getElementById(div_more).style.display = "block";
			
			document.getElementById(link_less).style.display = "none";
			document.getElementById(link_more).style.display = "block";
			
		}
		else {
			document.getElementById(div_less).style.display = "block";
			//document.getElementById(div_more).style.display = "none";
			
			document.getElementById(link_less).style.display = "block";
			document.getElementById(link_more).style.display = "none";		
		}
	}

	else if (document.all)
	{
		// this is the way old msie versions work

		if(document.getElementById(div_less).style.display == "block") {
			
			document.all[div_less].style.display = "none";
			document.all[div_more].style.display = "block";
		}
		else {
			document.all[div_less].style.display = "block";
			document.all[div_more].style.display = "none";
		}		
	}

	else if (document.layers)
	{
		// this is the way nn4 works
		
		if(document.getElementById(div_less).style.display == "block") {
			
			document.layers[div_less].style.display = "none";
			document.layers[div_more].style.display = "block";
		}
		else {
			document.layers[div_less].style.display = "block";
			document.layers[div_more].style.display = "none";
		}
		
	}

}
