// Local app variables
var current = 1;
var total = 0;
var elements = new Array('name','email','address1','city','state,','zip','phone','quantity');
var errorFlag = false;

/************************
>>>>>> Initial run <<<<<<
*************************/
$(document).ready(
	function() {
		// Add one block by default
		//addBlock();
    }
);


/*************************
>>>> Custom functions <<<<
**************************/
// Add a block to the page
function addBlock() {
	// Call a PHP script to get the contents of names.txt
	$.get("getNames.php", function(data) {
		lines = data.split("|");  // split into lines
		
		// Initialize output
		var output = '<div id="' + current + '"><select name="item' + current + '" class="Wide">';

		// Populate drop down with all names from names.txt file
		for(var i=0; i<lines.length-1; i++)
			output += '<option value="' + lines[i] + '">' + lines[i] + '</option>';
	
		// Finish up output var
		output += '</select><br /><input type="text" name="quantity' + current + '" id="quantity' + current + '" style="width: 30px;" /> Quantity <a href="javascript:removeBlock(\'' + current + '\')" style="margin-left: 20px;">Remove</a> <br /><br /></div>';
		
		// Add output to #Items div
		$("#Items").append( output );
		
		elements.push('quantity' + current);
		
		current++;	// Increment current var
		total++;

	});
}


// Remove a block with the id of "block"
function removeBlock( block ) {

	if( total == 1 ) {
		$("#Errors").show().html("Well you gotta buy <i>something</i>!");
	} else {
		$("#" + block).remove();
		total--;
	}
	
}


// Validate the user form...just easier...
function checkForm() {
	errorFlag = false;
	
	for(var i=0; i<elements.length; i++) {
		
		// Check all required fields for values
		if( $("#" + elements[i]).attr("value") == '' ) {
			if( elements[i] == 'address1' )
				$("#address2").css("border","1px solid red");
				
			$("#" + elements[i]).css("border","1px solid red");
			errorFlag = true;
		} else {
			$("#" + elements[i]).css("border","1px solid #ddd9cf");
			
			if( elements[i] == 'address1' )
				$("#address2").css("border","1px solid #ddd9cf");
		}
	}

	if( !echeck( $("#email").attr("value") ) ) {
		$("#email").css("border","1px solid red");
		errorFlag = true;
	}



	if( errorFlag )
		$("#Errors").show().html("<b>There was a problem with your submission</b><br />Fix the errors in red and try again!");
	else {
		$("#Errors").hide();
		$("form:first").submit();
	}
		
		
	
}


// Validate e-mail
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
}



/*****************************
>>>> Shopping cart <<<<<<<<<<
******************************/
function removeShoppingItem( block ) {
	// Do fancy close of the shopping item div
	$("#SI_" + block).fadeOut("normal", function() { $(this).empty(); });
}
