// JavaScript Document
/* Strip whitespace from the beginning and end of a string */
if(typeof String.prototype.trim == "undefined"){
	String.prototype.trim = function (){
		var s = this.replace(/^\s*/, "");
		return s.replace(/\s*$/, "");
	}
}

function safeNodeValue(node)
{
	if(node.firstChild)
		return node.firstChild.nodeValue;
	else
		return '';
}

function prepareParam(value)
{
	value = value.trim();
	value = value.replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">");
	value = encodeURIComponent(value);

	return value;
}

function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   }


function num(value){

	if (document.getElementById(value).value != "") {
	
			if (!IsNumeric(document.getElementById(value).value)){
			
				alert('Only numbers for the '+ value +' please. Thanks.');
			
			}
	}
}

function clearuser(){
if ($('username').value == 'username'){
$('username').value = '';
}
}

function clearpass(){
$('password').value = '';
}

var gift_item_id = "";
var descr = "";
var amount = "";
var button_id = "";


function cart_itemz (gift_item_id, descr, amount, button_id) {

$(button_id).innerHTML = '<a href="javascript: void();" onclick="add_cart(\''+gift_item_id+'\',\''+descr+'\',\''+amount+'\',\''+button_id+'\');"><img src="/images/add_cart.jpg" border="0" style="vertical-align: middle"  /></a>';

}


function add_cart(gift_item_id, descr, amount, button_id){

$('cart-container').innerHTML = 'Updating';

var url = '/ajax/cart.php';
var params = 'gift_item_id=' + prepareParam(gift_item_id) + '&descr=' + prepareParam(descr)  + '&amount=' + prepareParam(amount);
//alert(gift_item_id + ' ' + descr + ' ' + amount);
var ajax = new Ajax.Updater({success: 'cart-container'},url,{method: 'post', parameters: params, onFailure: reportError});
	
	$(button_id).innerHTML = '<img src="/images/added.jpg" border="0" style="vertical-align: middle"  />';
	
	var timer = setTimeout("cart_itemz('"+gift_item_id+"','"+descr+"','"+amount+"','"+button_id+"')",2250);
	
}


function signup2() {

	$('signup-results').innerHTML = '<img src="/images/ajax-loader.gif" style="vertical-align: middle;" /> Processing...';

var policy ="";

if ($('policy').checked == 1){
policy = $F('policy');	
} else {

policy = "";

}

var married = "";

if ($('married').checked == 1){
married = $F('married');	
} else {

married = "";

}

	var url = '/register-process/';
	var params = 'passwd=' + $F('passwd') + '&email=' + $F('email') + '&word=' + $F('word') + '&passwd2=' + $F('passwd2') + '&firstname=' + $F('firstname') + '&lastname=' + $F('lastname') + '&address=' + $F('address') + '&phone=' + $F('phone') + '&city=' + $F('city') + '&state=' + $F('state') + '&zip=' + $F('zip') + '&married=' + married + '&policy=' + policy +  '&b_day=' + $F('b_day') + '&b_month=' + $F('b_month')  + '&a_day=' + $F('a_day') + '&a_month=' + $F('a_month') + '&a_year=' + $F('a_year') + '&email2=' + $F('email2');
	
	var ajax = new Ajax.Updater(
	{success: 'signup-results'}, 
	url, 
	{method: 'post', parameters: params, onFailure: reportError});
	
}
function reportError(request) {

	$F('signup-results') = "Error";
	
}


function new_freecap()
{
	// loads new freeCap image
	if(document.getElementById)
	{
		// extract image name from image source (i.e. cut off ?randomness)
		thesrc = document.getElementById("freecap").src;
		thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
		// add ?(random) to prevent browser/isp caching
		document.getElementById("freecap").src = thesrc+"?"+Math.round(Math.random()*100000);
	} else {
		alert("Sorry, cannot autoreload freeCap image\nSubmit the form and a new freeCap will be loaded");
	}
}


function reportError(request) {

	$F('submit-results') = "Error";
	
}

function check_inv()
{

	new Ajax.Request('/ajax/check_inv.php',
	{
		method : 'post',
		parameters : 'post=true',
		onSuccess : check_inv_onSuccess
	});
	
	return false;
}
function check_inv_onSuccess(t)
{
	
	var xmlDoc = t.responseXML;
	
    var err = safeNodeValue(xmlDoc.getElementsByTagName('err')[0]);
	var error_cart = safeNodeValue(xmlDoc.getElementsByTagName('error')[0]);
	
	if (err == "err"){
	
	$('list_items').innerHTML = error_cart;
	
	return false;
	
	} else if(err == "ok"){
		
		if ($('total').value == "0.00"){	
		
	

			$('coupon_form').submit();
			
	
		
		} else {

	
			
				$('checkout_form').submit();
	
		
		}
	
	
	}
	
}

function submit_paypal2(){
	$('paypal_form').submit();
}
function submit_coupon2(){
	$('coupon_form').submit();	
}

function submit_paypal(){

if($('cd_num').value != ""){
	
	var cart_id = $('inv_item').value;
	var discount_code = $('cd_num').value;
	
	new Ajax.Request('/add-discount/',
	{
		method : 'post',
		parameters : '&discount_code=' + discount_code + '&cart_id=' + cart_id, 
		onSuccess : submit_paypal2
	});

}
	
	
}
function submit_coupon(){

if($('cd_num').value != ""){
	
	var cart_id = $('inv_item').value;
	var discount_code = $('cd_num').value;
	
	new Ajax.Request('/add-discount/',
	{
		method : 'post',
		parameters : '&discount_code=' + discount_code + '&cart_id=' + cart_id, 
		onSuccess : submit_coupon2
	});

}


}

function validate_discount(cd_num, type){

if (type == 'cc'){
	
	new Ajax.Request('/v-discount/',
	{
		method : 'post',
		parameters : '&disc_code=' + cd_num, 
		onSuccess : submit_coupon
	});

} else if(type == 'pp'){

	new Ajax.Request('/v-discount/',
	{
		method : 'post',
		parameters : '&disc_code=' + cd_num, 
		onSuccess : submit_paypal
	});
	
}

}

function validate_comment(){
var theMessage = "Please complete the following: \n-----------------------------------\n";
var noErrors = theMessage
// make sure field is not blank
if ($('comment-text').value=="") {
theMessage = theMessage + "\n --> Please enter your review";
}

function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}

var rad = valButton(document.comment_box.overall);

 if (rad == null){
	theMessage = theMessage + "\n --> Please rate the overall business"; 
 }

if (theMessage == noErrors) {

return true;

} else {

alert(theMessage);
return false;
}	
	
}