// JavaScript Document
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

//-->








function createCookie(name,value,days) {
//check for new cookie and read it
	var oldvalue =  process_cookies();
	//alert('oldvalue is '+oldvalue); //debug
	var addedalready = process_cart_for_duplicate_cookies(value);
	//alert('addedalready is '+addedalready+'  and value is '+value+' and oldvalue is'+oldvalue); //debug
	if(addedalready == "no")
	{
		if (oldvalue != "ifnotcname then"){
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
				
			}
			else
			{ var expires = "";
			document.cookie = name+"="+oldvalue+value+expires+"; path=/; domain=recreation.du.edu";
			//document.cookie = name+"="+value+expires+"; path=/scripts/webtrac.wsc; domain=recreation.du.edu";
			}
		}
		else
		{
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
				
			}
			else {
			var expires = "";
			document.cookie = name+"="+value+expires+"; path=/; domain=recreation.du.edu";
			//document.cookie = name+"="+value+expires+"; path=/scripts/webtrac.wsc; domain=recreation.du.edu";
			}
		}
	}//end if addedalready is no

}


function process_cookies(){
//reading and splitting the whole cookie
var whole_cookie = unescape(document.cookie);
var each_cookie = whole_cookie.split(";");
//sorting cookies
var name_cookie_data = "namecookiedata before";
for (i = 0; i < each_cookie.length; i++){

//alert('show the index'+each_cookie[i].indexOf("vsicookie")); //debug
if (each_cookie[i].indexOf("vsicookie") > -1){name_cookie_data = each_cookie[i];}
//else{var name_cookie_data = "y";}
//alert('eachcookie is '+name_cookie_data); //debug
}


//splitting data and assigning final display variable
var name_split = name_cookie_data.split("=");
var cname = "before split";
cname = name_split[1];
var value_split = "before_split";
//value_split[0] = "blah";
//value_split[1] = "blah";
//
if(cname){
	value_split = cname.split("	");
	var values = "";
	for(j=0; j<value_split.length; j++)
	{
		if(value_split[j] == "|"){
			values  += value_split[j];
		}else{
			values  += value_split[j];
			/*values  += value_split[j]+"|";*/
		}
	}
}else{var values = "";}

//error control
//if (cname == ""){cname = "none entered";}
if (cname){}
else{cname = "ifnotcname then";}
//if (values){}
//else{values = "ifnotcname then";}
//if(cname){
//return cname;
//}else{
//cname = "";
return values;

//}
}//ends process cookies() function


function process_cart_for_duplicate_cookies(addedtocart){
//reading and splitting the whole cookie
var whole_cookie = unescape(document.cookie);
var each_cookie = whole_cookie.split(";");
//sorting cookies
var name_cookie_data = "namecookiedata before";
for (i = 0; i < each_cookie.length; i++){

//alert('show the index '+each_cookie[i].indexOf("vsicookie")+' and the value '+each_cookie_[i]); //debug
if (each_cookie[i].indexOf("vsicookie") > -1){name_cookie_data = each_cookie[i];}
//else{var name_cookie_data = "y";}
//alert('eachcookie is '+name_cookie_data); //debug
}


//splitting data and assigning final display variable
var name_split = name_cookie_data.split("=");
var cname = "before split";
cname = name_split[1];
var value_split = "before_split";
var added_split = "befor split";

var addedalready = "no";
if(cname){
	added_split = addedtocart.split("|");
	var valuecount = 0;
	var comparecode = "no";
	var comparesection = "no";
	value_split = cname.split("	");
	var values = "";
	for(j=0; j<value_split.length; j++)
	{
		//alert('j is '+j+' valuecount ='+valuecount+' comparecode is '+comparecode+' comparesection is '+comparesection+' value_split j is '+value_split[j]); 
		if(value_split[j] == "ar"){
			if(valuecount == 3)
			{
				valuecount = 0;
				comparesection = "no";
				comparecode = "no";
			}
		}else{
			if(valuecount == 1)
			{
			//alert(' valuecount ='+valuecount+'added_split is '+added_split[1]+' and value split[j] is '+value_split[j]); 
				//compare the added or just added value of activity code to values already in cookie
				if(added_split[1] == value_split[j])
				{
					comparecode= "yes";
				}
			//alert(' comparecode ='+comparecode); 
			
			}
			if(valuecount == 2)
			{
			//alert(' valuecount ='+valuecount+'added_split is '+added_split[2]+' and value split[j] is '+value_split[j]); 
				//compare the added or just added value of activity section to values already in cookie
				if(added_split[2] == value_split[j])
				{
					comparesection= "yes";
				}
			//alert(' comparesection ='+comparesection); 

			}
			//if code is yes then activity code has already been saved, it does not mean the the section is the same though
			if(comparecode == "yes")
			{
				//check to see if the section is also saved in the same line, if it is then the whole unique activity is already in shopping cart
				if(comparesection =="yes")
				{
						addedalready= "yes";
						valuecount = 10;
				//alert(' valuecount ='+valuecount+'both are yes so  comparesection ='+comparesection+' and  comparecode ='+comparecode); 
				}
			}
			
		}
		valuecount++;
	}
}else{
	var values = "";
	addedalready = "no";//the cart is empty so it cannot be added already
}
if(addedalready == "yes"){					
	alert('This program or activity is already in your shopping cart. You will now be forwarded to your shopping cart. Thank you! '); 
}
return addedalready;

//}
}//ends process_cart_for_duplicate_cookies() function




