﻿
// Share div js
function openShare()
{
    var viewWidthAndHeight = getWidthAndHeight()
	var viewScrollXY = getScrollXY()
	var mask = 	document.getElementById('share_shadow');
	var popup = document.getElementById('share_site')
	var select = document.getElementById('reg_user_State');
	
	if (select) {select.style.visibility = "hidden";}
	
	mask.style.display = "block";
	mask.style.height = viewWidthAndHeight[1] + "px";
	mask.style.top = viewScrollXY[1] + "px";
	
	popup.style.display = "block";	
	popup.style.top = viewScrollXY[1] + 50 + "px";
	popup.style.left = (viewWidthAndHeight[0] - 388)/2 + "px";
	
	document.getElementById('step1').style.display = "block";
	document.getElementById('step2').style.display = "none";
    document.getElementById("ctl00_objEmailThisPage_txtRecipientEmail").value = "";
    document.getElementById("ctl00_objEmailThisPage_txtYourEmail").value = "";
    document.getElementById("txtRecipientName").value = "";
    document.getElementById("txtYourName").value = "";
    document.getElementById("l_recipientEmail").className = "norm";
    document.getElementById("l_yourEmail").className = "norm"
    document.getElementById("l_recipientName").className = "norm";
    document.getElementById("l_yourName").className = "norm"
}

function closeShare()
{
    document.getElementById('share_shadow').style.display = "none";
    document.getElementById('share_site').style.display = "none";
    var select = document.getElementById('reg_user_State');
	
	if (select) {select.style.visibility = "visible";}
}

window.onscroll = checkMask;
window.onresize = checkMask;

function checkMask(){
	var mask = 	document.getElementById('share_shadow');
	var popup = document.getElementById('share_site');
	if (mask) {
	    if( mask.style.display == "block") {
		
		    var viewWidthAndHeight = getWidthAndHeight(); // 0 for width 1 for height
		    var viewScrollXY = getScrollXY(); // 0 for X 1 for Y
    		
		    mask.style.top = viewScrollXY[1] + "px";
		    mask.style.height = viewWidthAndHeight[1] + "px";
		}
	}
}

