
function ValidateForm( p_evt ) {

 //  save tinyMCE content (if exists)
  try { tinyMCE.triggerSave(); } catch (e) {}

  //  get required fields
  var form = p_evt.element();
  var req_fields = $F(form['ReqFields']).split(',');
  //  make sure all required fields have a value
  var msg = 'All fields must have a value.  You are missing:';
  for ( var i = 0; i < req_fields.length; i++ ) {
    var field = form[req_fields[i]];
	 
    if ( $F(field).blank() ) {
      msg += "\n\t"+req_fields[i];
    }
  }

  if ( msg != 'All fields must have a value.  You are missing:') {
    alert(msg);
    if ( p_evt && p_evt.preventDefault ) {
      p_evt.preventDefault();
    }
    return false;
  }

	
	
  //  if we go here, then all fields that were required had a value
  //  now, check to make sure all fields are validated properly
  var msg = 'You entered some information incorrectly.';
  var val_fields = $F(form['ValidateFields']).split(',');
  var email_exp = new RegExp("^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$");
  var website_exp = new RegExp("(http:\/\/)*$");
  var pwd_exp = new RegExp("\\S{6,16}");
  
  for ( var i = 0; i < val_fields.length; i++ ) {
	 
    //  get info on field and against what to validate
    var info = val_fields[i].split('|');
    switch ( info[0] ) {
		 
      case 'Email':
        if ( !email_exp.test($F(form[info[1]])) ) {
          msg += "\n\t"+info[1]+' is not a valid address.';
        }
        break;
      case 'Password':
        if ( !pwd_exp.test($F(form[info[1]])) ) {
          msg += "\n\t"+info[1]+' needs to be between 6 and 16 characters long.  It cannot contain spaces.';
        }
        break;
		case 'Website':
				
				if($F(form[info[1]])=="")
				{
				 var result=true;	
				}
				else
				{
					var result=isUrl($F(form[info[1]]));
				}
				
				if (result==false)
				{
					msg += "\n\tYou must add Http:// to your URL for it to upload.";		
				}
											
			/*if ( !website_exp.test($F(form[info[1]])) ) {
          msg += "\n\t"+info[1]+' is not proper.';
        }*/
		  break;
		case 'VideoHtml':
				
				if($F(form[info[1]])=="")
				{
				 var result_url=true;	
				}
				else
				{
					var result_url=isURLOld($F(form[info[1]]));
				}
				
				if (result_url==false)
				{
					msg += "\n\tOnly URL allow to Upload and You must add Http:// to your URL for it to upload.";		
				}
											
			/*if ( !website_exp.test($F(form[info[1]])) ) {
          msg += "\n\t"+info[1]+' is not proper.';
        }*/
		  break;
      case 'Age14':
        var err_msg = "\n\tThat date you entered makes you younger than 14 years old.";
        //  we should have three fields passed in in a colon-delimited string
        //  they need to be in the order of M/D/Y
        var age_fields = info[1].split(':');
        //  get today's information
        var today = new Date();
        var year = today.getFullYear() - 14;
        var month = today.getMonth();
        var day = today.getDay();
        //  make sure date entered makes the person 14 or older
        if ( $F(form[age_fields[2]]) > year ) {
          msg += err_msg;
        }
        else if ( $F(form[age_fields[2]]) == year ) {
          if ( $F(form[age_fields[0]]) > month ) {
            msg += err_msg;
          }
          else if ( $F(form[age_fields[0]]) == month ) {
            if ( $F(form[age_fields[1]]) > day ) {
              msg += err_msg;
            }
          }
        }
        break;
    }
  }
  //alert(msg);
  if ( msg != 'You entered some information incorrectly.') {
    alert(msg);
    if ( p_evt && p_evt.preventDefault ) {
      p_evt.preventDefault();
    }
    return false;
  }
  
  /*CAEGORY AND SUBCATEGORY VALIDATION CODE BEGIN*/
	var msg="Select at least one ";
	var select_field=$F(form['SelectFields']).split(',');
	
	select_cnt=select_field.length;
	
	if(select_cnt==2)
	{
		category=select_field[0];
		subcategory=select_field[1];
		cat_length=document.frmRecipeSubmit.cat.length;
		
		var flag=0;
		for (var i=0;i<cat_length;i++)
			{
				if(document.frmRecipeSubmit.cat[i].checked==true)
				{
				 	flag=1;
					subcat_length=document.frmRecipeSubmit.subcat.length;					
					//alert('Sub Cat:'+subcat_length);
					break;
				}				
			}
			
			if(flag==1)
			{
			 	for (var j=0;j<subcat_length;j++)
					{						
						if(document.frmRecipeSubmit.subcat[j].checked==true)
						{
							flag=2;
							//alert('Sub Cat1:'+subcat_length);
							//alert('Flag:'+flag);
							break;
						}
					}	
			}
			//alert(flag);
		if(flag==0)
		{
		  msg+="\nCategory\nSub Category";	
		}
		else if(flag==1)
		{
		 	msg+="\nSub Category\nFor giveaway recipes, please add recipe to more than one category and one subcategory.";
		}
		
		/*if($F(category)==null)
			{
			 msg+="\nCategory\nSub Category";
			}
		else
			{
				if($F(subcategory)==null)
					{
				 		msg+="\nSub Category";
					}
			}		*/
	}
	
	
	if ( msg != 'Select at least one ') {
    alert(msg);
    if ( p_evt && p_evt.preventDefault ) {
      p_evt.preventDefault();
    }
    return false;
  }
  
  
	/*CAEGORY AND SUBCATEGORY VALIDATION CODE END */	 
	

  //  if we go here, then all fields that were required had a value
  //  and all fields validated with appropriate values
  //  now, check to make sure fields that need to match do
  var msg = '';
  var match_fields = $F(form['MatchFields']).split(',');
  for ( var i = 0; i < match_fields.length; i++ ) {
    //  get info on field and against what to validate
    var info = match_fields[i].split('|');
    if ( $F(form[info[0]]) != $F(form[info[1]]) ) {
      msg += "\n"+info[0]+"s did not match.";
    }
  }
  if ( msg != '' ) {
    alert(msg);
    if ( p_evt && p_evt.preventDefault ) {
      p_evt.preventDefault();
    }
    return false;
  }
  
  
  return true;
}


