// ********************************************************************************//
// Functions related to admin of the shop
// *******************************************************************************//
// OPEN IMAGE-LIB
function popURL(url)
	{
	// Popup window with links to image library. 
	var popleft=((document.body.clientWidth - 440) / 2)+window.screenLeft; 
	var poptop=(((document.body.clientHeight - 460) / 2))+window.screenTop-40;		
	window.open(url,"_blank","scrollbars=NO,width=480,height=400,left="+popleft+",top="+poptop)
	}

// SAVE ARTICLE
function SaveArticle()
 {
 var msg
 msg = "Du måste" + ":\n";

 document.forms[0].elements.inpContent.value = oEdit1.getHTMLBody();

 if (document.forms[0].sel_prodcat.value == '') { msg += ("\n - välja Kategori");}
 if (document.forms[0].str_prodname.value == '') { msg += ("\n - fylla i Produktnamn");}
 if (document.forms[0].dbl_prodprice.value == '') { msg += ("\n - fylla i Pris");}
 if (document.forms[0].dbl_prodweight.value == '') { msg += ("\n - fylla i Vikt");}

 if (msg != "Du måste" + ":\n") {
	alert(msg)
	return;
	}

	else 
	{ document.forms[0].submit() }
}

// ************************************* //
// Functions for (multiple rows update)
// ************************************* //
function RecUpdate(Rec_Id)
{
var ThisID = "*" + (Rec_Id) + "*"
 if (document.forms[0].hidRec_Ids.value == "")
 {	
  document.forms[0].hidRec_Ids.value = (ThisID)
  }
 if (document.forms[0].hidRec_Ids.value != "")
 {  
  var str = document.forms[0].hidRec_Ids.value;
  var pos = str.indexOf(ThisID);
  if (pos == -1) 
  {
  document.forms[0].hidRec_Ids.value = document.forms[0].hidRec_Ids.value + ", " + (ThisID)  
  } 											 
 }	
}

// Settings
function SaveShopSettings()
{
var msg
msg = "Du måste:\n";

 if (document.forms[0].i_IntPageSize.value == '') { msg += ("- ange antal poster per sida.\n");}
 if (document.forms[0].i_NumDec.value == '') {msg += ("- ange antal decimaler.\n");}
 if (document.forms[0].dbl_PercentTAX.value == '') {msg += ("- ange momspåslag.\n");}
 if (document.forms[0].str_Cur_01.value == '') {msg += ("- välja standardvaluta.\n");}
 if (document.forms[0].str_Cur_02.value == '') {msg += ("- välja alternativ valuta.\n");}
 if (document.forms[0].dbl_CurConvFactor_02.value == '') {msg += ("- ange konverteringsfaktor för valuta nr 2.\n");}
 if (document.forms[0].b_ShowSessionTimer.value == '') {msg += ("- välja ja/nej på sessionstimer.\n");}
 if (document.forms[0].dbl_LeaseAdminCost.value == '') {msg += ("- ange avgift för leasing.\n");}
 if (document.forms[0].str_SMTPServer.value == '') {msg += ("- ange SMTP-server.\n");}
 if (document.forms[0].str_ShopEmail.value == '') {msg += ("- ange epostadress för shopen.\n");}
 if (document.forms[0].str_CompanyOrgNr.value == '') {msg += ("- ange organisations- el. personnr.\n");}
 if (document.forms[0].str_DocCommonTerms.value == '') {msg += ("- ange sökväg för allmänna villkor.\n");}
 if (document.forms[0].str_DocLeaseTerms.value == '') {msg += ("- ange sökväg för hyresavtal.\n");}

 checkEmail = document.forms[0].str_ShopEmail.value
 if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.'))) 
 { msg += ("- fylla i en korrekt emailadress" + "\n"); }

if (msg != "Du måste:\n") {
alert(msg)
return;
}

document.forms[0].submit()
}

// ******************************************************************************************************
//Function to go to URL
// usage: <input type=button value=""Avbryt"" onClick=gotoURL('" & THIS_SCRIPT & "?mode=listSchemas')>
// ******************************************************************************************************
function gotoURL(url)
{ self.location.href = url; }

