var Xmlhttp;

var isIE=false;
if(document.all)
{
	isIE=true;
}

function createXmlHttp()
{
    var moz = (typeof document.implementation != 'undefined') && (typeof 
    document.implementation.createDocument != 'undefined');
    if (moz)
    {
	 Xmlhttp = new XMLHttpRequest();
    }
	else // for Ie
	{
		try
			{
				Xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
		catch(e)
		{
			try
			{
				Xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(oc)
			{
				Xmlhttp = null;
			}
		}
	}  //end else;	
}// end createXmlhttp;

function sendMail()
{
	if(ValidateControls()==true)
	{
		
		var url;
		var intloop;
		
		url="ajaxPage.aspx?From=Simple&EmailId=" + document.getElementById('txtemail').value;
		
		url +="&contactName=" + document.getElementById('txtContactName').value;
		
		
		url +="&comments=" + URLEncode(document.getElementById('txtComments').value);
		createXmlHttp();
		
		
		if(Xmlhttp)
		{
		
			if(document.all)
                  {
                        Xmlhttp.open("POST", url, true);
                        Xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                  }
                  else
                    {Xmlhttp.open("GET", url, true); } 
                    
			
			Xmlhttp.onreadystatechange =function ()
												{
													if(Xmlhttp.readyState==4)
													{
														if(Xmlhttp.status==200)
														{
															if(Xmlhttp.responseText=='sent')
															{
																document.getElementById("pnlconfirmation").style.display='';
																document.getElementById("pnlDetails").style.display='none';
															}
															else
															{
																document.getElementById("lblErrMsg").innerHTML=Xmlhttp.responseText;
															}
														}
														else 
														{
															document.getElementById("lblErrMsg").innerHTML="There was a problem retrieving data from the server.";
														}
													}
												}
			/*
			Xmlhttp.open("POST", url, true);
			Xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			*/
			Xmlhttp.send(null);	
		}
	}
	return false;
}


function URLEncode(strString)
{
	if(isIE==true)
	{
		strString = Replace(strString, "\r\n", "ampltr;" + "BR" + "ampgtr;");
	}
	else
	{
		strString = Replace(strString, "\n", "ampltr;" + "BR" + "ampgtr;");
	}
	strString = Replace(strString, "&", "amp;");
	return strString;
}
	
function Replace(strMessage, ExistingChar, ReplaceChar)
{
	while(strMessage.lastIndexOf(ExistingChar) >= 0)
	{
		strMessage = strMessage.replace(ExistingChar,ReplaceChar);
	}
	return strMessage;
}


function ValidateControls()
     {
		var valid=true;
		var intFirst=0;
		//document.getElementById("lblErrMsg").innerHTML="The following field(s) have invalid values: ";
		
	
		if(Trim(document.getElementById("txtContactName").value) == "")
		{
			if(intFirst==1)
			addComma();					
			//document.getElementById("lblErrMsg").innerHTML =  document.getElementById("lblErrMsg").innerHTML + "Contact Person";
			intFirst=1;
			valid=false
		}
		
		if(Trim(document.getElementById("txtemail").value) == "")
		{
			if(intFirst==1)
			addComma();					
			//document.getElementById("lblErrMsg").innerHTML =  document.getElementById("lblErrMsg").innerHTML + "Email";
			intFirst=1;
			valid=false
		}
		if(Trim(document.getElementById("txtemail").value)!="")
		if(!ValidateEmail(document.getElementById("txtemail").value))
		{
			if(intFirst==1)
			addComma();		
			//document.getElementById("lblErrMsg").innerHTML =  document.getElementById("lblErrMsg").innerHTML + "Email";
			intFirst=1;
			valid=false
		}
       if(intFirst==0)
		{
			document.getElementById("lblErrMsg").innerHTML = "";
		}
		else
		{
			document.getElementById("lblErrMsg").innerHTML="There where errors in your form. Please make sure all the required fields are correct and submit again.";
		}
		
		checkMaxLength('txtComments');
		
		if(document.getElementById("lblErrMsg").innerHTML != "")
		{
			valid=false;
		}
		
		
		if(valid==false)
		{
			window.location="#divErrMsg";
		}
		
		return valid; 	 
     }
     
function Trim(sString) 
{
			
	while (sString.substring(0,1) == ' ')
	{
	sString = sString.substring(1, sString.length)
	}
	while(sString.substring(sString.length-1, sString.length) == ' ')
	{
	sString = sString.substring(0,sString.length-1);
	}
	return sString;
}
			
function ValidateEmail(mail)
{

	var valid=true
	var v1=mail.split('@')
	if(v1.length!=2)
			{
				valid=false
			}
	else
		{ 
			var v2=v1[1].split('.')
			if(v2.length < 2)
				{
					valid=false
				} 
				
			if(v1[0] == "" || v2[0] == "" || v2[1] == "")
				{	
					valid=false
				}
		}
	return valid

}
			

			
function addComma()
{
	document.getElementById("lblErrMsg").innerHTML+=", "
}	
	
function calonkeyInt32(event)
{
	if(isIE==true)
	{
		if (!((window.event.keyCode >= 48) && (window.event.keyCode <= 57))) 
			{
				window.event.keyCode = null;						
			}
	}
	else
	{
		if (!((event.which >= 48) && (event.which <= 57))) 
			{
				event.preventDefault();		
			}
	}
}
function RedirectToHome()
{
	window.opener=self;
	//window.close();
	location.href="../index.htm";
}

function openEmail()
{
	window.location="mailto:outsourcing@winitsoftware.com";
	return false;
}

function checkMaxLength(obj)
{
	if(document.getElementById(obj).value.length>1000)
	{
		document.getElementById("lblErrMsg").innerHTML ="Invalid data entry: You have exceeded 1000 characters for the Decription";
		document.getElementById(obj).value=document.getElementById(obj).value.substring(0,999);
	
	}

}