$(document).ready(function() {    
    $("#submitbtn").click(function(){
        $("#errors").remove();
        
        if(checkForm()){
            $.ajax({
                type: "post",
                url: '_includes/handleData.php',
                data: $("form").serialize(),
                success: function(msg){
                    if(msg == 'true'){
                    	var source = $("input[name='Source_Code']").val();
                        window.location = "http://www.motorolarfidkit.com/2819/thankyou.php?source="+ source;
                	}
                    else {
                        $("p.req-note").before("<div id='errors'>"+ msg +"</div>");
                    }
                }
            });
        }
        
        return false;
    });
    
    $("#submitemail").click(function() {
        $("#errors").remove();

        if(checkForm()){
            $.ajax({
                type: "post",
                url: '_includes/handleEmail.php',
                data: $("form").serialize(),
                success: function(msg){
                    if(msg == 'true'){
                    	$("#sharewithcolleague").fadeOut("slow");
                    	$("#shareform input").val();
                	}
                    else {
                        $("p.req-note").before("<div id='errors'>"+ msg +"</div>");
                        return false;
                    }
                }
            });
        }
    });
    
    $("#sharelink").click(function() {
        $("#sharewithcolleague").fadeIn("slow");
    });
    
    $("#closeshare").click(function() {
        $("#sharewithcolleague").fadeOut("slow");
    });
});
    



function trim( str ) {
	return str.replace(/^\s+|\s+$/g, '') ;
}

function clearElement( e ) {
	while ( e.hasChildNodes() ) {
		e.removeChild( e.fistChild );
	}
}


function checkForm()
{
	// get all label tags
	var requiredLabelsArray = document.getElementsByTagName( 'label' );
	var error = false;

	// loop through labels
	for ( var i=0; i<requiredLabelsArray.length; i++ ) {
		var currentLabel = requiredLabelsArray[i];

		// see if the 'required' class is applied
		if ( currentLabel.className.search( /required/ig ) != -1 ) {

			// get the field the label refers to (the 'for' attribute specifies the id of the field...IE calls it 'htmlFor')
			var field = document.getElementById( currentLabel.getAttribute('for') ? currentLabel.getAttribute('for') : currentLabel.getAttribute('htmlFor') );

			if ( field ) {			
				// check type of input
				var fieldType = field.getAttribute( 'type' );

				if ( fieldType ) {
					fieldType = fieldType.toLowerCase();
				} else {
					fieldType = field.tagName.toLowerCase();
				}
					
				if ( fieldType == 'select-one' )  {
					fieldType = 'select';
				}
				
				//alert(' field type ' + fieldType + ' and value ' + field.value );
				if ( ( fieldType == 'select' && ( field.value == '0' || field.value == '' ) )  ||
					 ( fieldType == 'text' && trim( field.value ) == '' ) ) {
							currentLabel.className += ' headerColor';
						//	msg += '<br />- ' + currentLabel.firstChild.nodeValue;
							error = true;
				} else {
					currentLabel.className = currentLabel.className.replace( /headerColor/ig, '' );
				}
			}
		}
	}
	

	var infoSpan = document.getElementById( 'requiredMsg' );
	
	// show or hide the error message
	if ( infoSpan ) {
		infoSpan.style.display = error ? 'block' : 'none';
	}
	
	// scroll to error message
	if ( error ) {
		window.location.hash = "requiredMsg";
	}

	return !error;
}