// *************************
// function confirmSubmit()
// used to clear session
// *************************
function confirmSubmit(url)
{
var agree=confirm("VARNING!\nDenna åtgärd avbryter ordern\noch loggar ut dig!\nVill du fortsätta?");
if (agree)
	{self.location.href = url
	return true ;}
else
	return false ;
}

 // ******************************* //
 // Check login form
 // ******************************* //
 function checkLogin()
 {
 var msg
 var btn = valButton(login.customer_type);
 
 msg = "Du måste" + ":\n";

 checkEmail = document.login.customer_email.value
 if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.'))) 
 { msg += ("- fylla i en korrekt emailadress" + "\n"); }

 if (btn == null) {msg += ("- välja företag eller privatperson" + "\n"); }

	if (msg != "Du måste" + ":\n") {
	alert(msg)
	return;
	}

	else 
	{ document.login.submit() }
}

 // ******************************* //
 // Check Customer submit form
 // ******************************* //
 function checkCustomerSubmit(mInput)
 {
 var msg = "Du måste" + ":\n";
 var myCustType = mInput;

 // var btn = valButton(editcustomer.cust_paymode);
 // if (btn == null) {msg += ("\n - Välja betalningssätt"); }

if (myCustType == 2) { // företag
 if (document.editcustomer.cust_orgnr.value == '') { msg += ("\n - Ange Organisationsnr");}
 if (document.editcustomer.cust_companyname.value == '') { msg += ("\n - Ange Företagsnamn");}
 if (document.editcustomer.cust_invoiceaddress.value == '') { msg += ("\n - Ange faktureringsadress");}
 if (document.editcustomer.cust_invoicezipcode.value == '') { msg += ("\n - Ange faktura postnr");}
 if (document.editcustomer.cust_invoicecity.value == '') { msg += ("\n - Ange faktura ort");}
 if (document.editcustomer.cust_invoicecountry.value == '') { msg += ("\n - Ange faktura land");}

}

 if (document.editcustomer.cust_contactname.value == '') { msg += ("\n - Ange Ditt namn");}
 if (document.editcustomer.cust_phone.value == '') { msg += ("\n - Ange telefonnr");}
 if (document.editcustomer.cust_deliveryaddress.value == '') { msg += ("\n - Ange leveransadress");}
 if (document.editcustomer.cust_deliveryzipcode.value == '') { msg += ("\n - Ange postnr leveransadress");}
 if (document.editcustomer.cust_deliverycity.value == '') { msg += ("\n - Ange leveransort");}
 if (document.editcustomer.cust_deliverycountry.value == '') { msg += ("\n - Ange leveransland");}

checkEmail = document.editcustomer.cust_email.value
 if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.'))) 
 { msg += ("\n - Fylla i en korrekt emailadress"); }

 if (msg != "Du måste" + ":\n") {
  alert(msg)
  return;
  }

 else 
 { 
  document.editcustomer.save.value = "Sparar...";
  document.editcustomer.save.disabled = true;
  document.editcustomer.submit() 
  }
}

 // ******************************* //
 // Check Order submit form
 // ******************************* //
 function checkOrderSubmit(mInput,bRadios)
 {
 var msg = "Du måste" + ":\n";
 var myTerms = mInput;
 var blnRadios = bRadios;
 
 
 var btn = valButton(doplaceorder.cust_paymode);

if (blnRadios == '1') {
 if (btn == null) {msg += ("\n - Välja betalningssätt"); }
}

 if (document.doplaceorder.cust_orgnr.value == '') { msg += ("\n - Ange Organisationsnr");}
 if (document.doplaceorder.cust_companyname.value == '') { msg += ("\n - Ange Företagsnamn");}
 if (document.doplaceorder.cust_contactname.value == '') { msg += ("\n - Ange Ditt namn");}
 if (document.doplaceorder.cust_phone.value == '') { msg += ("\n - Ange telefonnr");}
 
 if (document.doplaceorder.cust_invoiceaddress.value == '') { msg += ("\n - Ange faktureringsadress");}
 if (document.doplaceorder.cust_invoicezipcode.value == '') { msg += ("\n - Ange faktura postnr");}
 if (document.doplaceorder.cust_invoicecity.value == '') { msg += ("\n - Ange faktura ort");}
 if (document.doplaceorder.cust_invoicecountry.value == '') { msg += ("\n - Ange faktura land");}

 if (document.doplaceorder.cust_deliveryaddress.value == '') { msg += ("\n - Ange leveransadress");}
 if (document.doplaceorder.cust_deliveryzipcode.value == '') { msg += ("\n - Ange postnr leveransadress");}
 if (document.doplaceorder.cust_deliverycity.value == '') { msg += ("\n - Ange leveransort");}
 if (document.doplaceorder.cust_deliverycountry.value == '') { msg += ("\n - Ange leveransland");}

 
// test av frekvens-validering
 for (var i = 0; i < document.doplaceorder.elements.length; i++) {
  if ((document.doplaceorder.elements[i].name.indexOf('prod_freq_') > -1)) {
   if (document.doplaceorder.elements[i].selectedIndex == 0) {
    var mstring = document.doplaceorder.elements[i].name;
    mstring = mstring.replace("prod_freq_","artikel ");
	msg += ("\n - Välja frekvens för " + mstring);
     }
    }
   }

if (myTerms == 1) { // privatperson + beställt hyrd produkt
 if (!doplaceorder.terms_of_delivery.checked) { msg += "\n\n - Godkänna försäljningsvillkoren"; } 
 if (!doplaceorder.terms_of_hire.checked) { msg += "\n - Godkänna hyresvillkoren"; }
 if (!doplaceorder.cust_over18.checked) { msg += "\n - Garantera att du är 18 år eller äldre"; }
}

if (myTerms == 2) { // privatperson som ej beställt hyrd produkt
 if (!doplaceorder.terms_of_delivery.checked) { msg += "\n - Godkänna försäljningsvillkoren"; } 
 if (!doplaceorder.cust_over18.checked) { msg += "\n - Garantera att du är 18 år eller äldre"; }
}

checkEmail = document.doplaceorder.cust_email.value
 if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.'))) 
 { msg += ("\n - Fylla i en korrekt emailadress"); }

 if (msg != "Du måste" + ":\n") {
  alert(msg)
  return;
  }

 else 
 { 
  document.doplaceorder.pay.value = "Bearbetar order...";
  document.doplaceorder.exit.disabled = true;
  document.doplaceorder.pay.disabled = true;
  document.doplaceorder.submit()
  if(id_waitmsg) id_waitmsg.style.visibility = 'visible';
  setTimeout('document.images["pbar"].src = "/assets/shop/layout/loading.gif"', 200);
  }
}

