/* 
 * 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;
	}
	if(trim(theForm.primaryKeyword.value) == "")
	{
		alert ("Seems you missed entering the Primary Keyword. Please enter Primary Keyword/s too. ");
		return false;
	}

        key = trim(theForm.primaryKeyword.value).split("\n");
	key1 = trim(theForm.secondaryKeyword.value).split("\n");

        var iChars = "!@#$%^&*()+=[]\\\;,./{}|\":<>?";
	for (var i=0; i<key.length; i++)
	{
		if (iChars.indexOf(theForm.primaryKeyword.value.charAt(i)) != -1) {
		alert ("Primary Keywords will not process special characters. Please remove them before continuing. Thank you!");
		return false;
		}
	}
	if(trim(theForm.secondaryKeyword.value) == "")
	{
		alert ("Seems you missed entering the Secondary Keyword. Please enter Secondary Keyword/s too.");
		return false;
	}
        for (var i=0; i<key.length; i++)
	{
		if (iChars.indexOf(theForm.secondaryKeyword.value.charAt(i)) != -1) {
		alert ("Secondary Keywords will not process special characters. Please remove them before continuing. Thank you!");
		return false;
		}
	}
        if(trim(theForm.clientLink.value) == "")
	{
		alert ("Oops! You forgot to enter the website URL...");
		return false;
	}
        if(!v.test(theForm.clientLink.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;
	}
	if(key.length > 10) {
		alert("Oops... too many keywords! Please enter less than 10 Primary keywords at a time.");
		return false;
	}
	if(key1.length > 10) {
		alert("Oops... too many keywords! Please enter less than 10 Secondary keywords at a time.");
		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 && key.length <= 10 && key1.length <= 10)
	{
		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 && key.length <= 10)
	{
		//showPopup();
		return true;
	}
	if(theForm.submitID.value == 1 )
	{
		if((resultCount > 2 && resultCount <= 5) && (key.length <= 10) && (key1.length <= 10) )
		{
			if(confirm("You have entered "+resultCount+" URLs,  "+key.length+" Primary Keyword and "+key1.length+" Secondary Keyword. Please click OK to proceed!"))
			{
				showPopup();
				return true;
			}
			else
				return false;
		}
		if(resultCount > 6 && resultCount <= 15 && (key.length <= 10) && (key1.length <= 10))
		{
			if(confirm("You have entered "+resultCount+" URLs,  "+key.length+" Primary Keyword and "+key1.length+" Secondary Keyword. Please click OK to proceed!"))
			{
				showPopup();
				return true;
			}
			else
				return false;
		}
		if(resultCount > 15 && resultCount <= 20 && (key.length <= 10) && (key1.length <= 10))
		{
			if(confirm("You have entered "+resultCount+" URLs,  "+key.length+" Primary Keyword and "+key1.length+" Secondary Keyword. Please click OK to proceed!"))
			{
				showPopup();
				return true;
			}
			else
				return false;
		}
		if(resultCount > 20)
		{
			alert ("You have entered "+resultCount+" URLs,  "+key.length+" Primary Keyword and "+key1.length+" Secondary Keyword. Please enter less than 20 URLs at a time.");
			return false;
		}
		
	}
	//showPopup();
	return true;
}



