
function srcPresign() {
	var p;
	
	p = $('#signinPassword',portalSpace).val();
	$('#signinPassword',portalSpace).val(portal_sha1(p));
	return true;
}

function srcLanguage(){
	var lng, curhref, href;
	
	lng = $('#_srcLanguages');
	lng= $('option:selected', lng).val();
	curhref = window.location.href;
	if (curhref.indexOf('?') != -1) {
		curhref = curhref.split('?');
		href = curhref[0];
		curhref[0] = '';
		curhref = curhref.join('');
	}
	else {
		href = curhref;
		curhref = '';
	}
	if (curhref=='') {
		href += '?lng='+lng;
	}
	else if (curhref.substr(0,4)=='lng=' && curhref.indexOf('&')==-1) {
		href += '?lng='+lng;
	}
	else {
		curhref = curhref.split('&');
		for (var i = 0; i < curhref.length; i++) {
			if (curhref[i].substr(0, 4) == 'lng=' || curhref[i] == '') {
				curhref[i] = '';
			}
			else {
				curhref[i] = '&' + curhref[i];
			}
		}
		curhref = curhref.join('');
		href += '?' + curhref + '&lng='+lng;
	}
	window.location.href = href;
}

function srcPageSize() {
	var state;

	state = getState();	
	if (state.sf!='' && state.fp!='' && state.tb!='' && state.pp!='') {
		window.location.href = getHref(state,'');
	}
	return false;
}

function srcTableSearch() {
	var state;
	
	state = getState();
	if (state.sf!='' && state.fp!='' && state.tb!='') {
		window.location.href = getHref(state,'');
	}
	return false;
}

function srcTableSearchClear() {
	var state;
	
	state = getState();
	if (state.fp!='' && state.tb!='' && state.sq!='') {
		state.sq = '';
		window.location.href = getHref(state,'');
	}
	return false;
}

function srcToolAdd() {
	var state;
	
	state = getState();
	if (state.form!='' && state.tb!='') { 
		state.fp = state.form;
		window.location.href = getHref(state, 'id=*');	
	}
	return false;
}

function srcToolTranslate() {
	var state;
	
	state = getState();	
	if (state.fp!='' && state.tb!='' && state.trname!='') {
		window.location.href = getHref(state, 'id=*' + '&key=' + state.trname);
	}
	return false;
}

function srcTranslationKeyChange() {
	var state;
	
	state = getState();	
	if (state.trval!='' && state.fp!='') {
		state.fp = state.form;
		window.location.href = getHref(state, 'id=' + state.trval);
	}
	return false;
}

function srcToolClose(){
	var state;
	
	state = getState();
	if (state.fp!='') {
		if (!state.notlister) {state.tb = state.rettb;}
		else {state.tb = '';}
		window.location.href = getHref(state,'');
	}
	return false;
}

(function ($) {
  $.fn.fixOverflow = function() {
    if ($.browser.msie) {
      return this.each(function() {
        if (this.scrollWidth > this.offsetWidth) {
          $(this).css({'padding-bottom':'20px', 'overflow-y':'hidden'});
        }
      });            
    } else {return this;}
  };
})(jQuery);


function getState() {	
	var el, state = {};
	
	state.tb = $('#_currentTable', portalSpace).val();
	state.fp = $('#_frontPage', portalSpace).val();
	state.form = $('#_formPage', portalSpace).val();
	state.cp = $('#_currentPage', portalSpace).val();
	state.pp = $('#_pageSize', portalSpace).val();
	state.rettb = $('#_ret_tb', portalSpace).val();
	state.notlister = $('#_notlister', portalSpace).val();
	state.sq = $('#_searchText', portalSpace).val();
	el = $('#_searchField', portalSpace);
	state.sf = $('option:selected', el).val();
	if (!state.sf) state.sf=$('#_searchField', portalSpace).val();

	el = $('#_srcTranslationKeysList', portalSpace);
	state.trname = $('option:selected', el).text();
	state.trval = el.val();
	
	return state;	
}

