/*

  melmac.se / formsmedjan.se javascript - please behave
  
*/
var nummer = 1;
var xmlHttp;
var xmlHttp2;
var browser;
var id;
var id2;
var stoppother = false;
var vtopusername1 = 0;
var vtopusername2 = 0;
var vtoppass1 = 0;
var vtoppass2 = 0;
var flashid = 0;
function ajaxFunction()
{
   xmlHttp == null;
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    browser = true;
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      browser = false;
    }
    catch (e)
    {
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        browser = false;
      }
      catch (e)
      {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
   }
   
   return xmlHttp;
}

function ajaxFunction2()
{
   xmlHttp2 == null;
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp2=new XMLHttpRequest();
    browser = true;
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");
      browser = false;
    }
    catch (e)
    {
      try
      {
        xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
        browser = false;
      }
      catch (e)
      {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
   }
   
   return xmlHttp2;
}


function check_ajax(xmlHttp)
{
	if(xmlHttp == null)
	{
		alert("Din browser st�djer inte Ajax, v�nligen uppdatera browsern.")
		return false;
	}
	else
		return true;
}

function check_ajax2(xmlHttp2)
{
	if(xmlHttp2 == null)
	{
		alert("Din browser st�djer inte Ajax, v�nligen uppdatera browsern.")
		return false;
	}
	else
		return true;
}
function contact_update()
{
if(document.getElementById('foretagprivat1').checked) {
document.getElementById('contactforetag').style.display = 'none'
document.getElementById('contactprivat').style.display = 'block'
}
if (document.getElementById('foretagprivat2').checked) {
document.getElementById('contactforetag').style.display = 'block'
document.getElementById('contactprivat').style.display = 'none'	
}
}
function contact_faktura()
{
if(document.getElementById('samma').checked) {
document.getElementById('editdiv').style.display = 'none'
} else {
document.getElementById('editdiv').style.display = 'block'	
}
}

function ssh_div()
{
if(document.getElementById('val').checked) {
document.getElementById('editdiv').style.display = 'block'
} else {
document.getElementById('editdiv').style.display = 'none'	
}
}

function setflik(flikid)
{	
var nums;
	for (nums=1;nums<=10;nums=nums+1) 
	{
		if(document.getElementById("knapp" + nums))
		{
			if (nums == flikid)
			{
			document.getElementById("knapp" + nums).className = "activebut";
			} else
			document.getElementById("knapp" + nums).className = "nonactivebut";
		}
	}

}

function aliasmal()
{
nummer = 1 + nummer;
if (nummer < 100)
document.getElementById('aliastarget').innerHTML = document.getElementById('aliastarget').innerHTML + '<label>Mål-adress (#' + nummer + '):</label><br><input onkeypress="return disableEnterKey(event)" name="mal' + nummer + '" id="mal' + nummer + '" value="" size="35" onkeyup="verifyfield(\'_ajax/verify.php?type=EPOST&amp;val=\',\'mal' + nummer + '\')" type="text"><br> ';	
else
document.getElementById('aliastarget').innerHTML = document.getElementById('aliastarget').innerHTML + 'inga fler tack :)';
}

function send_post(script, params, funktionen){//Function som sänder data via post
                             
                             
                             if(check_ajax(xmlHttp)){
                                                          xmlHttp.onreadystatechange = funktionen;
                                                          xmlHttp.open("POST",script,true);
                                                          xmlHttp.setRequestHeader("If-Modified-Since", "Fri, 31 Dec 1999 23:59:59 GMT");
                                                          xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; ");
                                                          xmlHttp.setRequestHeader("Content-length", params.length);
                                                          xmlHttp.setRequestHeader("Accept-Charset","UTF-8");
                                                          xmlHttp.setRequestHeader("Connection", "close");
                                                          
                                                          xmlHttp.send(params);
                                                          
                             }
}

function putloadingin(id2)
{
document.getElementById(id2).innerHTML = ''
document.getElementById(id2).innerHTML = '<center><br><br><img src="/gfx/load_notrans.gif"><p>Laddar, var god vänta.</p></center>';
}

function focusclear(varid,varname)
{
	if (varid == 0)
	{
	document.getElementById(varname).value = '';
	}
	return 1;
}

function disableEnterKey(e)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}

