/*this is this the class that shows more details*/


function getHTTPObject() 
{
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try 
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) 
	{
		try 
		{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch (E)
		{
			xmlhttp = false;
		}
	}
	
	@else
	xmlhttp = false;
	@end @*/

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try 
		{
			xmlhttp = new XMLHttpRequest();
		} 
		catch (e) 
		{
			xmlhttp = false;
		}
	}

	return xmlhttp;

}

function ajax_valid()
{
	this.form 		= new Array();
	this.unique 		= 0 ;
	this.functionlist 	= new Array();
	
	this.pasreform 			= ajax_valid_paseForm;
	this.createImage 		= ajax_createImage;		// this will create a space for the inserting images
	this.createError		= ajax_createError;		// this will create a space for teh error message
	this.action			= ajax_action;
	this.defaultFunc		= ajax_defaultFunction;
	this.createfunction 		= ajax_createFunction;
	this.validate			= ajax_completeValidation;
	this.editing			= ajax_edititng;
	this.keydown			= ajax_keydown;
	this.outfocus			= ajax_outfocus;
	
	
	// validating function
	this.required			= ajax_required;
	this.checkString		= ajax_checkstring;
	this.checkNumber		= ajax_checkNumber;
	this.checkPhone			= ajax_checkPhone;
	this.checkZip			= ajax_checkZip;
	this.checkEmail			= ajax_checkEmail;
	this.checkDate			= ajax_checkdate;
	this.checkCompare		= ajax_checkCompare;
	this.checkFile			= ajax_checkFile;
	this.checkOnline		= ajax_checkOnline;
	
	
	//calling default function
	this.defaultFunc();

}

