var companyHttp;
var companyXML;

function getXmlHttpObject()
{
    var _http=null;
    try
    {
      // Firefox, Opera 8.0+, Safari
      _http=new XMLHttpRequest();
    }
    catch (e)
    {
      // Internet Explorer
      try
      {
		 _http=new ActiveXObject("Microsoft.XMLHTTP");
         
      }
      catch (e)
      {
        _http=new ActiveXObject("Msxml2.XMLHTTP");
      }
    }
    if(_http==null){
        alert("Ajax is Not Supported");
    }
    return _http;
}

function sendRequest(form){
	 hideForm()

	var url=form.elements["url"].value;
	var xmlData="<xml>";
	for(i=0;i<form.elements.length;i++){
		xmlData+="<item>"
		xmlData+="<name><![CDATA["+form.elements[i].name+"]]></name>"
												 
		xmlData+="<value><![CDATA[";
								  
		switch (form.elements[i].type)
		{
			case "text":
			case "textarea":
			case "password":
				if (!form.elements[i].disabled)
					xmlData+=form.elements[i].value
				break;
			case "hidden":
//hidden cannot be disabled 
				xmlData+=form.elements[i].value
				break;
			case "checkbox":
			case "radio":
				if (form.elements[i].checked && !form.elements[i].disabled)
					xmlData+=form.elements[i].value
				break;
			case "select-one":
				if (!form.elements[i].disabled)
					xmlData+=form.elements[i].options[form.elements[i].selectedIndex].value 
				break;
			case "select-multiple":
				if (!form.elements[i].disabled)
				{
					for (var j=0; j < form.elements[i].length; j++)
					{
						var optElem=form.elements[i].options[j];
						if (optElem.selected == true)
						{
							xmlData+= optElem.value+","
						}
					}
				}
				break;
		}						  
		xmlData+="]]></value>"
		xmlData+="</item>"
	}
	xmlData+="</xml>"
	

	
	companyHttp=getXmlHttpObject();
	try{	
	
		companyHttp.open("POST",url);
		//companyHttp.open("GET",url+"?search="+searchString+"&pageID="+pageID,true);
		//Send the proper header information along with the request
		companyHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		companyHttp.setRequestHeader("Content-length", xmlData.length);
		companyHttp.setRequestHeader("Connection", "close");
		
		//companyHttp.onreadystatechange=confirmation;
	 }
	 catch(e){
		 alert("HTTP Init error:"+e);
	 }
	 
	// alert("Send");
	if( window.ActiveXObject && /Win/.test(navigator.userAgent) ) { 
	  try{
		companyHttp.send(xmlData);
		}
		catch(e){
			alert("SEND ERROR");
		}
	}else{
	 
		companyHttp.async=true;
		companyHttp.send(xmlData);
		
//		companyHttp.send(document.location.href+"?name=1");
	}
		
	return false;
	
}

function hideForm(){
	document.getElementById("form_div").style.display="none";
	document.getElementById("response_div").style.display="block";
}
function showForm(){
	document.getElementById("form_div").style.display="block";
	document.getElementById("response_div").style.display="none";
}

// form auto validation
$(document).ready(function(){
	$('input[title]').each(function() {
			if($(this).val() === '') {
				$(this).val($(this).attr('title'));	
			}
			$(this).focus(function() {
				if($(this).val() == $(this).attr('title')) {
					$(this).val('').addClass('focused');
				}
			});
			$(this).blur(function() {
				if($(this).attr('title') == "DD"){
					if($(this).val()>31){
						$(this).removeClass('warning');
						$(this).addClass('warning');	
					}else if($(this).val() === ''){
						$(this).removeClass('warning');
						$(this).addClass('warning');	
					}else{
						$(this).removeClass('warning');
						$(this).addClass('ticked');
					}
				}else if($(this).attr('title') == "MM"){
					if($(this).val()>12){
						$(this).removeClass('warning');
						$(this).addClass('warning');	
					}else if($(this).val() === ''){
						$(this).removeClass('warning');
						$(this).addClass('warning');	
					}else{
						$(this).removeClass('warning');
						$(this).addClass('ticked');
					}
				}else if($(this).attr('title') == "YYYY"){
					if($(this).val() === ''){
						$(this).removeClass('warning');
						$(this).addClass('warning');	
					}else{
						$(this).removeClass('warning');
						$(this).addClass('ticked');
					}
				}else{
					if($(this).val() === '' && $(this).attr('required')=="true") {
						$(this).removeClass('focused')
						$(this).addClass('warning');	
					}
					if($(this).val() == $(this).attr('title')) {
						$(this).removeClass('focused')
						$(this).addClass('warning');	
					}
					else if($(this).val() != '') {
						$(this).removeClass('warning');
						$(this).addClass('ticked');
					}
				}
			});
		});
	});

function isValidInteger(num_element,len){
	var num=document.getElementById(num_element).value;
	if(num.length!=len){
		document.getElementById(num_element).className='warning';
		return
	}
	var emailRegEx =/0-9/;
	if(!num.match(emailRegEx)){
		document.getElementById(num_element).className='warning';
		
	}
	
}
function  isValidEmail(email_element,email_confirm_element){
	
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	email= document.getElementById(email_element).value;
	email_confirm= document.getElementById(email_confirm_element).value;
	if(email_confirm!=null && email!=email_confirm){
		document.getElementById(email_element).className='full warning';
		document.getElementById(email_confirm_element).className='full warning';
		return false;
	}
	if(!email.match(emailRegEx)){
		document.getElementById(email_element).className='full warning';
		document.getElementById(email_confirm_element).className='full warning';
		return false;
	}
	return true;
}

