if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

$(document).ready(function() {
	$("span input[type='checkbox']").each(function() {
		var val = '';
		if($(this).attr('checked') == true) {
			val = 'on';
		}
		$(this).parent().parent().find('span').eq(1).html('<span onclick="switchBox(\'' + this.name + '\')">' + $(this).parent().parent().find('span').eq(1).html() + '</span>').addClass('pointer');
		$(this).parent().html('<img src="./img/checkbox' + val + '.gif" id="chkimg' + this.name + '" alt="" align="top" class="pointer" onclick="switchBox(\'' + this.name + '\')"><input type="hidden" name="' + this.name +'" id="chk' + this.name + '" value="' + val + '" />');
	});
	
	var radios = new Array();
	$("span input[type='radio']").each(function() {
		var val = '';
		var inputfield = '';
		if($(this).attr('checked') == true) {
			val = 'on';
		}
		
		if(radios.indexOf(this.name) == -1) {
			inputfield = '<input type="hidden" name="' + this.name +'" id="radio' + this.name + '" value="" />';	
		}
		
		radios.push(this.name);
		$(this).parent().parent().find('span').eq(1).html('<span onclick="switchRadio(\'' + this.name + '\',\'' + this.value + '\')">' + $(this).parent().parent().find('span').eq(1).html() + '</span>').addClass('pointer');
		$(this).parent().html('<img src="./img/radio' + val + '.gif" id="radioimg' + this.name + '_' + this.value + '" alt="" align="top" class="pointer radio' + this.name + '" onclick="switchRadio(\'' + this.name + '\',\'' + this.value + '\')">' + inputfield);
		
		if($(this).attr('checked') == true) {
			switchRadio(this.name,this.value);	
		}
	});
});

var switchBox = function(id) {
	if($('#chk' + id).val() == 'on') {
		$('#chk' + id).val('');
		$('#chkimg' + id).attr('src','./img/checkbox.gif');
	} else {
		$('#chk' + id).val('on');
		$('#chkimg' + id).attr('src','./img/checkboxon.gif');
	}
};

var switchRadio = function(id,val) {
	$('#radio' + id).val(val);
	$('.radio' + id).attr('src','./img/radio.gif');
	$('#radioimg' + id + '_' + val).attr('src','./img/radioon.gif');
};

var checkRegForm = function() {
	var errors = new Array();
	var fields = new Array('company','address','address2','country','phone','email','vat','contact','pass','pass2')
	
	for(i=0;i<fields.length;i++) {
		if($('input[name="' + fields[i] + '"]').val() == '') {
			errors.push(fields[i]);
		}
	}
	
	if($('input[name="pass"]').val() != $('input[name="pass2"]').val()) {
		errors.push('pass');
		errors.push('pass2');
	}
	
	if(errors.length > 0) {
		$().scrollTo($('.f' + errors[0]), 500 );
		
		$('#regform input.field').removeClass('formerror');
		for(i=0;i<errors.length;i++) {
			$('#regform input.f' + errors[i]).addClass('formerror');	
		}
		
		return false;
	} else {
		return true;	
	}
};

var newsletter = function(id,lang) {
	return false;	
};
