// JavaScript Document


function cleanUpCC(id){		
	var str = document.getElementById(id).value;
	var newstr = str.replace(/ /gi,"");
	newstr = newstr.replace(/_/gi,"");
	newstr = newstr.replace(/-/gi,"");	
	document.getElementById(id).value = newstr;
}

function helpBox(txt){	
	blurb(txt,400,400,400);	
}

function stripHtml(str) {
	
    var newstr = str;
    var regExp = /<\/?(\w+)(.*?)>/ig;
    var i = 0;	
	
    while(i++ < 5000 && (mt = regExp.exec(str))) {
	
      	oldstr = mt[0];
      	tag    = mt[1];
      	pars   = mt[2];	  
	 	
      	if(tag.match(/^(map|area|small|iframe|ul|param|object|embed|li|ol|em|strong|span|div|br|i|center|table|tr|td|b|u|p|a|img)$/i)) {		  
       // 	if(tag.match(/^(p)$/i)) 
		//	{	repl = oldstr.replace(pars,''); // get rid of attributes
		//	}
		//	else
		//	{
				repl = oldstr; // don't get rid of the tag or its attributes
		//	}
		
      	} else 
	  	{  repl = ''; // get rid of entire tag	
	  	}	
		

      	newstr = newstr.replace(oldstr, repl, "g");
	 // 	newstr = newstr.replace("<p>","<br/><br/>");
	 // 	newstr = newstr.replace("</p>","");
    }
    regExp = null;
    return newstr;
  }  
  
function removeHTMLTags(fldname){
 	if(document.getElementById && document.getElementById(fldname)){
 	//	var strInputCode = document.getElementById(fldname).innerHTML;
		var strInputCode = document.getElementById(fldname).value;
 		/* 	This line is optional, it replaces escaped brackets with real ones, 
  			i.e. < is replaced with < and > is replaced with >
 		*/	
 	 	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
 		 	return (p1 == "lt")? "<" : ">";
 		});
 		var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");	  
	  
 	//	alert("Output text:\n" + strTagStrippedText);	
   // 		Use the alert below if you want to show the input and the output text
   		alert("Input code:\n" + strInputCode + "\n\nOutput text:\n" + strTagStrippedText);	
 	}	
}




function copyright(){	
	alert('Icons are copyright.');	
}
function imageCopyright(){	
	alert('Image is not available for download.');	
}

function valBilling(checkship,checkcard,checkamount){
	if(checkamount == true)
	{ var str = "Amount,First Name,Last Name,Address,City,State,Zip,Country,Phone";	}
	else
	{ var str = "First Name,Last Name,Address,City,State,Zip,Country,Phone"; }
	
	
	// validate billing info
	var list = str.split(',');
	for(var i in list)
	{	
		if(document.getElementById(list[i]).value == "" || document.getElementById(list[i]).value == "0")
		{alert(list[i] + " is blank.");	
		document.getElementById(list[i]).focus();
		return false;}
	}
	
	// validate email address
	if (validateEmail(document.getElementById('Email').value) !== true)
	{
		document.getElementById('Email').focus();	
		return false;
	}
		
	// validate shipping address
	if(checkship==true)
	{
	var str = "Shipping First Name,Shipping Last Name,Shipping Address,Shipping City,Shipping State,Shipping Zip,Shipping Country";
	var list = str.split(',');
	for(var i in list)
		{	
		if(document.getElementById(list[i]).value == "" || document.getElementById(list[i]).value == "0")
			{	alert(list[i] + " is blank.");	document.getElementById(list[i]).focus();return false;}
		}
	
	}
	
	// validate credit card format
	if(checkcard == true)
	{
		if (isCreditCard(document.getElementById('creditcard').value) !== true)
		{	alert("Invalid Credit Card");	return false;}
		if (document.getElementById('ccname').value == "")
		{	alert("Name on Credit Card Required");	return false;}			
	}
	
	return true;
}