// validating function
function ajax_required(value)
{

	if(value.length > 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

/*Check string*/
function ajax_checkstring(value)
{
	if(value.length < 1) { return 0; }
	value = value.replace(" ","");
	if(value.length > 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

/*Check Number*/
function ajax_checkNumber(value)
{
	if(value.length < 1) { return 0; }
	var anum=/(^\d+$)|(^\d+\.\d+$)/

	if(value.match(anum))
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

// this will check the phone for the validity
function ajax_checkPhone(value)
{	
	if(value.length < 1) { return 0; }
	var phonedata = /(^\d{10}\d$)|(^\d{3}-\d{3}-\d\d\d\d+$)|(^\d{3}.\d{3}.\d+$)|(^\d{3} \d{3} \d+$)|(\(\d{3}\) \d{3} \d+$)|(\(\d{3}\) \d{3}-\d+$)/	
	if(value.match(phonedata))
	{
		return 1;
	}
	else
	{
		return 0;
	}
}


// this will validate zip code
function ajax_checkZip(value)
{
	if(value.length < 1) { return 0; }
	var zipdata = /(^\d{4}\d$)|(^([a-zA-Z]{2})-(\d{3})\d+$)|(^([a-zA-Z]{2})-(\d{3})\d+$)/
	if(value.match(zipdata))
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

// this will check for the error message
function ajax_checkEmail(str)
{

	if(str.length < 1)
	{
		return 0;
	}
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){	   return 0;	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){	   return 0;	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){	    return 0;	}
	if (str.indexOf(at,(lat+1))!=-1){	    return 0;	 }
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){	    return 0;	 }
	if (str.indexOf(dot,(lat+2))==-1){   return 0;	 }
	if (str.indexOf(" ")!=-1){    return 0; }

	return 1;					
}


/* check for the valid data */
function ajax_checkdate(value)
{
	if(value.length < 1) { return 0; }
	
	// check th consitnecy of the date
	var datepart = /^(\d{2})-(\d{2})-(\d{4})$/;
	if(value.match(datepart ))
	{
		return 1;
	}
	else
	{
		return 0;
	}
}


/*check for 2 or more instance*/
function ajax_checkCompare(value,$unique,$form_id)
{
	$form = __ajax_valid.form[$form_id]
	
	// get the required refernce value
	$related_field = $form.elements[$unique]["related"];
	
	// get the filed name
	$maindata = $related_field.split(",");
	
	for($__count_1 = 0;$__count_1 < $maindata.length;$__count_1++)
	{
		if($form.reference[$maindata[$__count_1]].value != value)
		{
			return 0;
		} // end of the condition
	}
	return 1;
	
}



// this will check the upload file url
function ajax_checkFile(value,$unique,$form_id)
{
	$form = __ajax_valid.form[$form_id];
	$file_array = value.split(".");
	$file_ext = $file_array[parseInt($file_array.length)  - 1];
	
	// get the required refernce value
	$related_field = $form.elements[$unique]["related"];
	
	$file_extension = $related_field.split(",");
	for($__count_2 = 0;$__count_2 < $file_extension.length;$__count_2++)
	{
		if($file_ext == $file_extension[$__count_2])
		{
			return 1;
		}
	} // end of the for loop
	
	return 0;
	
	
	
} // end of the function


function ajax_checkOnline(value,$unique,$form_id)
{
	$xml = getHTTPObject();
	$form = __ajax_valid.form[$form_id];
	$related_field = $form.elements[$unique]["related"] + value;
	

	$xml.open("POST",$related_field,false);
	$xml.send("<ml></ml>");
	
	return $xml.responseText;
}

function ajax_defaultFunction()
{
	this.createfunction("checkString"," ","","system");
	this.createfunction("checkNumber"," ","","system");
	this.createfunction("checkPhone"," ","","system");
	this.createfunction("checkZip"," ","","system");
	this.createfunction("checkEmail"," ","","system");
	this.createfunction("checkDate"," ","","system");
	this.createfunction("checkCheckbox"," ","","system");
	this.createfunction("checkFile"," ","","system");
	this.createfunction("checkCompare"," ","","system");
	this.createfunction("checkOnline"," ","","system");
	
	this.createfunction("required"," ","","system");
	
}




// editing status chnage
function ajax_edititng()
{

	$x_data = this.x_data;
	$x_form_data = this.x_form_data;
	$currentfiled = __ajax_valid.form[$x_form_data].elements[$x_data];
	
	if((typeof $currentfiled["normal"]) != "undefined")
	{
		document.getElementById("error_" + this.x_data + "_x").innerHTML = $currentfiled["normal"];
	}
	
	if((typeof $currentfiled["rollovercss"]) != "undefined")
	{
		this.className = $currentfiled["rollovercss"];
	}
	
	k = document.createElement("IMG");
	k.setAttribute("id","images_" + this.x_data + "_x");
	k.setAttribute("src", __configure.editingicon);
	
	document.getElementById("images_" + this.x_data + "_x").parentNode.replaceChild(k,document.getElementById("images_" + this.x_data + "_x"));
}


function ajax_outfocus()
{
	$x_data = this.x_data;
	$x_form_data = this.x_form_data;
	$currentfiled = __ajax_valid.form[$x_form_data].elements[$x_data];
	
	if((typeof $currentfiled["normal"]) != "undefined")
	{
		document.getElementById("error_" + this.x_data + "_x").innerHTML = $currentfiled["normal"];
	}
	
	if((typeof $currentfiled["normalcss"]) != "undefined")
	{
		this.className = $currentfiled["normalcss"];
	}
	
	__ajax_valid.keydown(this,1);
}

// end of the function
function ajax_keydown($elements,$enddata)
{

	try
	{
		if((typeof $elements.x_data) == "undefined")
		{
			$elements = this;
		}
	}
	catch(e)
	{
		$elements = this;
	}
	// getting the form details
	$x_data = $elements.x_data;
	$x_form_data = $elements.x_form_data;
	
	$__commonvalue = 0;
	$__totalcount  = 0;
	$__s_message  = "";
	$__e_message  = "";
	$currentfiled = __ajax_valid.form[$x_form_data].elements[$x_data];
	
	for($__k in $currentfiled["function"])
	{
		// excute the work dynamically
		if(__ajax_valid.functionlist[$__k]["type"] == "system" && $currentfiled["function"][$__k] ==  1)
		{
			if($__k == "checkOnline" && $enddata == 1)
			{
				eval("$output=__ajax_valid." + $__k + "($elements.value,$x_data,$x_form_data)");
			}
			else
			{
				if($__k != "checkOnline")
				{
					if($elements.type=="checkbox")
					{
						if($elements.checked)
						{
							eval("$output=__ajax_valid." + $__k + "('yes',$x_data,$x_form_data)");
						}
						else
						{
							eval("$output=__ajax_valid." + $__k + "('',$x_data,$x_form_data)");
						}
					}
					else
					{
						eval("$output=__ajax_valid." + $__k + "($elements.value,$x_data,$x_form_data)");
					}
				}
				else
				{
					$output = 0;
				}
			}
			$__commonvalue = $__commonvalue  + parseInt($output);
			$__e_message = __ajax_valid.functionlist[$__k]["error"];
			$__s_message = __ajax_valid.functionlist[$__k]["success"];
			$__totalcount++;
		} 
		
	}
	
	if((typeof $currentfiled["success"]) != "undefined")
	{
		$__s_message = $currentfiled["success"];
	}

	if((typeof $currentfiled["error"]) != "undefined")
	{
		$__e_message = $currentfiled["error"];
	}
	
	if($__totalcount > 0)
	{
		if($__commonvalue == $__totalcount )
		{
			if(__configure.completeicon != document.getElementById("images_" + $elements.x_data + "_x").src)
			{
				k = document.createElement("IMG");
				k.setAttribute("id","images_" + $elements.x_data + "_x");
				k.setAttribute("src", __configure.completeicon);
				
				if($currentfiled["function"]["required"] == 1)
				{
					if($elements.getAttribute("recorded") == null || $elements.getAttribute("recorded") == 0)
					{
						__ajax_valid.form[$x_form_data].current = __ajax_valid.form[$x_form_data].current + 1;
						$elements.setAttribute("recorded",1);
					}
				}

				document.getElementById("images_" + $elements.x_data + "_x").parentNode.replaceChild(k,document.getElementById("images_" + $elements.x_data + "_x"));	
				document.getElementById("error_" + $elements.x_data + "_x").innerHTML = $__s_message;
			}
		}
		else
		{
			if(__configure.erroricon != document.getElementById("images_" + $elements.x_data + "_x").src)
			{
				if($elements.getAttribute("recorded") == 1)
				{
					__ajax_valid.form[$x_form_data].current = __ajax_valid.form[$x_form_data].current - 1;
					$elements.setAttribute("recorded",0);
				}
				
				if($elements.value.length > 0)
				{
					k = document.createElement("IMG");
					k.setAttribute("id","images_" + $elements.x_data + "_x");
					if($currentfiled["function"]["required"] == 0)
					{
						k.setAttribute("src", __configure.warningicon);
					}
					else
					{
						k.setAttribute("src", __configure.erroricon);
					}

					document.getElementById("images_" + $elements.x_data + "_x").parentNode.replaceChild(k,document.getElementById("images_" + $elements.x_data + "_x"));	
					document.getElementById("error_" + $elements.x_data + "_x").innerHTML = $__e_message;
				}
				else
				{
					k = document.createElement("IMG");
					k.setAttribute("id","images_" + $elements.x_data + "_x");
					if($currentfiled["function"]["required"] == 0)
					{
						k.setAttribute("src", __configure.blankicon);
					}
					else
					{
						k.setAttribute("src", __configure.requiredicon);
					}	
					document.getElementById("images_" + $elements.x_data + "_x").parentNode.replaceChild(k,document.getElementById("images_" + $elements.x_data + "_x"));	
					document.getElementById("error_" + $elements.x_data + "_x").innerHTML = "";

				}
			}	
		}
	}
	else
	{

		k = document.createElement("IMG");
		k.setAttribute("id","images_" + $elements.x_data + "_x");
		k.setAttribute("src", __configure.blankicon);
		document.getElementById("images_" + $elements.x_data + "_x").parentNode.replaceChild(k,document.getElementById("images_" + $elements.x_data + "_x"));	
		document.getElementById("error_" + $elements.x_data + "_x").innerHTML = "";

	}
	
	if(__ajax_valid.form[$x_form_data].current == __ajax_valid.form[$x_form_data].totalrequired)
	{
		if(__ajax_valid.form[$x_form_data].required == 1 && __ajax_valid.form[$x_form_data].enabled == 0)
		{
			for(k in  __ajax_valid.form[$x_form_data]["elements_submit"])
			{
				__ajax_valid.form[$x_form_data]["elements_submit"][k].disabled = false;
			}
		}
		__ajax_valid.form[$x_form_data].enabled = 1;
	}
	else
	{
		if(__ajax_valid.form[$x_form_data].enabled == 1)
		{
			if(__ajax_valid.form[$x_form_data].required == 1)
			{
				for(k in  __ajax_valid.form[$x_form_data]["elements_submit"])
				{
					__ajax_valid.form[$x_form_data]["elements_submit"][k].disabled = true;
				}
			}
			__ajax_valid.form[$x_form_data].enabled = 0;
		}
	}
	
} // end of the function



// complete validation
function ajax_completeValidation($formid)
{
	if(__ajax_valid.form[$formid].current == __ajax_valid.form[$formid].totalrequired)
	{
		return true;
	}
	else
	{
		return false;
	}
}


// creating a function
function ajax_createFunction($functionname,$successmessage,$errormessage,$type)
{
	this.functionlist[$functionname] = new Array();
	this.functionlist[$functionname]["success"] = $successmessage;
	this.functionlist[$functionname]["error"] = $errormessage;
	this.functionlist[$functionname]["type"] = $type;
}


/* makeform*/
function formdata()
{
	this.name		= "";			// get the name of teh form
	this.elements		= new Array();		// create the element array
	this.add		= formdata_add;
	this.elements_submit	= new Array();
	this.totalrequired	= 0;
	this.current		= 0;
	this.enabled            = 0;
	this.reference          = new Array();
}


function formdata_add()
{
} // end of the function


function ajax_valid_paseForm()
{
	// this function is pasre the form and make the necessary validation 
	// to the page

	
	$__main = document.getElementsByTagName("form");
	for($__count = 0;$__count < $__main.length;$__count++)
	{
		
		$__main[$__count].x_count = $__count;
		$__main[$__count].onsubmit = function() { return __ajax_valid.validate(this.x_count) };
		this.form[$__count] = new formdata();				// new form data
		if(typeof($__main[$__count].name) != "undefined")
		{
			this.form[$__count].name =  $__main[$__count].name;
		}
		else
		{
			this.form[$__count].name = "form" + $__count;
		}
		
		if((typeof $__main[$__count].name) != "undefined")
		{
			this.form[$__count].name = $__main[$__count].name;
		}
		
		$input 		= $__main[$__count].getElementsByTagName("INPUT");
		$select 	= $__main[$__count].getElementsByTagName("SELECT");
		$textarea 	= $__main[$__count].getElementsByTagName("TEXTAREA");
		
		
		for($__count1 = 0;$__count1 < $input.length;$__count1++)
		{
			this.unique++;
			// assigning to the details			

			this.form[$__count].elements[this.unique] = new Array();
			this.form[$__count].elements[this.unique]["name"] = $input[$__count1].name;
			this.form[$__count].elements[this.unique]["id"] = $input[$__count1].id;
			this.form[$__count].elements[this.unique]["type"] = $input[$__count1].type;

				
			this.form[$__count].reference[$input[$__count1].name] = $input[$__count1];

			$input[$__count1].x_data = this.unique;
			$input[$__count1].x_form_data = $__count;
			
			// getting the required property
			if($input[$__count1].type == "submit"  )
			{

				this.form[$__count]["elements_submit"][$input[$__count1].name] = $input[$__count1];
			}
				
			if($input[$__count1].type == "text" || $input[$__count1].type == "password" || $input[$__count1].type == "file")
			{
				if($input[$__count1].className != "")
				{
					var anum=/check|\[|\}|required/
					if($input[$__count1].className.match(anum) != null)
					{
						this.action($input[$__count1],this.unique,$__count,"text");
						this.createImage($input[$__count1],$__count);
						this.createError($input[$__count1]);
						this.keydown($input[$__count1]);
					}
				}
			}
			else if($input[$__count1].type == "checkbox")
			{
				if($input[$__count1].className != "")
				{
					var anum=/check|\[|\}|required/
					if($input[$__count1].className.match(anum) != null)
					{
						this.action($input[$__count1],this.unique,$__count,"checkbox");
						this.createImage($input[$__count1],$__count);
						this.createError($input[$__count1]);
						this.keydown($input[$__count1]);
					}
				}				
			}
			else if($input[$__count1].type == "radio")
			{
			}			
			
		}
		
		$select 	= $__main[$__count].getElementsByTagName("SELECT");
		//now the funcationalty for the combobox
		for($__count1 = 0;$__count1 < $select.length;$__count1++)
		{
			this.unique++;
			this.form[$__count].elements[this.unique] = new Array();
			this.form[$__count].elements[this.unique]["name"] = $select[$__count1].name;
			this.form[$__count].elements[this.unique]["id"] = $select[$__count1].id;
			this.form[$__count].elements[this.unique]["type"] = $select[$__count1].type;
			
			this.form[$__count].reference[$select[$__count1].name] = $select[$__count1];

			$select[$__count1].x_data = this.unique;
			$select[$__count1].x_form_data = $__count;
			if($select[$__count1].className != "")
			{
				var anum=/check|\[|\}|required/
				if($select[$__count1].className.match(anum) != null)
				{			
					this.action($select[$__count1],this.unique,$__count,"select");
					this.createImage($select[$__count1],$__count);
					this.createError($select[$__count1]);
					this.keydown($select[$__count1]);
				}
			}

		}
		
		if(this.form[$__count].required == 1)
		{
			for(k in  this.form[$__count]["elements_submit"])
			{
				this.form[$__count]["elements_submit"][k].disabled = true;
			}
		}
	}
	
	
}

/**/
function ajax_action($element,$unique,$form,$type_data)
{
	$function_details = "";
	$element.setAttribute("unique",$unique);
	$element.setAttribute("formdata",$form);
	$function_details = $element.className;
	
	
	this.form[$form].elements[$unique]["function"] = new Array();
	this.form[$form].elements[$unique]["function"]["required"] = 0;
		
	if((typeof $function_details) == "string" && $function_details != "")
	{
		
		// this will check for the error message
		$x_data = $function_details.split("{");
		if($x_data.length > 1)
		{
			// we got the error message and success message
			$x_data1 = $x_data[1].split("}");
			$mainvalue = $x_data1[0].split("/");
			

			this.form[$form].elements[$unique]["normal"] = $mainvalue[0];
			this.form[$form].elements[$unique]["success"] = $mainvalue[1];
			this.form[$form].elements[$unique]["error"] = $mainvalue[2];
		}
		
		// css changes
		$x_data = $function_details.split("[");
		if($x_data.length > 1)
		{
			$x_data1 = $x_data[1].split("]");
			$mainvalue = $x_data1[0].split("/");
			
			this.form[$form].elements[$unique]["normalcss"] = $mainvalue[0];
			this.form[$form].elements[$unique]["rollovercss"] = $mainvalue[1];
			
			$element.className = this.form[$form].elements[$unique]["normalcss"];
		}
		

		// related thing
		$x_data = $function_details.split("(");
		if($x_data.length > 1)
		{
			$x_data1 = $x_data[1].split(")");
			
			this.form[$form].elements[$unique]["related"] = $x_data1[0];
		}
		
		// replace all teh error message and the css exitsance
		reg =  /{(.*)}/
		$function_details = $function_details.replace(reg,"");
		
		reg =  /\[(.*)\]/
		$function_details = $function_details.replace(reg,"");

		
		reg =  /\((.*)\)/
		$function_details = $function_details.replace(reg,"");	
		
		// check for function details
		$x_function = $function_details.split(" ");
		

		for($count_l =0;$count_l < $x_function.length;$count_l++)
		{
			$x_function[$count_l] = $x_function[$count_l].replace(" ","");
			if($x_function[$count_l] != "")
			{
				this.form[$form].elements[$unique]["function"][$x_function[$count_l]] = 1;
				
				if($x_function[$count_l] == "required")
				{
					this.form[$form].required=1;
					this.form[$form].totalrequired = this.form[$form].totalrequired + this.form[$form].required;
				} // end of the if conditon
			}
		}
		
	
		if($type_data == "text")
		{
			$element.onfocus 	= __ajax_valid.editing;
			$element.onkeyup 	= __ajax_valid.keydown;
			$element.onblur 	= __ajax_valid.outfocus;
		}
		else if($type_data == "select")
		{
			$element.onfocus 	= __ajax_valid.editing;
			$element.onchange 	= __ajax_valid.keydown;
			$element.onblur 	= __ajax_valid.outfocus;		
		}
		else if($type_data == "checkbox")
		{
			$element.onfocus 	= __ajax_valid.editing;
			$element.onchange 	= __ajax_valid.keydown;
			$element.onblur 	= __ajax_valid.outfocus;		
		}
		
	}
	

}


// this will create the image and error tag
function ajax_createImage($imagedata,$form)
{



	k = document.createElement("IMG");
	if(this.form[$form].elements[this.unique]["function"]["required"] == 1)
	{
		k.setAttribute("src",__configure.requiredicon);
	}
	else
	{
		k.setAttribute("src",__configure.blankicon);
	}
	k.setAttribute("id","images_" + this.unique + "_x");	

	if(__configure.showimage == "left")
	{
		$imagedata.parentNode.insertBefore(k,$imagedata);
	}
	else
	{
		$imagedata.parentNode.insertBefore(k,$imagedata.nextSibling);
	}
	
}

// this will create error
function ajax_createError($imagedata)
{
	k = document.createElement("SPAN");
	k.setAttribute("class",__configure.errorCSS);
	k.setAttribute("id","error_" + this.unique + "_x");	
	k.innerHTML = "";

	if(__configure.showimage == "left")
	{
		$imagedata.parentNode.insertBefore(k,$imagedata);
	}
	else
	{
		$imagedata.parentNode.insertBefore(k,$imagedata.nextSibling);
	}
}

function configuration()
{
	this.requiredicon   = "icons2/required.gif";
	this.editingicon    = "icons2/editing.gif";
	this.erroricon      = "icons2/required_invalid.gif";
	this.warningicon    = "icons2/invalid.gif";
	this.completeicon    = "icons2/validated.gif";
	this.blankicon       = "icons2/blank.gif";
	
	// show image
	this.showimage	    = "right";
	
	// decoration
	this.errorCSS	    = "error";
	this.successCSS	    = "success";
	
	// below code will help use to load teh images previous 
	$img = new Image();
	$img.src = this.requiredicon;
	
	$img1 = new Image();
	$img1.src = this.editingicon;
		
	$img2 = new Image();
	$img2.src = this.erroricon;	
	
	$img3 = new Image();
	$img3.src = this.warningicon;	
	
	$img4 = new Image();
	$img4.src = this.completeicon;	
	
	$img5 = new Image();
	$img5.src = this.blankicon;		
}

__configure = new configuration();

__ajax_valid = new ajax_valid();
window.onload = function ()
{
	__ajax_valid.pasreform();		// this will parse the form
}


