// Just plug-in the correct values into the variables below to get your splashpage up and going.
//
//	jQuery v1.4 or higher must be called first for this to work.
//
//	jQuery.cookie plugin 1.0 included in this script.
//
//	Make sure that the email input field has the id #form-email of and the zipcode input form has the id #form-zip.
//
//
//	For your form:
//	For the no thank you link, use <a href="javascript:gotopage();">Word</a>
//	Make sure your form has an id to be used in the script and assigned to $formid below. Ex: "<form name="frmSample" id="frmSample">"
//	And finally make sure your submit button is type="submit" as that is what jQuerys live() looks for: <input class="btn" value="SIGN UP" type="submit" />
//
//	$splashdiv - The <div> id of the splash page that will appear over the page. Make sure this is the one that wraps everything.
//
//	$bodydiv - <div> id of the main page wrapper that the overlay will appear on. This is what wrapps the entire page and will become transparent when the overlay is visible.
//
//	$formid - id of the <form> used in the overlay.
//
//	$cookiename - Name of the cookie that is being set to stop the splash page from being displayed again.
//
//	$cookiesettings - Sets advanced settings for the cookie. Expires is number of days. Path on the domain. Actual domain cookie is assigned to. If the cookie is set with SSL (leave false).
//
//	$htmlcontent - This is the actual content of the overlay.
//
//

var $splashdiv = '#cp-splash-container';
var $bodydiv = '#pageWrap';
var $formid = '#frmSample';
var $cookiename = 'cp_sawsplash';
var $cookiesettings = {expires: 730, path: '/', domain: 'ewg.org', secure: false };
var $htmlcontent = '<div id="cp-splash-container"><div id="top"><span class="h1import">Limit Your Exposures To Cell Phone Radiation</span><div class="splash-text"><p>Cell phone companies, aware of heightened consumer concerns about radiation exposure, have created <br><strong>new cell phones for 2010 to minimize emissions.</strong></p><p>Do you have one of the new phones? Is it low radiation? You won\'t be able to find out from their ad campaign or even the labels.</p><p>Use our guide to look up your phone and read our tips about how to use your phone safely.</p></div></div><div id="cp-splash-content"><div class="signupbox"> <p>Welcome! Sign up with us to learn more about cell phones:</p><form style="margin: 0px;" name="frmSample" id="frmSample"><input name="Email" value="Email address here" onfocus="this.value=\'\';" style="padding: 3px; width: 200px;" type="text" id="form-email"/><input name="Zip" value="ZIP code" onfocus="this.value=\'\';" style="padding: 3px;" size="9" id="form-zip" type="text" /><input class="btn" value="SIGN UP" style="border: 1px solid rgb(192, 192, 192); padding: 4px; width: 105px; background-color: rgb(244, 134, 36); color: rgb(255, 255, 255); font-size: 14px; font-weight: bold;" type="submit" /></form></div><img src="/project/2009cellphone/images/icons2010.jpg" width="427" height="148" /><br /><p style="font-size: 0.9em; text-align: right; margin: 50px 20px 0px 0px;"><a href="javascript:gotopage();" class="go2home">Continue to 2010 Cell Phone Radiation Report.</a></p></div></div>';

// jQuery .getparam plugin.
jQuery.fn.extend({
/**
* Returns get parameters.
*
* If the desired param does not exist, null will be returned
*
* To get the document params:
* @example value = $(document).getUrlParam("paramName");
* 
* To get the params of a html-attribut (uses src attribute)
* @example value = $('#imgLink').getUrlParam("paramName");
*/ 
 getUrlParam: function(strParamName){
	  strParamName = escape(unescape(strParamName));
	  
	  var returnVal = new Array();
	  var qString = null;
	  
	  if ($(this).attr("nodeName")=="#document") {
	  	//document-handler
		
		if (window.location.search.search(strParamName) > -1 ){
			
			qString = window.location.search.substr(1,window.location.search.length).split("&");
		}
			
	  } else if ($(this).attr("src")!="undefined") {
	  	
	  	var strHref = $(this).attr("src")
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else if ($(this).attr("href")!="undefined") {
	  	
	  	var strHref = $(this).attr("href")
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else {
	  	return null;
	  }
	  	
	  
	  if (qString==null) return null;
	  
	  
	  for (var i=0;i<qString.length; i++){
			if (escape(unescape(qString[i].split("=")[0])) == strParamName){
				returnVal.push(qString[i].split("=")[1]);
			}
			
	  }
	  
	  
	  if (returnVal.length==0) return null;
	  else if (returnVal.length==1) return returnVal[0];
	  else return returnVal;
	}
});


// jQuery .cookie plugin.
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

// Used to validate the e-mail address used in the form. See next function below to see it in action.
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid email address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid email address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid email address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid email address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid email address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid email address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid email address")
		    return false
		 }
				
	}

//Function for validating the form. Uses above function to make sure the e-mail address is of correct form.
function ValidateForm() {
	var emailID=document.frmSample.Email
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please enter your email address")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	} else {
		submitform();
	}
 }

// Takes away the splash page.
function gotopage() {
	$($splashdiv).fadeOut(500);
	$($bodydiv).css("opacity","1.0");
	$($bodydiv).css("filter","alpha(opacity = 100)");
}

// To stop redirecting afer submiting the form.
function stopEvent(event) {
    event.preventDefault();
    event.stopPropagation();
    if ($.browser.msie) {
        event.originalEvent.keyCode = 0;
        event.originalEvent.cancelBubble = true;
        event.originalEvent.returnValue = false;
    }
}

// This is the actual function that sends the AJAX call, thus submitting the form.
function submitform() {
	var formemail = $('input#form-email').val();;
	var formzip = $('input#form-zip').val();
	if (formzip == "Zip code") {
		formzip = '';	
	}
	var formdata = 'Email=' + formemail + '&Zip=' + formzip;
	$.ajax({
		type: 'GET',
		url: '/project/2009cellphone/2010splash/cellsplash.php',
		data: formdata,
		success:function() {
			alert('Thank you for subscribing!');
			gotopage();
		}
		});
	return false;
}

// Checks to see if the "inlist=Y" parameter is set so that the splash does not show to people already in the page.
$listmember = $(document).getUrlParam("inlist");
if ($listmember == "Y") {
	$.cookie($cookiename, 'Y', $cookiesettings);
}


// Check to make sure browser accepts cookies before actually putting the overlay up. Don't want to annoy those weird people will cookie off and JS on.
splashcookie = $.cookie($cookiename);
if (splashcookie != "Y") {

$.cookie('testcookie', 'Y', {expires: 1, path: '/', domain: 'ewg.org', secure: false });
testcookie = $.cookie('testcookie');

if (testcookie == "Y") {
	
	$(function () {	
		$('body').prepend($htmlcontent);
		$($splashdiv).fadeIn(500);
		$($bodydiv).css("opacity","0.35");
		$($bodydiv).css("filter","alpha(opacity = 35)");
		$.cookie($cookiename, 'Y', $cookiesettings);
	});
	
	$($formid).live('submit', function(e) {
		stopEvent(e);
		ValidateForm();
		return false;
		});
} 
} // End of the if statment no-cookie people.