function valCustomForm(multi,checkbill,checkship,checkcard,itemreq,reqFields){	
	
	// ---- check fields -----------------------------
	var fldval,fld,fldtype,listitem,fldid;	
	var rlist = reqFields.split(',');	
	for(listitem in rlist)
	{	
		fld = rlist[listitem].split('#');		
		fldid = fld[0];		
		fldtype = fld[1];			
				
		
		if(parseInt(fldtype) == 1 || parseInt(fldtype) == 2)
		{	
			fldval = document.getElementById("fld_" + fldid).value;	
			if(fldval == "" )			
			{		
				alert("Field is required.");
				document.getElementById("fld_" + fldid).focus();
				return false;
			}
		}	
		else
		{	
				
		}
			
	}

		
	// ---- check items  -----------------------------
	var itemcnt = 0;
	
	if(multi == 1){		
		for(i=0; i<document.orderform.elements.length; i++)
		{	if(document.orderform.elements[i].type=="checkbox" && document.orderform.elements[i].checked==true )
			{ itemcnt = parseInt(itemcnt) + 1 }
		}	
	}
	else	
	{
		for(i=0; i<document.orderform.elements.length; i++)
		{	if(document.orderform.elements[i].type=="radio" && document.orderform.elements[i].checked==true )
			{ itemcnt = parseInt(itemcnt) + 1 }
		}		
	}
	
	if(itemreq == 1){
		if(itemcnt == 0)
		{	alert("Please select at least one item before submitting this form.");
			return false;
		}	
	}
	
	if(checkbill==1)
	{	// check billing address --------------------------------------
	var str = "First Name,Last Name,Address,City,State,Zip,Country,Phone";
	var list = str.split(',');
	for(var i in list)
	{	if(document.getElementById(list[i]).value == "")
		{alert(list[i] + " is blank.");	document.getElementById(list[i]).focus();return false;}
	}
	}
	
	if (validateEmail(document.getElementById('Email').value) !== true)
	{document.getElementById('Email').focus(); return false;}
		
	if(checkship==1)
	{
	var str = "Shipping First Name,Shipping Last Name,Shipping Address,Shipping City,Shipping State,Shipping Zip,Shipping Country";
	var list = str.split(',');
	for(var i in list)
		{	
		if(document.getElementById(list[i]).value == "")
			{	alert(list[i] + " is blank.");	document.getElementById(list[i]).focus();return false;}
		}	
	}
	
	if(checkcard ==1)
	{
		if (isCreditCard(document.getElementById('creditcard').value) !== true)
		{	alert("Invalid Credit Card");	return false;}
		if (document.getElementById('ccname').value == "")
		{	alert("Name on Credit Card Required");	return false;}	
	}
	
	return true;
}


function valOrderForm(checkbill,checkship,checkcard){	
	
	// check items first -----------------------------
	var itemcnt = 0;
	for(i=0; i<document.orderform.elements.length; i++)
	{	if(document.orderform.elements[i].type=="checkbox" && document.orderform.elements[i].checked==true && document.orderform.elements[i].id !="copy")
		{ itemcnt = parseInt(itemcnt) + 1 }
	}	
	if(itemcnt == 0)
	{	alert("Please select at least one product before submitting this form.");
		return false;
	}		
	
	if(checkbill==true)
	{	// check billing address --------------------------------------
	var str = "First Name,Last Name,Address,City,State,Zip,Country,Phone";
	var list = str.split(',');
	for(var i in list)
	{	if(document.getElementById(list[i]).value == "")
		{alert(list[i] + " is blank.");	document.getElementById(list[i]).focus();return false;}
	}
	}
	
	if (validateEmail(document.getElementById('Email').value) !== true)
	{document.getElementById('Email').focus(); return false;}
		
	if(checkship==true)
	{
	var str = "Shipping First Name,Shipping Last Name,Shipping Address,Shipping City,Shipping State,Shipping Zip,Shipping Country";
	var list = str.split(',');
	for(var i in list)
		{	
		if(document.getElementById(list[i]).value == "")
			{	alert(list[i] + " is blank.");	document.getElementById(list[i]).focus();return false;}
		}	
	}
	
	if(checkcard ==true)
	{
		if (isCreditCard(document.getElementById('creditcard').value) !== true)
		{	alert("Invalid Credit Card");	return false;}
		if (document.getElementById('ccname').value == "")
		{	alert("Name on Credit Card Required");	return false;}	
	}
	
	return true;
}

function copyBillToShip(obj){
	var str = "First Name,Last Name,Address,Address Line 2,City,State,Zip,Country";
	var list = str.split(',');
	for(var i in list)
	{	
		if (obj.checked == true)
		{document.getElementById("Shipping " +list[i]).value = document.getElementById(list[i]).value }
		else
		{document.getElementById("Shipping " +list[i]).value = ""}
	}	
}



function clearBlurb(){
	
	$("#blurbzone").empty();
	$("#blurbzone").hide();
//	$("#blurbzone").removeShadow();
}

function newWindow(url,height,width,toolbar,menubar,scrollbars,resizable,location){
	window.open(url, 'newwindow', config='height=' + height +', width=' + width +', toolbar=' + toolbar + ', menubar=' + menubar + ', scrollbars=' + scrollbars + ', resizable=' + resizable + ', location=' + location)
}

function close_refresh(destination) {
window.opener.location.href=destination;
self.close();		
}