function ValidateEditRecipe()
{
 //alert('hi');
 var req_fields = document.frmRecipeSubmit.ReqFields.value;
 //alert(req_fields);
 var req_fields_array=Array();
 req_fields_array=req_fields.split(',');
 //alert(req_fields_array[0]);
  //  make sure all required fields have a value
  var msg = 'All fields must have a value.  You are missing:';
  for ( var i = 0; i < req_fields_array.length; i++ ) {
    var field = document.getElementById(req_fields_array[i]).value;
	// alert(field);
    if ( field.blank() ) {
      msg += "\n\t"+req_fields_array[i];
    }
  }
  if ( msg != 'All fields must have a value.  You are missing:') {
    alert(msg);    
    return false;
  }
  var msg = 'You entered some information incorrectly.';
 //var val_fields = $F(form['ValidateFields']).split(',');
  var website_exp = new RegExp("(http:\/\/)*$");
  var website=document.getElementById('WebsiteCredit').value;
	//alert(website);			
 if(website.blank())	{
	 	var result=true;	
	}
 else	{
		var result=isUrl(website);
	}
	
 if (result==false)	{
		msg += "\n\tYou must add Http:// to your URL for it to upload.";		
	}
											
			/*if ( !website_exp.test($F(form[info[1]])) ) {
          msg += "\n\t"+info[1]+' is not proper.';
        }*/
	/*var video_html=document.getElementById('VideoHtml').value;
	if(video_html.blank())	{
	 var result_url=true;	
	}
	else{
		var result_url=isURLOld(video_html);
	}

	if (result_url==false)	{
		msg += "\n\tOnly URL allow to Upload and You must add Http:// to your URL for it to upload.";		
	}	*/  
      
  //alert(msg);
  if ( msg != 'You entered some information incorrectly.') {
    alert(msg);
    return false;
  }
  
  /*CAEGORY AND SUBCATEGORY VALIDATION CODE BEGIN*/
	var msg="Select at least one ";
	cat_length=document.frmRecipeSubmit.cat.length;
		
	var flag=0;
	for (var i=0;i<cat_length;i++){
		if(document.frmRecipeSubmit.cat[i].checked==true) {
			flag=1;
			subcat_length=document.frmRecipeSubmit.subcat.length;					
			//alert('Sub Cat:'+subcat_length);
			break;
		}				
	}
		
	if(flag==1)	{
		for (var j=0;j<subcat_length;j++){						
			if(document.frmRecipeSubmit.subcat[j].checked==true){
				flag=2;				
				break;
			}
		}	
	}
		//alert(flag);
	if(flag==0)	{
	  msg+="\nCategory\nSub Category";	
	}
	else if(flag==1)	{
		msg+="\nSub Category";
	}

	if ( msg != 'Select at least one ') {
    alert(msg);    
    return false;
  }
  else  {
	 return true;
  }
  /*CAEGORY AND SUBCATEGORY VALIDATION CODE END */	 
	 
}