function postonEnterKey(e,formet,sub)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
     {
     	if (sub == 1)
          formet.submit();
        else if (sub == 2)
          skicka_webmail();
        else if (sub == 3)
          skicka_domansok();
        else {
          document.getElementById(sub).focus();
  
        }
     } else
          return true;
}

function skicka_domansok() 
{
	document.getElementById('inloggning3').action = document.getElementById('inloggning3').action + document.getElementById('prefilled').value;
	document.getElementById('inloggning3').submit();
}


function skicka_webmail()
{
	doreqnfunc_post(document.getElementById('inloggning2'),'/verifywebmail.php?',webmail_server);
}

function webmail_server()
{
	if(xmlHttp.readyState == 4){
		if (xmlHttp.responseText == 1)
		{
		document.getElementById('inloggning2').action = 'http://w.melmac.se/dologin.php';
		document.getElementById('inloggning2').submit();
		}
		else if(xmlHttp.responseText == 2)
		{
		document.getElementById('inloggning2').action = 'https://mail.melmac.se/webmail/dologin.php';
		document.getElementById('inloggning2').submit();
		}
		else if(xmlHttp.responseText == 3)
		alert('Fel: Ogiltig epostadress.');
		else if(xmlHttp.responseText == 4)
		alert('Tekniskt fel, kontakta support eller försök igen senare.');
	
		document.getElementById('toppass2').value = '';
	}
}


function send(script,funktionen){

	if(check_ajax(xmlHttp)){
		var time = new Date();
		var time2 = time.getTime()
		xmlHttp.onreadystatechange = funktionen;
		xmlHttp.open("GET",script + "&nocache=" + time2,true);
		xmlHttp.send(null);
	}
	
}
function send2(script2,funktionen2){

	if(check_ajax(xmlHttp2)){
		var time = new Date();
		var time2 = time.getTime()
		xmlHttp2.onreadystatechange = funktionen2;
		xmlHttp2.open("GET",script2 + "&nocache=" + time2,true);
		xmlHttp2.send(null);
	}
	
}

function go_to(url) 
{
	if (stoppother == false)
	{
	location.href=url;		
		
	}
}
function doreqnfunc_post(this_var,url,functiontodo,inid){

id = inid;
if (stoppother == false)
{
	var formHandler = this_var;
	for(i=0;i<formHandler.elements.length;i++){
	var params;
		if(formHandler.elements[i].type == 'text'){
			params += '&' + formHandler.elements[i].name + '=' + encodeURIComponent(formHandler.elements[i].value);
		} else if(formHandler.elements[i].type == 'checkbox'){
			if(formHandler.elements[i].checked)
				params += '&' + formHandler.elements[i].name + '=1';
	 		else
				params += '&' + formHandler.elements[i].name + '=0';   
				                                                                         
	    } else if (formHandler.elements[i].type == 'radio') {
	    	if(formHandler.elements[i].checked)
			params += '&' + formHandler.elements[i].name + '=' + encodeURIComponent(formHandler.elements[i].value);
	    } else if (formHandler.elements[i].type == 'hidden') {
			params += '&' + formHandler.elements[i].name + '=' + encodeURIComponent(formHandler.elements[i].value);
	    } else if (formHandler.elements[i].type == 'password') {
			params += '&' + formHandler.elements[i].name + '=' + encodeURIComponent(formHandler.elements[i].value);
	    } else if (formHandler.elements[i].type == 'textarea') {
			params += '&' + formHandler.elements[i].name + '=' + encodeURIComponent(formHandler.elements[i].value);
	    } else if (formHandler.elements[i].type == "select-one")
		  params += '&' + formHandler.elements[i].name + '=' + encodeURIComponent(formHandler.elements[i].value);
			  
	}
	                             
	xmlHttp = ajaxFunction();
	var time = new Date();
	var time2 = time.getTime()                            
	var script = url + "&nocache=" + time2;
	send_post(script,params,functiontodo);
}
}


function doreqnfunc(url,functiontodo,inid) {
	if (stoppother == false)
	{
        id = inid;
		xmlHttp = ajaxFunction();
        send(url, functiontodo);
	}
}

function doreqnfunc2(url2,functiontodo2,inid2) {
	if (stoppother == false)
	{
        id2 = inid2;
		xmlHttp2 = ajaxFunction2();
        send2(url2, functiontodo2);
	}
}

