// JavaScript Document
function urlencode(str) {
    str = str.replace('&', '%26');
    return str;
}



function create_company_list(value, selected) {
    var callurl = '/ajaxfeed/ax_anketa_controller.php';
    $('#loading').show();
	$.ajax({
  		url: callurl,
	  	data: {branch_code:  value, selected: selected, ajax: 'true'},
  		success: function(j){
			$('#select_tag').html(j);
		},
  		complete: function(){
  			$('#loading').hide();
  		}
	});
}


function unlock_company_input(value) {
    if (value == 'other' && $('#branch').value != '24') {
        $('#add_company').show();
    } else {
        $('#add_company').hide();
        $('#new_company').val('');
    }
}
function unlock_motivator_input() {
    deselect_19();
    if ($('#ch_7').attr('checked','true')) {
        $('motovator_input').html('Cits:<input type="text" class="form_box"  id="ch8_input" name="motovator_input"/>');
    } else if ($('#ch_7').attr('checked','true')) {
        $('motovator_input').html('Cits:<input class="form_grey" type="text" id="ch8_input" name="motovator_input" disabled="disabled"/>');
    }
}


function deselect_other_checkboxes() {
    if($('.ch_none').attr('checked')) {
    	$('.ch_other').attr('checked',false);
    }
}


function deselect_19() {
    $('#ch_6').attr('checked',false);
}


function next_1() {
    var branch = $('#branch').val();
    var company = $('#select_tag').val();
    var new_company = $('#new_company').val();
    if (company=='other') {
        company = new_company;
    }
    var post_level = '';
    for (var i = 0; i < document.part1.post_level.length; i++) {
        if (document.part1.post_level[i].checked) {
            post_level = document.part1.post_level[i].value;
        }
    }
    if (branch == "" || company == "") {
        $('#error').html('Laukiem,kas apzimeti ar * ir jabut obligati aizpilditiem!');
    } else {
        var comp = company;
        if (new_company != "") {
            comp = new_company
        }
        var callurl = "/ajaxfeed/ax_anketa_controller.php";
        var enc_comp = comp;//urlencode(comp);
        var params = "branch_save=" + branch + "&company_save=" + comp + "&post_level_save=" + post_level;

	$.ajax({
  		url: callurl,
	  	data: {branch_save:  branch, company_save: comp, post_level_save: post_level, ajax: 'true'},
  		success: function(j){
			window.location.href = '?ide=anketa&part=2';
		}
	});
    }
}
function next_2() {
    var salary = $('#salary').val();
    var colleagues = '';
    colleagues = document.part2.colleagues.value;
    var work_conditions = '';
    work_conditions = document.part2.work_conditions.value;
    var attitude = '';
    attitude = document.part2.attitude.value;
    if (colleagues == 0 || work_conditions == 0 || attitude == 0) {
        $('#error').html('Laukiem,kas apzimeti ar * ir jabut obligati aizpilditiem!');
    } else {
        var callurl = "/ajaxfeed/ax_anketa_controller.php?salary_save=" + salary + "&colleagues_save=" + colleagues + "&work_conditions_save=" + work_conditions + "&attitude_save=" + attitude;


	$.ajax({
  		url: callurl,
	  	data: {salary_save:  salary, colleagues_save: colleagues, work_conditions_save: work_conditions, attitude_save: attitude, ajax: 'true'},
  		success: function(j){
           window.location = '?ide=anketa&part=3';
		},
	});
	}
}
function next_3() {
    m = new Array(document.part3.motivators.length);
    for (var i = 0; i < document.part3.motivators.length; i++) {
        if (document.part3.motivators[i].checked) {
            if (document.part3.motivators[i].value == 'other') {
                m[i] = $('#ch8_input').val();
            } else {
                m[i] = document.part3.motivators[i].value;
            }
        } else {
            if (document.part3.motivators[i].value == 'other') {
                m[i] = '';
            } else {
                m[i] = 24;
            }
        }
    }
    var career_code = '';
    for (var i = 0; i < document.part3.career_code.length; i++) {
        if (document.part3.career_code[i].checked) {
            career_code = document.part3.career_code[i].value;
        }
    }
    if (career_code == "") {
        $('#error').html('Laukiem,kas apzimeti ar * ir jabut obligati aizpilditiem!');
    } else {
        var callurl = "/ajaxfeed/ax_anketa_controller.php";
        params = "m1=" + m[0] + "&m2=" + m[1] + "&m3=" + m[2] + "&m4=" + m[3] + "&m5=" + m[4] + "&m6=" + m[5] + "&m7=" + urlencode(m[6]) + "&career_code=" + career_code;
 

		$.ajax({
			url: callurl,
			type: "POST",
			data: {m1:  m[0], m2: m[1], m3: m[2], m4: m[3], m5: m[4], m6: m[5], m7: m[6], career_code: career_code, ajax: 'true'},
			success: function(j){
                window.location = '?ide=anketa&part=sum';
			},
		});
    }
}
function next_4() {
    var comment = $('#user_comment').val();
    var email = $('#email').val();
    var newsletter = "";
    var nwsval = 0;
    if ($('#newsletter').attr('checked')) {
        newsletter = "newsletter";
        nwsval = 1;
    }
    params = "user_comment=" + urlencode(comment) + "&email=" + email + "&" + newsletter + "=1";
    var callurl = "/ajaxfeed/ax_anketa_controller.php";

		$.ajax({
			url: callurl,
			type: "POST",
			data: {user_comment:  comment, email: email, newsletter: nwsval, ajax: 'true'},
			success: function(j){
            window.location = '?ide=anketa&part=4';
			},
		});
}