function toggle(id){
if(document.getElementById(id).style.display == 'none')
{document.getElementById(id).style.display = 'inline';}
else
{document.getElementById(id).style.display = 'none';}
}

function toggleBlock(id){
if(document.getElementById(id).style.display == 'none')
{document.getElementById(id).style.display = 'block';}
else
{document.getElementById(id).style.display = 'none';}
}

function hideshow(id,show){
if(show==true)
{document.getElementById(id).style.display = 'block';}
else
{document.getElementById(id).style.display = 'none';}
}

function toggleBlockIfChecked(ch,id){
if(ch == true)
{document.getElementById(id).style.display = 'block';}
else
{document.getElementById(id).style.display = 'none';}
}

function toggleIfChecked(ch,id){
if(ch == true)
{document.getElementById(id).style.display = '';}
else
{document.getElementById(id).style.display = 'none';}
}

function toggleHiddenIfChecked(ch,id){
if(ch == true)
{document.getElementById(id).style.display = 'none';}
else
{document.getElementById(id).style.display = 'block';}
}

function toggleHiddenInlineIfChecked(ch,id){
if(ch == true)
{document.getElementById(id).style.display = 'none';}
else
{document.getElementById(id).style.display = '';}
}

function toggleInlineIfChecked(ch,id){
if(ch == true)
{document.getElementById(id).style.display = 'inline';}
else
{document.getElementById(id).style.display = 'none';}
}

function toggleIcon(id,icon1,icon2){
if(document.getElementById(id).src==icon1)
{document.getElementById(id).src=icon2;}
else
{document.getElementById(id).src=icon1;}
}

function confirmDelete(linkto, object){		
	
		rv = confirm('Are you sure you want to remove this ' + object + '?');	
		if (rv == true)
		{	alert('Removing');
			window.location = linkto;
			return true
		}	
		else{return false}	
	}
	
function confirmDeleteDouble(linkto, object){		

		rv = confirm('Are you sure you want to delete this ' + object + '?');	
		
		if (rv == true)
		{rv = confirm('Are you SURE? Deleting cannot be undone. Press cancel if you do not want to delete.');}
		
		if (rv == true)
		{	alert('Deleting');
			window.location = linkto;
			return true
		}	
		else{return false}	
	}
	
	function confirmDeleteDoubleDeactivate(linkto, object){		

		rv = confirm('Are you sure you want to delete this ' + object + '?');	
		
		if (rv == true)
		{rv = confirm('Are you SURE? Deleting cannot be undone. You may choose to deactivate this ' + object + ' instead of deleting it permanently. It will not be visible on the front end until you reactivate it. Press cancel if you do not want to delete.');}
		
		if (rv == true)
		{	alert('Deleting');
			window.location = linkto;
			return true
		}	
		else{return false}	
	}

function blurb(msg,x,y,w)
{	
	msg = unescape(msg);
	
	$("#blurbzone").html(msg);
	$("#blurbzone").css("top", y + "px");
	$("#blurbzone").css("left",x + "px");
	if(w != ""){$("#blurbzone").css("width", w + "px");}	
	$("#blurbzone").show();
//	$("#blurbzone").dropShadow({left: 2, top: 2, opacity: 0.7, blur: 6});
	
//	document.getElementById("blurbzone").innerHTML = msg;
//	document.getElementById("blurbzone").style.top = y + 20;
//	document.getElementById("blurbzone").style.left = x- 5;
//	document.getElementById("blurbzone").style.display = 'inline';		
}

function checkOneDedication(){	
	var itemcnt = 0;
	for(i=0; i<document.form1.elements.length; i++)
	{	if(document.form1.elements[i].type=="checkbox" && document.form1.elements[i].checked==true && document.form1.elements[i].id !="freegift" )
		{ itemcnt = parseInt(itemcnt) + 1 }
	}	
	if(itemcnt == 0)
	{	return false;	}
	else
	{	return true; }
}

function valDedication(){
	if(checkOneDedication() == false ){
		alert("Please select at least one Dedication Opportunity before submitting this form.");
		return false;
	}
	if(document.getElementById('pcc').checked == true )
	{	return valBilling(false,true,false);	}
}

function valRaffle(memoReq){
	if(checkOneDedication() == false ){
		alert("Please select a Raffle Ticket before submitting this form.");
		return false;
	}
	
	if(parseInt(memoReq) == 1 && document.getElementById("memo").value ==""){
		alert("Please fill in the Memo/Referrer box before submitting this form.");
		return false;
	}
	if(document.getElementById('pcc').checked == true )
	{	return valBilling(false,true,false);	}
}



