// JavaScript Document

$forms_array = new Array();
function fieldcheck()
{
	// now we have to check the forms
	document.getElementById("search").onclick=checksearchvalue;
	$all_form = document.getElementsByTagName("FORM");
	
	if($all_form.length > 0)
	{
		checkinput($all_form);
	}
}


function checkinput()
{
	for($count = 0;$count < $all_form.length;$count++)
	{
		$forms_array[$all_form[$count].id] = new Array();
		$data = $all_form[$count].getElementsByTagName("INPUT");
		
		
		
		for($count_1 =0;$count_1 <  $data.length;$count_1++)
		{
			$forms_array[$all_form[$count].id][$data[$count_1].id] = $data[$count_1];
			$forms_array[$all_form[$count].id][$data[$count_1].id].onfocus =  formfocus;
			if( $data[$count_1].type == "submit")
			{
				$data[$count_1].disabled = true;
				$forms_array[$all_form[$count].id]["submit"] = $data[$count_1];
			}
		} // end of the if condition
		
		$data = $all_form[$count].getElementsByTagName("SELECT");		
		for($count_1 =0;$count_1 <  $data.length;$count_1++)
		{
			$forms_array[$all_form[$count].id][$data[$count_1].id] = $data[$count_1];
			
		} // end of the if condition

		$data = $all_form[$count].getElementsByTagName("TEXTAREA");		
		for($count_1 =0;$count_1 <  $data.length;$count_1++)
		{
			$forms_array[$all_form[$count].id][$data[$count_1].id] = $data[$count_1];
		} // end of the if condition

		if((typeof $forms_array[$all_form[$count].id]["validatecode"]) == "object")
		{
			data = $forms_array[$all_form[$count].id]["validatecode"].value;
			$maindata = makeRequest("getnumber.php");
			$l_data = $maindata.split("-")
			
			$forms_array[$all_form[$count].id]["validatecode"].value = $l_data[0];
			document.getElementById(data).innerHTML = "Please input this code <br />";
			document.getElementById(data).innerHTML = document.getElementById(data).innerHTML + "<strong>" + $l_data[1] + " </strong>";
			document.getElementById(data).innerHTML = document.getElementById(data).innerHTML +"<input type='text' name='" + data + "_data'  id='" + data + "_data'><br /><br />";
			
			$forms_array[$all_form[$count].id]["validatecode_data"] = document.getElementById(data + "_data")
			$forms_array[$all_form[$count].id]["validatecode_data"].onblur =  formvalidate;
			$forms_array[$all_form[$count].id]["validatecode_data"].onfocus =  formfocus;
			$forms_array[$all_form[$count].id]["validatecode_data"].formname = $all_form[$count].id;
			$forms_array[$all_form[$count].id]["validatecode_data"].validation = 1;
			$forms_array[$all_form[$count].id]["validatecode_data"].valid = 0;
					
		} // end of the condition

		if((typeof $forms_array[$all_form[$count].id]["validate"]) == "object")
		{
			$x_data = $forms_array[$all_form[$count].id]["validate"].value.split(",");
			for($count_x = 0;$count_x < $x_data.length;$count_x++)
			{
				try
				{
					$forms_array[$all_form[$count].id][$x_data[$count_x]].onblur =  formchecker;
					$forms_array[$all_form[$count].id][$x_data[$count_x]].onfocus =  formfocus;
					$forms_array[$all_form[$count].id][$x_data[$count_x]].formname = $all_form[$count].id;
					$forms_array[$all_form[$count].id][$x_data[$count_x]].validation = 1;
					$forms_array[$all_form[$count].id][$x_data[$count_x]].valid = 0;
				}
				catch(e)
				{
				}
				
			} // end of the for loop
		}
	} // end of the for loop
} // end of the fucntion

function formfocus()
{
	if(this.value == "...")
	{
		this.value= "";
	}
}


function formchecker()
{
	$dataform = $forms_array [this.formname];
	$dataform["submit"].value = "Checking....";
	if(this.value == "" || this.value == "...")
	{
		this.value = "...";
		this.valid=0;
	}
	else
	{
		this.valid=1;
	}
	enablesubmit(this.formname);
}


function makeRequest(url) 
{ 

   if (window.XMLHttpRequest) 
   { // Mozilla, Safari,... 
	   http_request = new XMLHttpRequest(); 
   } 
   else if (window.ActiveXObject) 
   { // IE 
	   http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
   } 
  // http_request.onreadystatechange = alertContents; 
   http_request.open('GET', url, false); 
   http_request.send(null);
   return http_request.responseText; 

} 


function formvalidate()
{
	
	$dataform = $forms_array [this.formname];
	$dataform["submit"].value = "Checking....";
	$x_data = document.getElementById("validatecode").value +"-" + this.value;
	data = makeRequest("checknumber.php?number=" + $x_data);
	document.getElementById("validatecode_actual").value=this.value;
	if(data == 1)
	{
		this.valid=1;
	}
	else
	{
		this.valid=0;
	}
	enablesubmit(this.formname)
}

function enablesubmit($formname)
{
	$dataform = $forms_array [$formname];
	var $totalvalid;
	var $total;
	for($key in $dataform)
	{
		if((typeof $dataform[$key ].validation) != "undefined")
		{
			$totalvalid = parseInt($dataform[$key ].validation) + 1;
			$total = parseInt($dataform[$key ].valid) + 1;
		}
	}
	
	if($totalvalid == $total)
	{
		$dataform["submit"].disabled=false;
	}
	else
	{
		$dataform["submit"].disabled=true;
	}
	$dataform["submit"].value="Submit";
}


window.onload =  fieldcheck;