function isUrl(s) {
var regexp = /(http):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return regexp.test(s);
}

function isURLOld(argvalue) {

  if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf("http://") == -1)
    return false;
  else if (argvalue == "http://")
    return false;
  else if (argvalue.indexOf("http://") > 0)
    return false;

  argvalue = argvalue.substring(7, argvalue.length);
  if (argvalue.indexOf(".") == -1)
    return false;
  else if (argvalue.indexOf(".") == 0)
    return false;
  else if (argvalue.charAt(argvalue.length - 1) == ".")
    return false;

  if (argvalue.indexOf("/") != -1) {
    argvalue = argvalue.substring(0, argvalue.indexOf("/"));
    if (argvalue.charAt(argvalue.length - 1) == ".")
      return false;
  }

  if (argvalue.indexOf(":") != -1) {
    if (argvalue.indexOf(":") == (argvalue.length - 1))
      return false;
    else if (argvalue.charAt(argvalue.indexOf(":") + 1) == ".")
      return false;
    argvalue = argvalue.substring(0, argvalue.indexOf(":"));
    if (argvalue.charAt(argvalue.length - 1) == ".")
      return false;
  }

  return true;

}
/*function addElement(divId) {
  var myDiv = divId;
  var ni = document.getElementById('myDiv_'+divId);
  var numi = document.getElementById('tipNo_'+divId);
  var num = (document.getElementById('tipNo_'+divId).value -1)+ 2;
  numi.value = num;
  var newdiv = document.createElement('div');
  var divIdName = 'myDiv_'+divId+'_'+num;
  var textIdName = 'Tips_'+divId+'_'+num;
  newdiv.setAttribute('id',divIdName);
  //newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';
  newdiv.innerHTML = "<textarea id="+textIdName+" name=" +textIdName+ "></textarea>";
  ni.appendChild(newdiv);
}*/

function addStep(divId, textId, stepNo) {

  var ni = document.getElementById(divId);
  var numi = document.getElementById(stepNo);
  var num = (document.getElementById(stepNo).value -1)+ 2;
  numi.value = num;

  var newdiv = document.createElement('div');
  var divIdName = 'divId'+'_'+num;
  var textIdName = textId+'[]';
  newdiv.setAttribute('id',divIdName);
  //newdiv.innerHTML = "<input type=text id="+textIdName+" maxlength=255 style=width:450px; name=" +textIdName+ " />";
  newdiv.innerHTML = "<textarea name="+textIdName+" cols=63  rows=1 id="+textIdName+" ></textarea>"
  ni.appendChild(newdiv);
}