function getHref(state, spec) {
	var i=-1, args=[], href;
	
	if (state.fp!==undefined && state.fp=='') {return '';}
	if (state.tb!==undefined && state.tb!='') {i++;args[i] = 'tb=' + state.tb;}
	if (state.sq!==undefined && state.sq!='') {i++;args[i] = 'sq=' + state.sq;}
	if (state.cp!==undefined && state.cp!='') {i++;args[i] = 'cp=' + state.cp;}
	if (state.pp!==undefined && state.pp!='') {i++;args[i] = 'pp=' + state.pp;}
	if (state.sf!==undefined && state.sf!='') {i++;args[i] = 'sf=' + state.sf;}
	if (state.notlister!==undefined && state.notlister!='') {i++;args[i] = 'notlister=' + state.notlister;}
	href = state.fp;
	if (i>-1) {
		if (spec!='') {spec +='&';}
		href += '?' +spec + args.join('&');
	}
	else if (spec!='') {
		href += '?' +spec;
	}
	return href;
}

function srcFormValidate(ev) {
	
	$('input', ev.target).not('input:hidden, file').foreach(function () {
		
	});
}


//dateStr = Input value to be parsed
//format = date format, ie mm/dd or dd/mm
function dateComponents(dateStr, format) {
	
	var results = new Array();
	var datePat = /^(\d{1,2})(\/|-|\.)(\d{1,2})\2(\d{2}|\d{4})$/;
	var matchArray = dateStr.match(datePat);
	if (matchArray == null) { 
		return null; 
	}
	//check for two digit (21th century) years and prepend 20.
	matchArray[4] = (matchArray[4].length == 2) ? '20' + matchArray[4] : matchArray[4];
	// parse date into variables
	if (format.charAt(0)=="d"){ //what format does the server use for dates? 
		results[0] = matchArray[1];
		results[1] = matchArray[3];
	} else { 
		results[1] = matchArray[1];
		results[0] = matchArray[3];}
	results[2] = matchArray[4];
	return results;
}

//obj = Input whose value is to be checked
//min = Optional minimum allowed value
//max = Optional maximum allowed value
//format = date format, ie mm/dd or dd/mm
function valiDate(obj, min, max, format){
	
	dateBits = dateComponents(obj.value, format);
	if (dateBits == null) return false;
	day = dateBits[0]; //Check it is a valid date first
	month = dateBits[1];
	year = dateBits[2];
	if ((month < 1 || month > 12) || (day < 1 || day > 31)) { // check month range 
		return false;
	} 
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		return false;
	}
	if (month == 2) {
	// check for february 29th 
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); 
		if (day>29 || (day==29 && !isleap)) {
			return false;
		}
	} 
	//Now check whether a range is specified and if in bounds
	var theDate = new Date(dateBits[2], parseInt(dateBits[1]) - 1, dateBits[0]);
	if ( min ) {
		minBits = dateComponents (min, format);
		var minDate = new Date(minBits[2], parseInt(minBits[1]) - 1, minBits[0]);
		if ( minDate.getTime() > theDate.getTime() ) return false;
	} 
	if ( max) {
		maxBits = dateComponents (max, format);
		var maxDate = new Date(maxBits[2], parseInt(maxBits[1]) - 1, maxBits[0]);
		if ( theDate.getTime() > maxDate.getTime() ) return false;
	}
	return true;
}

//obj = Input whose value is to be checked
function validateEmail( obj ) {
	
//	var emailStr = obj.value;
//	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
//	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
//	if ( !reg1.test( emailStr ) && reg2.test( emailStr ) ) {
//		return true;
//	} else {
//	 	return false;
//	}

    document.getElementById('emailAlert').style.display='none';
    //if(obj!=''){
        var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
        if (obj.search(emailRegEx) == -1) {
             document.getElementById('emailAlert').style.display='';
             return false;

        }
         else return true;
    //}
}

function validateUsername (value){
    document.getElementById('usernameWhiteSpaceAlert').style.display='none';
    document.getElementById('usernameCyrilicAlert').style.display='none';
    var userRegEx = /^([a-zA-Z0-9_-]+)$/;
    //if(value!=''){
        if (value.search(/\s/g)!=-1){
        document.getElementById('usernameWhiteSpaceAlert').style.display='';
        return false;
        }
        else
            if(userRegEx.test(value)==false){
                document.getElementById('usernameCyrilicAlert').style.display='';
                return false;
            }
        else{
            if (value.toString().length > 20){
                document.getElementById('usernameLengthAlert').style.display='';
                return false;
            }
            else
                return true;
        }
   // }
}

