//Tell A Friend Version 1.0

//CONFIGURATION
//mode: either of allFields or minimumFields
tellAFriendMode = "minimumFields";


//siteRoot: root of your web site, should end with a forward slash
siteRoot = "http://www.otcointernational.com/";


function tell(title){
	var infoUrl = window.location.href;
	var title=title;
	var url = siteRoot+"TellAFriend.asp?infoUrl="+infoUrl+"&title="+title;
	
	var h=460;
	if(tellAFriendMode == "minimumFields") h = 290;
	
	var win = open(url, "mailwin", "width=400, height="+h+", scrollbars=1");
	if (win.focus) win.focus();
}


//all the functions below are used by TellAFriend.asp

function count(){
	document.f1.msgLen.value = document.f1.message.value.length;
}

function validateForm(){
	var form = document.f1;
	var to=form.toEmails.value
	if(to=="" || isEmptyString(to))
		{ alert("Enter email id of the person to whom you want to send mail"); form.toEmails.focus(); return false;}
	
	var toEmails = (to).split(",");	
	for(i=0; i<toEmails.length; i++){
		if(!validateEmail(toEmails[i])) 
			{alert("Email id "+toEmails[i]+" in 'To' emails not valid"); form.toEmails.focus();return false;}
	}
	
	var from=form.fromName.value
	if(from!="" && !validateName(from))
		{ alert("Name not valid\nIt cannot contain numbers"); form.fromName.focus(); return false;}		
	
	if(form.fromEmail){
		var fMail=form.fromEmail.value
		if(fMail!="" && !validateEmail(fMail))
			{ alert("From email id not valid"); form.fromEmail.focus(); return false;}
		
		if(form.message.value.length>1000){ alert("Message length cannot exceed 1000 characters"); form.message.focus(); return false; }		
	}
	
	return true;
}

function isEmptyString(str){ 
	var i
	for(i=0; i<str.length; i++){ 
		if( str.charAt(i) != " " ){break;}
	}
	if(i<str.length) return false;
	return true;
}

function validateEmail(mailId){
	lastAt = mailId.lastIndexOf('@');
	lastDot = mailId.lastIndexOf('.');

	if ( lastAt == -1 || lastDot == -1 ){ return false; }
	if ( lastAt > lastDot ){ return false; }
	if ( mailId.length == 2 ){ return false; }
	if ( hasSubString( mailId, "@.") ){ return false; }
	if ( mailId.charAt(0) == "@" ||  mailId.charAt(0) == "." ||
		 mailId.charAt(mailId.length-1) == "@" ||  mailId.charAt(mailId.length-1) == "."){ return false; }

	//more than one @
	if( lastAt != mailId.indexOf('@') ){return false;}
	if ( hasSubString( mailId, "..") ){ return false; }

	return true;
}

function hasSubString(str, subStr){
	var l1 = str.length
	var l2 = subStr.length
	if ( l1 < l2 ){ return false; }
	for(var i=0; i<(l1-l2+1); i++){
		var temp = str.substr( i, l2 );
		if ( temp == subStr ){ return true; }
	}

	return false;
}

function validateName(str){
	var re = /[0-9]/gi;
	return (! re.test(str));
}


