
var xmlHttp;
var foundflag = 0;
var homepageflag = 1;
var downloadflag = 1;
var imageflag = 1;
function validate_email(field,alerttxt)
{
	with (field)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) 
		{
			alert(alerttxt);
			return false;
		}
		else {
			return true;
		     }
	}
}
function validate_form(thisform)
{
	with (thisform)
	{
		if (validate_email(email,"Not a valid e-mail address!")==false)
		  {
			email.focus();
			return false;
		  }
		
		if(authorname.value.length == 0)
		{
			authorname.focus();
			alert("Author/Publisher name is empty");
			return false;
		}
		if(softwarename.value.length == 0)
		{
			softwarename.focus();
			alert("software name is empty");
			return false;
		}
		if(foundflag == 1 & R2.value == "updatestatusnew")
		{
			alert("Software name already present, enter new name");
			return false;
		}
		if(version.value.length == 0)
		{
			version.focus();
			alert("Software version is empty");
			return false;
		}
		if(size.value.length == 0)
		{
			size.focus();
			alert("software size is empty");
			return false;
		}
		if(isNaN(Number(size.value))==true)
		{
			size.focus();
			alert("Enter number is size field");
			return false;			
		}
		if(OS.value.length == 0)
		{
			OS.focus();
			alert(" OS is empty");
			return false;
		}
		if(description.value.length == 0)
		{
			description.focus();
			alert("Description is empty");
			return false;
		}
		if(Price.value.length == 0)
		{
			Price.focus();
			alert("Price is empty");
			return false;
		}
		if(isNaN(Number(Price.value))==true)
		{
			Price.focus();
			alert("Enter Number in Price field");
			return false;			
		}

		if(keywords.value.length == 0)
		{
			keywords.focus();
			alert("keywords field is empty");
			return false;
		}
		if(description.value.length >2000)
		{
			description.focus();
			alert("Full description should be < 2000");
			return false;
		}
		if(shortdescription.value.length >500)
		{
			description.focus();
			alert("Short description should be < 500");
			return false;
		}
		if(homepageurl.value.length == 0)
		{
			homepageurl.focus();
			alert("homepage field is empty");
			return false;
		}
		if(homepageflag == 0)
		{
			homepageurl.focus();
			alert("homepage url is not valid");
			return false;
		}
		if(imageurl.value.length == 0)
		{
			imageurl.focus();
			alert("homepage field is empty");
			return false;
		}
		if(imageflag == 0)
		{
			imageurl.focus();
			alert("image url is not valid");
			return false;
		}
		if(downloadurl.value.length == 0)
		{
			downloadurl.focus();
			alert("download url field is empty");
			return false;
		}
		if(downloadflag == 0)
		{
			downloadurl.focus();
			alert("download url is not valid");
			return false;
		}
		
	}
}


function showHint(str)
{
	if (str.length==0)
	{ 
		document.getElementById("txtHint").innerHTML="";
		return;
	}
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support AJAX HTTP Request");
		return;
	} 
	var url="checksoftwarename.php";
	url=url+"?name="+str;	
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function checkurl(str,type)
{
	if (str.length==0)
	{ 
		document.getElementById("txtHint").innerHTML="";
		return;
	}
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support AJAX HTTP Request");
		return;
	} 
	var url="checkurl.php";
	url=url+"?type="+type;	
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function showSubCategory(str)
{
	/*if (str.length==0)
	{ 
		return;
	}
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support AJAX HTTP Request");
		return;
	} 
	var url="getsubcategorylist.php";
	url=url+"?category="+str;	
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);*/

}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		//document.getElementById("txtHint").innerHTML=
		if(xmlHttp.responseText == "1")
		{
			alert("Software name already present, enter new name. Continue, if you are updating the software");
			foundflag = 1;
		}
		else if(xmlHttp.responseText == "2")
		{
			foundflag = 0
		}
		else if(xmlHttp.responseText == "3")
		{
			alert("HomePage URL not found");
			homepageflag = 0;			
		}
		else if(xmlHttp.responseText == "4")
		{
			homepageflag = 1;			
		}
 		else if(xmlHttp.responseText == "5")
		{
			alert("Download URL not found");
			downloadflag = 0;			
		}
		else if(xmlHttp.responseText == "6")
		{
			downloadflag = 1;
		}
		else if(xmlHttp.responseText == "7")
		{
			alert("Download URL not found");
			imageflag = 0;			
		}
		else if(xmlHttp.responseText == "8")
		{
			imageflag = 1;
		}


	} 
}
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