function filldivmanual(id,txt)
{
if (id == "error")
document.getElementById('error').style.display = 'block'
document.getElementById(id).innerHTML = ''
document.getElementById(id).innerHTML = txt;
}
function filldiv_fliken()
{
	if(xmlHttp.readyState == 4){
	document.getElementById('flikar_content').innerHTML = ''
	document.getElementById('flikar_content').innerHTML = xmlHttp.responseText;
	}
}

function filldiv()
{
	if(xmlHttp.readyState == 4){
	document.getElementById(id).innerHTML = ''
	document.getElementById(id).innerHTML = xmlHttp.responseText;
	}
}

function filldiv_ajax2()
{
	if(xmlHttp2.readyState == 4){
	document.getElementById(id2).innerHTML = ''
	document.getElementById(id2).innerHTML = xmlHttp2.responseText;
	}
}
function filldivnflash()
{
	if(xmlHttp.readyState == 4){
	document.getElementById(id).innerHTML = ''
	document.getElementById(id).innerHTML = xmlHttp.responseText;
	if (document.getElementById(flashid).className == "oddfunc" || document.getElementById(flashid).className == "odd")
	colorFade(flashid,'background','959b0b','eaebce',30,30);
	else if(document.getElementById(flashid).className == "evenfunc" || document.getElementById(flashid).className == "even")
	colorFade(flashid,'background','959b0b','f5f5f5',30,30);
	else
	colorFade(flashid,'background','959b0b','ffffff',30,30);
	}
}

		

function verifyfield(url,inid)
{
	id = inid
	var sendurl = url + document.getElementById(inid).value
	xmlHttp = ajaxFunction();
	send(sendurl, verifyresponse);
}

function oneorerror_regular()
{
	if(xmlHttp.readyState == 4){
		if (xmlHttp.responseText == 1)
		{
		document.getElementById('error').style.display = 'block'
		document.getElementById('error').className = "okmsg"
		document.getElementById('error').innerHTML = 'Din ändring är sparad'
		popuphide();
		} else {
		document.getElementById('error').className = "errormsg"
		document.getElementById('error').style.display = 'block'
		document.getElementById('error').innerHTML = xmlHttp.responseText
		popuphide();
		}
	}
}

function oneorerror_nextstage()
{
	if(xmlHttp.readyState == 4){
		if (xmlHttp.responseText == 1)
		{
		popuphide();
		doreqnfunc_post(document.getElementById('1'),document.getElementById('1').action,filldiv_fliken,'flikar_content');
		} else {
			document.getElementById('error').className = "errormsg"
		document.getElementById('error').style.display = 'block'
		document.getElementById('error').innerHTML = xmlHttp.responseText
		popuphide();
		}
	}
}

function oneorerror_postform()
{
	if(xmlHttp.readyState == 4){
		if (xmlHttp.responseText == 1)
		{
		popuphide();
		document.getElementById('1').submit();
		} else {
		document.getElementById('error').className = "errormsg"
		document.getElementById('error').style.display = 'block'
		document.getElementById('error').innerHTML = xmlHttp.responseText
		popuphide();
		}
	}
}
function oneorerror_onlyhide()
{
	if(xmlHttp.readyState == 4){
		if(xmlHttp.responseText.substr(0,1) == 1)
		{
		popuphide();
		} else {
		alert('Kan ej utföra åtgärden');
		popuphide();
		}
	}
}


function oneorerror_okwithtextwithoutbutton()
{
	if(xmlHttp.readyState == 4){
		if(xmlHttp.responseText.substr(0,1) == 1)
		{
		document.getElementById('error').style.display = 'block'
		document.getElementById('error').className = "okmsg"
		document.getElementById('error').innerHTML = xmlHttp.responseText.substr(1);
		popuphide();
		document.getElementById('button').disabled = true;
		} else {
		document.getElementById('error').className = "errormsg"
		document.getElementById('error').style.display = 'block'
		document.getElementById('error').innerHTML = xmlHttp.responseText
		popuphide();
		}
	}
}

