<!--
notfilled = new Image(20,20);
notfilled.src = "images/notfilled.jpg";

okay = new Image(20,20);
okay.src = "images/okay.jpg";

function addItemToOrder(id, formName)
{
thisQty = "qty"+id;
thisQty = document.getElementById(thisQty).value;
  
if(document.getElementById("itemQty"))
{
 //check if the hidden qty exists and if so del it.
 var oDelP = document.getElementById("itemQty");
 document.forms[formName].removeChild(oDelP);
}

var oP = document.createElement("input");
with(oP)
{
      setAttribute("id", "itemQty");
	  setAttribute("type", "hidden"); 
	  setAttribute("name", "addQty");
	  setAttribute("value", thisQty);
}
document.forms[formName].appendChild(oP);
return true;
}

function def(myCheckValue, setting)
{
	return;
	myCheckValue = document.getElementById(myCheckValue);
	myCheckValue.checked = setting;
}

function changestate(myCheckValue)
{
	myCheckValue = document.getElementById(myCheckValue);
	//alert(myCheckValue.checked);
	if(myCheckValue.checked == false)
	{
		deleteCookie("first_name_store");
		deleteCookie("last_name_store");
		deleteCookie("address_store");
		deleteCookie("suburb_store");
		deleteCookie("city_store");
		deleteCookie("post_code_store");
		deleteCookie("country_store");
		deleteCookie("phone_store");
		deleteCookie("email_store");
	}
	else 
	{
		var d = new Date();
		d = d.getTime();
		//alert(d);
		var oExpires = d+63072000;
		var  first_name = document.personal_details.first_name.value;
		var  last_name = document.personal_details.last_name.value;
		var  address = document.personal_details.address.value;
		var  suburb = document.personal_details.suburb.value;
		var  city = document.personal_details.city.value;
		var  post_code = document.personal_details.post_code.value;
		var  country = document.personal_details.country.value;
		var  phone = document.personal_details.phone.value;
		var  email = document.personal_details.email.value;
				
		setCookie("first_name_store", first_name, oExpires);
		setCookie("last_name_store", last_name, oExpires);
		setCookie("address_store", address, oExpires);
		setCookie("suburb_store", suburb, oExpires);
		setCookie("city_name_store", city, oExpires);	
		setCookie("country_name_store", country, oExpires);	
		setCookie("phone_name_store", phone, oExpires);	
		setCookie("email_name_store", email, oExpires);	
	}
	//alert(myCheckValue.checked);
}

function validate()
{
	if(first_name_changePicture() == true && suburb_changePicture() == true && address_changePicture() == true  && suburb_changePicture() == true && city_changePicture() == true && post_code_changePicture() == true && country_changePicture() == true && phone_changePicture() == true && email_changePicture() == true)
	{
		if(document.personal_details.pay_by_internet_banking.checked == false)
		{
			if(card_name_changePicture() == true && card_number_changePicture() == true && card_exp_month_changePicture() == true && card_exp_year_changePicture() == true)
			{
				//alert("1it worked");
				return true;
			}
			else
			{
				alert("Oops, we appear to be missing some details. Please check all the fields which have a question mark beside them.");
				return false;
			}
		}
		else
		{
			//alert("2it worked");
			return true;
		}
	}
	else
	{
		alert("Oops, we appear to be missing some details. Please check all the fields which have a question mark beside them.");
		return false;
	}
}


function first_name_changePicture()
{
	if(document.personal_details.first_name.value == '')
	{
		document.first_name_picture.src=notfilled.src;
		return false;
	}
	else
	{
		var  myvalue = stripSpaces(document.personal_details.first_name.value);
		var  myvalue = toUpper(myvalue);
		document.personal_details.first_name.value = myvalue;
		document.first_name_picture.src=okay.src;
		myCheckValue = document.getElementById("store_details_check");
		if(myCheckValue.checked == 1)
		{		
		setCookie("first_name_store", myvalue);
		}
		return true;
	}
}

function last_name_changePicture()
{
	if(document.personal_details.last_name.value == '')
	{
		document.last_name_picture.src=notfilled.src;
		return false;
	}
	else
	{
		var  myvalue = stripSpaces(document.personal_details.last_name.value);
		var  myvalue = toUpper(myvalue);
		document.personal_details.last_name.value = myvalue;
		document.last_name_picture.src=okay.src;	
		myCheckValue = document.getElementById("store_details_check");
		if(myCheckValue.checked == 1)
		{		
		setCookie("last_name_store", myvalue);
		}
		
		return true;
	}
}


function address_changePicture()
{
	if(document.personal_details.address.value == '')
	{
		document.address_picture.src=notfilled.src;
		return false;
	}
	else
	{
		var  myvalue = stripSpaces(document.personal_details.address.value);
		var  myvalue = toUpper(myvalue);
		document.personal_details.address.value = myvalue;
		document.address_picture.src=okay.src;	
		myCheckValue = document.getElementById("store_details_check");
		if(myCheckValue.checked == 1)
		{		
		setCookie("address_store", myvalue);
		}
		return true;
	}
}

