/* 
 * 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 > 5)
// 	{
// 		if(confirm("You have entered "+resultCount+" URLs. Entering more than 5 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 > 5 && resultCount <= 25 )
		{
			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 > 25 && resultCount <= 50 )
		{
			if(confirm("You have entered "+resultCount+" URLs. Approximate processing time - 20 seconds. Please click OK to proceed!"))
			{
				showPopup();
				return true;
			}
			else
				return false;
		}

		if(resultCount > 50 && resultCount <= 200 )
		{
			if(confirm("You have entered "+resultCount+" URLs. Entering 51 to 200 URLs will shortly send the results to you in an email. Please click OK to proceed!"))
			{
				//showPopup();
				return true;
			}
			else
				return false;
		}
		if(resultCount > 200 && resultCount <= 500 )
		{
			if(confirm("You have entered "+resultCount+" URLs. Entering 201 to 500 URLs will shortly send the results to you in an email. Please click OK to proceed!"))
			{
				//showPopup();
				return true;
			}
			else
				return false;
		}
		if(resultCount > 500 && resultCount <= 1000 )
		{
			if(confirm("You have entered "+resultCount+" URLs. Entering 501 to 1000 URLs will shortly send the results to you in an email. Please click OK to proceed!"))
			{
				//showPopup();
				return true;
			}
			else
				return false;
		}
		if(resultCount > 1000)
		{
			alert ("Oops... too many URLs! Please enter less than 1000 URLs at a time.");
			return false;
		}
	}
	//showPopup();
	return true;


}