function getWidthAndHeight(){
    var mask = 	document.getElementById('share_shadow');
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
		mask.style.width = myWidth + "px";
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [ myWidth, myHeight ];
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function submitShare()
{
    var bRegistration = validateShare()
    var v_form = document.getElementById("step1");
    
    if (!bRegistration)
    {
        error = true;
        v_form.style.display = "block";
        return false;
     }
     
        //alert("to: " + to.value);
        //alert("from: " + from.value);
    
    if (sendToAjax() == "Y"){
        showConfirmShare();
    }
    else { 
        showConfirmShare();
    };
   
}

/* Ajax check */
var request = null;
	try{		
			request = new XMLHttpRequest();			
			}catch (trymicrosoft) {			
			try{			
				request = new ActiveXObject("Msxml2.XMLHTTP");			
			}catch (othermicrosoft) {			
				try{					
					request = new ActiveXObject("Microsoft.XMLHTTP");					
				}catch (failed) {				
					request = null;					
				}
			}
		} 
		
		if (request == null){
			alert("Error: AJAX interaction failed.");
		}
		var sendSuccess = "default";
/* end ajax check */

function sendToAjax() {
    
    var to = document.getElementById("ctl00_objEmailThisPage_txtRecipientEmail");
    var from = document.getElementById("ctl00_objEmailThisPage_txtYourEmail");
    var url = "/controls/email-ajax.aspx?to="+to.value+"&from="+from.value;
    
    request.open("GET", url, false);
	request.onreadystatechange = test2;				 		
	request.send(null);
    
	var sendSuccess = request.responseText;
	sendSuccess = request.responseText;
	//alert(sendSuccess);
	return sendSuccess;
	
}
function test2() {
		if (request.readyState == 4) {
			//alert("inside test2= " +request.responseText);
			sendSuccess = request.responseText;
			//alert("inside test = "+inOrnotInDb);
		}
		else {
		//alert("else");
		}
	}
function showConfirmShare()
{
    //show thank you
    var v_form = document.getElementById("step1");
    if(v_form != null)
    {
        v_form.style.display = "none";
        document.getElementById("step2").style.display = "block";
    }
}

function validateShare()
    {
        var totalErrors = 0;
        totalErrors += addError(validateEmailAddress(d("ctl00_objEmailThisPage_txtRecipientEmail"), "l_recipientEmail"));
        totalErrors += addError(validateEmailAddress(d("ctl00_objEmailThisPage_txtYourEmail"), "l_yourEmail"));
        totalErrors += addError(validateName(d("txtYourName"), "l_yourName"));
        totalErrors += addError(validateName(d("txtRecipientName"), "l_recipientName"));

        if (totalErrors == 0)
        {
            return true;        // Everything validated
        }
        else
        {
            return false;       // One or more didn't validate
        }
    }

function addError(test)
    {
        if (test) 
            return 0;
        else
            return 1;
    }

function getValue(myField)
	{
		var myValue = '';
		switch(myField.type)
		{
			case "button" :
			case "text" :
			case "textarea" :
			default : 
				myValue = myField.value;
				break;
		}
		return myValue;
	}

function validateEmailAddress(myField, myLabel)
	{
		var myValue = getValue(myField);
		if ( (isValidEmailAddress(myValue)) && 
			 (hasNoSpaces(myValue)) && 
			 (hasNoCrLf(myValue)) &&
			 (maxLength(myValue,50)) )
		{
            toggleError(myLabel, "norm");
			return true;
		} else {
			toggleError(myLabel, "error_state");
			return false;
		}
	}

function validateName(myField, myLabel)
	{
		var myValue = getValue(myField);
		if ( (isNotNull(myValue)) && 
			 (maxLength(myValue,50)) )
		{
            toggleError(myLabel, "norm");
			return true;
		} else {
			toggleError(myLabel, "error_state");
			return false;
		}
	}
	
	function toggleError(myLabel, myClass)
	{
	    if (d(myLabel))
	    {
	        var el = d(myLabel);
	        el.className = myClass;
	    }
	}
	
	function isNotNull(myText)
	{
		if ( (myText.length > 0) && (trim(myText).length > 0) ) {
			return true;
		} else {
			// alert("Not Null!");
			return false;
		}
	}
	function trim(stringToTrim) {
	    return stringToTrim.replace(/^\s+|\s+$/g,"");
    }
	
	function isValidEmailAddress(myText)
	{
		//var regEmail = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
		var regEmail = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
		var regex = new RegExp(regEmail);
		if (!regex.test(myText))
		{
			// alert("E-mail Address Is Not Valid");
			return false;
		}
		return true;
	}
	
	function hasNoSpaces(myText)
	{
		var charpos = myText.search(/\s/); 
		if(myText.length > 0 && charpos >= 0) 
		{		  
			// alert("Space at position " + eval(charpos + 1)); 
			return false; 
		}
		return true;
	}

	function hasNoCrLf(myText)
	{
		var charpos = myText.search(/\r\n/); 
		if(myText.length > 0 && charpos >= 0) 
		{		  
			// alert("Carriage Return or Line Feed at position " + eval(charpos + 1)); 
			return false; 
		}
		return true;
	}
	
	function maxLength(myText,myMaxLength)
	{
		if (myText.length > myMaxLength)
		{
			// alert("More than " + myMaxLength + "!");
			return false;
		} else {
			return true;
		}
	}
	
function d(fieldName)
	{
        var x;
        return (x = document.getElementById(fieldName)) ? x : alert(fieldName + " not found");
    }



/*
 * Clear Default Text: functions for clearing and replacing default text in
 * <input> elements.
 *
 * by Ross Shannon, http://www.yourhtmlsource.com/
 */

addEvent(window, 'load', init, false);

function init() {
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        
        if (theInput.type == 'text' && theInput.className.match(/\bcleardefault\b/)) {  
            /* Add event handlers */          
            addEvent(theInput, 'focus', clearDefaultText, false);
            addEvent(theInput, 'blur', replaceDefaultText, false);
            
            /* Save the current value */
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    }
}

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}

/* 
 * Cross-browser event handling, by Scott Andrew
 */
function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
} //input must be tagged with class="cleardefault"


