/*Common Libraries*/
function valid_email_address(email_addr)
{
	var x = email_addr;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else return false;
}

function addslashes(str) {
    str=str.replace(/\'/g,'\\\'');
    str=str.replace(/\"/g,'\\"');
    str=str.replace(/\\/g,'\\\\');
    str=str.replace(/\0/g,'\\0');
    return str;
}
function stripslashes(str) {
    str=str.replace(/\\'/g,'\'');
    str=str.replace(/\\"/g,'"');
    str=str.replace(/\\\\/g,'\\');
    str=str.replace(/\\0/g,'\0');
    return str;
}

function createWindow(cUrl,cName,cFeatures)
{
		var xWin = window.open(cUrl,cName,cFeatures)
}
          

    function goto_page(url)
    {
        window.location=url;
    }

              var http_request = false;
  
              function makeRequest(url) {
  
                  http_request = false;
  
                  if (window.XMLHttpRequest) { // Mozilla, Safari,...
                      http_request = new XMLHttpRequest();
                      if (http_request.overrideMimeType) {
                          http_request.overrideMimeType('text/xml');
                      }
                  } else if (window.ActiveXObject) { // IE
                      try {
                          http_request = new ActiveXObject("Msxml2.XMLHTTP");
                      } catch (e) {
                          try {
                          http_request = new ActiveXObject("Microsoft.XMLHTTP");
                          } catch (e) {}
                      }
                  }
  
                  if (!http_request) {
                      alert('Giving up :( Cannot create an XMLHTTP instance');
                      return false;
                  }
                  http_request.onreadystatechange = alertContents;
                  http_request.open('GET', url, true);
                  http_request.send(null);
  
              }


              function ajaxPopup(url) {
  
                  http_request = false;
  
                  if (window.XMLHttpRequest) { // Mozilla, Safari,...
                      http_request = new XMLHttpRequest();
                      if (http_request.overrideMimeType) {
                          http_request.overrideMimeType('text/xml');
                      }
                  } else if (window.ActiveXObject) { // IE
                      try {
                          http_request = new ActiveXObject("Msxml2.XMLHTTP");
                      } catch (e) {
                          try {
                          http_request = new ActiveXObject("Microsoft.XMLHTTP");
                          } catch (e) {}
                      }
                  }
  
                  if (!http_request) {
                      alert('Giving up :( Cannot create an XMLHTTP instance');
                      return false;
                  }
                  http_request.onreadystatechange = ajax_dialog;
                  http_request.open('GET', url, true);
                  http_request.send(null);
  
              }
              
              function ajax_dialog() {
  
                  if (http_request.readyState == 4) {
                      if (http_request.status == 200) {
                          var xmldoc = http_request.responseText;
                          //var root_node = xmldoc.getElementsByTagName('root').item(0);
                          //alert(root_node.firstChild.data);
                          //alert(xmldoc);
                          document.getElementById("pop_up_dialogs").innerHTML=xmldoc;
                          position_dialog("pop_up_dialog");
                      } else {
                          alert('There was a problem with the request.');
                          
                      }
                  }
  
              }              




              /* Buddy Mgr Javascript functions */
              function verify_add_a_buddy()
              {
                var type="username";
                var value="";
                if( document.add_a_buddy.email.disabled == false )
                {
                  if( document.add_a_buddy.email.value == "" )
                  {
                    alert("You have chosen to select a buddy via typing an email address, " +
                            "yet you did not enter the address into the field\n\n " +
                            "Please type an EMAIL ADDRESS and then click SUBMIT");
                    document.add_a_buddy.button_submit.type="button";
                    return;
                  }
                  type = "email";
                  value = document.add_a_buddy.email.value;
                  
                }
                
                if( document.add_a_buddy.username.disabled == false )
                {
                  if( document.add_a_buddy.username.value == "" )
                  {
                    alert("You have chosen to select a buddy via typing a username, " +
                            "yet you did not enter a username into the field\n\n " +
                            "Please type a USERNAME and then click SUBMIT");
                    document.add_a_buddy.button_submit.type="button";
                    return;
                  }
                  value = document.add_a_buddy.username.value;
                }
                
                if( document.getElementById('existing_buddies').disabled == false )
                {
                  value = new Array();
									var counter =0;
                  for( var i=0; i<document.add_a_buddy.existing_buddies.length; i++)
                  {
										if(document.add_a_buddy.existing_buddies[i].selected)
										{
												value[counter] = document.add_a_buddy.existing_buddies[i].value
												counter ++;
										}
                  }
                }
                document.add_a_buddy.button_submit.type = "button";
                
                if( typeof(value) != "object" )
                {
                    value = value.replace(/\n/g, " ");
                }
								ajaxPopup("lib/gadget_buddy_mgr.php?state=process_buddy_add&type=" + type + "&value=" + value);
                
              }              

              function buddy_mgr_click_delete()
              {
                //document.buddy_mgr_members_form.action = 'javascript:ajaxPopup("lib/gadget_buddy_mgr.php?state=process_buddy_remove");';
                var res;
                res = confirm("Are you sure you want to remove the selected buddies from your list?");
								if(res)
                {
                  document.buddy_mgr_members_form.action = 'lib/gadget_buddy_mgr.php?state=process_buddy_remove';
                  document.buddy_mgr_members_form.submit();
                }
              }

    function position_dialog(dlg_id)
    {
        if(document.getElementById(dlg_id))
        {
            document.getElementById(dlg_id).style.top = document.getElementById("page_body").clientHeight/2 - 
                document.getElementById(dlg_id).offsetHeight/2;
            if( document.getElementById("page_body").clientHeight/2 -
                document.getElementById(dlg_id).offsetHeight/2 < 0)
            {
                document.getElementById(dlg_id).style.top = 0;
            }
            document.getElementById(dlg_id).style.left = document.getElementById("page_body").clientWidth/2 - 
                document.getElementById(dlg_id).offsetWidth/2;
        }
    }
    
    
/* wl_explorer Javascript functions */
function set_submit_button_state()
{
    //alert(document.new_list_form.button_submit.disabled);
    if( document.new_list_form.list_name.value == "" )
    {
        document.new_list_form.button_submit.disabled = true;
    } else
    {
        document.new_list_form.button_submit.disabled = false;
    }
}

function submit_new_list_dialog()
{
    //alert(document.new_list_form.list_name.value);
    if( document.new_list_form.list_name.value == "" )
    {
        alert("You must include at least a name for your new list");
        document.new_list_form.button_submit.type = "button";
    } else
    {
        document.new_list_form.submit();
    }
}

function wl_button_delete_pushed()
{
    res = confirm("Are you sure you want to DELETE this list?\n" +
            "There is no recovering from a deleted list.");
    if( res == true )
    {
        document.new_list_form.state.value="delete_list";
        document.new_list_form.submit();
    }
}


/*gadget_wl_mgr.php - WishList Page*/
              function checkUncheckAll(theElement) 
              {
                var theForm = theElement.form, z = 0;
                for(z=0; z<theForm.length;z++)
                {
                  if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall')
                  {
                    theForm[z].checked = theElement.checked;
	                }
                }
              }
              
              
    
              function sendCancelAction()
              {
                document.new_item_form.action="wishlist_mgr.php";
                document.new_item_form.submit();
              }
              function verifyNewItemAction()
              {
                if( document.new_item_form.itemname.value == "" )
                {
                  alert("You must enter in a name of the item");
                } else
                {
                  document.new_item_form.submit();
                }
              }
              
              function verifyDeleteItemAction(list_id)
              {
                checkbox_choices = 0;
                if( document.wish_list_items_form.item.length == undefined ) //Only one item, thus not an array
                {
                    if (document.wish_list_items_form.item.checked)
                    {
                      checkbox_choices = checkbox_choices + 1;
                    }
                }
                //for (counter = 0; counter < document.wish_list_items_form.item.length; counter++)
                for (counter = 0; counter < document.wish_list_items_form.item.length; counter++)
                  // If a checkbox has been selected it will return true
                  // (If not it will return false)
                  //if (document.wish_list_items_form.item[counter].checked)
                  if (document.wish_list_items_form.item[counter].checked)
                  { checkbox_choices = checkbox_choices + 1; }
                
                if (checkbox_choices == 0 )
                {
                  // If there were 0 items, we throw an error
                  msg="You need to select an item if you wish to delete it.";
                  alert(msg);
                  document.delete_link.href="";
                  return (false);
                }
                document.wish_list_items_form.action= 'wishlist_mgr.php?state=deleteitemsinlist&list_id=' + list_id;
                document.wish_list_items_form.submit();
              }
              
              function verifyClaimItemAction(list_id)
              {
                checkbox_choices = 0;
                if( document.wish_list_items_form.item.length == undefined ) //Only one item, thus not an array
                {
                    if (document.wish_list_items_form.item.checked)
                    {
                      checkbox_choices = checkbox_choices + 1;
                    }
                }
                for (counter = 0; counter < document.wish_list_items_form.item.length; counter++)
                  // If a checkbox has been selected it will return true
                  // (If not it will return false)
                  if (document.wish_list_items_form.item[counter].checked)
                  {
                    checkbox_choices = checkbox_choices + 1;
                  }
                
                if (checkbox_choices == 0 )
                {
                  // If there were 0 items, we throw an error
                  msg="You need to select at least one item if you wish to claim it.";
                  alert(msg);
                  document.claim_link.href="";
                  return (false);
                }
                document.wish_list_items_form.action= 'wishlist_mgr.php?state=claimitemsinlist&list_id=' + list_id;
                document.wish_list_items_form.submit();
              }
              
              function verifyPurchasedItemAction(list_id)
              {
                checkbox_choices = 0;
                if( document.wish_list_items_form.item.length == undefined ) //Only one item, thus not an array
                {
                    if (document.wish_list_items_form.item.checked)
                    {
                      checkbox_choices = checkbox_choices + 1;
                    }
                }
                //for (counter = 0; counter < document.wish_list_items_form.item.length; counter++)
                for (counter = 0; counter < document.wish_list_items_form.item.length; counter++)
                  // If a checkbox has been selected it will return true
                  // (If not it will return false)
                  //if (document.wish_list_items_form.item[counter].checked)
                  if (document.wish_list_items_form.item[counter].checked)
                  { checkbox_choices = checkbox_choices + 1; }
                
                if (checkbox_choices == 0 )
                {
                  // If there were 0 items, we throw an error
                  msg="You need to select at least one item if you wish to mark it as having been purchased.";
                  alert(msg);
                  document.purchased_link.href="";
                  return (false);
                }
                document.wish_list_items_form.action= 'wishlist_mgr.php?state=purchaseditemsinlist&list_id=' + list_id;
                document.wish_list_items_form.submit();
              }
              
              function verifyAvailableItemAction(list_id)
              {
                checkbox_choices = 0;
                if( document.wish_list_items_form.item.length == undefined ) //Only one item, thus not an array
                {
                    if (document.wish_list_items_form.item.checked)
                    {
                      checkbox_choices = checkbox_choices + 1;
                    }
                }
                //for (counter = 0; counter < document.wish_list_items_form.item.length; counter++)
                for (counter = 0; counter < document.wish_list_items_form.item.length; counter++)
                  // If a checkbox has been selected it will return true
                  // (If not it will return false)
                  //if (document.wish_list_items_form.item[counter].checked)
                  if (document.wish_list_items_form.item[counter].checked)
                  { checkbox_choices = checkbox_choices + 1; }
                
                if (checkbox_choices == 0 )
                {
                  // If there were 0 items, we throw an error
                  msg="You need to select at least one item if you wish to mark it as having become available.";
                  alert(msg);
                  document.available_link.href="";
                  return (false);
                }
                document.wish_list_items_form.action= 'wishlist_mgr.php?state=availableitemsinlist&list_id=' + list_id;
                document.wish_list_items_form.submit();
              }              
              
              
              function verifySaveItemAction()
              {
                if( document.new_item_form.itemname.value == "" )
                {
                  alert("You must enter in a name of the item");
                } else
                {
                  document.new_item_form.action="wishlist_mgr.php?state=saveitemedits";
                  document.new_item_form.submit();
                }
              }
              
              
              function alertContents() {
  
                  if (http_request.readyState == 4) {
                      if (http_request.status == 200) {
                          var xmldoc = http_request.responseText;
                          //var root_node = xmldoc.getElementsByTagName('root').item(0);
                          //alert(root_node.firstChild.data);
                          //alert(xmldoc);
                          document.getElementById("main_section").innerHTML=xmldoc;
                      } else {
                          alert('There was a problem with the request.');
                          
                      }
                  }
  
              }

function toggle_hide(id)
{
    //alert(document.getElementById("nerk").style.display);
    if( document.getElementById(id).style.display == 'none' )
    {
        document.getElementById(id).style.display='block';
    } else
    {
        document.getElementById(id).style.display='none';
    }
}

function show(id)
{
    document.getElementById(id).style.display='block';
}

function hide(id)
{
    document.getElementById(id).style.display='none';
}


/*gadget_list_code*/
 
function gadget_list_code_button_view()
{
	document.list_code_entry_form.submit();
}

/*gadget_about_us*/
function gadget_about_us_erase_input_box(input)
{
    //document.getElementsByName(name).value = "";
    if( input.store == undefined )
    {
        input.value="";
        input.store = 1;
    }
}

function gadget_about_us_verify_send()
{
    //Check Email Address Field
    if( document.contact_us_email_form.from.value == "" ||
       document.contact_us_email_form.from.store == undefined ||
       !valid_email_address(document.contact_us_email_form.from.value)
       )
    {
        alert("You must insert a valid email address for us to repond to you");
        return(false);
    }
    
    //Ensure there is a subject
    if( document.contact_us_email_form.subject.value == "" ||
       document.contact_us_email_form.subject.store == undefined )
    {
        alert("You need to include a subject");
        return(false);
    }
    
    //Ensure that there is a message
    if( document.contact_us_email_form.message.value == "" ||
       document.contact_us_email_form.message.store == undefined )
    {
        alert("You need to include a message in your email");
        return(false);
    }
}

function is_firefox()
{
	if( document.getElementById && !document.all )
	{
		return true;
	}
	return false;
}
function is_ie()
{
	if( document.all )
	{
		return true;
	}
	return false;
}
				
				
function determine_browser_type()
{
		if( !document.getElementById("browser_type_set") )
		{
				if( is_firefox() )
				{
						document.browser = "firefox";
				}
				if( is_ie() )
				{
						document.browser = "ie";
				}
				window.location=window.location + "&browser=" + document.browser;
				
		}
}

/* gadget_profile */

              function cancel_account_info()
              {
                window.location = "/";
              }
              
              function submit_account_info(obj)
              {
								if( obj!=null )
								{
										document.update_profile = obj;
								}
								//alert(obj);
								//return false;
								//document.update_profile.button_submit.type = "button";
								
								validusernamechars=/^[0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]+$/;
                if( document.update_profile.username.value.search(validusernamechars) == -1 )
                {
                  alert("You must use only letters and numbers in your username");
                  return false;
                }
                
								
                //Ensure that a firstname was entered
                if( document.update_profile.firstname.value == "" )
                {
                  alert("You must supply your First Name");
                  return false;
                } 
                
                //Ensure that a lastname was entered
                if( document.update_profile.lastname.value == "" )
                {
                  alert("You must supply your Last Name");
                  return false;
                }          
                
                //Ensure that an email was entered
                if( document.update_profile.email.value == "" )
                {
                  alert("You must supply your current email address");
                  return false;
                }
                
                //Ensure that ZIPCode was entered
                if( document.update_profile.zipcode.value == "" )
                {
                  alert("You must supply your current zipcode.");
                  return false;
                }
                //document.update_profile.button_submit.type = "submit";
                //document.update_profile.button_submit2.type = "submit";
                
              }
              
              function check_username()
              {
                //alert(document.signup_information.username.value);
                var user = document.signup_information.username.value;
                if( user == "" )
                {
                  alert("You must specify a username before you check to see if it is available!");
                  return;
                }
                ajax_check_username("signup.php?username=" + user + "&state=check_username");
              }
              
              function ajax_check_username(url) {
  
                  http_request = false;
  
                  if (window.XMLHttpRequest) { // Mozilla, Safari,...
                      http_request = new XMLHttpRequest();
                      if (http_request.overrideMimeType) {
                          http_request.overrideMimeType('text/xml');
                      }
                  } else if (window.ActiveXObject) { // IE
                      try {
                          http_request = new ActiveXObject("Msxml2.XMLHTTP");
                      } catch (e) {
                          try {
                          http_request = new ActiveXObject("Microsoft.XMLHTTP");
                          } catch (e) {}
                      }
                  }
  
                  if (!http_request) {
                      alert('Giving up :( Cannot create an XMLHTTP instance');
                      return false;
                  }
                  http_request.onreadystatechange = publish_username_availability;
                  http_request.open('GET', url, true);
                  http_request.send(null);
  
              }
              
              function publish_username_availability() {
                  if (http_request.readyState == 4) {
                      if (http_request.status == 200) {
                          var xmldoc = http_request.responseText;
                          document.getElementById("avail_result").innerHTML=xmldoc;
                      } else {
                          alert('There was a problem with the request.');
                          
                      }
                  }                
              }
              
              
              function alertContents() {
  
                  if (http_request.readyState == 4) {
                      if (http_request.status == 200) {
                          var xmldoc = http_request.responseText;
                          //var root_node = xmldoc.getElementsByTagName('root').item(0);
                          //alert(root_node.firstChild.data);
                          //alert(xmldoc);
                          document.getElementById("main_section").innerHTML=xmldoc;
                      } else {
                          alert('There was a problem with the request.');
                          
                      }
                  }
  
              }
							
							
/* gadget_advertising */
function  get_selected_icon_image()
{
		var icon = Array();
		var i=0;
		icon[i] = document.getElementById("icon" + (i+1));
		//alert(icon[i]);
		while( icon[i] != "undefined")
		{
				if(icon[i].checked)
				{
						break;
				}
				i++;
				icon[i] = document.getElementById("icon" + (i+1));
				//alert(icon[i]);
		}
		//alert("Now for the image");
		//alert(i);
		//alert(document.getElementById("img" + i).src);
		return document.getElementById("img" + (i+1)).src;
}

function updated_generated_view()
{
		var html = "";
		var html_text = "";
		var imgloc = "";
		var icon_text = "";
		var url = "";
		var price = "";
		
		imgloc = get_selected_icon_image();
		icon_text = document.getElementById("icon_text").value;
		item_name = document.getElementById("product_name").value;
		url = document.getElementById("url").value;
		price = document.getElementById("price").value;
		
		html += "<a href='http://www.wishtips.com/wishlist_mgr.php?state=add_item_from_external_page";
		if( item_name )
		{
				html += "&item_name=" + item_name;
		}
		if( url )
		{
				html += "&url=" + url;
		}
		if( price )
		{
				html += "&item_cost=" + price;
		}
		html += "'><img src='" + imgloc + "' align='middle' /> " + icon_text + "<br>";
		html += "</a>";
		
		html_text += "&lt;a href='http://www.wishtips.com/wishlist_mgr.php?state=add_item_from_external_page";
		if( item_name )
		{
				html_text += "&item_name=" + item_name;
		}
		if( url )
		{
				html_text += "&url=" + url;
		}
		if( price )
		{
				html_text += "&item_cost=" + price;
		}
		html_text += "'&gt;&lt;img src='" + imgloc + "' align='middle' /&gt; " + icon_text + "&lt;br&gt;";
		html_text += "&lt;/a&gt;";
		
		document.getElementById("generated_view").innerHTML = html;
		document.getElementById("generated_code").innerHTML = html_text;
}

function copy_to_clipboard(obj)
{
		r = obj.createTextRange();
		r.select();
		r.execCommand('copy');
		alert("The text has been copied");
}



/* Refer a Friend Javascripts */
function verify_and_send_referrals()
{
		var get_string = "";
		get_string += "?fullname=" + document.add_referrals.fullname.value;
		get_string += "&sender_email=" + document.add_referrals.sender_email.value;
		get_string += "&refer_state=" + document.add_referrals.refer_state.value;
		if( document.add_referrals.e1.value != "" )
		{
				get_string += "&e1=" + document.add_referrals.e1.value;
		}
		if( document.add_referrals.e2.value != "" )
		{
				get_string += "&e2=" + document.add_referrals.e2.value;
		}
		if( document.add_referrals.e3.value != "" )
		{
				get_string += "&e3=" + document.add_referrals.e3.value;
		}
		if( document.add_referrals.e4.value != "" )
		{
				get_string += "&e4=" + document.add_referrals.e4.value;
		}
		if( document.add_referrals.e5.value != "" )
		{
				get_string += "&e5=" + document.add_referrals.e5.value;
		}		
		get_string += "&comment=" + document.add_referrals.comment.value;
		makeRequest('lib/gadget_refer_a_friend.php' + get_string);
}