﻿// JScript File

//Validate Customer
function Validate_Customer()
{
    
	if (IsBlank(document.thisform.fld_firstname.value))
	    {
	        alert("Du mangler at udfylde fornavn");             	    
            return false;
	    }
    if (IsBlank(document.thisform.fld_lastname.value))
    {
        alert("Du mangler at udfylde efternavn");             	    
        return false;
    }
    if (IsBlank(document.thisform.fld_address.value))
    {
        alert("Du mangler at udfylde adresse");             	    
        return false;
    }
    if (IsBlank(document.thisform.fld_zipcode.value))
    {
        alert("Du mangler at udfylde postnummer");             	    
        return false;
    }
    if (IsBlank(document.thisform.fld_city.value))
    {
        alert("Du mangler at udfylde by");             	    
        return false;
    }
    if (IsBlank(document.thisform.fld_country.value))
    {
        alert("Du mangler at udfylde land");             	    
        return false;
    }
    if (IsBlank(document.thisform.fld_phone.value) && IsBlank(document.thisform.fld_mobile_phone.value))
    {
        alert("Du skal udfylde telefon eller mobiltelefon");             	    
        return false;
    }
    if (!IsValidEmail(document.thisform.fld_email.value))
    {
        alert("Du mangler at udfylde email eller email er ikke korrekt");             	    
        return false;
    }
    
    
	if (IsBlank(document.thisform.fld_password.value))
    {
        alert("Du mangler at udfylde kodeord");             	    
        return false;
    }
	
    
	else 
	{
	return true;
	}
}


/* ======================================================================
FUNCTION:	IsBlank
 
INPUT:		val - the value to be tested

RETURN:  	true, if the string is null, undefined or an empty string, ""
      		false, otherwise.

CALLS:		IsNull(), IsUndef() 
====================================================================== */
function IsBlank( str ) {
	var isValid = false;

 	if ( IsNull(str) || IsUndef(str) || (str+"" == "") )
 		isValid = true;
		
	return isValid;
} 

/* ======================================================================
FUNCTION:	IsNull
 
INPUT:		val - the value to be tested

RETURN:  	true, if the value is null; false, otherwise.
====================================================================== */
function IsNull( val ) {
	var isValid = false;

 	if (val+"" == "null")
 		isValid = true;
		
	return isValid;
}  

/* ======================================================================
FUNCTION:	IsUndef
 
INPUT:		val - the value to be tested

RETURN:  	true, if the value is undefined
      		false, otherwise.
====================================================================== */
function IsUndef( val ) {
	var isValid = false;

 	if (val+"" == "undefined")
 		isValid = true;
		
	return isValid;
} 
/* ======================================================================
FUNCTION:  	IsValidEmail
 
INPUT:    	str (string) - an e-mail address to be tested

RETURN:  	true, if the string contains a valid e-mail address which is a string
		plus an '@' character followed by another string containing at least 
		one '.' and ending in an alpha (non-punctuation) character.
		false, otherwise

CALLS:		IsBlank(), IsAlpha() 
====================================================================== */
function IsValidEmail( str ) {
	
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;

	str += "";

	namestr = str.substring(0, str.indexOf("@"));  // everything before the '@'
	domainstr = str.substring(str.indexOf("@")+1, str.length); 

	if (IsBlank(str) || (namestr.length == 0) || 
			(domainstr.indexOf(".") <= 0) ||
			(domainstr.indexOf("@") != -1) ||
			!IsAlpha(str.charAt(str.length-1)))
		isValid = false;
   
   	return isValid;
} 
/* ======================================================================
FUNCTION:  	IsAlpha

INPUT:		str (string) - the string to be tested

RETURN:  	true, if the string contains only alphabetic characters false, otherwise.
====================================================================== */
function IsAlpha( str ) {
	
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;
	str += "";	

  	for (i = 0; i < str.length; i++) {
	
		if ( !( ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
      			((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ) ) {
         				isValid = false;
         				break;
      			}
   } 
   	return isValid;
}  

/* ======================================================================
FUNCTION:  	IsNum
 
INPUT:  	numstr (string/number) - the string that will be tested to ensure 
	        that the value is a number (int or float)

RETURN:  	true, if all characters represent a valid integer or float
 		false, otherwise.
====================================================================== */
function IsNum( numstr ) {
	
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")	
		return false;

	var isValid = true;
	var decCount = 0;		

	numstr += "";	

	for (i = 0; i < numstr.length; i++) {
		// track number of decimal points
		if (numstr.charAt(i) == "." || numstr.charAt(i) == ",")
			decCount++;

    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || 
				(numstr.charAt(i) == ",") || (numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(i) == "-" && i != 0) ||
				(numstr.charAt(i) == "." && numstr.length == 1) ||
			  (numstr.charAt(i) == "." && decCount > 1)) {
       	isValid = false;
       	break;
      }         	         	       

   }   
   	return isValid;
}  

/* ======================================================================
FUNCTION:	IsUndef
 
INPUT:		val - the value to be tested

RETURN:  	true, if the value is undefined
      		false, otherwise.
====================================================================== */
function IsUndef( val ) {
	var isValid = false;

 	if (val+"" == "undefined")
 		isValid = true;
		
	return isValid;
} 
 
function surfto(form) 
{       
var myindex=form.fld_category.selectedIndex
if (form.fld_category.options[myindex].value != "0") 
  {
   window.open(form.fld_category.options[myindex].value, target="_self");
  }
}

function ShowLager()
 {
  if( document.getElementById("fld_in_stock").checked )
   {
    document.getElementById("identStockLimit").style.display = "block";
    document.getElementById("identStockAmount").style.display = "block";
   }
  else
   {
    document.getElementById("identStockLimit").style.display = "none";
    document.getElementById("identStockAmount").style.display = "none";
   }
 }

function ShowOffer()
 {
  if( document.getElementById("fld_offer").checked )
   {
    document.getElementById("fld_offer_prc").disabled = false;
   }
  else
   {
    document.getElementById("fld_offer_prc").disabled = true;
   }
 }

function OfferCheck()
 {
  if( document.getElementById("fld_offer_prc").value.lenght > 0 )
   {
    document.getElementById("fld_offer").checked = false;
   }
  else
   {
    document.getElementById("fld_offer").checked = true;
   }
 }