//process delete cookies deletes a certain value
function process_delete_cookies(position){
//reading and splitting the whole cookie
var whole_cookie = unescape(document.cookie);
var each_cookie = whole_cookie.split(";");
//sorting cookies
var name_cookie_data = "namecookiedata before";
for (i = 0; i < each_cookie.length; i++){

//alert('show the index'+each_cookie[i].indexOf("vsicookie")); //debug
if (each_cookie[i].indexOf("vsicookie") > -1){name_cookie_data = each_cookie[i];}
//else{var name_cookie_data = "y";}
//alert('eachcookie is '+name_cookie_data); //debug
}


//splitting data and assigning final display variable
var name_split = name_cookie_data.split("=");
var cname = "before split";
cname = name_split[1];
var value_split = "before_split";
//value_split[0] = "blah";
//value_split[1] = "blah";
//
if(cname){
	value_split = cname.split("	");
	var values = "";
	
	for(j=0; j<value_split.length; j++)
	{
		if(value_split[j] == "^"){
			values  += value_split[j];
		}else{
			
			values  += value_split[j]+"^";
		}
	}
	var each_value = "befor split";
	each_value = values.split("^");
	each_value[position] = "deleted";

	var lastvalue = "zzzzz"+each_value[2]+"cccccc";
	//alert(' the length is '+each_value.length+'  | position 0 is '+each_value[0]+' |  position 1 is  '+each_value[1]+' |  position 2 is  '+each_value[2]+' |  position 3 is  '+each_value[3]); //debug
	
	
//	afterposiition = position +1;
//alert('each_value at position '+position+' is '+each_value[position]+'<br/>each_value at position+1 or '+afterposiition+' is '+each_value[afterposiition]); //debug

	var new_values = "";
	//each_value[each_value.length-1] = "lastvalue";
	for(k=0; k <each_value.length-1; k++)
	{
	//	alert('eachvalue.length is '+each_value.length+' eachvalue k='+k+' is '+each_value[k]); 
/*		if(each_value[k] != "deleted")
		{
			new_values += each_value[k];
			
			if(new_values == "")
			{
				new_values += "";
			}
			else
			{
				new_values += "^";
			}
		}
*/		if(k == position || each_value[k] == "")
		{
			new_values += "";
		}else
		{
			
			
			new_values += each_value[k]+"^";
			//new_values += each_value[k]+"^=position"+position+"=andk="+k+"=andeachvaluelength="+each_value.length+"=";
			
			
			
		}
	//	alert(' newvalues k='+k+' is '+new_values ); 
	}
	var expires = "";
	var name = "vsicookie";
	document.cookie = name+"="+new_values+expires+"; path=/; domain=recreation.du.edu";

}else{var values = "";}

//error control
//if (cname == ""){cname = "none entered";}
if (cname){}
else{cname = "ifnotcname then";}
//if (values){}
//else{values = "ifnotcname then";}
//if(cname){
//return cname;
//}else{
//cname = "";
return new_values;

//}
}//ends process delete cookies() function


/////////////////////////////////////////////////////////////////////////
////////////////////// collapse section for shopping cart
/////////////////////////////////////////////////////////////////////////

 function expandcollapse(item) {
      if(document.getElementById(item) != null){
		  if (document.getElementById(item).style.display == "block") {
			document.getElementById(item).style.display = "none";
			//document.getElementById(item + "tog").innerHTML = " Expand this section [+]";
			//document.getElementById(item + "tog").title = "Expand section";
		  } else {
			document.getElementById(item).style.display = "block";
			//document.getElementById(item + "tog").innerHTML = " Collapse this section [-]";
			//document.getElementById(item + "tog").title = "Collapse section";
		  }
	  }
	  
    }

////////////////////////////////////////////////////////////////////////////////
////////////////////// validation section
////////////////////////////////////////////////////////////////////////////////

//////validate interest form on family pages
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
  {alert(alerttxt);return false}
else {return true}
}
}

function validate_intform(thisform)
{
with (thisform)
{
if (validate_required(txt_fname,"You need to enter a first name")==false)
  {txt_programname.focus();return false}
if (validate_required(txt_lname,"You need to enter a last name")==false)
  {txt_description.focus();return false}
if (validate_required(txt_address,"In order to send you the right information, we need your address. Don't worry we will not sell or abuse it.")==false)
  {txt_registration.focus();return false}
if (validate_required(txt_city,"The address is incomplete without the city.")==false)
  {txt_other1.focus();return false}
if (validate_required(txt_state,"The address is incomplete without the state.")==false)
  {txt_other2.focus();return false}
if (validate_required(txt_zip,"The address is incomplete without the zip code.")==false)
  {txt_programdates.focus();return false}
if (validate_required(txt_phone,"We also need your phone number. We only use it to get back to you with the information you requested. We will not sell it or abuse it in any way.")==false)
  {txt_costs.focus();return false}
if (validate_required(txt_email,"We also need your email address. We only use it to get back to you with the information you requested. We will not sell it or abuse it in any way.")==false)
  {txt_prerequisites.focus();return false}
}
}




///////////////////////////////////
////////////collapsing functions
////////////////////////////////////

 function collapsethis(item) {
      if(document.getElementById(item) != null){
		  if (document.getElementById(item).style.display == "block") {
			document.getElementById(item).style.display = "none";
			document.getElementById(item + "tog").innerHTML = " Submit Feedback [+]";
			document.getElementById(item + "tog").title = "Submit Feedback";
		  } else {
			document.getElementById(item).style.display = "block";
			document.getElementById(item + "tog").innerHTML = " Collapse Feedback Form [-]";
			document.getElementById(item + "tog").title = "Collapse Feedback Form";
		  }
	  }
	  
    }
