﻿
//Een nieuwe class declareren
function common(){

//declaring the array that will be used for preloading images
this.pre_img = new Array();
this.debug=false;

	this.console =function(msg) {
		if(this.debug){
		document.getElementById('console').innerHTML=document.getElementById('console').innerHTML+msg+'<br>';
		}
	}
	
	this.getElementsByClassName =function(element, tag, classes) {
	var arrElements = (tag == "*" && element.all)? element.all : element.getElementsByTagName(tag);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
		if(typeof classes == "object"){
			for(var i=0; i<classes.length; i++){
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + classes[i].replace(/\-/g, "\\-") + "(\\s|$)"));
			}
		}
		else{
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + classes.replace(/\-/g, "\\-") + "(\\s|$)"));
		}
		var oElement;
		var bMatchesAll;
		for(var j=0; j<arrElements.length; j++){
			oElement = arrElements[j];
			bMatchesAll = true;
			for(var k=0; k<arrRegExpClassNames.length; k++){
				if(!arrRegExpClassNames[k].test(oElement.className)){
					bMatchesAll = false;
					break;
				}
			}
			if(bMatchesAll){
				arrReturnElements.push(oElement);
			}
		}
	return (arrReturnElements)
	}

	this.preload_images = function(img) {
		if(document.images){
		var preload_image_object = new Image();	
		var i = 0;
			for(i=0; i<img.length; i++) {
			//alert('preloading '+img[i]);
			preload_image_object.src = img[i];
			}
		}
	}

	//called at 'window.onload';
	this.externallinks = function() {
	 if (!document.getElementsByTagName) return;
	 var anchors = document.getElementsByTagName('a');
	 for (var i=0; i<anchors.length; i++) {
	   var anchor = anchors[i];
	   if (anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external')
		 anchor.target = '_blank';
		}
	}

	this.val_by_id = function(id){		
	var value = document.getElementById(id).value;
	return value;
	}
	
	this.val_by_name = function(form, name){
	var value = document.forms[form][name].value;
	return value;
	}
	
	this.getradiovalue=function(form, name){
	var radioObj = document.forms[form][name]; 
		if(!radioObj)
		return "";
	var radioLength = radioObj.length;
		if(radioLength == undefined)
			if(radioObj.checked)
			return radioObj.value;
			else
			return "";
		for(var i = 0; i < radioLength; i++) {
			if(radioObj[i].checked) {
			return radioObj[i].value;
			}
		}
	}
	
	this.setradiovalue=function(form, name, newvalue){
	var radioObj = document.forms[form][name]; 
		if(!radioObj)
		return;
		var radioLength = radioObj.length;
		if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
		}
		for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
			if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
			}
		}
	}
		
		
	this.get_content_from_object = function(id){
		if(document.getElementById(id)){
		return document.getElementById(id).innerHTML;
		}
	}
	
	this.sent_content_to_object = function(object, content){
		if(document.getElementById(object)){
		document.getElementById(object).innerHTML=content;
		}
	}

	this.change_class = function(id, newClass) { 
		if(document.getElementById(id)){
		document.getElementById(id).className=newClass; 
		}
	}
	
	this.string_only_spaces = function(string){
		//going to the num of chars of the string
		for(c=0; c<string.length; c++){
		var current_string_char=string.substring(c,c+1);
			if(current_string_char!=" "){
			return false
			}
		}
	return true;
	}
	
	this.valid_email = function(email) {
		if (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1){
		return true;
		}
		else{
		return false;
		}
	}
	
	this.isCharAndNum = function(str){
		var expr = /(?=.*\d)(?=.*[a-zA-Z])/;
		if (expr.test(str)){
			return true;
		}else{
		return false;
		}
	}



	this.enableform = function(form, enable) {	
		if(form=='*'){
			for (formIndex=0; formIndex<document.forms.length; formIndex++){
				for(x=0;x<document.forms[formIndex].length;x++){
				document.forms[formIndex].elements[x].disabled=true;
				}
			}
		}
		else{
		var el = document.forms[form].elements;
			for(var i=0;i<el.length;i++){
				if(enable==false){
				el[i].setAttribute('disabled',true);
				}
				else{
				el[i].removeAttribute('disabled');
				}
			}
		}
	}
	
	
	this.getabsy = function(id){	
		if(document.getElementById(id)){
		var ret = 0;
		div=document.getElementById(id);
			while( div != null ){
			ret += div.offsetTop;
			div = div.offsetParent;
			}
		return ret;
		}
	}
	
	this.getabsx = function(id){	
		if(document.getElementById(id)){
		var ret = 0;
		div=document.getElementById(id);
			while( div != null ){
			ret += div.offsetLeft;
			div = div.offsetParent;
			}
		return ret;
		}
	}
	
	this.after_upload = function(formname, path, file, varname, preview, extra_action, succes){
		if(succes=="yes"){
			if(preview=="yes"){
			//refreshing the image
			this.sent_content_to_object("upload_preview_"+varname, "<img src='"+this.ptr+path+"/"+file+"' width='80' height='80' alt='"+file+"' />");
			}
		//refreshing the label
		this.sent_content_to_object("upload_label_"+varname, "Toegevoegd: <b class='blue'>"+file+"</b>");
		//refreshing the hidden inputfield that holds the filename
		document.forms[formname][varname].value=file;
			if(document.getElementById(varname+"_dell_button")){
			//making 'the delete button' visible
			this.change_class(varname+"_dell_button", '');
			}
		}
		else{
		alert("De upload is mislukt. Probeer het opnieuw!");
		}
		if(extra_action=="yes"){
		//calling specific module-related after_upload action
		sw.specific_after_upload(path, file, varname, preview, succes);
		}
	}
		
	//to ammount label
	this.num2amountlabel = function(num, lan){
	var sep='.';
		if(lan=='nl'){
		sep=',';
		}
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num.replace(',','.') + sep + cents);
	}

			
	this.goto_url = function(form, url, send_method){
		if(send_method=='submit'){
		//defining form action
		document.forms[form].action = url;
		//submitting the form
		document.forms[form].submit();
		}
		else{
		document.location = url;
		}
	}
	
	this.open_win = function(url){
	window.open(this.ptr+url, 'new_window');
	}
	
	this.valign_colomns = function(){
	//layout actions on page change
	}
	
	
	this.onload_action = function(){
	this.pre_img[this.pre_img.length]=this.ptr+'CmWare/images/website/ajaxloader.gif';
	this.externallinks();
	this.preload_images(this.pre_img);
	
	this.wrap_height=document.getElementById('wrap').offsetHeight;

	this.valign_colomns();
	}
	

	//CUSTOMER SPECIFIC METHODS

}