function validatePassword (value){
    document.getElementById('passwordWhiteSpaceAlert').style.display='none';
    document.getElementById('passwordCyrilicAlert').style.display='none';
    var passRegEx = /^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$/;
    //if(value!=''){
        if (value.search(/\s/g)!=-1){
        document.getElementById('passwordWhiteSpaceAlert').style.display='';
        return false;
        }
        else
            if(passRegEx.test(value)==false){
                document.getElementById('passwordCyrilicAlert').style.display='';
                return false;
            }
        else{
            if (value.toString().length < 6){
                document.getElementById('passwordLengthAlert').style.display='';
                return false;
            }
            else
                return true;
        }
   // }
}



var portalSpace;

jQuery(function ($) {
	
	portalSpace = $('#srcPortalSpace');
	
	$(portalSpace).fixOverflow();
	
	$('#srcToolbar div.rightTool', portalSpace)
		.css({'cursor':'pointer'})
		.hover(
			function() {$(this).css({'background-color': '#c3d9ff'});},
			function() {$(this).css({'background-color': '#ffffff'});}
		);
	
	$('.srcMessageBoxes, .alertRow, .infoRow', portalSpace).click(function() {$(this).hide();});
	
	$('#_searchText',portalSpace).keypress(function(ev) {
		if (ev.which==13) {srcTableSearch();}
	});
	
	if ($.datepicker) {
		$.datepicker.setDefaults({
   			firstDay: 1,
   			dateFormat: 'dd.mm.yy',
   			changeYear: true,
   			yearRange: '1900:2100',
			changeMonth: true,
			gotoCurrent: true});
			
		$('.inputDate', portalSpace).datepicker();
	}

	//$('#srcForm, #srcSigninForm', portalSpace).submit(srcFormValidate);

	$('input[type="text"]:first', portalSpace).focus();
	
});


function toggleSignon( displayMode ) {
    displayMode.form.action="signup.php?usertype="+displayMode.value;
    displayMode.form.submit();
}


/*Date:01.02.2010
 Jscript function for EDB and EMBG validation */
function checkEDBEMBG(matBroj)
 {
     document.getElementById('divEMBGalert').style.display='none';
     document.getElementById('divEDBalert').style.display='none';      
     userType = document.getElementById("USERTYPE").value;
    if(userType==0)  //for private subjects  perform check based on modulo 13
    {
        if((matBroj.length != 13) || (matBroj.substring(0,2)>31)){
            document.getElementById('divEMBGalert').style.display='';
                return false;
        }else{
           sum = (7 * matBroj.substring(0,1)) + (6 * matBroj.substring(1,2)) + (5 * matBroj.substring(2,3)) + (4 * matBroj.substring(3,4));
           sum = sum + (3 * matBroj.substring(4,5)) + (2 * matBroj.substring(5,6)) + (7 * matBroj.substring(6,7)) + (6 * matBroj.substring(7,8));
           sum = sum + (5 * matBroj.substring(8,9)) + (4 * matBroj.substring(9,10)) + (3 * matBroj.substring(10,11)) + (2 * matBroj.substring(11,12));
           ostatok = sum % 11;
           if(ostatok == 0 | ostatok == 1){
                cifra = 0;
           }else{
                cifra = (11 - ostatok);
           }
           if(cifra != matBroj.substring(12,13)){
                document.getElementById('divEMBGalert').style.display='';  
                return false; 
           }else{return true;}
        }
    }
    else  //if legal entity check in the central register database using checkEDB php script
    {
        count=$.ajax({url: "units/checkEDB.php?q="+matBroj, async: false}).responseText;
        if(count!=1)
       {
          document.getElementById('divEDBalert').style.display=''; 
          return false;
       }
       else
       {
            document.getElementById('divEDBalert').style.display='none'; 
            return true; 
       } 
   } 
 }
 
