//Globals

var showError = 0;

function validate_searchbox(form)
{
if (form.search.value == "") {
    form.search.focus();
    return false;
}
return true;
}



function validate_email(field)
{
with (field)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (apos<1||dotpos-apos<2)
  {showError = 1;return false;}
else {return true;}
}
}

function validate_required(field,invalidValue)
{
with (field)
{
if (value==null||value==""||value==""+invalidValue+"")
  {showError = 1;return false;}
else {return true}
}
}

function validate_numeric(field,invalidValue)
{
with (field)
{
if (value!=parseInt(value))
  {showError = 1;return false;}
else {return true}
}
}

function validate_postcode(field)
{
with (field)
{
if (value.length<3)
  {showError = 1;return false;}
else {return true}
}
}

function validate_checkbox(field,invalidValue)
{
with (field)
{
if (field.checked==false)
  {showError = 1;return false;}
else {return true}
}
}

function validate_form(thisform)
{
var errorText = [];
showError = 0;
with (thisform)
{
if (validate_required(delivery,"0")==false)
  {errorText.push('Toimitustapa');}
if (validate_required(payment,"0")==false)
  {errorText.push('Maksutapa');}
if (validate_required(name,"")==false)
  {errorText.push('Nimi');}
if (validate_required(address,"")==false)
  {errorText.push('Lahiosoite');}
if (validate_postcode(zip)==false)
  {errorText.push('Postinumero');}
if (validate_required(city,"")==false)
  {errorText.push('Postitoimipaikka');}
if (validate_required(phone,"")==false)
  {errorText.push('Puhelin');}
if (validate_email(email)==false)
  {errorText.push('\nSahkoposti');}
if (validate_checkbox(agree,"")==false)
  {errorText.push('\nVerkkokaupan ehdot');}
}

if (showError==1)
{
 alert('Puutteelliset tiedot:\n\n'+errorText.toString());
 return false;
}
}

function ChangeColor(tableRow, highLight)
{
    if (highLight)
    {
      //tableRow.style.backgroundColor = '#d8e3fb';
      tableRow.style.backgroundColor = '#6a8ed9';
      tableRow.style.color = '#ffffff';
    }
    else
    {
      tableRow.style.backgroundColor = '#eeeeee';
      tableRow.style.color = '#000000';
    }
}

function DoNav(theUrl)
  {
  document.location.href = theUrl;
  }

function openIMGwin_static(imgURL,pageWidth,pageHeight) {
var Wname = "popup";
var exec = 'window.open("';
// Set name of common html file here
var URL   = "popup.html?"+imgURL;
exec+= URL;      exec+= '", "';
exec+= Wname;    exec+= '", "';
// default dimensions set next line
exec+='width='+pageWidth;
exec+=',height='+pageHeight;
// 0,0 positions to top left
// top=0,left=0 does for MSIE
// screenX=0,screenY=0 does for Netscape
// other attributes set for a very plain window
// note that resizable=1 because
// Netscape 4.x users will have to resize manually
// NN6.x ??
exec+=',top=0,left=0,screenX=0,screenY=0,toolbar=0,location=0';
exec+=',directories=0,resizable=1,status=0,menubar=0,scrollbars=0")';
// eval() takes the string called exec built and executes as code
eval (exec);
}

function www_analytics()
{
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-13246491-2']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

}

function eshop_analytics()
{
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-13246491-4']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
}

function isMSIE() {
  return '\v' == 'v';
}

function isIE8Browser() {
    var rv = -1;
    var ua = navigator.userAgent;
    var re = new RegExp("Trident\/([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null) {
        rv = parseFloat(RegExp.$1);
    }
    return (rv == 4);
}

function calendar() {
    $(document).ready(function() {
        $('#calendarDate').calendar({
            parentElement: '#calendarFull'
        });
    });
}

function ajaxFunction(action)
  {
  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    xmlHttp.onreadystatechange=function()
    {
      	if(xmlHttp.readyState==4)
      	{
       	 	if (action=="get_payment")
       	 	{
                	var response = xmlHttp.responseText;

                	//var returnarray = response.toString();
                	//var update = new Array();

                	//update = returnarray.split('|');

			//var ie = isMSIE();
			//alert(ie);
			//var ie = isIE8Browser();
			//if (ie == true)
			//{
			//	document.getElementById("payment").innerHTML=response;
			//	document.getElementById("payment").outerHTML=document.getElementById("payment").outerHTML;
			//}
			//else
			//{
			//	document.getElementById("payment").style.display = 'block';
			//	document.getElementById("payment").innerHTML=response;
			//}

			//document.cartform.payment.innerHTML=update;
			//document.getElementById('payment').innerHTML = '' + update;
		
			document.getElementById("paymenttextid").innerHTML="Valitse maksutapa: ";
			document.getElementById("paymentid").innerHTML=response.toString();
 		}
       	 	else if (action=="get_zip")
       	 	{
                	var response = xmlHttp.responseText;

                	var returnarray = response.toString();
                	var update = new Array();

                	update = returnarray.split('|');

                	document.cartform.city.value=update;
 		}
       	 	else if (action=="get_zip2")
       	 	{
                	var response = xmlHttp.responseText;

                	var returnarray = response.toString();
                	var update = new Array();

                	update = returnarray.split('|');

                	document.cartform.city2.value=update;
 		}
       	 	else if (action=="get_calendar_event")
       	 	{
                	var response = xmlHttp.responseText;

                	var returnarray = response.toString();
                	var update = new Array();

                	update = returnarray.split('|');

                	document.getElementById('calendarEvent').innerHTML=update;
 		}
        	else
        	{
            		//document.cartform.payment.innerHTML="<option value=0>--- Valitse maksutapa ---</option>";

        	}

      	}
    }


if (action == "get_payment")
{
    	xmlHttp.open("GET","ajaxUpdate.php?id="+action+"&value="+document.getElementById('deliveryid').value+"",true);
	xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	//xmlhttp.setRequestHeader("Cache-Control", "no-cache")
	xmlHttp.send(null);
}
if (action == "get_zip")
{
    	xmlHttp.open("GET","ajaxUpdate.php?id="+action+"&value="+document.cartform.zip.value+"",true);
	xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	xmlHttp.send(null);
}
if (action == "get_zip2")
{
    	xmlHttp.open("GET","ajaxUpdate.php?id="+action+"&value="+document.cartform.zip2.value+"",true);
	xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	xmlHttp.send(null);
}
if (action == "get_calendar_event")
{
    	xmlHttp.open("GET","template/ext/calendar/calendar.php?id="+action+"&value="+document.getElementById('calendarHidden').value+"",true);
	xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	xmlHttp.send(null);
}

}




