/*
** Sitewide Functions
*/


/*
** Script Auto-loader, takes array of filenames
*/
var scripts = new Array('testknowledge', 'layerMaker', 'expander', 'utilities', 'tooltip');
var pathToRoot;
function dynLoadScript(scriptArr) {
	// Find the relative path to our javascript includes
	handler = document.getElementsByTagName("head")[0].getElementsByTagName("script");
	for(var x=0; x<handler.length; x++) {
		if(handler[x].getAttribute("src")) {
			if(handler[x].getAttribute("src").match(/functions.js/)) {
				scriptPrefix = handler[x].getAttribute("src").replace(/functions.js/, "");
				pathToRoot = scriptPrefix.replace(/javascript/, "").replace(/\/\//, "/");
				if(pathToRoot == "/") {
					pathToRoot = "";
				}
			}
		}
	}
	for(var x=0; x<scriptArr.length; x++) {
		// safari fails on dynamic loading, brute force
		document.write('<script type="text/javascript" src="'+scriptPrefix+scriptArr[x]+'.js"></script>');
	}
	return false;
}
dynLoadScript(scripts);



/*
** Basic site-wide and site-specific functionality
*/
function initSite() {
	// Loading external file functions first
	initExpander();
	layerMaker.init();
	validateForm.prep();
	initUtilitiesArea();
	initLeaveSite();
	testYourKnowledge.init();
	setTimeout("", 2000);
}




function initUtilitiesArea() {
	sandbox = document.getElementsByTagName("*");
	for(var x=0; x<sandbox.length; x++) {
		if(sandbox[x].className == "emailArea") {
			sandbox[x].getElementsByTagName("a")[0].href = "javascript:void(0);";
			sandbox[x].getElementsByTagName("a")[0].onclick = function() { emailThisPage(); };
		} else if(sandbox[x].className == "printArea") {
			sandbox[x].getElementsByTagName("a")[0].href = "javascript:void(0);";
			sandbox[x].getElementsByTagName("a")[0].onclick = function() { printThisPage(); return false; };
		}
	}
	
	//annice: if page is email to ...
    var nowURL = document.location.href;
    if ( nowURL.indexOf("email_this_page_confirm",0) != -1 ) {return;}
    if ( nowURL.indexOf("email_this_page_form",0) != -1 ) {return;}
    if ( nowURL.indexOf("tell_a_friend_form",0) != -1 ) {return;}
    if ( nowURL.indexOf("tell_a_friend_confirm",0) != -1 ) {return;}
    //annice: end
	
	// Initialise the [Tell a friend] This Page button
	tellFriendArea = findClass("tellFriendBtn", "headerArea");
	if(tellFriendArea != "" && tellFriendArea != null) {
		tellFriendArea.getElementsByTagName("a")[0].href = "javascript:void(0)";
		tellFriendArea.getElementsByTagName("a")[0].onclick = function() { tellFriend(); };
	}
	
	// Initialise the [Tell a Colleague] This Page button
	tellColleagueArea = findClass("tellColleagueBtn", "headerArea");
	if(tellColleagueArea != "" && tellColleagueArea != null) {
		tellColleagueArea.getElementsByTagName("a")[0].href = "javascript:void(0)";
		tellColleagueArea.getElementsByTagName("a")[0].onclick = function() { tellColleague(); };
	}
}


/*
** Leaving the Site functionality
*/
var leavingSiteExcludeSites = Array('thinprep.com');
var leavingSiteOwnSites = Array('thinprep.com', 'fullterm.net', 'cytologystuff.com', 'hologic.com', 'cervicalhealth.com', 'cytyc.com', 'promisetome.com');
var leavingSiteActiveLink;

function initLeaveSite() {
	sandbox = document.getElementsByTagName("a");
	for(var x=0; x<sandbox.length; x++) {
		currentLinkIsExt = 0;
		currentHref = sandbox[x].href;
		if(currentHref.match(/http:\/\//) || currentHref.match(/https:\/\//)) {
			if(leavingSiteExcludeSites) {
				for(var i=0; i<leavingSiteExcludeSites.length; i++) {
					if(currentHref.search(leavingSiteExcludeSites[i]) > -1) {
						currentLinkIsExt += 1;
					}
				}
			}			
			if(currentHref.match("\\b" + currentUrl.domainName() + "\\b")) {
				currentLinkIsExt += 1;
			}
			if(currentLinkIsExt == 0) {
				// current link is definitely external, now check if link goes to another corporate site
				sandbox[x].onclick = function() { layerMaker.activate('Leaving Site A', this); return false; };
				if(leavingSiteOwnSites.length > 0) {
					for(var i=0; i<leavingSiteOwnSites.length; i++) {
						if(currentHref.search(leavingSiteOwnSites[i]) > -1) {
							// alert(currentHref + "__" + leavingSiteOwnSites[i]);
							sandbox[x].onclick = function() {
								layerMaker.activate('Leaving Site B', this); return false;
							};
						}
					}
				}
			}
		}
	}
	return false;
}



// Mozilla and Opera
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", initSite, false);
} else {
	if(window.onload) {
		oldOnloader = window.onload;
		window.onload = function() { oldOnloader(); initSite(); return false; };
	} else {
		window.onload = function() { initSite(); return false; };
	}
}



/* *************************** **
** Core functions for thinprep **
** Created: January 11, 2008   **
** *************************** */

function emailThisPage() { email_this_page(); }
function tellFriend() { email_tell_friend(); }
function tellColleague() {email_tell_Collegue(); }

/*
** add friend input for email this page
*/

var currentExisting=-1;

function addFriendInput(personType) {
	friendLimit = 5;
	if(document.getElementById("extraFriendInputs")) {
		handler = document.getElementById('extraFriendInputs');
	} else {
		handler = document.getElementById('extraFriendInputs2');
	}
	
	if(handler.getElementsByTagName("div").length == 12) {
		if(document.getElementById("addMoreFriendLink")) {
			document.getElementById("addMoreFriendLink").style.display = "none";
		} else if(document.getElementById("addMoreFriendLink2")) {
			document.getElementById("addMoreFriendLink2").style.display = "none";
		}
	}
	
	if(handler.getElementsByTagName("div").length < 16) {
		currentExisting = handler.getElementsByTagName("div").length / 4;
		
		/*
			<p class="popTexter">* Recipient's email</p>
			<input type="text" name="toEmailAddress" value="" class="popTextInputter" />
			<div class="clearBoth"></div>
		*/
		
		fieldFormArea = document.createElement("div");
		
			pTag = document.createElement("p");
			pTag.className = "popTexter";
			pTag.innerHTML += "* Recipient's name";
			fieldFormArea.appendChild(pTag);
			
			inputTag = document.createElement("input");
			inputTag.setAttribute("type", "text");
			inputTag.name = "toName"+currentExisting;
			inputTag.id = "toName"+currentExisting;
			inputTag.className = "popTextInputter";
			fieldFormArea.appendChild(inputTag);
		
		fieldFormAreaB = document.createElement("div");
		fieldFormAreaB.className = "fieldFormArea";
		
			pTagB = document.createElement("p");
			pTagB.className = "popTexter";
			pTagB.innerHTML += "* Recipient's email";
			fieldFormArea.appendChild(pTagB);
			
			inputTagB = document.createElement("input");
			inputTagB.setAttribute("type", "text");
			inputTagB.name = "toEmailAddress"+currentExisting;
			inputTagB.id = "toEmailAddress"+currentExisting;
			inputTagB.className = "popTextInputter";
			fieldFormArea.appendChild(inputTagB);
		
		divClearBoth = document.createElement("div");
		divClearBoth.className = "clearBoth";
		
		divClearBothB = document.createElement("div");
		divClearBothB.className = "clearBoth";
		
		handler.appendChild(fieldFormArea);
		handler.appendChild(divClearBoth);
		handler.appendChild(fieldFormAreaB);
		handler.appendChild(divClearBothB);
	}
}



/*
 * Author: annice
 * Description: Send email functions
 */
function email_this_page() {
	window_open('477','520',utilities_relative_path(document.location.href) + "email_this_page_form.html?linkpage="+document.location.href);
}

function email_tell_friend() {
    window_open('477','615',utilities_relative_path(document.location.href) + "tell_a_friend_form.html?linkpage="+document.location.href);
}

function email_tell_Collegue() {
    window_open('477','610',utilities_relative_path(document.location.href) + "tell_a_colleague.html?linkpage="+document.location.href);
}

function window_open(objW,objH,objURL) {
    var posX = (screen.width - 700)/2;
    var posY = (screen.height - 450)/3;
    var pstyle = "left=" + posX + ",top=" + posY + ",width=" + objW + ",height=" + objH + ",location=no,menubar=no,status=yes,scrollbars=yes,resizeable=yes";
    window.open(objURL,"",pstyle);
}

function utilities_relative_path(nowURL) {
    if ( nowURL.indexOf("/hcp/",0) != -1 ) {
        return nowURL.substr(0,nowURL.indexOf("/hcp/",0)) + "/hcp/utilities/";
    }    
    if ( nowURL.indexOf("/info/",0) != -1 ) {
        return nowURL.substr(0,nowURL.indexOf("/info/",0)) + "/info/utilities/";
    }    
    return "info/utilities/";
}

function validate_Email(theEmail) {
	var chkFormat = /.+@.+\..+/;
	if ( !theEmail.match(chkFormat) )
		return false;

	return true;
}

function gup( name ){
    var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    
    if( results == null )
        return "";
    else
        return results[1];
}

function validate_form_contact() {
     var objFrm = document.contactRepForm;
     var flag = false;
     
    if ( objFrm.howCanWe.value == '' ) {
        flag = true;
        document.getElementById('howCanWeMSG').style.display = "block";
    } else {
		document.getElementById('howCanWeMSG').style.display = "none";
	}
        
    if ( objFrm.title.value == '' ) {
        flag = true;
        document.getElementById('titleMSG').style.display = "block";
    } else {
        document.getElementById('titleMSG').style.display = "none";
	}
        
    if ( objFrm.firstName.value == '' )
    {
        flag = true;
        document.getElementById('firstNameMSG').style.display = "block";
    }
    else
        document.getElementById('firstNameMSG').style.display = "none";
        
    if ( objFrm.lastName.value == '' )
    {
        flag = true;
        document.getElementById('lastNameMSG').style.display = "block";
    }
    else
        document.getElementById('lastNameMSG').style.display = "none";
        
    if ( objFrm.address1.value == '' )
    {
        flag = true;
        document.getElementById('address1MSG').style.display = "block";
    }
    else
        document.getElementById('address1MSG').style.display = "none";
        
    if ( objFrm.city.value == '' )
    {
        flag = true;
        document.getElementById('cityMSG').style.display = "block";
    }
    else
        document.getElementById('cityMSG').style.display = "none";
        
    if ( objFrm.state.value == '' )
    {
        flag = true;
        document.getElementById('stateMSG').style.display = "block";
    }
    else
        document.getElementById('stateMSG').style.display = "none";
        
    if ( objFrm.zipCode.value == '' )
    {
        flag = true;
        document.getElementById('zipCodeMSG').style.display = "block";
    }
    else
        document.getElementById('zipCodeMSG').style.display = "none";
        
    if ( objFrm.emailAddress.value == '' )
    {
        flag = true;
        document.getElementById('fromEmailAddressMSG').style.display = "block";
    }
    else
        document.getElementById('fromEmailAddressMSG').style.display = "none";
        
    if ( objFrm.verifyEmailAddress.value == '' )
    {
        flag = true;
        document.getElementById('verifyEmailAddressMSG').style.display = "block";
    }
    else
        document.getElementById('verifyEmailAddressMSG').style.display = "none";
	if( objFrm.verifyEmailAddress.value != objFrm.emailAddress.value)
	{
		flag = true;
		document.getElementById('verifyEmailAddressMatch').style.display = "block";
	}
	else 
		document.getElementById('verifyEmailAddressMatch').style.display = "none";
		

    if ( flag == true )
        document.getElementById('errrorMsg').style.display = "block";
    else
    {
        document.getElementById('errrorMsg').style.display = "none";
        document.contactRepForm.submit();
    }     
}

function validate_form_hcp_email() {
	objFrm = document.emailThisPageHCPForm;
    
    var flag = false;
	var flagB = 0;

    if ( objFrm.firstName.value == '' )
    {
        flag = true;
		flagB = 1;
        document.getElementById('firstNameMSG').style.display = "block";
    }
    else
        document.getElementById('firstNameMSG').style.display = "none";
    
    if ( objFrm.lastName.value == '' )
    {
        flag = true;
		flagB = 2;
        document.getElementById('lastNameMSG').style.display = "block";
    }
    else
        document.getElementById('lastNameMSG').style.display = "none";
    
    if ( objFrm.fromEmailAddress.value == '' )
    {
        flag = true;
		flagB = 3;
        document.getElementById('fromEmailAddressMSG').style.display = "block";
    }
    else
        document.getElementById('fromEmailAddressMSG').style.display = "none";
    
    if ( objFrm.toName.value == '' )
    {
        flag = true;
		flagB = 4;
        document.getElementById('toNameMSG').style.display = "block";
    }
    else
        document.getElementById('toNameMSG').style.display = "none";
    
    if ( objFrm.toEmailAddress.value == '' )
    {
        flag = true;
		flagB = 5;
        document.getElementById('toEmailAddressMSG').style.display = "block";
    }
    else
        document.getElementById('toEmailAddressMSG').style.display = "none";
        
    if ( validate_Email(objFrm.toEmailAddress.value) == false && objFrm.toEmailAddress.value != ''  )
    {
        flag = true;
		flagB = 7;
        document.getElementById('emailFormatErrorMSG').style.display = "block";
    }
    else
        document.getElementById('emailFormatErrorMSG').style.display = "none";
        
    //2008.02.11 other friends
    if ( currentExisting > -1 )
    {
        for (var i=0; i<= currentExisting; i++)
        {
            if ( document.getElementById('toName'+i).value == '' )
            {
                flag = true;
                document.getElementById('toNameMSG').style.display = "block";
            }
            if ( document.getElementById('toEmailAddress'+i).value == '' )
            {
                flag = true;
                document.getElementById('toEmailAddressMSG').style.display = "block";
            }
            if ( validate_Email(document.getElementById('toEmailAddress'+i).value) == false )
            {
                flag = true;
                document.getElementById('emailFormatErrorMSG').style.display = "block";
            }
        }
    }
        
    if ( flag == true ) {
        document.getElementById('errrorMsg').style.display = "block";
	} else {
        document.getElementById('errrorMsg').style.display = "none";
        //currentExisting+1: currentExisting's index is start from 0
        objFrm.friendCount.value = currentExisting+1;
        
		objFrm.submit();
    }
	
}

function validate_form(frm)
{
    var objFrm;
    if ( frm == "friend" ) { objFrm = document.getElementById("tellaFriendForm"); }
    else if ( frm == "colleague" ) { objFrm = document.getElementById("tellaColleagueForm"); }
    else if ( frm == "hcp" ) { objFrm = document.getElementById("emailThisPageHCPForm"); }
    else {
		if(document.getElementById("emailThisPageForm")) {
			objFrm = document.getElementById("emailThisPageForm");
		} else {
			objFrm = document.emailThisPageForm;
		}
	}
	
    var flag = false;
    if ( objFrm.firstName.value == '' )
    {
        flag = true;
        document.getElementById('firstNameMSG').style.display = "block";
    }
    else
        document.getElementById('firstNameMSG').style.display = "none";
		
    if ( objFrm.lastName.value == '' )
    {
        flag = true;
        document.getElementById('lastNameMSG').style.display = "block";
    }
    else
        document.getElementById('lastNameMSG').style.display = "none";
        
    if ( objFrm.fromEmailAddress.value == '' )
    {
        flag = true;
        document.getElementById('fromEmailAddressMSG').style.display = "block";
    }
    else
        document.getElementById('fromEmailAddressMSG').style.display = "none";
    
    if ( objFrm.toName.value == '' )
    {
        flag = true;
        document.getElementById('toNameMSG').style.display = "block";
    }
    else
        document.getElementById('toNameMSG').style.display = "none";
    
    if ( objFrm.toEmailAddress.value == '' )
    {
        flag = true;
        document.getElementById('toEmailAddressMSG').style.display = "block";
    }
    else
        document.getElementById('toEmailAddressMSG').style.display = "none";
    
    if ( validate_Email(objFrm.fromEmailAddress.value) == false && objFrm.fromEmailAddress.value != ''  )
    {
        flag = true;
        document.getElementById('emailFormatErrorMSG').style.display = "block";
    }
    else
        document.getElementById('emailFormatErrorMSG').style.display = "none";
        
    if ( validate_Email(objFrm.toEmailAddress.value) == false && objFrm.toEmailAddress.value != ''  )
    {
        flag = true;
        document.getElementById('emailFormatErrorMSG').style.display = "block";
    }
    else
        document.getElementById('emailFormatErrorMSG').style.display = "none";
        
    //2008.02.11 other friends
    if ( currentExisting > -1 )
    {
        for (var i=0; i<= currentExisting; i++)
        {
            if ( document.getElementById('toName'+i).value == '' )
            {
                flag = true;
                document.getElementById('toNameMSG').style.display = "block";
            }
            if ( document.getElementById('toEmailAddress'+i).value == '' )
            {
                flag = true;
                document.getElementById('toEmailAddressMSG').style.display = "block";
            }
            if ( validate_Email(document.getElementById('toEmailAddress'+i).value) == false )
            {
                flag = true;
                document.getElementById('emailFormatErrorMSG').style.display = "block";
            }
        }
    }
    //2008.02.11 other friends end
        
    if ( flag == true )
        document.getElementById('errrorMsg').style.display = "block";
    else
    {
        document.getElementById('errrorMsg').style.display = "none";
        
        //currentExisting+1: currentExisting's index is start from 0
        objFrm.toOtherFriendCount.value = currentExisting+1;
                    
        if ( frm == "friend" ) { document.tellaFriendForm.submit(); }
        else if ( frm == "colleague" ) { document.tellaColleagueForm.submit(); }
        else if ( frm == "hcp" ) { objFrm = document.emailThisPageHCPForm.submit(); }
        else { 
			objFrm2 = document.getElementById("emailThisPageForm");
			objFrm2.submit(); 
		}
    }
}
