function e(id) { return document.getElementById(id) }

window.onload = function () { showTime() }

function showTime() {
	updatetime();
	myInterval = window.setInterval(function () {
		updatetime()
	},10000);
}

function updatetime() {
	loadDoc('/site/ajax/currenttime.php','currenttime')
}


function CheckSearchbox()
{
	var MyElement = document.getElementById("search_term");
	if (MyElement.value)
	{
		e("search_form").submit(); return false;
	}
}


/* Contactform functions */

function toggleErrorLabel(label,state) {
	if(state == 'on') {
		document.getElementById(label).className = "errorlabel";
	}else{
		document.getElementById(label).className = "normallabel";
	}
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function ValidateForm(form) {
	
	var error = 0;
	var errorMessage = 'The form is not filled in completely:\n\n';
		
	if(typeof(form.required)=='undefined') {
		return true;
	}
	var fieldnames = form.required.value.split(',');
	
	for(key in fieldnames) {
		if(e(fieldnames[key])) {
			type = e(fieldnames[key]).type
			label = e('label_'+fieldnames[key]).innerHTML;
			label = strip_tags(label);
			//label = fieldnames[key];
			if(type=='text' || type=='textarea') {
				if(e(fieldnames[key]).value == "") { 
					errorMessage += '- '+label+' is missing\n';
					error = 1;
					toggleErrorLabel('label_'+fieldnames[key],'on');
				}else{
					toggleErrorLabel('label_'+fieldnames[key]);
				}
			} else if(type=='select-one') {
				if(e(fieldnames[key]).options[e(fieldnames[key]).selectedIndex].value=='' || e(fieldnames[key]).options[e(fieldnames[key]).selectedIndex].value=='0') {
					errorMessage += '- '+label+' is not selected\n';
					error = 1;
					toggleErrorLabel('label_'+fieldnames[key],'on');
				}
			} else {
				alert(type)
			}
		}
	
	}

	if(error){
		alert(errorMessage);
		return false; 
	}else{
		return true;
	}
}

function strip_tags(str, allowed_tags) { // needed for validateform!
   
    var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = '';
 
    var replacer = function(search, replace, str) {
        return str.split(search).join(replace);
    };
 
    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
    }
 
    str += '';
 
    // Match tags
    matches = str.match(/(<\/?[\S][^>]*>)/gi);
 
    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;
        }
 
        // Save HTML tag
        html = matches[key].toString();
 
        // Is tag not in allowed list? Remove from str!
        allowed = false;
 
        // Go through all allowed tags
        for (k in allowed_array) {
            // Init
            allowed_tag = allowed_array[k];
            i = -1;
 
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
 
            // Determine
            if (i == 0) {
                allowed = true;
                break;
            }
        }
 
        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
 
    return str;
}

function openSite(URLStr)
{
    var maxx = screen.width;
    var maxy = screen.height;
    windowprops = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=100,height=100,left=100,top=100";
    window.open(URLStr+"&maxx="+maxx+"&maxy="+maxy,"",windowprops);
}

// This is my simple Ajax tric

var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
@else
	xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp = false;
	}
}

function loadDoc(docname, id) {
	if (xmlhttp) {
		xmlhttp.open("GET",docname,true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				if(document.getElementById(id)) document.getElementById(id).innerHTML=xmlhttp.responseText;
			}
		}
		xmlhttp.send(null)
	}
}

