/*========================================================
  ANVÄNDS I INPUT-17_I
  2004-04-26 ML
========================================================*/
function duplicatePassword() {
  var p1 = document.getElementById("f6");
  var p2 = document.getElementById("f6b");
  if(p1 && p2) {
    if(p1.value) {
      p2.value = p1.value;
    }
  }
}

function checkPassword() {
  var p1 = document.getElementById("f6");
  var p2 = document.getElementById("f6b");
  if(p1 && p2) {
    if(p1.value && p2.value) {
      if(p1.value == p2.value) {
        return true;
      } else {
        alert("Lösenorden stämmer inte överens.\nFörsök igen.");
        p1.value = "";
        p2.value = "";
        p1.focus();
        return false;
      }
    } else {
      alert("Båda lösenordsfälten måste fyllas i.\nFörsök igen.");
      p1.value = "";
      p2.value = "";
      p1.focus();
      return false;
    }
  }
}
/*========================================================
##########################################################
========================================================*/


/* Tömmer de små "visa" fälten. */
function clearAllSmall() {
  for (var i=0; i<500; i++) {
    var pField=document.getElementById("p"+i);
    if (pField) {
      pField.value="";
    }
  }
}

/* Öppnar popupfönster. */
function windowOpen(url) {
  window.open(url,'','width=500, height=500, resizable=yes, scrollbars=yes, left=50, top=50');
}

/* Sätter en variabel för att veta om fältet är ändrat eller inte. */
var iChanged=0
function setChange() {
  iChanged=1;
  return true;
}

/* Kollar om lösenordn innehåller 6 tecken och om de stämmer överens. */
function pwdCheck() {
  var sPwd= new String(document.forms(0).uspwNew.value);
  var sPwd2=new String(document.forms(0).uspwNew2.value);
			
  if(iChanged==1) {	
    if(sPwd.length<6) {
      alert("Lösenordet måste innehålla minst 6 tecken!");
      document.forms(0).uspw.select();
      return false;	
    }

    if(sPwd.length != sPwd2.length || sPwd.indexOf(sPwd2)==-1) {
      alert("Lösenorden överensstämmer inte!");
      document.forms(0).uspw2.select();
      return false;
    }
    return true;
  return true;
  }	
}

/* Sätter LexMaria till att vara Nej om det är tomt. */
function setLex() {
  var element = document.getElementById("W06267");
  if(element) {
    if(element.value == "") {
      var element2 = document.getElementById("W06267");
      if(element2) {
        element2.value = "Nej";
      }
    }
  }
}

/* Kollar vad dropDown menyn "Identifiering av prod" har för värde. 
function checkIfDis() {
  var fieldv06199 = document.getElementById("v06199");

  if(fieldv06199) {
    if(fieldv06199.value=="06201") {		// Engånsanvändning
      disable("F43", false);
      disable("F48", false);
      disable("F33", false);
      disable("F34", true);
      disable("F36", true);
    }else if(fieldv06199.value=="06346") {	// Flergånsanvändning
      disable("F43", true);
      disable("F48", true);
      disable("F33", true);
      disable("F34", false);
      disable("F36", false);
    }else{					// Tom
      disable("F43", false);
      disable("F48", false);
      disable("F33", false);
      disable("F34", false);
      disable("F36", false);
    }
  }
}
*/


/****************************************************************************/
/*******************************     nytt     **********************************/

/* Kollar vad dropDown menyn "Identifiering av prod" har för värde. */
function checkIfDis() {
  var fieldv06199 = document.getElementById("v06199");

  if(fieldv06199) {
    if(fieldv06199.value=="06201") {		// Engånsanvändning
      disable("F43", false);
      disable("F48", true);
      disable("F33", true);
      disable("F34", false);
      disable("F36", false);
    }else if(fieldv06199.value=="06346") {	// Flergånsanvändning
      disable("F43", false);
      disable("F48", false);
      disable("F33", false);
      disable("F34", true);
      disable("F36", true);
    }else{					// Tom
      disable("F43", true);
      disable("F48", true);
      disable("F33", true);
      disable("F34", true);
      disable("F36", true);
    }
  }
}

/****************************************************************************/

/* Kollar om ett element finns och i så fall sätter den till disabled eller enabled.
   param 1, element: vilket element att förändra.
   param 2, bool: om bool är true ska elementet disablas annars enablas. */