function suburb_changePicture()
{
	if(document.personal_details.suburb.value == '')
	{
		document.suburb_picture.src=notfilled.src;
		return false;
	}
	else
	{
		var  myvalue = stripSpaces(document.personal_details.suburb.value);
		var  myvalue = toUpper(myvalue);
		document.personal_details.suburb.value = myvalue;
		document.suburb_picture.src=okay.src;	
		myCheckValue = document.getElementById("store_details_check");
		if(myCheckValue.checked == 1)
		{		
		setCookie("suburb_store", myvalue);
		}
		return true;
	}
}

function city_changePicture()
{
	if(document.personal_details.city.value == '')
	{

		document.city_picture.src=notfilled.src;
		return false;
	}
	else
	{
		var  myvalue = stripSpaces(document.personal_details.city.value);
		var  myvalue = toUpper(myvalue);
		document.personal_details.city.value = myvalue;
		document.city_picture.src=okay.src;	
		myCheckValue = document.getElementById("store_details_check");
		if(myCheckValue.checked == 1)
		{		
		setCookie("city_store", myvalue);
		}
		return true;
	}
}

function post_code_changePicture()
{
	if(document.personal_details.post_code.value == '')
	{
		document.post_code_picture.src=notfilled.src;
		return false;
	}
	else
	{
		var  myvalue = stripSpaces(document.personal_details.post_code.value);
		document.post_code_picture.src=okay.src;
		myCheckValue = document.getElementById("store_details_check");
		if(myCheckValue.checked == 1)
		{		
		setCookie("post_code_store", myvalue);
		}
		return true;
	}
}

function country_changePicture()
{
	if(document.personal_details.country.value == '')
	{
		document.country_picture.src=notfilled.src;
		return false;
	}
	else
	{
		var  myvalue = stripSpaces(document.personal_details.country.value);
		var  myvalue = toUpper(myvalue);
		document.personal_details.country.value = myvalue;
		document.country_picture.src=okay.src;
		myCheckValue = document.getElementById("store_details_check");
		if(myCheckValue.checked == 1)
		{		
		setCookie("country_store", myvalue);
		}
		return true;
	}
}

function phone_changePicture()
{
	if(document.personal_details.phone.value == '')
	{
		document.phone_picture.src=notfilled.src;
		return false;
	}
	else
	{
		var  myvalue = stripSpaces(document.personal_details.phone.value);
		document.phone_picture.src=okay.src;
		myCheckValue = document.getElementById("store_details_check");
		if(myCheckValue.checked == 1)
		{		
		setCookie("phone_store", myvalue);
		}
		return true;
	}
}

function email_changePicture()
{
	var myEmail = validate_email(document.personal_details.email.value);
	
	if(document.personal_details.email.value == '' || myEmail == false)
	{
		document.email_picture.src=notfilled.src;
		return false;
	}
	else
	{
		var  myvalue = stripSpaces(document.personal_details.email.value);
		document.email_picture.src=okay.src;
		myCheckValue = document.getElementById("store_details_check");
		if(myCheckValue.checked == 1)
		{		
		setCookie("email_store", myvalue);
		}
		return true;
	}
}

function card_name_changePicture()
{
	if(document.personal_details.card_name.value == '')
	{
		document.card_name_picture.src=notfilled.src
		return false;
	}
	else
	{
		var  myvalue = stripSpaces(document.personal_details.card_name.value);
		var  myvalue = toUpper(myvalue);
		document.personal_details.card_name.value = myvalue;
		document.card_name_picture.src=okay.src	
		return true;
	}
}

function card_number_changePicture()
{
	if(document.personal_details.card_number.value == '')
	{
		document.card_number_picture.src=notfilled.src
		return false;
	}
	else
	{
		var myResultVisa = isValidVisa(document.personal_details.card_number.value);
		var myResultMaster = isValidMasterCard(document.personal_details.card_number.value);
		
		if (myResultVisa == false && myResultMaster == false)
		{
			document.card_number_picture.src=notfilled.src
			return false;
		}
		else
		{
			document.card_number_picture.src=okay.src	
			return true;
		}
	}
}

function card_exp_month_changePicture()
{
	var myvalue = document.personal_details.card_exp_month.value;
	if(isNaN(myvalue) || myvalue < 1  || myvalue > 12)
	{
		document.card_exp_month_picture.src=notfilled.src
		return false;
	}
	else
	{
		document.card_exp_month_picture.src=okay.src	
		return true;
	}
}


function card_exp_year_changePicture()
{
	var myvalue = document.personal_details.card_exp_year.value
	if(isNaN(myvalue) || myvalue < 7 || myvalue > 14)
	{
		document.card_exp_year_picture.src=notfilled.src
		return false;
	}
	else
	{
		document.card_exp_year_picture.src=okay.src	
		return true;
	}
}