//declating the first instance
var sw = new common();

	//declaring the 'all_labels' class
	function language_labels(){
	//emty class (only declared for creating 'label-instance')
	}

//declaring the 'label-instance' (this will be extended epending on the modules)
var label = new language_labels();


    function create_http_object(){
	var ActiveXTypes = [
		"Microsoft.XMLHTTP",
		"MSXML2.XMLHTTP.5.0",
		"MSXML2.XMLHTTP.4.0",
		"MSXML2.XMLHTTP.3.0",
		"MSXML2.XMLHTTP"
	];
	for( var i = 0; i < ActiveXTypes.length; i++ ){
		try
		{
			return new ActiveXObject( ActiveXTypes[i] );
		}
		catch( e )
		{ }
	}
	try{
		return new XMLHttpRequest();
	}
	catch( e ){ }
	return false;
    }

    function make_request(url, callback_function, http_method, post_values, return_xml){
	http = create_http_object();
        if(!http){
        alert('This browser doesnt support ajax requests.');
        return false;
        }
		http.onreadystatechange = function(){
			if(http.readyState == 4){
				if(http.status == 200){
					if(callback_function){
						if(return_xml){
						eval(callback_function + '(http.responseXML)');
						}
						else{
						eval(callback_function + '(http.responseText)');
						}
					}
				}
				else{
				alert('Error! (' + http.status + ')');
				}
			}
		}
        if(!post_values){
        post_values = null;
        }
        if(!http_method){
        http_method = "GET";
        }
        http.open(http_method, url, true);
        if(http_method == "POST"){
        http.setRequestHeader('Content-Type', 'application/x-www-form-URLencoded');
		http.setRequestHeader("Content-length", post_values.length);
		http.setRequestHeader("Accept-Charset","UTF-8");
        }
    http.send(post_values);
    }

//
window.onload = function(){
sw.onload_action();
}

window.onresize = function (){
sw.valign_colomns();
}

