/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function submitFrm(theForm)
{
	var v = new RegExp();
	v.compile(/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/ );

	function trim( str )
	{
		str = str.replace(/^\s+/, "");
		for( var i=str.length-1; i>0; --i )
		{
			if( /\S/.test( str[i] ) ) {
				str = str.substring( 0, i+1 );
				break;
			}
		}
		return str;
	}
	String.prototype.trimAll = function()
	{
		return this.replace(/\s+/g,"");
	}
	if(theForm.articleText.value == "" && theForm.articleUrl.value == "")
	{
		alert ("We need one of the following to proceed: Text/Article in the (Copy and Paste the text) Box; OR URL/URLs in the (Enter URL (One URL per line)) Box. Please enter data in one of the two boxes to proceed, thank you!");
		return false;
	}
	if(theForm.articleText.value != "" && theForm.articleUrl.value != "")
	{
		alert ("You can only enter information in one of the two text boxes at a time. Please remove data from either one (one you do not want to process request for) to continue.");
		return false;
	}
	if(theForm.articleText.value == "" && theForm.articleUrl.value != "") {

		result = (theForm.articleUrl.value).split("\n");
		var resultCount = 0;
	var temp2 =new Array();
	var invalidURL = new Array();
	var invalidURLIndex=0;
	for(i=0; i<result.length; i++)
	{

		var temp3=trim(result[i]);

		if(trim(temp3)!="")
		{

			if (!v.test(temp3))
			{

				invalidURL[invalidURLIndex]= temp3;
				temp3 = "";
				//alert(temp3);
				//return false;
				invalidURLIndex++;


			}

		}
		if(temp3 ==""){
		}
		else{
		temp2[resultCount]=temp3.trimAll();
		resultCount++;
		}


	}

	var postData="";
	for(j=0; j<temp2.length; j++){
		postData +=temp2[j]+"\n";

	}

	if(invalidURL != ""){
		var invalid="";
		var srNo=1;
		for(l=0; l<invalidURL.length; l++){
			invalid +=srNo+"."+invalidURL[l]+"\n";
			srNo++;
		}
		if(confirm("Some of the URLs you entered are invalid: \n\n"+invalid+"\n Shall we process the request after excluding them?"))

		{
			if(postData == "") {
				theForm.articleUrl.value=postData;
				return false;
			}
		}
		else{
			return false;
		}
	}

	theForm.articleUrl.value=postData;

		if(theForm.submitID.value == 0 && resultCount > 2)
		{
			if(confirm("You have entered "+resultCount+" URLs. Entering more than 2 URLs requires you to login. Please login and try again."))
			{
				//showPopup();
				return true;
			}
			else
				return false;
		}

		if(theForm.submitID.value == 0 && resultCount <= 5)
		{
			//showPopup();
			return true;
		}
		if(theForm.submitID.value == 1 )
		{

			if(resultCount > 2 && resultCount <= 10 )
			{
				if(confirm("You have entered "+resultCount+" URLs.  Approximate processing time - 5 seconds. Please click OK to proceed!"))
				{
					showPopup();
					return true;
				}
				else
					return false;
			}
			if(resultCount > 10 && resultCount <= 20 )
			{
				if(confirm("You have entered "+resultCount+" URLs.  Approximate processing time - 10 seconds. Please click OK to proceed!"))
				{
					showPopup();
					return true;
				}
				else
					return false;
			}
			if(resultCount > 20 && resultCount <= 50 )
			{
				if(confirm("You have entered "+resultCount+" URLs. Entering 21 to 50 URLs will shortly send the results to you in an email. Please click OK to proceed!"))
				{
					//showPopup();
					return true;
				}
				else
					return false;
			}
			if(resultCount > 50 && resultCount <= 100 )
			{
				if(confirm("You have entered "+resultCount+" URLs. Entering 21 to 50 URLs will shortly send the results to you in an email. Please click OK to proceed!"))
				{
					//showPopup();
					return true;
				}
				else
					return false;
			}
			if(resultCount > 100)
			{
				alert ("Oops... too many URLs! Please enter less than 100 URLs at a time.");
				return false;
			}
		}
	}
	//showPopup();
	return true;
}



