// JavaScript Document
function cv_upload_fileExt(path) 
{
    return path.substr(path.lastIndexOf(".") + 1);
}
function cv_upload_validateCVForm()
{
	var cv = document.getElementById("cv_upload_cv");	
	var letter = document.getElementById("cv_upload_letter");
	if(cv.value == "")
	{
		alert("CV prisegti būtina.");
		return false;
	}
	
	if(!(cv_upload_fileExt(cv.value)== "doc" || cv_upload_fileExt(cv.value)== "pdf" || cv_upload_fileExt(cv.value)== "xls"))
	{		
		alert("Neteisingas failo tipas (turi būti .doc, .xls arba .pdf)");
		return false;
	}
	if( cv_upload_fileExt(letter.value!=""))
	{
		if(!(cv_upload_fileExt(letter.value) == "doc" || cv_upload_fileExt(letter.value) == "pdf" || cv_upload_fileExt(letter.value) == "xls"))
		{
			alert("Neteisingas failo tipas (turi būti .doc, .xls arba .pdf)");
			return false;
		}						
	}
	return true;
}
function getMotLaisk(guid)
{
	window.open('http://www.naujasdarbas.lt/employers/mot_view.php?guid='+guid,'_blank', 'width=870,height=600,menubar=no,resizable=yes,scrollbars=yes,toolbar=no,top=100,left=100');
}
		
		function urlencode(str) {                                   
		    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
		    var ret = str.toString();
		    var replacer = function(search, replace, str) {
		        var tmp_arr = [];
		        tmp_arr = str.split(search);
		        return tmp_arr.join(replace);
		    };
		    histogram['!']   = '%21';
		    histogram['%20'] = '+';
		    ret = encodeURIComponent(ret);
		    for (search in histogram) {
		        replace = histogram[search];
		        ret = replacer(search, replace, ret) // Custom replace. No regexing
		    }
		    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
		        return "%"+m2.toUpperCase();
		    });		    
		    return ret;
		}
		
		function implode( glue, pieces ) 
		{
		    return ( ( pieces instanceof Array ) ? pieces.join ( glue ) : pieces );
		}
		
		function isArray(obj) 
		{
		   if (obj.constructor.toString().indexOf("Array") == -1)
		      return false;
		   else
		      return true;
		}
		/*
		function getMultiple(id)
		{
			var citySelected = new Array();
			var ob = document.getElementById(id);	
			while (ob.selectedIndex != -1) 
			{ 
			//	if (ob.selectedIndex != 0)
					citySelected.push(ob.options[ob.selectedIndex].value); 
				ob.options[ob.selectedIndex].selected = false; 	
			}		
			return implode(',', citySelected);	
		}*/
		
		/****/
		function getMultiple(id)
		{
			var citySelected = new Array();
			var cities = new Array();
			var i = 0;
			
			var ob = document.getElementById(id);	
			while (ob.selectedIndex != -1) 
			{ 
				var nr = ob.selectedIndex;
				cities.push(nr);			
			//	if (ob.selectedIndex != 0)
					citySelected.push(ob.options[nr].value); 
				ob.options[nr].selected = false;
				
			}		
				
			for(i=0;i<cities.length;i++)
			{
				ob.options[parseInt(cities[i])].selected = "1";
			}
			
			return implode(',', citySelected);	
		}
		/****/
		
   		function getValue(id)
		{
			var obj = document.getElementById(id);
			return obj.value;
		}
		
		function getCheckbox(id)
		{
			var obj = document.getElementById(id);
			if(obj.checked==1) return 1;
			return 0;
		}
		
		function doSearch(id, sec)
		{
			location.href = "/darbdavys/"+id+"&sec="+sec+"&search=true&company="+encodeURIComponent(getValue("company"))+"&cities="+getMultiple("cities")+"&areas="+getMultiple("areas")+"&workers="+getValue("workers")+"&rate_from="+getValue("rate_from")+"&rate_to="+getValue("rate_to")+"&video="+getCheckbox("video_checkbox")+"&show="+getValue("show");
			return false;
		}
		
		function doSimpleSearch(id, sec)
		{
			location.href = "/darbdavys/"+id+"&sec="+sec+"&search=true&company="+encodeURIComponent(getValue("company"))+"&search_field="+jQuery("input[@name=search_field]:checked").val();
			return false;
		}
		function doSearchCount(id)
		{
			jQuery.ajax({
				type: "POST",
				url: "../employers/search_ajax.php",
			//	data: "name=John&location=Boston",
				data: "company="+getValue("company")+"&cities="+getMultiple("cities")+"&areas="+getMultiple("areas")+"&workers="+getValue("workers")+"&rate_from="+getValue("rate_from")+"&rate_to="+getValue("rate_to")+"&video="+getCheckbox("video_checkbox")+"&show="+getValue("show"),
				success: function(msg){
					jQuery("#rezultatai").html(msg);
				}
			});			
		}
		
// });

function sort_redirect(url, value)
{	
	window.location = url+value;
}

function simple_redirect(url)
{
	window.location = url;
}

function submitLoginForm()
{
	document.mainlogin.submit();
}


function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function searchDetail(elem, helper, id, sec)
{
/*	if(!notEmpty(elem, helper))
	{
		return false;
	}*/
	doSearch(id, sec);
	return false;
}

function searchSimple(elem, helper, id, sec)
{
/*	if(!notEmpty(elem, helper))
	{
		return false;
	}*/
	doSimpleSearch(id, sec);
	return false;	
}

function popupStaticVarEditor(id)
{
	window.open("/templates/static_var_editor.php?id="+id, null, "height=360, width=760, status=no, toolbar=no, menubar=no, location=no, screenX=400, screenY=200");
}