function addTip() {

  var ni = document.getElementById('addTipDiv');
  var numi = document.getElementById('tipNo');
  var num = (document.getElementById('tipNo').value -1)+ 2;
  numi.value = num;
  var newdiv = document.createElement('div');
  var divIdName = 'addTipDiv'+'_'+num;
  var textIdName = 'Tips_'+num;
  newdiv.setAttribute('id',divIdName);
  //newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';
  newdiv.innerHTML = "<input type=text id="+textIdName+" maxlength=255 style=width:450px; name=" +textIdName+ " />";
  ni.appendChild(newdiv);
}
function removeElement(divNum) {
  var d = document.getElementById('myDiv');
  var olddiv = document.getElementById(divNum);
  d.removeChild(olddiv);
}

function showHide(divId) {
	var eleDiv = document.getElementById(divId);
	if (eleDiv.style.display == 'none') {
		eleDiv.style.display = 'inline';
	} else if (eleDiv.style.display == 'inline') {
		eleDiv.style.display = 'none';
	}
}

//common function to check and uncheck check box....
function alterCheckBox(frm, checkboxid, obj)
{
	var count = frm.elements.length;
	for(i=1;i<count;i++)
	if(frm.elements[i].type=="checkbox" && frm.elements[i].id==checkboxid)
	frm.elements[i].checked=obj.checked;

}

//check wheather any record is seleced bfore performing action
function checkselect(frm,checkboxid,obj,name)
{
// 	alert(frm);
// 	alert(checkboxid);
// 	alert(obj);
// 	alert(name);
	var count = frm.elements.length;
	for(i=1;i<count;i++)
	{
		if(frm.elements[i].type=="checkbox" && frm.elements[i].id==checkboxid)
		{
			//alert(frm.elements[i].checked);
			if(frm.elements[i].checked)
			{
				if(obj.name == 'Delete')
				{
					if(confirm("Are you sure you want to delete ?"))
					{return true;}
					else
					{return false;}
				}
				else
				{return true;}
			}
		}
	}
	if(obj.name == 'Delete')
	{
		alert("Please select "+ name +" to "+ obj.name);
	}
	else
	{
		alert("Please select "+ name +" to make "+ obj.name);
	}

	return 	false;
}

function trim(Value)
{
	return Value.replace(/^\s+|\s+$/g, "");
}


function checkForm(frm,checkboxid,obj,name,textBoxId) {

	var eleText = document.getElementById(textBoxId);
	if(trim(eleText.value) =='') {
		alert('Email is Required');
		eleText.focus();
		return false;
	} else {
		var email_exp = new RegExp("^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$");
		if ( !email_exp.test(trim(eleText.value)) ) {
         	 	alert('Please Enter a Valid Email Address');
			eleText.focus();
			return false;
		} else {
			return checkselect(frm,checkboxid,obj,name);
		}
        }
}



function CheckAll()
{
 //alert('hi');
 
 var count = document.frmRecipeSubmit.cat.length;
	for (i=0;i<count;i++) {
		document.frmRecipeSubmit.cat[i].checked=true;
		}
}

function ValidateMentorSearch() {
	 /*CAEGORY AND SUBCATEGORY VALIDATION CODE BEGIN*/
	var msg="Select at least one ";
	cat_length=document.frmSearchMentor.cat.length;
		
	var flag=0;
	for (var i=0;i<cat_length;i++){
		if(document.frmSearchMentor.cat[i].checked==true) {
			flag=1;
			subcat_length=document.frmSearchMentor.subcat.length;					
			//alert('Sub Cat:'+subcat_length);
			break;
		}				
	}
		
	if(flag==1)	{
		for (var j=0;j<subcat_length;j++){						
			if(document.frmSearchMentor.subcat[j].checked==true){
				flag=2;				
				break;
			}
		}	
	}
		//alert(flag);
 	if(flag==0)	{
// 	  msg+="\nCategory\nSub Category";	
 	}
	else if(flag==1)	{
		msg+="\nSubcategory";
	}

	if ( msg != 'Select at least one ') {
    alert(msg);    
    return false;
  }
  else  {
	 return true;
  }
  /*CAEGORY AND SUBCATEGORY VALIDATION CODE END */	

}
