//function loadPage(main, content) {
//var myurl = <?=$_SERVER['PHP_SELF']; ?> + "?" + main;
//	alert (myurl);
//		self.location.href=(myurl);
//}


function ajax(strURL, strRes) {
    var xmlHttpReq = false;

    try {
		xmlHttpReq = new XMLHttpRequest();
    }
	catch(e)
	{
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
	
    xmlHttpReq.onreadystatechange = function() {
		
 		if (xmlHttpReq.readyState == 4)
		{
			if(xmlHttpReq.status == 200)
			{
				try
				{
			   		document.getElementById(strRes).innerHTML = xmlHttpReq.responseText;
				}
				catch(e)
				{
					document.getElementById(strRes).innerHTML = '';
					var wrapping = document.createElement('div');
					wrapping.innerHTML = xmlHttpReq.responseText;
					document.getElementById(strRes).appendChild(wrapping);
				}
			}
			else
			{
				document.getElementById(strRes).innerHTML = xmlHttpReq.status;
			}
		}
		else if (xmlHttpReq.readyState != 4)
			document.getElementById(strRes).innerHTML = '<img src="/__admin/i/ajax-loader.gif" border="0">';
    };
	
    xmlHttpReq.open('GET', '/__admin/index.php?pop=1&page=' + strURL, true);
	xmlHttpReq.send(null);
    
}
function viewImg(url, placeholder)
{
	
	document.getElementById(placeholder).style.display='';
	document.getElementById(placeholder).innerHTML = '<a href=\"javascript:void(0)\" onclick=\"closeViewImg(\''+ placeholder +'\')\"><img src='+ url +' border=0><br><center>Stäng</center></a>';
	
}

function closeViewImg(placeholder)
{
	document.getElementById(placeholder).style.display='none';	
}


function forgotPass()
{

	var ep = document.getElementById('ep').value
	
	if(ep > '')
	{
		alert('Ett epost med ditt nya lösenordet har skickats');
		location.href='/__admin/forgotpass.php?ep='+ ep;
		return true;
	}
	else
	{
		alert('Fyll i ditt användarid så kommer det nya lösenordet till angiven epost');
		return false;
	}

}


function disableSend(foc)
{
	var thesend  = document.getElementById('btnSend');
	if(foc)
	{
		thesend.className='btnNormal_disabled';
		thesend.onclick = function () { return checkSubform(); } ;
	}
	else
	{
		thesend.className='btnNormal';
		thesend.onclick = function () { return true; } ;
	}
}

function checkSubform()
{
	alert('Du måste spara bilmodellerna innan du kan spara formuläret');	
	return false;
}

function subForm(which)
{
	if(which == 'car_model')
	{
		var model_name = document.getElementById('model_name');
		var productid = document.getElementsByTagName('select')[1];
		
		ajax('subform_model&act=add&carid='+ document.getElementById('carid').value +'&model_name='+ escape(model_name.value) +'&productid='+ productid.value, 'subform');
		
		model_name.value = '';
		productid.selectedIndex = 0;
	}
}



function loadPage(main, content)
{
	if (content != '')
	{
		if (main.charAt(main.length - 1) !='=') content = "&" + content;
		self.location.href=(main + content);
	}
	else
	{ 
		self.location.href=(main + "?page=welcome" );
	}
	
}

function changeColor(felt, farg) {
	var fargfelt = felt;
	fargfelt.bgColor = farg;
}

//--- Skiftar class på menybutton vid mouseover och mouseout
//--- Inparametrar:
//---	- btn:		Namn på button
function changeBtnMenuStyle(btn)
{
	var thisBtn = btn;		
	
	if (thisBtn.className == 'btnMenuNormal')
	{
		thisBtn.className = 'btnMenuDown';
	}
	else
	{
		thisBtn.className = 'btnMenuNormal';
	}
}

//--- Skiftar class på button vid mouseover och mouseout
//--- Inparametrar:
//---	- btn:		Namn på button
function changeBtnStyle(btn)
{
	var thisBtn = btn;		
	
	if (thisBtn.className == 'btnNormal')
	{
		thisBtn.className = 'btnMouseOver';
	}
	else
	{
		thisBtn.className = 'btnNormal';
	}
}

//--- Initiering av ett fält
//--- Inparametrar:
//---	- fld:		Namnet på id-taggen för fältet som skall initieras
//---	- varde:	Värdet som fältet skall initieras med.
function setDateFld(fld, varde){
	var setFld;
	var newFld;
						
	setFld = fld.id; //--- Namnet på id-taggen som efterfrågas.
		
	newFld = document.getElementById(setFld);
	newFld.value = varde; //--- Tilldela id-taggen värdet.
		
}


//--- Jonas Forsman 2003-12-31
//--- Öppna fönster för infotext
function editInfoText(strUrl) {

	window.open(strUrl,"","width=450px, height=440px, top=160px, left=150px, location=no, menubar=yes, resizable=yes, scrollbars=no");
	return;
}

//--- Utskrift från ett nytt fönster
//
function printIt(strUrl) {
	if (strUrl == "")
	{
		alert("Rapport saknas för utskrift!");
	}
	else
	{
		window.open(strUrl,"","width=700px, height=400px, top=160px, left=150px, location=no, menubar=yes, resizable=yes, scrollbars=yes");
	}
	return;
}

//--- Validering av telefonnummer
//
function checkPhoneNumber(field) {
	var fieldToValidate;
	var strToValidate;

	fieldToValidate = field;
	
	if (window.document.forms[0][fieldToValidate].value !== "") {
		strToValidate = window.document.forms[0][fieldToValidate].value;
		if (validator.isValidTelephoneNum(strToValidate)) {
		}
		else {
			alert("Detta är inget telefonnummer!");
			window.document.forms[0][fieldToValidate].focus();
		}
	}
}

//--- Validering av numeriska fält
//
function checkNumeric(field) {
	var fieldToValidate;
	var strToValidate;
		
	fieldToValidate = field;
	
	if (window.document.forms[0][fieldToValidate].value !== "") {
		strToValidate = window.document.forms[0][fieldToValidate].value;
		if (validator.isOnlyNumeric(strToValidate)) {
		}
		else {
			alert("Detta fält får endast innehålla siffervärden!");
			window.document.forms[0][fieldToValidate].value = "";
			window.document.forms[0][fieldToValidate].focus();
		}
	}
}

//--- Fält som skall ha focus
//
function setFocusField(setTo, formular) {
	var thisForm = document[formular];
	thisForm[setTo].focus();
}


function resetFields()
{
	document.loginFrm.epost.value = "";
	document.loginFrm.password.value = "";
	
	setFocusField('epost','loginFrm');
}
	

function changeDivStyle(divTagg)
{

	if (divTagg.className == 'menuchoiceMouseover')
	{
		divTagg.className = 'menuchoiceNormal';
	}
	else
	{
		divTagg.className = 'menuchoiceMouseover';
	}
}

//--- Jonas Forsman 2003-12-31
//--- Tvinga input i önskade fält

function checkEmpty(fields) {

	var fieldsToValidate;
	var strToValidate;
	var f;
	var mesg = '';
	fieldsToValidate = fields;
	
    strToValidate = fieldsToValidate.split(',');
    for (f = 0; f < strToValidate.length; f++) {
		if (window.document.forms[0][strToValidate[f]].value == "") {
			mesg+="Fältet är obligatoriskt och måste fyllas i.\n";
			window.document.forms[0][strToValidate[f]].focus();
		}
	}
    if (mesg != "") {
        alert(mesg);
   }
   if (mesg == "") {
	window.document.forms[0].submit();
   }
}

function checkOrgno(fields) {
// kan inte använda ovan funktion checkempty då den submittar formuläret
	var fieldsToValidate;
	var strToValidate;
	var f;
	var mesg = '';
	fieldsToValidate = fields;
	
    strToValidate = fieldsToValidate.split(',');
    for (f = 0; f < strToValidate.length; f++) {
		if (window.document.forms[0][strToValidate[f]].value == "") {
			mesg+="Fältet är obligatoriskt och måste fyllas i.\n";
			window.document.forms[0][strToValidate[f]].focus();
		}
	}
    if (mesg != "") {
        alert(mesg);
   }
}

//--- Jonas Forsman 2004-01-01
//--- Konvertera siffror till db-stil.

function convertNumForDB (myfield) {
  
   var   Content = window.document.forms["wizard"].elements[myfield].value;
   var tmpContent = Content;
   var tmpBefore = new String();   
   var tmpAfter = new String();
   var tmpOutput = new String();
   var intBefore = 0;
   var intAfter = 0;
   var v_comma = ',';
   var v_punkt = '.';

   while (tmpContent.indexOf(v_comma) > -1) {
   
      intBefore = tmpContent.indexOf(v_comma);
      tmpBefore = tmpContent.substring(0, intBefore);
      tmpOutput = tmpOutput + tmpBefore;

      tmpOutput = tmpOutput + v_punkt;
      intAfter = tmpContent.length - v_comma.length + 1;
      tmpContent = tmpContent.substring(intBefore + v_comma.length);

   }

  window.document.forms["wizard"].elements[myfield].value = tmpOutput + tmpContent;

   return true;

}

function f_setfocus( aForm )
{
	var aForm = document.forms[0];
	
	if( aForm.elements[0]!=null) {
		var i;
		var max = aForm.length;
		for( i = 0; i < max; i++ ) {
			if( aForm.elements[ i ].type != "hidden" &&
				!aForm.elements[ i ].disabled &&
				!aForm.elements[ i ].readOnly ) {
				aForm.elements[ i ].focus();
				break;
			}
		}
	}
}

function confirmLink(msg)
{
    if (msg == '' || typeof(window.opera) != 'undefined') {
        return true;
    }

    var is_confirmed = confirm(msg + '\n');

    return is_confirmed;
}

        var markerHTML = "|";
        var minWidth = 100;
        var dragingColumn = null;
        var startingX = 0;
        var currentX = 0;

        function getNewWidth () {
            var newWidth = minWidth;
            if (dragingColumn != null) {
                newWidth = parseInt (dragingColumn.parentNode.style.width);
                if (isNaN (newWidth)) {
                    newWidth = 0;
                }
                newWidth += currentX - startingX;
                if (newWidth < minWidth) {
                    newWidth = minWidth;
                }
            }
            return newWidth;
        }

        function columnMouseDown (event) {
            if (!event) {
                event = window.event;
            }
            if (dragingColumn != null) {
                ColumnGrabberMouseUp ();
            }
            startingX = event.clientX;
            currentX = startingX;
            dragingColumn = this;
            return true;
        }

        function columnMouseUp () {
            if (dragingColumn != null) {
                dragingColumn.parentNode.style.width = getNewWidth ();
                dragingColumn = null;
            }
			return true;
        }

        function columnMouseMove (event) {
            if (!event) {
                event = window.event;
            }
            if (dragingColumn != null) {
			    currentX = event.clientX;
                dragingColumn.parentNode.style.width = getNewWidth ();
                startingX = event.clientX;
                currentX = startingX;
			}
			return true;
        }

        function installTable (tableId) {
            var table = document.getElementById (tableId);
            // Test if there is such element in the document
            if (table != null) {
                // Test is this element a table
                if (table.nodeName.toUpperCase () == "TABLE") {
                    document.body.onmouseup = columnMouseUp;
                    document.body.onmousemove = columnMouseMove;
                    for (i = 0; i < table.childNodes.length; i++) {
                        var tableHead = table.childNodes[i];
                        // Look for the header
                        // Tables without header will not be handled.
                        if (tableHead.nodeName.toUpperCase () == "THEAD") {
                            // Go through THEAD nodes and set resize markers
                            // IE in THEAD contains TR element which contains TH elements
                            // Mozilla in THEAD contains TH elements
                            for (j = 0; j < tableHead.childNodes.length; j++) {
                                var tableHeadNode = tableHead.childNodes[j];
                                // Handles IE style THEAD with TR
                                if (tableHeadNode.nodeName.toUpperCase () == "TR") {
                                    for (k = 0; k < tableHeadNode.childNodes.length; k++) {
                                        var column = tableHeadNode.childNodes[k];
                                        var marker = document.createElement ("span");
                                        marker.innerHTML = markerHTML;
                                        marker.style.cursor = "move";
                                        marker.style.align = "right";
                                        marker.onmousedown = columnMouseDown;
                                        column.appendChild (marker);
                                        if (column.offsetWidth < minWidth) {
                                            column.style.width = minWidth;
                                        }
                                        else {
                                            column.style.width = column.offsetWidth;
                                        }
                                    }
                                }
                                // Handles Mozilla style THEAD
                                else if (tableHeadNode.nodeName.toUpperCase () == "TH") {
                                    var column = tableHeadNode;
                                    var marker = document.createElement ("span");
                                    marker.innerHTML = markerHTML;
                                    marker.style.cursor = "move";
                                    marker.style.textalign = "right";
                                    marker.onmousedown = columnMouseDown;
                                    column.appendChild (marker);
                                    if (column.offsetWidth < minWidth) {
                                        column.style.width = minWidth;
                                    }
                                    else {
                                        column.style.width = column.offsetWidth;
                                    }
                                }
                            }
                            table.style.tableLayout = "fixed";
                            // Once we have found THEAD element and updated it
                            // there is no need to go through rest of the table
                            i = table.childNodes.length;
                        }
                    }
                }
            }
        }