function card_id_changePicture()
{
	return true;
	if(document.personal_details.card_id.value == '')
	{
		document.card_id_picture.src=notfilled.src
		return false;
	}
	else
	{
		document.card_id_picture.src=okay.src	
		return true;
	}
}

function stripSpaces(value)
{
    x = value;
	while (x.substring(0,1) == ' ') x = x.substring(1);
    while (x.substring(x.length-1,x.length) == ' ') x = x.substring(0,x.length-1);
	return x;
}

	
function toUpper(value)
{
    if (value == "-")
	{
		return value;
	}
	
	var pattern = /(\w)(\w*)/; // a letter, and then one, none or more letters 

    var a = value.split(/\s+/g); // split the sentence into an array of words

    for (i = 0 ; i < a.length ; i ++ ) {
        var parts = a[i].match(pattern); // just a temp variable to store the fragments in.

        var firstLetter = parts[1].toUpperCase();
        var restOfWord = parts[2].toLowerCase();

        a[i] = firstLetter + restOfWord; // re-assign it back to the array and move on
    }
   	return a.join(' '); // join it back together
}

function luhnCheckSum(sCardNum)
{
	var iOddSum = 0;
 	var iEvenSum = 0;
    var bIsOdd = true;
                
    for (var i=sCardNum.length-1; i >= 0; i--)
	{
 	   	var iNum = parseInt(sCardNum.charAt(i));
  		if (bIsOdd)
		{
        	iOddSum += iNum;
   		}
		else
		{
			iNum = iNum * 2;
         	if (iNum > 9)
			{
             	iNum = eval(iNum.toString().split("").join("+"));
           	}
       		iEvenSum += iNum;
        }
        bIsOdd = !bIsOdd;
	}
return ((iEvenSum + iOddSum) % 10 == 0);
}

      
function isValidVisa(sText)
{
    var reVisa = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
	
	if (reVisa.test(sText))
	{
     return luhnCheckSum(RegExp.$1);
	} 
	else
	{
		return false; 
  	}
}				
				
function isValidMasterCard(sText)
{
	var reMasterCard = /^(5[1-5]\d{2})[\s\-]?(\d{4})[\s\-]?(\d{4})[\s\-]?(\d{4})$/;
  	if (reMasterCard.test(sText))
	{
   		var sCardNum = RegExp.$1 + RegExp.$2 + RegExp.$3 + RegExp.$4;
    	return luhnCheckSum(sCardNum);
	}
	else
	{
	return false;
	}
}

function validate_email(field)
{
	with (field)
	{
		apos=field.indexOf("@")
		dotpos=field.lastIndexOf(".")
		if (apos<1||dotpos-apos<2) 
  		{
			return false;
		}
		else
		{
			return true;
		}
	}
}

function show_payment_details()
{
	myElement = document.getElementById("payment_information");
	if(document.personal_details.pay_by_internet_banking.checked == true)
	{
		myElement.style.display='';
		document.personal_details.pay_by_internet_banking.checked = true;
	}
	else
	{
		myElement.style.display='none';
		document.personal_details.pay_by_internet_banking.checked = false;
	}
}

function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure)
{
var sCookie = sName + "=" + encodeURIComponent(sValue);

if (oExpires){sCookie +="; expires=" + oExpires;}

if (sPath) {sCookie +="; path=" +sPath;}

if (sDomain) {sCookie += "; domain=" + sDomain;}

if (bSecure) {sCookie += "; secure";}

document.cookie = sCookie;
}

function deleteCookie(sName)
{
	setCookie(sName,"", new Date(0))
}

function getCookie(sName)
{
	var sRE = "<?:;)?" + sName + "=([^;]*);?";
	var oRE = new RegExp(sRE);
	if (oRE.test(document.cookie))
	{
	return decodeURIComponent(RegExp["$1"]);
	}
	else
	{
		return null;
	}
}

function resize(i, target)
{
	inWidth = document.getElementById(i).offsetWidth;
	inHeight = document.getElementById(i).offsetHeight;
	
	if (inWidth > inHeight)
	{ 
		percentage = (target / inWidth);
	}
	else
	{
		percentage = (target / inHeight);
	}
		
	outWidth = Math.round(inWidth * percentage);
	outHeight = Math.round(inHeight * percentage);
	document.getElementById(i).width= outWidth;
	document.getElementById(i).height= outHeight;
}

function openWin(url)
{
window.open("../picture_viewer.html?p="+url,"_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, statusbar=no");
}

function openLink(product)
{
	url = "http://www.maerklin.de/produkte/frontend/index.php?artikel_nr="
url = url+product;
url = url +"&stichwort=&baureihe=&spur=all&gruppe=all&untergruppe=all&merkmale=all&epoche=all&anzeigen_y=1&sCountryCode=en&bShowLogo=0&fromSLIMSITE=3&anzeigen.x=14&anzeigen.y=8";
window.open("../popup_viewer.html?p="+url,"_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, statusbar=no, location=no");
}
//-->