/* GLOBAL JS VARIABLES */

var validEmail;		//set by form submit functions with multiple email address fields

/* GLOBALLY USED FUNCTIONS */

//Removes unwanted DoubleClick spacing
function rmDCSpace(){
	if(document.iframe != "undefined"){
		var elements = document.getElementsByTagName("iframe");
		var pattern = /doubleclick/i;
		for(var i=0; i < elements.length; i++){
			if(elements[i].getAttribute("src").match(pattern))
				elements[i].style.height = 0 + "px";
		}
	}
}

function layerControl(layerID,prop) {
	var obj = document.getElementById(layerID);
	obj.style.display = prop;
}

function hoverThis(imgID,filename) {
	document[imgID].src="/images/" + filename;
}

function numOnly(key){
	var unicode = key.charCode ? key.charCode : key.keyCode

	//if the key isn't the backspace key (which we should allow)
	if (unicode!=8){
		//if not a number
		if (unicode<48||unicode>57)
			return false; //disable key press
	}
	return true;
}

var __pcl = 0;
function popupCleanLink(link) {
	if (link == undefined || link.href == undefined) return;
	if (link.__old_href__ == undefined) {
		link.__old_href__ = link.href;
		link.href = "#";

		// This can be turned off if desired, but it helps remove visual jumps within the page
		// Generate a unique ID for the element
		// Use that ID in the resulting hash mark
		// link.href += "ID[" + (++__pcl) + "]";
	}
	return link.__old_href__;
}

function windowSizedOpener(url , w , h ) {
	if(navigator.appName == "Microsoft Internet Explorer"){
		window.open (url,"", "scrollbars=0,width=" + w + ",height=" + h );
	}
	else{
		window.open (url,url,"location=0,status=0,scrollbars=0,width=" + w + ",height=" + h);
	}
}

function openPage(url, width, height){
		window.open(url,"","width="+width+",height="+height+",left=200,top=100");
}

function openWin(filename,w,h,link) {
	popupCleanLink(link);
	window.open(filename,null,"width="+w+",height="+h+",top=10,left=30,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=no");
}

function openWinNew(filename,w,h,link) {
	popupCleanLink(link);
	window.open(filename,"_blank","width="+w+",height="+h+",top=10,left=30,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes");
}

function openWinScroll(filename,w,h,link) {
	popupCleanLink(link);
	window.open(filename,"_blank","width="+w+",height="+h+",top=10,left=30,status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=yes");
}

function openWinNormal(filename,w,h,link) {
	popupCleanLink(link);
	window.open(filename,null,"width="+w+",height="+h+",top=10,left=30,status=yes,toolbar=yes,menubar=yes,location=yes,resizable=yes,scrollbars=yes");
}

function selectAll(thisObj, checkboxObj) {

	var i = 0;
	if (thisObj.checked == true) {
		for (i=0; i < checkboxObj.length; i++){
			toggleCheck(checkboxObj[i],false);
		}
	}
	else {
		for (i=0; i < checkboxObj.length; i++){
			toggleCheck(checkboxObj[i],true);
		}
	}
}

function toggleCheck(checkboxObj, state) {
	checkboxObj.checked = state;
}

function autoPopulate(getField, postField) {

	var defaultUserName = "";
	var userName = document.getElementById(getField).value;

	if ((userName != "") && (userName.length > 0))
		document.getElementById(postField).innerHTML = userName;
	else
		document.getElementById(postField).innerHTML = defaultUserName;
}