function disable(element, bool) {
  if(bool==true && document.getElementById(element)) {		// Sätt till disabled.
    document.getElementById(element).disabled = true;
    document.getElementById(element).style.backgroundColor = "#cccccc";
  }else if(bool==false && document.getElementById(element)){	// Sätt till enabled.
    document.getElementById(element).disabled = false;
    document.getElementById(element).style.backgroundColor = "";
  }
}


function checkUserAndPsw() {
  if(checkUser()) {
    if(checkFirstPsw()) {
      if(checkMatch()) {
        return true;
      }else{
        return false;
      }
    }else{
      return false;
    }
  }else{
    return false;
  }
}

function checkUser() {
  var user = document.getElementById("f3");
  if(user) {
    var userVal = user.value;
    if(userVal.length>=1) {
      return true;
    }else{
      alert(strconstNoUser);
      user.focus();
    }
  }
}

function checkFirstPsw() {
  var psw = document.getElementById("uspwNew");
  if(psw) {
    var pswVal = psw.value;
    if(pswVal.length>=6) {
      return true;
    }else{
      alert(strconstPswToShort);
      psw.select();
      psw.focus();
    }
  }
}

function checkMatch() {
  var psw = document.getElementById("uspwNew");
  var psw2 = document.getElementById("uspwNew2");
  if(psw && psw2) {
    if(psw.value == psw2.value) {
      return true;
    }else{
      alert(strconstPswDontMatch);
      psw.value = "";
      psw2.value = "";
      psw.focus();
    }
  }
}




/****************************************************************************/

function setHighRiskAlarm(){
  // When a high risk alarm is activated a codetype is set to preserve status
    
  if (document.getElementById("highRiskNew").checked) {
    document.getElementById("highRiskShow").checked = 1;
  }
  else {
    document.getElementById("highRiskShow").checked = 0;
  }

}


/****************************************************************************/

function chkHighRiskAlarm (){
  // Sets lable red if high risk alarm has been activated.
  
  if(document.getElementById("highRiskShow")){
    if(document.getElementById("highRiskShow").checked){
      document.getElementById("spnHighRiskShow").style.color = "red";
    }
  }

}


/****************************************************************************/

function confirmHighRiskAlarm(){
  // Ask user if it is OK to activate high risk alarm
  
  if(document.getElementById("highRiskNew")){
  
    if(document.getElementById("highRiskNew").checked){
      var msg="Du har markerat högrisklarm!\nVill du utlösa larmet?"; 
      var activateAlarm = confirm(msg);
    
      if(activateAlarm){
        return true;  // User agrees
      }
      else{
        document.getElementById("highRiskNew").checked = 0    // Uncheck alarm
        document.getElementById("highRiskShow").checked = 0;  // Uncheck codetype
        return true;  // OK
      }
    }
  }
  return true;
}

/****************************************************************************/
// POK 2005-04-21
// Abonnenten väljer eget urval eller alla. Vid valet "eget urval" visas fälten
// för att välja produkt.
//
// POK 2005-04-26
// Funktionsnamnet har sitt ursprung i en kodtyp som inte används längre men 
// är inte ändrat med hänvisning till "If it ain't broken don't fix it"...

function checkCss06418(){
  
  var tempValue = radioGroupValue("t53_f8_1");	// Hämta värdet från radiogruppen. I detta fall: inget ifyllt/Ja/Nej
  //alert("Debug: " + tempValue);
  
  if(tempValue){				// Något är ifyllt
    if(tempValue == "custom"){			// Ifyllt: Ja
      showCSS("css06418");			// Visa span
			
    }else{					// Det ifyllda var inte ja
      showCSS("css06418",false);		// Göm
      document.frames("ifrmName").document.getElementById("f4").value="";
      document.frames("ifrmBrand").document.getElementById("f7").value="";
      document.frames("ifrmModel").document.getElementById("f8").value="";
      document.getElementById("T53_F8_4").value="";
      document.getElementById("T53_F8_7").value="";
      document.getElementById("T53_F8_8").value="";
    }
  }else{					// Inget ifyllt
    showCSS("css06418",false);			// Göm span
  }

}

/****************************************************************************/
// När en användare vill radera sig som abonnent måste han/hon bekräfta innan 
// borttagning sker.
// POK 2005-04-21

function confirmDeleteSubscr(subscrId) { 
  if(confirm(strconstConfirmDeleteSubscr)){
    window.location="/rc?SubscriptionsDeleteUser_" + subscrId + "_selectform=subscriberDeleted";
  }
}

