/* 
 * 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.keyword.value) == "")
	{
		alert ("Oops... no keyword entered! Please re-enter and try again.");
		return false;
	}
	
        key = (theForm.keyword.value).split("\n");

	
	var iChars = "!@#$%^&*()+=[]\\\;,./{}|\":<>?";
	for (var i=0; i<key.length; i++)
	{
		if (iChars.indexOf(theForm.keyword.value.charAt(i)) != -1) {
		alert ("Sorry! We can't process the request with special characters included. Please remove them and continue.");
		return false;
		}
	}

        if(trim(theForm.website.value) == "")
	{
		alert ("Oops! You forgot to enter the website URL.");
		return false;
	}
        if(!v.test(theForm.website.value))
	{
		alert("We're afraid the URL format entered is invalid! Please re-enter the URL as http://www.domain.com; OR www.domain.com.");
		return false;
	}

	var resultCount = 0;
	var temp2 =new Array();
	var invalidURL = new Array();
	var invalidURLIndex=0;
	for(i=0; i<key.length; i++)
	{
		var temp3=trim(key[i]);

		if(trim(temp3)!="")
		{
			resultCount++;
		}
	}

	if(theForm.submitID.value == 0 && resultCount > 1)
	{

		if(confirm("You have entered "+resultCount+" Keywords. Entering more than 1 keywords 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+" Keywords. Approximate processing time - 20 seconds. Hold on - we'll be right back with fetched results."))
			{
				showPopup();
				return true;
			}
			else
				return false;
		}
		if(resultCount > 5 && resultCount <= 15 )
		{
			if(confirm("You have entered "+resultCount+" Keywords. Entering 6 to 15 Keywords will shortly send the results to you in an email. Please click OK to proceed!"))
			{
				//showPopup();
				return true;
			}
			else
				return false;
		}
		
		if(resultCount > 15 && resultCount <= 30 )
		{
			if(confirm("You have entered "+resultCount+" Keywords. Entering 16 to 30 Keywords will shortly send the results to you in an email. Please click OK to proceed!"))
			{
				//showPopup();
				return true;
			}
			else
				return false;
		}
		if(resultCount > 30 && resultCount <= 50 )
		{
			if(confirm("You have entered "+resultCount+" Keywords. Entering 30 to 50 Keywords 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+" Keywords. Entering 50 to 100 Keywords 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 keywords! Please enter less than 100 Keywords at a time.");
			return false;
		}
	}
	//showPopup();
	return true;
}