function submit_tellfriend() {

	//Reset variables to default state
	validEmail = true;
	var emailaddress = "";
	var emails = "";

	//Hide error messages}
	document.getElementById('personalErrorMsg').style.display = "none";
	document.getElementById('tafErrorMsg').style.display = "none";
	
	emailAddress = document.getElementById('email').value;
	
	if (document.getElementById('REF_FNAME').value == "") {
		validEmail = false;
		document.getElementById('personalErrorMsg').style.display = "block";
	}
	if (emailAddress == null || emailAddress == "" || !(validateEAddress(emailAddress,1,1,'personal'))){
		validEmail = false;
		document.getElementById('personalErrorMsg').style.display = "block";
	}
	
	emailAddress = document.getElementById('FRIEND_EMAIL1').value;
	if (document.getElementById('FRIEND_FNAME1').value == "") {
		validEmail = false;
		document.getElementById('tafErrorMsg').style.display = "block";
	}
	if (emailAddress == null || emailAddress == "" || !(validateEAddress(emailAddress,1,1,'taf'))){
		validEmail = false;
		document.getElementById('tafErrorMsg').style.display = "block";
	}
			
	if ((validateFriends())&(validEmail)) {
		document.tafForm.submit();
		document.getElementById('tafFormContent').style.display = "none";
		loadList();
		document.getElementById('tafFormThankYou').style.display = "block";
		//document.getElementById('sendRemindToEmail').innerHTML = emails;

		//Throw Omniture
		var s=s_gi(s_account);s.linkTrackVars='prop1,events,prop16,eVar16,prop17,eVar17,prop20,eVar20';s.linkTrackEvents='event6';s.prop1='click>tafsend';s.prop16='08goteaser';s.eVar16='08goteaser';s.prop17='08goteaser_tafsend';s.eVar17='08goteaser_tafsend';s.prop20='tafsend';s.eVar20='tafsend';s.events='event6';s.tl(this,'o','click>tafsend');
		
		//Throw DoubleClick
		var axel = Math.random()+"";
		var a = axel * 10000000000000;
		var img = new Image();
		img.src = 'http://ad.doubleclick.net/activity;src=189445;type=2008g757;cat=2008g059;ord=1;num='+ a;
	}

	return;
}

function loadList(){
	next = true;
	var i=1;
	var ul = document.getElementById("list");
	while ((next)&(i <= 5))
	{	
		email = "FRIEND_EMAIL"+i;
		fname = "FRIEND_FNAME"+i;
		
		emailAddress = document.getElementById(email).value;
		fname = document.getElementById(fname).value;
		
		if(validateFriendlist(fname, emailAddress)){
			var li = document.createElement("LI");
			li.appendChild(document.createTextNode(emailAddress));
			ul.appendChild(li);
		}
		else{
			next = false;
		}
		i++
	}
	return next;
}

function validateFriends(){
	next = true;
	out = false;
	var i=2;
	var friendValidation=0;
	while ((next)&(i <= 5))
	{	
		email = "FRIEND_EMAIL"+i;
		fname = "FRIEND_FNAME"+i;
		
		emailAddress = document.getElementById(email).value;
		fname = document.getElementById(fname).value;

		friendValidation = validateFriend(fname, emailAddress);
		if(friendValidation==0){
			if(!(validateEAddress(emailAddress,1,1,'taf'))){
				next = false;
			}
		}
		if(friendValidation==1){
			next = true;
		}
		if(friendValidation==2){
			document.getElementById('tafErrorMsg').style.display = "block";
			next = false;
		}
		i++
	}
	return next;
}


function validateFriendlist(fname, email){
	valid = true;
	if(fname=="" && email == ""){
		valid = false;
	}
	return valid;
}


function validateFriend(fname, email){
	valid = 0;
	if(fname=="" || email == ""){
		valid = 2;
	}
	if(fname=="" && email == ""){
		valid = 1;
	}
	return valid;
}