// *************************************************************************************** //
// Radio Button Validation helper function to checkLogin() and checkOrderSubmit(mInput)
// *************************************************************************************** //
function valButton(btn) {
 var cnt = -1;
 for (var i=btn.length-1; i > -1; i--) {
   if (btn[i].checked) {cnt = i; i = -1;}
   }
  if (cnt > -1) return btn[cnt].value;
 else return null;
}

// FUNCTIONS RELATED TO SESSION TIME-OUT //
// Usage: Add the following to your page //
// ************************************* //
// <BODY OnLoad="timeIt()">              //
// <form name="timerform"><input type="text" name="clock" size="7" value="2:00"></form> //
// ************************************* //

// Take user here after session timed out
timedouturl = "session-timed-out.html";

function Minutes(data) {
for (var i = 0; i < data.length; i++)
if (data.substring(i, i + 1) == ":")
break;
return (data.substring(0, i));
}

function Seconds(data) {
for (var i = 0; i < data.length; i++)
if (data.substring(i, i + 1) == ":")
break;
return (data.substring(i + 1, data.length));
}

function Display(min, sec) {
var disp;
if (min <= 9) disp = " 0";
else disp = " ";
disp += min + ":";
if (sec <= 9) disp += "0" + sec;
else disp += sec; 
return (disp);
}

function Down() { 
sec--;      
if (sec == -1) { sec = 59; min--; }
 document.timerform.clock.value = Display(min, sec);
 window.status = "Sessionen avslutas om: " + Display(min, sec) + " minuter";
if (min == 0 && sec == 0) {
alert("Din session har avslutats.");
// window.location.href = timedouturl;
}
else down = setTimeout("Down()", 1000);
}

function timeIt() {
min = 1 * Minutes(document.timerform.clock.value);
sec = 0 + Seconds(document.timerform.clock.value);
Down();
}


// END FUNCTIONS RELATED TO SESSION TIME-OUT //
