// TO DO: - make sure e-mail address does not contain spaces

function validate(form){
	//var aErrors = $(".errorMsg");
	//alert("num previous errors:" + aErrors.length);	
	$(".errorMsg").remove();
	$(":input").removeClass('errorField');	
	
	var stReturn = validateForm(form);
	if (stReturn.success == false){
		var aErrors = stReturn.errors;
		for (var fieldname in stReturn.errors){
			var fieldErrors = stReturn.errors[fieldname];
			for (var i=0; i < fieldErrors.length; i++){
				//alert(fieldErrors[i]);
				var errMsg = fieldErrors[i];
				var field = document.getElementById(fieldname);
				var errorImg=document.createElement('img');
				var errorSpan=document.createElement('span');
				//errorImg.src = "js/jtip/images/error.gif";
				errorImg.src = errorUrl;
				errorSpan.className = "errorMsg";
				
				// need to either remove the span if it already exists, or rewrite the text node so you don't get duplicates when form is submitted multiple times
				
				errorSpan.appendChild(errorImg);
				errorSpan.appendChild(document.createTextNode(" " + errMsg));
				//field.parentNode.appendChild(errorImg); 
				field.parentNode.appendChild(errorSpan);
				field.className = "errorField";				
			}
		}	
		return false;
	}	
	
	return true;
}

function getDisplayName(fieldname, stCurrField){
	var displayname = fieldname;
	if(stCurrField["displayname"] != undefined){
		displayname = stCurrField["displayname"];
	}
	return displayname;
}

function validateForm(form){
	var stReturn = new Object();
	stReturn.success = true;
	stReturn.errors = new Object();

	for(var fieldname in objValRules){		
		if(form[fieldname] != undefined){
			var data = form[fieldname].value;
			var obj=form[fieldname];
			var stCurrField = objValRules[fieldname];
			var displayname = getDisplayName(fieldname, stCurrField);
			var aErrors = new Array();
			if(stCurrField['req'] != undefined){
				if(stCurrField['req'] == 'true' && data == ''){
					if(stCurrField['errmsg'] == undefined){
						error = "The field '" + displayname + "'cannot be left blank.";
					}else{
						error = stCurrField['errmsg'];
					}
					aErrors.push(error);
					
				}
				if(stCurrField['zip'] == 'true' && data != ''){
					if (data.length != 5) {
					error = "Please enter a 5 digit'" + displayname + "'.";
					form[fieldname].focus();
					aErrors.push(error);
					}
					
					
				}
			}
			if(stCurrField['datatype'] != undefined){
				datatype = stCurrField.datatype;
				var stValid = validateDataType(displayname, datatype, data, obj);
				if(stValid.success == false){
					aErrors.push(stValid.error);					
				}
			}
				
			if(stCurrField['match'] != undefined){
				var fieldtomatch = stCurrField['match'];
				var datamatch = form[fieldtomatch];
				if(datamatch != '' && data != datamatch){ 
					error = "The field '" + displayname + "' must match the value of field '" + fieldtomatch + "'";
					aErrors.push(error);
				}
			}
			
			if(stCurrField['pattern'] != undefined){
				var pattern = stCurrField['pattern'];
				if(datamatch != '' && data != datamatch){ 
					error = "The field '" + displayname + "' must match the value of field '" + fieldtomatch + "'";
					aErrors.push(error);
				}
			}
		}
		
		if(aErrors.length){
			stReturn.errors[fieldname] = aErrors;
			stReturn.success = false;
		}
	}	

	return stReturn;
}

function validateDataType(fieldName, fieldtype, fielddata, dataObje){
	var stReturn = new Object();
	
	switch(fieldtype){
		case 'email':
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   			if(reg.test(fielddata) == false) {
      		stReturn.error="Invalid Email Address";
      		stReturn.success = false;
   			}
			break;
		case 'productid':
			if (dataObje.value == "") {
			stReturn.error="Please choose a product.";
      		dataObje.focus();
			stReturn.success = false;
			}

			break;
		case 'age':	
			stReturn.success=checkAge(fieldName);
			break;
		}
	if(stReturn['error'] != undefined){
		stReturn.success = false;
	}

 	return stReturn;
}

function listfindnocase(list,value,delimiters) {	
	if(!delimiters){var delimiters=',';}	
	list = list.toUpperCase();	
	value = value.toUpperCase();
	_TempListSplitArray = list.split(delimiters);
	FoundIdx = 0;	
	for(var i=0;i<_TempListSplitArray.length;i++){
		if(_TempListSplitArray[i]==value){
			FoundIdx= i+1;
			break;
		}
	}
	return FoundIdx;
}
function checkAge(path)	{
			/* the minumum age you want to allow in */
			var min_age = 13;

			/* change "age_form" to whatever your form has for a name="..." */
			var year = parseInt(document.forms["complaintForm"]["yob"].value);
			var month = parseInt(document.forms["complaintForm"]["mob"].value) - 1;
			var day = parseInt(document.forms["complaintForm"]["dob"].value);
			var theirDate = new Date((year + min_age), month, day);
			var today = new Date;
			if ( (today.getTime() - theirDate.getTime()) < 0) {
				SetCookie( 'under13', 'yes',1);
				window.location = path;
				return false;
			}
			else {
				return true;
			}
	}
function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();
 }
function contactUsAgeVerification(uPath) {
	
	var flag=Get_Cookie('under13');
	if(flag=='yes') {
	window.location = uPath;
	}
}
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
$(function(){
	/*$("#faqs h5").hover(
	  function () {
	    $(this).css('color','#ca6820');
	  }, 
	  function () {
	     $(this).css('color','#FF6600');
	  }
	  )*/
	$("#faqs h5").click(function(){
		if($(this).children().hasClass('show'))	{ 
				$(this).next().css('display','block');
				$(this).children().removeClass('show');
				$(this).children().addClass('hide');
				$(this).children().text('Hide'); 
			} else { 
				$(this).next().css('display','none');
				$(this).children().removeClass('hide');
				$(this).children().addClass('show');
				$(this).children().text('Show');  
				}
		});
	});