function finish_form() {
    var url = "/ajaxfeed/ax_anketa_controller.php?finish=true";
    new Ajax(url, {
        method: 'get',
        onComplete: function () {
            window.location = '?ide=stats';
        }
    }).request();
}
function validate_required(field) {
    with(field) {
        if (value == null || value == "") {
            return false
        } else {
            return true
        }
    }
}
function validate_email(field) {
    with(field) {
        apos = value.indexOf("@");
        dotpos = value.lastIndexOf(".");
        if (apos < 1 || dotpos - apos < 2) {
            return false;
        } else {
            return true;
        }
    }
}
function validate_topic_form(thisform) {
    with(thisform) {
        if (validate_required(name) == false) {
            name.focus();
            doRed('name');
            return false
        }
        if (validate_required(title) == false) {
            title.focus();
            doRed('title');
            return false
        }
        if (validate_required(text) == false) {
            text.focus();
            doRed('text');
            return false
        }
    }
}
function validate_comment_form(thisform) {
    with(thisform) {
        if (validate_required(name) == false) {
            name.focus();
            doRed('name');
            return false
        }
        if (validate_required(text) == false) {
            text.focus();
            doRed('text');
            return false
        }
    }
}
function validate_contact_form(thisform) {
    with(thisform) {
        if (validate_required(name) == false) {
            name.focus();
            doRed('name');
            return false
        }
        if (validate_required(text) == false) {
            text.focus();
            doRed('text');
            return false
        }
        if ((email.value != '') && (validate_email(email) == false)) {
            email.focus();
            doRed('email');
            return false
        }
    }
}
function doRed(id) {
    $('#'+id).style('background-color','#ff9');
}
function expandSearch() {
    $('#searchoptions').show();
}
function rate(star) {
    parentOf = star;
    if (star.title == 0) {
        parentOf = star;
        rating = $('#'+parentOf.id + 'value').val();
    } else {
        rating = star.title;
        parentOf = star.parentElement;
        parentOf = star.parentNode;
    }
    maxrating = 5;
    if (rating == 0) {
        outrating = 1;
    } else {
        outrating = rating;
    }
    $('#'+parentOf.id + 'display').html(outrating);
    for (var i = 1; i <= rating; i++) {
        astar = parentOf.id + i;
        $('#'+astar).attr('src','images/star.gif');
    }
    for (var j = i; j <= maxrating; ++j) {
        astar = parentOf.id + j;
        $('#'+astar).attr('src','images/star-empty.gif');
    }
}
function saveRate(star) {
    rate(star);
    document.getElementById(star.parentNode.id + 'value').value = star.title;
}
function showform(id) {
    document.getElementById(id).style.display = (document.getElementById(id).style.display != 'block') ? 'block' : 'none';
}
function rate2(star, maxrating, barcount) {
    var totalVal = 10;
    for (i = 1; i < 6; i++) {
        valfield = "bar" + i + "value";
        if (document.getElementById(valfield).value) {
            totalVal -= parseInt(document.getElementById(valfield).value);
        }
    }
    parentOf = star;
    if (star.title == 0) {
        parentOf = star;
        rating = document.getElementById(parentOf.id + 'value').value;
    } else {
        parentOf = star.parentElement;
        parentOf = star.parentNode;
        rating = parseInt(star.title);
        var current = parseInt(document.getElementById(parentOf.id + "value").value);
        if (rating - current > totalVal) {
            rating = current + totalVal;
        }
    }
    if (rating < 0) {
        outrating = 0;
    } else {
        outrating = rating;
    }
    document.getElementById(parentOf.id + 'display').innerHTML = outrating;
    for (var i = 1; i <= rating; i++) {
        astar = parentOf.id + i;
        document.getElementById(astar).src = 'images/star.gif';
    }
    for (var j = i; j <= maxrating; ++j) {
        astar = parentOf.id + j;
        document.getElementById(astar).src = 'images/star-empty.gif';
    }
}
function saveRate2(star, totalId) {
    var totalVal = 10;
    for (i = 1; i < 6; i++) {
        valfield = "bar" + i + "value";
        if (document.getElementById(valfield).value) {
            totalVal -= parseInt(document.getElementById(valfield).value);
        }
    }
    var current = parseInt(document.getElementById(star.parentNode.id + 'value').value);
    rate2(star);
    if (parseInt(star.title) == -1) {
        valueGot = 0;
    } else {
        if (parseInt(star.title) > totalVal + current) {
            valueGot = current + totalVal;
            totalVal = 0;
        } else {
            valueGot = star.title;
        }
    }
    document.getElementById(star.parentNode.id + 'value').value = valueGot;
    document.getElementById(totalId).value = totalVal;
    var totalVal = 10;
    for (i = 1; i < 6; i++) {
        valfield = "bar" + i + "value";
        if (document.getElementById(valfield).value) {
            totalVal -= parseInt(document.getElementById(valfield).value);
        }
    }
    if (infof = $('#info')) {
        infof.innerHTML = totalVal;
    }
    if (submitb = $('#poll_submit')) {
        if (totalVal == 0) {
            submitb.disabled = false;
        } else {
            submitb.disabled = true;
        }
    }
}
