<?php
session_start();
include('config.php');
//$IntegralPath = $_REQUEST['IntegralPathWeb'];
require_once($IntegralPath.'config.php');
require_once($IntegralPath.'database.php');
$_REQUEST['servicemod'] = 'applicants_online';
require_once($IntegralPath.'functions/DataBaseFormattedOutput.fnc.php');
require_once($IntegralPath.'functions/UserEditPermissions.fnc.php');
require_once($IntegralPath.'functions/GetSystemUser.fnc.php');
require_once($IntegralPath.'functions/GetCurrentSchoolData.fnc.php');
require_once($IntegralPath.'functions/RequestUserPreferences.fnc.php');
require_once($IntegralPath.'functions/SystemInputProperties.fnc.php');
function LoadFunctions()
{
    // Load functions.
    if($handle = opendir($staticpath."functions/"))
    {
            while (false !== ($file = readdir($handle)))
            {
                    // if filename isn't '.' '..' or in the Ignore list... load it.
                    if(preg_match("/.*\.php$/i",$file))
                    {
                            $file = str_replace(".#","",$file);
                            require_once($staticpath."functions/$file");
                    }
            }
    }
}
function dump($log = 'NULL',$logf='dump'){
        $FP = fopen($logf.'.log','w');
        ob_start();
        //var_dump($_REQUEST);
        echo "---------------------------------------------------------------\n";
        var_dump($log);
        $header = @ob_get_contents();
        ob_end_clean();
        fwrite($FP,$header);
        fclose($FP);
}

function GeneratePassword()
{
	$j=0;
	for($i=97;$i<>122;$i++){
		$mas[$j] = chr($i);
		$j++;
	}
	$rand_keys = array_rand ($mas, count($mas));
	$str = "";
        $count = rand(7,12);
	for($i=0;$i<>$count;$i++){
		$str.=$mas[$rand_keys[$i]];
		if(rand(0,1) == true)
			$str.=rand(0,9);
	}
	return $str;
}
require_once($IntegralPath.'gettext.fnc.php');
if(substr($_REQUEST['num'],0,3)<>'new' && is_numeric($_REQUEST['num']))
{
    $data_faculty = db_request(db_execute("SELECT * FROM student_spec_fields WHERE ID = '".$_REQUEST['num']."' AND  SCHOOL_YEAR='".get_school_year()."'"));
    $data_faculty = $data_faculty[1];
}
$function = $_REQUEST['function_'];

