/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function doFocus(formName,fieldName){

  var focusControl = document.forms[formName].elements[fieldName];

  if (focusControl.type != "hidden" && !focusControl.disabled && focusControl.style.display != "none") {
     focusControl.focus();
  }
}

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(trim(theForm.url.value) == "")
	{
		alert ("Oops! You forgot to enter the URL..");
		return false;
	}
	result = (theForm.url.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.url.value=postData;
				return false;
			}
		}
		else{
			return false;
		}
	}

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

		if(resultCount > 1 && resultCount <= 3)
		{
			if(confirm("You have entered "+resultCount+"  URLs. Approximate processing time - 50 seconds. Hold on - we'll be right back with fetched results."))
			{
				showPopUp();
				return true;
			}
			else
				return false;
		}
		if(resultCount > 3 && resultCount <= 10 )
		{
			
			if(confirm("You have entered "+resultCount+" URLs. Entering 4 to 10 URLs will shortly send the results to you in an email. Please click OK to proceed!"))
			{
				//showPopUp();
				return true;
			}
			else
				return false;
		}
		if(resultCount > 10 && resultCount <= 25 )
		{
			if(confirm("You have entered "+resultCount+" URLs. Entering 11 to 25 URLs will shortly send the results to you in an email. Please click OK to proceed!"))
			{
				//showPopUp();
				return true;
			}
			else
				return false;
		}
		if(resultCount > 25 && resultCount <= 50 )
		{
			if(confirm("You have entered "+resultCount+" URLs. Entering 26 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)
		{
			alert ("Oops... too many URLs! Please enter less than 50 URLs at a time.");
			return false;
		}
	}
	//showPopUp();
	return true;


}

