var date_el="";
var edit_nick=0;
var edit_alias=0;
var wait=0;
var rem_i=-1;

function go_to(url){
	window.location.href=url;
}

function get_el(id){
	return document.getElementById(id);
}
function call_user_func(cb, parameters) {
    // http://kevin.vanzonneveld.net
    // +   original by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: call_user_func('isNaN', 'a');
    // *     returns 1: true
 
    var func;
 
    if (typeof cb == 'string') {
        if (typeof this[cb] == 'function') {
            func = this[cb];
        } else {
            func = (new Function(null, 'return ' + cb))();
        }
    } else if (cb instanceof Array) {
        func = eval(cb[0]+"['"+cb[1]+"']");
    }
    
    if (typeof func != 'function') {
        throw new Error(func + ' is not a valid function');
    }
 
    return func.apply(null, Array.prototype.slice.call(arguments, 1));
}
function isUnicEmail(email){

	if(email.value.length==0)
		return false;
	else if( isValidEmail(email))
		return unic_field(email,'check_email');
	else
		return false;
	
}
function isUnicLogin(login){

	if(login.value.length==0)
		return false;
	else
		return unic_field(login,'check_login');
	
}
function unic_field(field,func){
	wait=1;
	load_file(site_url+'includes/php/unic_fields.php','unic_field_ok','field='+field.value+'&func='+func+'&reg_key='+reg_key);
	return false;
}
function unic_field_ok(){
	wait=0;
	//alert(result);
	if(result=='ok'){
		checked_fields[i]=require_fields[i].value;
		fail=-1;
		check_edit(check_form);
	}else{
		fail=i;
		check_edit(check_form);
		fail=-1;
	}
}
function isValidEmail (email, strict){
	if ( !strict ) 
		email.value = email.value.replace(/^\s+|\s+$/g, '');
 	return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email.value);
}
function isValidPhone(phone){
	test_phone=phone.value;
	test_phone=test_phone.replace(/([0-9\-\+\(\)\ ])/g, '');
	if(test_phone.length>0)
		return false;
	else
		return true;
}
function isValidMobilePhone(phone){
	if(phone.value.length==0)
		return true;
	else
		return isValidPhone(phone);
}
function view_calendar(){
	
	message=result;
	
	show_message();
	
}
function insert_date(date){
	date_el.value=date;
	show_message();
}
function isValidPasswordConfirm(password_c){
	if(password_c.value!=get_el('edit[password]').value)
		return false;
	else
		return true;
}
function ifChecked(element){
	if(element.checked)
		return true;
	else
		return false;
}
function change_regions(country){
	load_file(site_url+'includes/php/regions.php?language='+language+'&country='+country,'change_regions_ok','');
}
function change_regions_ok(){
	temp.innerHTML=result;
	region=get_el('edit[region]');
	new_region=get_el('regions_list');

	region.options.length=0;
	if(new_region)
		for(i=0;i<new_region.options.length;i++)
			region.options[i]=new Option(new_region.options[i].text,new_region.options[i].value);
	
	temp.innerHTML="";
	
}
function change_nick_name(){
	if(edit_nick==0 || get_el('edit[nick_name]').value.length==0){
		get_el('edit[nick_name]').value=get_el('edit[first_name]').value
		if(get_el('edit[first_name]').value.length>0 && get_el('edit[last_name]').value.length>0)
			get_el('edit[nick_name]').value+=' ';
		get_el('edit[nick_name]').value+=get_el('edit[last_name]').value;
		if(edit_alias==0 || get_el('edit[login]').value.length==0){
			get_el('edit[login]').value=get_el('edit[nick_name]').value;
			check_alias();
		}
	}
}
function check_alias(){
	alias=get_el('edit[login]');
	alias.value=alias.value.replace(/([^0-9\_A-Za-z])/g, '_');
	alias.value=alias.value.replace(/(_)+/g, '_');
	alias.value=alias.value.replace(/(^[_]+)/, '');
	alias.value=alias.value.replace(/([_]+$)/, '');
	alias.value=alias.value.toLowerCase();
}
function check_pass_complexity(pass){
	lowercaseletter = /[a-z]/.test(pass);
	uppercaseletter = /[A-Z]/.test(pass);
	digit 			= /\d/.test(pass);
					
	low_security    = lowercaseletter || uppercaseletter || digit;
	med_security    = (lowercaseletter && uppercaseletter) || (lowercaseletter && digit) || (digit && uppercaseletter);
	high_security   = lowercaseletter && uppercaseletter && digit;
	higher_security = high_security && pass.length >= 6;
					
	if ( low_security ){
		get_el('password_complexity').style.backgroundColor = 'red';
		get_el('password_text').innerHTML='Low complexity';
	}
	if ( med_security ){
		get_el('password_complexity').style.backgroundColor = 'yellow';
		get_el('password_text').innerHTML='Medium complexity';
	}
	if ( high_security ){
		get_el('password_complexity').style.backgroundColor = '#C1DFF0';
		get_el('password_text').innerHTML='Good complexity';
	}
	if ( higher_security ){
		get_el('password_complexity').style.backgroundColor = 'green';
		get_el('password_text').innerHTML='High complexity';
	}
}
function isFloat(field){
	
	field.value=field.value.replace(',','.');
	
	test=field.value.replace(/([0-9\.])/g, '');

	if(test.length!=0)
		return false;
	else
		return true;

}
function isInt(field){
	
	test=field.value.replace(/([0-9])/g, '');
	
	if(test.length!=0)
		return false;
	else
		return true;

}
function check_file(field){
	
	if(!get_el(field.id+'[delete]')){
		if(field.value=='')
			return false;
	}else if(get_el(field.id+'[delete]').checked && field.value=='')
		return false;

	return true;
	
}
function  getPageSize(){
       var xScroll, yScroll;

       if (window.innerHeight && window.scrollMaxY) {
               xScroll = document.body.scrollWidth;
               yScroll = window.innerHeight + window.scrollMaxY;
       } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
               xScroll = document.body.scrollWidth;
               yScroll = document.body.scrollHeight;
       } else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
               xScroll = document.documentElement.scrollWidth;
               yScroll = document.documentElement.scrollHeight;
       } else { // Explorer Mac...would also work in Mozilla and Safari
               xScroll = document.body.offsetWidth;
               yScroll = document.body.offsetHeight;
       }

       var windowWidth, windowHeight;
       if (self.innerHeight) { // all except Explorer
               windowWidth = self.innerWidth;
               windowHeight = self.innerHeight;
       } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
               windowWidth = document.documentElement.clientWidth;
               windowHeight = document.documentElement.clientHeight;
       } else if (document.body) { // other Explorers
               windowWidth = document.body.clientWidth;
               windowHeight = document.body.clientHeight;
       }

       // for small pages with total height less then height of the viewport
       if(yScroll < windowHeight){
               pageHeight = windowHeight;
       } else {
               pageHeight = yScroll;
       }

       // for small pages with total width less then width of the viewport
       if(xScroll < windowWidth){
               pageWidth = windowWidth;
       } else {
               pageWidth = xScroll;
       }
       
       result=new Array();
       result.pageWidth=pageWidth;
       result.pageHeight=pageHeight;
       result.windowWidth=windowWidth;
       result.windowHeight=windowHeight;

       return result;
}
function pre_load_image(src){
	pre = new Image(); 
	pre.src = src;
}

function viewObject(name) 
{ 
var obj = eval(name), i; 

if(!obj) 
{ 
alert("\""+name+"\" ia not an object"); 
return; 
} 

var w_Test = open("","Test","width=600,height=500,scrollbars=1"); 

if(!w_Test) 
{ 
alert("Cannot open window for "+name); 
return; 
} 

w_Test.document.open(); 

for(i in obj) 
w_Test.document.write(name+"."+i+"="+obj[i]+"<br>"); 

w_Test.document.close(); 
}