$msg = ''._("Unknown error").'. '._('System failure');
switch($function)
{
case 'search_school':
        $school_id = $_POST['cast_faculty']['SCHOOL_ID'];
        $json = array();
        $json[] = '{"id": "", "label": "'._('N/A').'"}';
        if($school_id<>'')
        {
            //cast_faculty[SCHOOL_ID]
            $sql = "SELECT ID,SHORT_NAME,TITLE,SCHOOL_YEAR FROM FACULTY WHERE SCHOOL_ID='".intval($school_id)."' AND  SCHOOL_YEAR='".get_school_year()."';";
            $faculty = db_request(db_execute($sql));
            foreach($faculty as $index=>$faculty){
                $sel = ($faculty['ID']==$data_faculty['FACULTY_ID'])?', "selected": "true"':'';
                $json[] = '{ "id": "' . $faculty['ID'] . '", "label": "' . $faculty['TITLE'] . '  ['.$faculty['SHORT_NAME'].']"'.$sel.' }';
            }
        }
      $msg = '[' . implode(',', $json) . ']';
break;
case 'search_faculty':
    $json = array();
    $json[] = '{"id": "", "label": "'._('N/A').'"}';
    $faculty_id = $_POST['cast_faculty']['FACULTY_ID'];
    if($faculty_id<>'')
    {
        $sql = "SELECT ID,TITLE,SHORT_NAME,SCHOOL_YEAR FROM specialty WHERE FACULTY_ID='".intval($faculty_id)."' AND  SCHOOL_YEAR='".get_school_year()."' ";
        $faculty = db_request(db_execute($sql));
        foreach($faculty as $index=>$faculty)
        {
            $sel = ($faculty['ID']==$data_faculty['SPECIALTY_ID'])?', "selected": "true"':'';
            $json[] = '{ "id": "' . $faculty['ID'] . '", "label": "' . $faculty['TITLE'] . '  ['.$faculty['SHORT_NAME'].']"'.$sel.' }';
        }
    }
    $msg =  '[' . implode(',', $json) . ']';
    if($msg=='[]')
        $msg = '[{ "id": "0", "label": "'._('There are no available majors').'"'.$sel.', "selected": "true" }]';
break;
case 'search_specialty':
    $specialty_id = $_POST['cast_faculty']['SPECIALTY_ID'];
    $json = array();
    $json[] = '{"id": "", "label": "'._('N/A').'"}';
    if($specialty_id<>'')
    {
        $sql = "SELECT ID,SHORT_NAME,TITLE,SCHOOL_YEAR FROM SPECIALIZATION WHERE SPECIALTY_ID='".intval($specialty_id)."' AND  SCHOOL_YEAR='".get_school_year()."';";
        $specializations = db_request(db_execute($sql));

        foreach($specializations as $index=>$specialization){
            $sel = ($specialization['ID']==$data_faculty['SPECIALIZATION_ID'])?', "selected": "true"':'';
            $json[] = '{ "id": "' . $specialization['ID'] . '", "label": "' . $specialization['TITLE'] . '  ['.$specialization['SHORT_NAME'].']"'.$sel.' }';
        }
    }
    $msg = '[' . implode(',', $json) . ']';
break;
case 'search_specializacion':
        $specialization_id = $_POST['cast_faculty']['SPECIALIZATION_ID'];
        $json = array();
        $json[] = '{"id": "", "label": "'._('N/A').'"}';
        if($specialization_id<>'')
        {
            $sql = "SELECT ID,TYPE_OF_STUDY,SYSTEM_CODE FROM specialization WHERE ID='".intval($specialization_id)."' AND  SCHOOL_YEAR='".get_school_year()."';";
            unset($array);
            $array = db_request(db_execute($sql));
            $array = $array[1];
            $sql = "SELECT ID,TITLE,SYSTEM_CODE FROM TYPE_OF_STUDY WHERE SCHOOL_YEAR='".get_school_year()."'ORDER BY TITLE";
            $QI = db_execute($sql);
            $tos_array = db_request($QI);
            foreach($tos_array as $value)
            {
                if(strpos($array['TYPE_OF_STUDY'],','.$value['ID'].',')!==false)
                    $json[] = '{ "id": "' . $value['ID'] . '", "label": "' . $value['TITLE'].'  ['.$value['SYSTEM_CODE'].']" }';
            }
        }
        $msg = '[' . implode(',', $json) . ']';
break;
case 'search_edu_forms':
    $specialization_id = $_POST['cast_faculty']['SPECIALIZATION_ID'];
    $json = array();
    $json[] = '{"id": "", "label": "'._('N/A').'"}';
    if($specialization_id<>'')
    {
        $sql = "SELECT DISTINCT aef.ID,aef.TITLE FROM edu_forms_in_spec efis,APP_EDU_FORMS aef WHERE efis.SCHOOL_YEAR='".get_school_year()."' AND efis.SPECIALIZATION_ID='".intval($specialization_id)."' AND efis.EDU_FORM_ID=aef.ID";
        unset($array);
        $array = db_request(db_execute($sql));
        foreach($array as $value)
            $json[] = '{ "id": "' . $value['ID'] . '", "label": "' . $value['TITLE'].'  ['.$value['SYSTEM_CODE'].']" }';
    }
    $msg = '[' . implode(',', $json) . ']';
break;
case 'get_script_search':
        $msg = "var school = jQuery('#inputapp_school_id');\n";
        $msg .= "var faculty = jQuery('#inputfaculty_id');\n";
        $msg .= "var specialty = jQuery('#inputmajor_id');\n";
        $msg .= "var specializacion = jQuery('#inputspecializacion_id');\n";
        $msg .= "var tos_id = jQuery('#inputtos_id');\n";
        $msg .= "var edu_form_id= jQuery('#inputedu_form_id');\n";

        $msg .= "school.selectChain({\n";
        $msg .= "target: faculty,\n";
        $msg .= "url: 'search_ajax.php',\n";
        $msg .= "type: 'post',\n";
        $msg .= "data: { NA:'"._('N/A')."',ajax: true, function_:'search_school', num:'new'}\n";
	$msg .= "}).trigger('change');\n";

        $msg .= "faculty.selectChain({\n";
        $msg .= "target: specialty,\n";
        $msg .= "url: 'search_ajax.php',\n";
        $msg .= "type: 'post',\n";
        $msg .= "data: { NA:'"._('N/A')."',ajax: true, function_:'search_faculty', num:'new'}\n";
	$msg .= "}).trigger('change');\n";

        $msg .= "specialty.selectChain({\n";
        $msg .= "target: specializacion,\n";
        $msg .= "url: 'search_ajax.php',\n";
        $msg .= "type: 'post',\n";
        $msg .= "data: { NA:'"._('N/A')."',ajax: true, function_:'search_specialty', num:'new', report:'true'}\n";
	$msg .= "}).trigger('change');\n";

        $msg .= "specializacion.selectChain({\n";
        $msg .= "target: tos_id,\n";
        $msg .= "url: 'search_ajax.php',\n";
        $msg .= "type: 'post',\n";
        $msg .= "data: { NA:'"._('N/A')."',ajax: true, function_:'search_specializacion', num:'new', report:'true'}\n";
	$msg .= "}).trigger('change');\n";

        $msg .= "specializacion.selectChain({\n";
        $msg .= "target: edu_form_id,\n";
        $msg .= "url: 'search_ajax.php',\n";
        $msg .= "type: 'post',\n";
        $msg .= "data: { NA:'"._('N/A')."',ajax: true, function_:'search_edu_forms', num:'new', report:'true'}\n";
	$msg .= "}).trigger('change');\n";
    break;
}
echo $msg;
?>