function submit_remindme() {

	var emailAddress = document.getElementById('email').value;
	var anyChecked = false;

	//Reset error messages
	document.getElementById('remindReqdMsg').style.display = "none";
	document.getElementById('remindErrorMsg').style.display = "none";
	document.getElementById('remindPkgMsg').style.display = "none";

	//If no packages selected show error message
	for (i=0; i<(document.remindForm.TA_DEST.length); i++) {
		if (document.remindForm.TA_DEST[i].checked == true){
			anyChecked = true;
		}
	}

	if (anyChecked == false) {
		document.getElementById('remindPkgMsg').style.display = "block";
	}

/************************************************************************************/

	//If email address field is blank show error
	if (emailAddress == "") {
		document.getElementById('remindReqdMsg').style.display = "block";
	}
	else{
		if (!validateEAddress(emailAddress,1,1,'remind')){
			return;
		}
		if (anyChecked == true){
            // Set the value for SOURCE
            document.getElementById('SOURCE').value = getCookieValue('cid');
			document.remindForm.submit();
            var axel = Math.random()+"";
            var a = axel * 10000000000000;
            var dcSrc ='http://fls.doubleclick.net/activityi;src=189445;type=2008g687;cat=2008g747;ord=1;num='+ a + '?';

            document.getElementById("doubeClickSubmit").src = dcSrc;
			autoPopulate('email','sendRemindToEmail');
			document.getElementById('remindFormContent').style.display = "none";
				
			getChecked();
				
			document.getElementById('remindEmailThanks').style.display = "block";
	
			//Throw Omniture
			var s=s_gi(s_account);s.linkTrackVars='prop1,events,prop16,eVar16,prop17,eVar17,prop20,eVar20';s.linkTrackEvents='event5';s.prop1='click>remindmesend';s.prop16='08goteaser';s.eVar16='08goteaser';s.prop17='08goteaser_remindmesend';s.eVar17='08goteaser_remindmesend';s.prop20='remindmesend';s.eVar20='remindmesend';s.events='event5';s.tl(this,'o','click>remindmesend');
			
			//Throw DoubleClick
			var axel = Math.random()+"";
			var a = axel * 10000000000000;
			var img = new Image();
			img.src = 'http://ad.doubleclick.net/activity;src=189445;type=2008g757;cat=2008g297;ord=1;num='+ a;
		}
	}
/************************************************************************************/

	/*
	//If email address entered, validate email address then submit email
	if (emailAddress != ""){
		if (!validateEAddress(emailAddress,1,1,'remind')){
			return;
		}
		
		if (emailAddress != confirmEmailAddress) {
			document.getElementById('confirmErrorMsg').style.display = "block";
			return;
		}
		else {
			if (anyChecked == true){
				document.remindForm.submit();
	
				autoPopulate('emailaddress','sendRemindToEmail');
				document.getElementById('remindFormContent').style.display = "none";
				
				getChecked();
				
				document.getElementById('remindEmailThanks').style.display = "block";
	
				//Throw webtrends beacon for 'thank you' message
				// track('DCS.dcsuri=/G1/RemindTy.html&WT.ti=RemindMe Thankyou&WT.cg_n=Promotion&WT.cg_s=GoingOnce');
	
				//Throw Omniture
				var s=s_gi(s_account);s.linkTrackVars='prop1,events';s.linkTrackEvents='event5';s.prop1='link>remindme_thankyou';s.events='event5';s.tl(this,'o','link>remindme_thankyou');	
			}
		}*/
/************************************************************************************/
}

/**/
function getChecked(){
	var data = "<ul>";
	if(document.getElementById('pkg1').checked){
		data += "<li>"+getImage(document.getElementById('pkg1').id)+"</li>"
	}
	if(document.getElementById('pkg2').checked){
		data += "<li>"+getImage(document.getElementById('pkg2').id)+"</li>"
	}
	if(document.getElementById('pkg3').checked){
		data += "<li>"+getImage(document.getElementById('pkg3').id)+"</li>"
	}
	if(document.getElementById('pkg4').checked){
		data += "<li>"+getImage(document.getElementById('pkg4').id)+"</li>"
	}
	if(document.getElementById('pkg5').checked){
		data += "<li>"+getImage(document.getElementById('pkg5').id)+"</li>"
	}
	if(document.getElementById('pkg6').checked){
		data += "<li>"+getImage(document.getElementById('pkg6').id)+"</li>"
	}
	if(document.getElementById('pkg7').checked){
		data += "<li>"+getImage(document.getElementById('pkg7').id)+"</li>"
	}
	if(document.getElementById('pkg8').checked){
		data += "<li>"+getImage(document.getElementById('pkg8').id)+"</li>"
	}
	if(document.getElementById('pkg9').checked){
		data += "<li>"+getImage(document.getElementById('pkg9').id)+"</li>"
	}
	data += "</ul>";
	document.getElementById('selectedList').innerHTML = data;
}
function getImage(id){
	var openTag = "<img src=";
	var closeTag = " alt='' />";
	var srcImage = "";
	if(id=='pkg1'){
		srcImage = "images/remind/hawaii.gif";
		return openTag+srcImage+closeTag;
	}
	if(id=='pkg2'){
		srcImage = "images/remind/tahoe.gif";
		return openTag+srcImage+closeTag;
	}
	if(id=='pkg3'){
		srcImage = "images/remind/newyork.gif";
		return openTag+srcImage+closeTag;
	}
	if(id=='pkg4'){
		srcImage = "images/remind/orlando.gif";
		return openTag+srcImage+closeTag;
	}
	if(id=='pkg5'){
		srcImage = "images/remind/lasvegas.gif";
		return openTag+srcImage+closeTag;
	}
	if(id=='pkg6'){
		srcImage = "images/remind/baja.gif";
		return openTag+srcImage+closeTag;
	}
	if(id=='pkg7'){
		srcImage = "images/remind/alaska.gif";
		return openTag+srcImage+closeTag;
	}
	if(id=='pkg8'){
		srcImage = "images/remind/sanfran.gif";
		return openTag+srcImage+closeTag;
	}
	if(id=='pkg9'){
		srcImage = "images/remind/miami.gif";
		return openTag+srcImage+closeTag;
	}
}
/**/

