/* general functions for the main pages on TT website */

//////////Function open window
function open_window(url) {
mywin = window.open(url,"win",'toolbar=yes,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width=800,height=600');
}
/////////////////////////////////////
//Index page functions
//This function is to perform rudimentary validation of email text entered by user
function checkMail(theForm){
	var emailAddress = theForm['email'].value;	//This is the address entered into the form by the user
	var testAddress1 = emailAddress.split('@'); //Make sure there is an '@' sign in the address
	var testAddress2;							//This will make sure there is at least one '.' after the '@' sign 
	var boolTest1, boolTest2, boolTest3;		//Flags that must be set to 'True' for the form to be submitted

	if(emailAddress.length==0){					//Test to make sure that there is at least something typed
		boolTest1=false;
		failedTest();							//failed test sends to second function with popup text
		return false;
	}else{boolTest1=true;}

	if(testAddress1.length!=2){					//Check for '@' sign
		boolTest2=false;
		failedTest();
		return false;
	}else{boolTest2=true;}

	testAddress2=testAddress1[1].split('.');
	
	if(testAddress2.length<2){					//Check for period after '@' sign
		boolTest3=false;
		failedTest();
		return false;
	}else{
		if(testAddress2[1].length<=1){			//Make sure that there are at least 2 characters after the '.'
			boolTest3=false;
			failedTest();
			return false;
		}else{boolTest3=true;}
	}
	
	if((boolTest1) && (boolTest2) && (boolTest3)){//Test the Flags to make sure they are all set to true
		document.forms[0].submit();				//Test succeeds submit form
	}else{
		failedTest();
		return false;
	}
}

function failedTest(){							//function called to alert user of error in email entered
	alert("You must enter an email address in the form of xxxx@xxxx.xxx");
	document.forms[0]["email"].focus();
}
/////end index page functions
///////////////////submit on click
/*added for - 
emailthisitem_other.asp
emailthisitem_default.asp
emailthisitem_attach.asp
partners.asp
tryit.asp
whitepaper.asp
*/
function submit_onclick2(theForm, onValidate) {//emailthisitem.asp
	if (checkEMail(document.getElementById('email_0_1'), 'Email')==false) return false;
		
	if (isFormValid2(theForm)==false){ 
		return false;
	}else{
		if(onValidate.length>0){
		eval(onValidate);}
		
	return true;
	}
}

function checkEMail(ctrl, field) {
  var str = ctrl.value;
  if (typeof(str) == "string" && str.length > 0) {
	    if (str.search(/;/) > 0) {
			var emailList = str.split(";");
			for(z=0; z<emailList.length; z++) {
				var tmp = emailList[z];
				if (!tmp.match(/^[^@]+@[^@]+$/)) {
				    focusAlert(ctrl, 'Please enter a valid e-mail address.');
					return false;
				}
			}
		}
		else {
			if (!str.match(/^[^@]+@[^@]+$/)) {
			    focusAlert(ctrl, 'Please enter a valid e-mail address.');
				return false;
			}
		}
	}
	else
	{
	  focusAlert(ctrl, 'Please enter a valid e-mail address.');
		return false;
	}
	return true;
}

function focusAlert(ctrl, str) {
    ctrl.focus();
	ctrl.select();
	alert(str);
}

///////////////////////////
/*
key
_0 - is the form field empty
_1 - calls (someone else's function) that checks for '@'
_2 - calls (someone else's function) that checks for ','
_5_<string> - this is a gateway condition whatever is held 
				for the value of the object whose name we 
				have called is compared to the string value 
				following 5
				when naming form elements this should always come last

arguments in: 
form - the name of the form - in 99% of cases this is documents.forms[0]
field2Test - not wired yet - this field will be tested to see if it has a
			value - if it does a simple check is performed (tbd)
***********************************************************************************
*/


