//////////////////////////// FORM VALIDATION ////////////////////////////

var validateerrors = 0
function ValidateInit(showrequired)
{
var inputArray = document.getElementsByTagName('input');
for ( var i=0, len=inputArray.length; i<len; ++i ){
		var sp1 = document.createElement("span");
		sp1.setAttribute("id", "validation_"+inputArray[i].name);
		var sp2 = document.getElementsByTagName('input')[i];
		var parent = sp2.parentNode;
		parent.insertBefore(sp1, sp2.nextSibling);
		if (sp2.name != "" && showrequired == "showrequired") {
		//ValidateInput(sp2.name)
		ValidateShowRequired(sp2.name)
		}
}
}

//////////////

function ValidateShowRequired(name)
{
var value = document.getElementsByName(name)[0].value
var validation = document.getElementsByName(name)[0].className
	if (validation.indexOf("formRequired") >= 0) {
		if (value != "") {
		document.getElementById("validation_"+name).innerHTML = "<img style='margin-left:5px;' src='magic_checkout/config/icons/validated.gif'>" 
		}
		else {
		document.getElementById("validation_"+name).innerHTML = "<img style='margin-left:5px;' src='magic_checkout/config/icons/required.gif'>" 
		validateerrors = validateerrors + 1
		}
	}
}

//////////////

function ValidateInput(name)
{
var valid
var value = document.getElementsByName(name)[0].value
var validation = document.getElementsByName(name)[0].className
	if (validation.indexOf("formRequired") >= 0) {
		if (value == "") {
		document.getElementById("validation_"+name).innerHTML = "<img style='margin-left:5px;' src='magic_checkout/config/icons/invalid.gif'>" 
		validateerrors = validateerrors + 1
		}
		else {
		document.getElementById("validation_"+name).innerHTML = "<img style='margin-left:5px;' src='magic_checkout/config/icons/validated.gif'>" 
		}
	}
	if (validation.indexOf("formEmail") >= 0) {
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2) {
		document.getElementById("validation_"+name).innerHTML = "<img style='margin-left:5px;' src='magic_checkout/config/icons/invalid.gif'>" 
		validateerrors = validateerrors + 1
 		}
		else 
		{
		document.getElementById("validation_"+name).innerHTML = "<img style='margin-left:5px;' src='magic_checkout/config/icons/validated.gif'>" 
		}
	}
}

//////////////

function ValidateForm()
{
validateerrors = 0
var inputArray = document.getElementsByTagName('input');
		for ( var i=0, len=inputArray.length; i<len; ++i ){
			if (inputArray[i].name != "") {
			ValidateInput(inputArray[i].name)
			}	
		}
if (validateerrors > 0){
document.getElementById("errorpanel").innerHTML = "<p><strong>Please check the form for required information and errors.</strong></p><ul><li>Missing information is highlighted with <img src='magic_checkout/config/icons/required.gif'></li><li>Invalid entries are highlighted with <img src='magic_checkout/config/icons/invalid.gif'></li></ul>"
document.getElementById("errorpanel").style.display = "block"
return false;
}else{
return true;
}
}

//////////////////////////// END FORM VALIDATION ////////////////////////////












function AJAX_get(outputid, request)
	  {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)
	        {
	        document.getElementById(outputid).innerHTML = xmlHttp.responseText
	        }
	      }
	    xmlHttp.open("GET", request,true);
	    xmlHttp.send(null);
	  }
	  
	 
function AJAX_confirmmatch(outputafter, request)
	   {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 (xmlHttp.responseText == 1) {
				document.getElementById(outputafter+'_confirm').innerHTML = "<img style='margin-left:5px;' src='magic_checkout/config/icons/validated.gif'>" 
				}else{
				document.getElementById(outputafter+'_confirm').innerHTML = "" 
				}
	        }
	      }
	    xmlHttp.open("GET", request,true);
	    xmlHttp.send(null);
	  }