/****************************************************************************/
// CR kan välja att spara ett ärende som utkast. Då sätts Status till Utkast
// och visas inte för andra användare vid sökning.
// Vid val av "Publicera" så görs ärendet allmänt tillgänligt.
// POK 2005-04-22
//
// Korrigeringar:
// POK 2008-04-14: Lagt till kontroll att överförd (från MT-ärende) artikel finns i databasen innan ärendet publiceras
// POK 2005-09-15: Lagt till kontroll av webbehörighet så att alla med "W" får sparaknapp.
// POK 2005-05-16: Ändrat kontrollen av inputopen-strängen i buildButtonString().


function toggleStatus(status)
{
    if(document.getElementById("v06413"))
    {
        if(status=="draft")
        {
            document.getElementById("v06413").value = "06414";
            doOnSubmit();
        }
        else 
        {
            if(chkArticleOk()) // Don't publish if article doesn't match
            {
                document.getElementById("v06413").value = "06415";
                doOnSubmit();
            }
        } 
    }
}


function buildButtonString(){
  var strButtonSave = "";
  var strButtonMailToFriend = "";

  if(document.getElementById("v06413")){
    if((document.getElementById("v06413").value=="06415")){
      strButtonSave="<input type=\"submit\" name=\"InputSubmitNew\" value=\"Spara/Skicka\" style=\"width:100px; height:20px;\" onClick=\"doOnSubmit();\">"
      strButtonMailToFriend="<input type=\"button\" value=\"Tipsa en v&auml;n\" onClick=\"mailToFriend();\">";
    }
    else{
      strButtonSave="<input type=\"button\" value=\"Spara som utkast\" onClick=\"toggleStatus('draft')\">&nbsp;<input type=\"button\" value=\"Publicera\" onClick=\"toggleStatus('publish')\">"
    }
  }
  else if((document.getElementById("webprofile")) && (document.getElementById("webprofile").innerHTML.indexOf("W")> -1)){
    strButtonSave="<input type=\"submit\" name=\"InputSubmitNew\" value=\"Spara/Skicka\" style=\"width:100px; height:20px;\" onClick=\"doOnSubmit();\">"
    strButtonMailToFriend="<input type=\"button\" value=\"Tipsa en v&auml;n\" onClick=\"mailToFriend();\">";
  }
  else if((location.href.indexOf("Open") > -1)||(location.href.indexOf("open") > -1)){
    strButtonMailToFriend="<input type=\"button\" value=\"Tipsa en v&auml;n\" onClick=\"mailToFriend();\">";
  }

  if(document.getElementById("tdSaveButtonTop") && document.getElementById("tdSaveButtonBottom")){
    document.getElementById("tdSaveButtonTop").innerHTML = strButtonSave;
    document.getElementById("tdSaveButtonBottom").innerHTML = document.getElementById("tdSaveButtonBottom").innerHTML + strButtonSave + " " + strButtonMailToFriend;
  }
}

/****************************************************************************/
// Byta lösenord
// POK 2005-04-22

function checkCssPassword(){
  
  var tempValue =  document.getElementById("btnPwd").value.indexOf("Byt");
  //alert(tempValue);
  
  if(tempValue > -1){				// Något är ifyllt
      showCSS("cssPassword");			// Visa span
      showCSS("cssPasswordPlaceholder",false);
      document.getElementById("btnPwd").value = "Göm lösenord";
  }
  else{					// Inget ifyllt
    showCSS("cssPassword",false);			// Göm span
    showCSS("cssPasswordPlaceholder");
    document.getElementById("btnPwd").value = "Byt lösenord";
  }

}


/****************************************************************************/
// Glömt lösenord
// POK 2005-04-25


function setSpecialTag(){
    if(paramValue=="false"){
      //document.getElementById("tdLostPassword").innerHTML="&nbsp;";
      document.getElementById("tdLabel").innerText="Din e-postadress";
      document.getElementById("tdField").innerHTML="<INPUT TYPE=\"text\" NAME=\"_email\" STYLE=\"width:280px;\" class=\"required\">";
      document.getElementById("tdButton").innerHTML="<INPUT TYPE=\"submit\" VALUE=\"Skicka\" STYLE=\"width:50px; height:20px;\">";
    }
  }



/****************************************************************************/
// Tipsa en vän
// POK 2005-04-22