function submit_cs() {

    var name = document.getElementById('name').value;
    var lastName = document.getElementById('lastName').value;
    var phone = document.getElementById('phone').value;
    var question = document.getElementById('questionForm');
    var emailAddress = document.getElementById('userEmail').value;
	var confirmEmailAddress = document.getElementById('confirmEmail').value;
	var comment = document.getElementById('comment').value;

    //Reset error messages
	document.getElementById('remindReqdMsg').style.display = "none";
	document.getElementById('remindErrorMsg').style.display = "none";
	document.getElementById('confirmErrorMsg').style.display = "none";
	document.getElementById('csTextReq').style.display = "none";
	document.getElementById('csTextTooLong').style.display = "none";
    document.getElementById('errorName').style.display = "none";
    document.getElementById('errorLName').style.display = "none";
    document.getElementById('errorPhone').style.display = "none";
    document.getElementById('errorQuestion').style.display = "none";

    //If name, last name, phone and question are filled correctly.
    if(name == ""){
        document.getElementById('errorName').style.display = "block";
        return false;
    }
    if(lastName == ""){
        document.getElementById('errorLName').style.display = "block";
        return false;
    }

    // If email address field is blank show error
	if (emailAddress == "") {
		document.getElementById('remindReqdMsg').style.display = "block";
		return false;
		
	// If email address entered, validate email address
	} else {
		if (!validateEAddress(emailAddress,1,1,'remind'))
			return false;

		if (emailAddress != confirmEmailAddress) {
			document.getElementById('confirmErrorMsg').style.display = "block";
			return false;
		}
	}

var phoneRE = /^\d\d\d[\- ]?\d\d\d[\- ]?\d\d\d\d$/;

   if (!(phone.match(phoneRE))) { 

   document.getElementById('errorPhone').style.display = "block";
   return false;

 } 

    if(question.selectedIndex == 0){
        document.getElementById('errorQuestion').style.display = "block";
        return false;
    }

    // Has the user entered any text?
	if (comment.length == 0) {
		document.getElementById('csTextReq').style.display = "block";
		return false;
	}
	
	// Has the user has entered more than 5000 characters of text?
	if (comment.length > 5000) {
		document.getElementById('csTextTooLong').style.display = "block";
		return false;
	}
	
	document.csForm.submit();
	
	autoPopulate('userEmail','csToEmail');
	document.getElementById('csFormFields1').style.display = "none";
	document.getElementById('csFormFields2').style.display = "none";
	document.getElementById('csFormFields3').style.display = "none";
	document.getElementById('csThanks').style.display = "block";

	
	// Throw webtrends beacon for 'thank you' message
	// track('DCS.dcsuri=/G1/RemindTy.html&WT.ti=RemindMe Thankyou&WT.cg_n=Promotion&WT.cg_s=GoingOnce');

	// Throw Omniture
	// var s=s_gi(s_account);s.linkTrackVars='prop1,events';s.linkTrackEvents='event5';s.prop1='link>remindme_thankyou';s.events='event5';s.tl(this,'o','link>remindme_thankyou');
}
/* Customer Service ShowHide */
function toggleAnswer(answerObject) {
	var currentDisplay = document.getElementById(answerObject).style.display;

	if (currentDisplay == "none") {
		document.getElementById(answerObject).style.display = "block";

		for (var i=1; i<6; i++) {
			var currentAnswerId = "answer" + i;

			if (currentAnswerId != answerObject)
				document.getElementById(currentAnswerId).style.display = "none";
		}
	} else {
		document.getElementById(answerObject).style.display = "none";
	}
	

}

function writeSessionCookie (cookieName, cookieValue) {
    document.cookie = cookieName + "=" + cookieValue + "; path=/";
}

function getCookieValue (cookieName) {
  var exp = new RegExp (cookieName + "=([^;]+)");
  if (exp.test (document.cookie + ";")) {
    exp.exec (document.cookie + ";");
    return RegExp.$1;
  }
  else return null;
}

// Get the CID value from the QueryString
function processCIDValue() {
    var qs = new Querystring(null);
    var cid = null;
    try {
        cid = qs.params['cid'];
        if(cid != null) writeSessionCookie('cid', cid);
    } catch(ex) {}
    if(cid == null) {
        try {
            cid = qs.params['CID'];
            if(cid != null) writeSessionCookie('cid', cid);
        } catch(ex) {}
    }
}
