/* 
 * 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(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;
		}
	}
        if(trim(theForm.domain.value) == "")
	{
		alert ("Oops! You forgot to enter the Linked URL...");
		return false;
	}
        if(!v.test(theForm.domain.value))
	{
		alert("We're afraid the Linked URL format entered is invalid! Please re-enter the URL as http://www.domain.com; OR www.domain.com");
		return false;
	}
	theForm.url.value=postData;
	if(theForm.submitID.value == 0 && resultCount > 1)
	{
		if(confirm(" You have entered "+resultCount+" URls. Entering more than 1 URl 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 <= 5 )
		{
			if(confirm("You have entered "+resultCount+" URLs. Entering 2 to 5  URLs will deduct 1 credit from your account. Approximate processing time - 30 seconds. Please click OK to proceed!"))
			{
				showPopup();
				return true;
			}
			else
				return false;
		}
		if(resultCount > 5 && resultCount <= 20 )
		{
			if(confirm(" You have entered "+resultCount+" URLs. Entering 6 to 20 URLs will shortly send the results to you in an email. 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 51 to 100 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;


}