function mailToFriend(){
  var recordId = document.getElementById("divF1").innerText;
  
  if((location.href.indexOf("Open") > -1)||(location.href.indexOf("open") > -1)){
    window.location="/rc?home_selectForm=tipsa_id=" + recordId + "_type=open";
  }
  else{
    window.location="/rc?home_selectForm=tipsa_id=" + recordId + "_type=user";
  }
  
}
//*********************************
//  Ändringskoll för box-to-box
//*********************************
function somethingChanged(){
  // Senast ändrat fält
  var f2=document.getElementById("F2");
  // Fält som finns i vårt gömda lager 
  var changed=document.getElementById("deviationIsChanged");
  if(f2&&changed){
    f2.value+=changed.value;
  }
}

function myBoxSubmit(){
  if(idMSAB04021)
    prepareBoxesForSubmit(idMSAB04021);
}


// Matrix functions
function MTopenMatrixGuide(){
	var theGuideWindow = showModalDialog("/forms/guideMatris.html", window, 'dialogWidth:650px;dialogHeight:750px;scroll:auto;resizable:yes;status:yes;center:yes;help:no;');
}

// Check if submitted article is same as in database. 20080411/POK
function chkArticleOk()
{
    var sNoArticleMsg = "Angiven produkt finns ej i databasen!"; 
    var sConnectAndDraft = document.getElementById("ConnectAndDraft").innerText; // Special tag "ConnectAndDraft"
        
    if(sConnectAndDraft == "Yes")
    {
        var iIndex = document.getElementById("f37").selectedIndex;
        var sArticle = document.getElementById("f37").options[iIndex].text;
        
        if(sArticle.length == 0)
        {
            makeRequired("f37",true);
            alert(sNoArticleMsg);
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        return true;
    }
    
}


//***********************************************
//  Kontroll om lokalt ärendenummer får ändras
//	2009-02-19/POK
//	2009-11-29/POK - Ändrat namn på temp-fältet och lagt till funktioner för att hantera omladdning av formuläret vid sökning
//***********************************************
function chkDevNo()
{
	var isValid = false;
	var webprofile = document.getElementById("webprofile").innerHTML;
	
	if(webprofile == "CR_W" || webprofile == "CR_WR")
	{
		var isValid = true;
	}
	else
		{
			document.getElementById("localDevNo").style.display = "";
		}
	
	if(document.getElementById("tmpLocalDevNo") && document.getElementById("tdF22") && isValid)
	{
		var localNo = strip(document.getElementById("tmpLocalDevNo").value);
		var reidarNo = strip(document.getElementById("reidarNo").value);
				
		if((reidarNo != "") && (localNo != reidarNo) && (localNo.indexOf("@") < 0))
		{
			if(document.getElementById("tmpLocalDevNo"))
			{
				document.getElementById("tmpLocalDevNo").style.display = "";
			}
		}
		else
		{
			document.getElementById("localDevNo").style.display = "";
		}
	}
}

function preserveLocalDevNo() // POK 091129
{
	if(document.getElementById("tmpLocalDevNo").value != "")
	{
		document.getElementById("_manualDevNum").value = document.getElementById("tmpLocalDevNo").value;
	}
}

function restoreLocalDevNo() // POK 091129
{
	if(document.getElementById("_manualDevNum").value != "")
	{
		document.getElementById("tmpLocalDevNo").value = document.getElementById("_manualDevNum").value;
	}
}

//***********************************************
//  Expanderande textarea
//***********************************************
function expandAreaOfText(obj,addHeight) {
  if(typeof obj=="string")
    obj = document.getElementById(obj);
  var taTmpStore = document.getElementById("expandTextareaContainer");
  if(obj&&taTmpStore) {
  	var fieldWidth = obj.style.width.substring(0,obj.style.width.length-2);
    if(!fieldWidth||fieldWidth=="") fieldWidth="615px";
    taTmpStore.style.width=parseFloat(fieldWidth-17).toString()+"px";
    if(!addHeight||addHeight=="") addHeight=20;
    var areaHeight = obj.offsetHeight;
    var content = obj.value;
    var rexp = new RegExp(/\n/g);
    var rexp2 = new RegExp(/</g);
    // Fix för "<" inte ska förstöra tempDiven 090401
    content = content.replace(rexp2,"&lt;");
    content = content.replace(rexp,"<br>");
    taTmpStore.innerHTML = content;
    taTmpStore.style.display="block";
    var contentHeight = taTmpStore.offsetHeight+28;
    taTmpStore.style.display="none";
    obj.style.height=parseFloat(contentHeight+parseFloat(addHeight))+"px";
    //alert(obj.style.height)
  }
}
