//////////////////////////////////////////////////////////////////////////////////////
//                                                                      			//
//  Copyright (c) 2009 Wilfred Wong <wilfred.wong@mamidadi.com>  					//
//  All rights reserved.                                                			//
//                                                                      			//
//  Implementation of a standard form validation script								//
//  Documentation and explanation found at:											//
//	http://www.mamidadi.com/readings/simple-javascript-form-validation-function		//
//                                                                      			//
//////////////////////////////////////////////////////////////////////////////////////

function validateForm()
{
	// FUNCTION CUSTOMIZATION
	var errorBackgroundColor = "lightgray";
	
	// get the standard elements
	var thisform = document.getElementById("validateform");
	var saveButton = document.getElementById("save");
	
	// setup some internal variables
	var stopSubmit = false;
	var emailpattern = "^\s*\w+@\w+\.\w+\s*$";
	
	// reset some fields
	saveButton.disabled = false;
	
	for(var i = 0; i < thisform.elements.length; i++) 
	{
		 var e = thisform.elements[i];  // the element we're working on
		 
		 // make sure to avoid the submit button
		 if (e.type != "submit")
		 {
		  	// reset background color
			e.style.backgroundColor = "";
			
		 	var cls = e.getAttribute("validate");
			
			// validate required field
			if (cls.indexOf("required") > -1)
			{
				if (e.value == "")
				{
					e.style.backgroundColor = errorBackgroundColor;
					stopSubmit = true;
				}	
			}
			
			// validate email field
			if (cls.indexOf("email") > -1)
			{				
				if (e.value.search(emailpattern) == -1)
				{
					e.style.backgroundColor = errorBackgroundColor;
					stopSubmit = true;
				}	
			}
		 }
	}
	
	if (stopSubmit == true)
	{
		saveButton.disabled = true;
	}
}


function trackImpression(categoryId)
{
	var site = "http://127.0.0.1:8080/mamidadi/";
	
	// Ajax to DB to record link clicking
	$.ajax
	( 
        { 
            type: "POST", 
            url: site + "processors/process_directoryimpression.php", 
            data: "CategoryId=" + categoryId, 
            cache: false, 
		}
	)	
}

function trackImpressionLink(linkId)
{
	// Ajax to DB to record link clicking
	$.ajax
	( 
        { 
            type: "POST", 
            url: site + "processors/process_linkimpression.php", 
            data: "LinkId=" + linkId, 
            cache: false, 
		}
	)	
}

function trackLink(linkId)
{
	// Ajax to DB to record link clicking
	$.ajax
	( 
        { 
            type: "POST", 
            url: site + "processors/process_directoryclick.php", 
            data: "LinkId=" + linkId, 
            cache: false, 
		}
	)	
	
}