function oneorerror_okwithtext()
{
	if(xmlHttp.readyState == 4){
		if(xmlHttp.responseText.substr(0,1) == 1)
		{
		document.getElementById('error').style.display = 'block'
		document.getElementById('error').className = "okmsg"
		document.getElementById('error').innerHTML = xmlHttp.responseText.substr(1);
		popuphide();
		document.getElementById('button').disabled = true;
		} else {
		document.getElementById('error').className = "errormsg"
		document.getElementById('error').style.display = 'block'
		document.getElementById('error').innerHTML = xmlHttp.responseText
		popuphide();
		}
	}
}

function oneorerror_okwithtext()
{
	if(xmlHttp.readyState == 4){
		if(xmlHttp.responseText.substr(0,1) == 1)
		{
		document.getElementById('error').style.display = 'block'
		document.getElementById('error').className = "okmsg"
		document.getElementById('error').innerHTML = xmlHttp.responseText.substr(1);
		popuphide();
		document.getElementById('button').disabled = true;
		} else {
		document.getElementById('error').className = "errormsg"
		document.getElementById('error').style.display = 'block'
		document.getElementById('error').innerHTML = xmlHttp.responseText
		popuphide();
		}
	}
}


function oneorerror_flikform()
{
	if(xmlHttp.readyState == 4){
		if(xmlHttp.responseText.substr(0,1) == 1)
		{
		document.getElementById('flikar_content').innerHTML = '';
		document.getElementById('flikar').innerHTML = '';
		document.getElementById('okruta').style.display = 'block'
		document.getElementById('okruta').innerHTML = xmlHttp.responseText.substr(12);
		scrollTo(0, 0);
		flashid = 'rowid' + Math.round(xmlHttp.responseText.substr(1,11));
		doreqnfunc(id,filldivnflash,'lista');
		popuphide();
		} else {
		document.getElementById('error').className = "errormsg"
		document.getElementById('error').style.display = 'block'
		document.getElementById('error').innerHTML = xmlHttp.responseText
		popuphide();
		}
	}
}



function oneorerror()
{
	if(xmlHttp.readyState == 4){
		if (xmlHttp.responseText == 1)
		{
		document.getElementById('error').style.display = 'none'
		document.getElementById('error').innerHTML = ''
		document.getElementById('dnsrows').innerHTML = ''
		doreqnfunc('_ajax/dns_rowlist.php?',filldiv,'dnsrows')
		document.getElementById('getcontent').innerHTML = ''
		popuphide();
		} else {
		document.getElementById('error').style.display = 'block'
		document.getElementById('error').innerHTML = xmlHttp.responseText
		popuphide();
		}
	}
}

function postadns () {
if (document.getElementById('src').className == "ok" & document.getElementById('typ').className == "ok" & document.getElementById('dnstrgt').className == "ok")
{
filldivmanual('error','')
var url = "_ajax/dns_editrow.php?action=save&src=" + document.getElementById('src').value + "&typ=" + document.getElementById('typ').value + "&dnstrgt=" + document.getElementById('dnstrgt').value + "&ttl=" + document.getElementById('ttl').value + "&prio=" + document.getElementById('prio').value + "&edit=" + document.getElementById('edit').value
xmlHttp = ajaxFunction();
send(url, oneorerror);
} else {
filldivmanual('error','Fyll i alla fält i rätt format innan du sparar.')	
}
}

function verifyresponse() {
if(xmlHttp.readyState == 4){
	if (xmlHttp.responseText == 1)
	document.getElementById(id).className = "ok"
	else
	document.getElementById(id).className = "error"
}
}
function popupcompletedns()
{
	if(xmlHttp.readyState == 4){
	popuphide()
	doreqnfunc('_ajax/dns_rowlist.php?',filldiv,'dnsrows')
	document.getElementById('getcontent').innerHTML = ''
	document.getElementById('error').innerHTML = ''
	document.getElementById('error').style.display = 'none'
	}
}

function popupcomplete()
{
	if(xmlHttp.readyState == 4){
	popuphide()
	}
}

function popuphide()
{
document.getElementById('popupinnehall').innerHTML = ''
document.getElementById('popuplager').style.top = -1000 + 'px';
stoppother = false
}

function popupshow(url)
{
stoppother = true
id = 'popupinnehall'
xmlHttp = ajaxFunction();
send(url, filldiv);
document.getElementById('popuplager').style.top = 70 + 'px';
}