function isFormValid2(form, field2Test) {
//encoded in the name of form elements that require validation are 
//numbers preceded by '_' underscore characters


var count = form.length;
var theName;
var theArray;
var theItem;
var tempArray;
var loop1 = 0;
var loop2 = 0;
	for(loop1=0;loop1<count;loop1++)
	{
		theArray="";
		theElement = form[loop1];
		theName = theElement.name;
		theArray = theName.split("_");
		//start finding out which form elements require validation
		//		
		if (theArray.length > 1){
			for(loop2=0;loop2<theArray.length;loop2++)
			{
				if(theArray[loop2] == 5){
				var tempNum = loop2 + 1;
					if(theElement.value != (theArray[tempNum]))	{
						alert("The submit method is not valid.\n" + theElement.value + "!=" + theArray[loop2+1]);
						return false;}
				}
				if(theArray[loop2] == 0){
					if(isEmpty(theElement)){
						alert("This is a mandatory part of the form.");
						theElement.focus();
						return false;
					}
				}
				if(theArray[loop2] == 1){
					if (!hasAmpersand(theElement)) {
						alert ('Please provide a valid email address: Missing @ symbol.');
						theElement.focus();
						return false;
					}
				}
				if(theArray[loop2]==2){
					if (hasComma(theElement)) {
						alert ('Please use a semi-colon to separate multiple addresses.');
						theElement.focus();
						return false;
					}
				}
				if(theArray[loop2]==3){
					if (!hasLegitSeparatedAddresses(theElement)) {
						alert ('Please separate multiple addresses with semi-colons.');
						theElement.focus();
						return false;
					}
				}
			}//exit j loop
		}//exit theArray length was greater than 1
	}//exit i loop - each element of the form has been tested
return true;
}
/////////////////////////////////////////////////////////

///////////////////// TTcpHelp
function TTcpHelp(file)//emailthisitem.asp
{ 
if (!file) file = "/TagTeam/Client/Help/emailthisitem.htm";
  window.open(file,"Info","width=400,height=500,scrollbars=yes,dependent=yes"); }
////////////////////////end TTcpHelp



// # 0
function isEmpty(textcontrol) {
    str = textcontrol.value
    for (i = 0; i < str.length; i++)
    {
      chr = str.substring(i, i + 1);
      if (chr != ' ')
      {
        return false;
      }
    }
    return true;
}
//#1
function hasAmpersand(textcontrol) {
	var found = false;
	var str = textcontrol.value;
	for (i = 0; i < str.length; i++) {
		chr = str.substring(i, i + 1);
		if (chr == '@') found = true;
	}
	return found;
}
// #x
function checkRadio(radio)
{
	var checked = false;
	var i;
	for (i = 0; i < radio.length; i++) {
		if (radio[i].checked)
			checked = true; 
	}
	if (!checked) {
		return (false);
	}
	return (true);
}
// #2 in test
function hasComma(textcontrol) {
	var found = false;
	var str = textcontrol.value;
	for (i = 0; i < str.length; i++) {
		chr = str.substring(i, i + 1);
		if (chr == ',') found = true;
	}
	return found;
}
// #3
function hasLegitSeparatedAddresses(textcontrol) {
	var result = false;
	var foundAt = 0;
	var foundSep = 0;
	var str = textcontrol.value;
	for (i = 0; i < str.length; i++) {
		chr = str.substring(i, i + 1);
		if (chr == '@') foundAt++
		if (chr == ';') foundSep++
	}
	if ((foundAt - 1) == foundSep) result = true;
	return result;
}


function viewKPLibrary(name){
 var w =
window.open('http://www.RevBase.com/TagTeam/client/login.asp?db=kinnepro&site=archive&user=demo&pass=demo', name,"toolbar=yes,width=720,height=550,scrollbars=yes,dependent=yes,resizable=yes");
}

function viewLWLibrary(name){
 var w =
window.open('http://www.RevBase.com/TagTeam/client/login.asp?dbid=188&siteid=826899', name,"toolbar=yes,width=720,height=550,scrollbars=yes,dependent=yes,resizable=yes");
}

