// JavaScript Document

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
//alert("inside");
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function validate(){
		if(TextValidate(document.frmlogin.txtusername,"the User Name")==false){ return false;}
		if(TextValidate(document.frmlogin.txtpassword,"the Password")==false){ return false;}
		document.frmlogin.submit();
}
function textfocus(){
		document.frmlogin.txtusername.focus();
		return false;
}
function Remove_Spaces_Editor(content,id,msg)
{
	//alert("fi");
  //alert("content="content);alert("msg="msg);
  //content = content.replace("<p>&nbsp;</p><p>&nbsp;</p>", "");
  content = content.replace("<p>&nbsp;</p>", "");
  content = content.replace(/&nbsp;/g, "");
  content = content.replace("<p></p>", "");
  content = content.replace("<br />", "");
  content = content.replace("<p></p>", "");
  content = content.replace("<br>", "");
  content = content.replace(" ", "");
  content = content.replace("<p></p>", "");
  //alert("."+content+".");
  if (content == ""){
	  alert(msg);
	  //id.selection.selectNode(inst.getBody(), true, true);
	  id.contentWindow.focus();
	  return false;
  }
 }
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}
function PriceValidate(txtCtrl, val) 
{
	var ctrName = val;
	if (trim(txtCtrl.value) == "")
	{
		alert("Please enter " + ctrName);
		txtCtrl.focus();	
		return false;		
	}
	var iChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@#$%^&*()+-=[]\\\;/{}|\"<>?`~,:'";
	for (var i = 0; i < txtCtrl.value.length; i++) {
		if (iChars.indexOf(txtCtrl.value.charAt(i)) != -1) {
			//alert ("The "+ctrName+" has special characters. \nThese are not allowed.\n Please remove them.");
			alert ("Please enter "+ctrName);
			txtCtrl.select();
			txtCtrl.focus();
			return false;
		}
 	 }	
}
function PhoneValidate(txtCtrl, val) 
{
	var ctrName = val;
	if (trim(txtCtrl.value) == "")
	{
		alert("Please enter " + ctrName);
		txtCtrl.focus();	
		return false;		
	}
	var iChars = "@#$%^&*+=[]\\\;/{}|\"<>?`~,:'";
	for (var i = 0; i < txtCtrl.value.length; i++) {
		if (iChars.indexOf(txtCtrl.value.charAt(i)) != -1) {
			alert ("Please enter your phone number with out special characters");
			//alert ("Please enter "+ctrName);
			txtCtrl.select();
			txtCtrl.focus();
			return false;
		}
 	 }	
}
function SpecialValidate(txtCtrl, val) 
{
	var ctrName = val;
	if (trim(txtCtrl.value) == "")
	{
		alert("Please enter " + ctrName);
		txtCtrl.focus();	
		return false;		
	}
	var iChars = '@#$%^*+=[]\\\;{}|\"<>?`~"';
	for (var i = 0; i < txtCtrl.value.length; i++) {
		if (iChars.indexOf(txtCtrl.value.charAt(i)) != -1) {
			//alert ("The "+ctrName+" has special characters. \nThese are not allowed.\n Please remove them.");
			alert ("Please enter "+ctrName);
			txtCtrl.select();
			txtCtrl.focus();
			return false;
		}
 	 }	
}
function changePwdvalidate()
{
	var form_name=document.frmpassword;
	if(TextValidate(form_name.admin_name,"Administrator Name")==false){return false;}
	if(TextValidate(form_name.old_password,"Old Password")==false){return false;}	
	if(SpecialValidate(frmpassword.old_password,"Old Password with out special characters")==false){return false;}
	if(TextValidate(form_name.new_password,"New Password")==false){return false;}
	if(SpecialValidate(frmpassword.new_password,"New Password with out special characters")==false){return false;}
	if(TextValidate(form_name.confirm_password,"Confirm Password")==false){return false;}
	if(SpecialValidate(frmpassword.confirm_password,"Confirm Password with out special characters")==false){return false;}
	if(Comparetextboxes(form_name.new_password,form_name.confirm_password)==false){return false;}
}
function deletenews(id)
{
	if(confirm("Are you sure to delete the news?"))
	{
		document.form1.action = "news_view.php?status=delete&news_id="+id;
		document.form1.submit();
	}
}
function deletecategory(id)
{
	if(confirm("Are you sure to delete the category?"))
	{
		document.form1.action = "category_view.php?status=delete&category_id="+id;
		document.form1.submit();
	}
}
function deletesubcategory(id)
{
	if(confirm("Are you sure to delete the subcategory?"))
	{
		document.form1.action = "subcategory_view.php?status=delete&subcategory_id="+id;
		document.form1.submit();
	}
}

function deleteseason(id)
{
	if(confirm("Are you sure to delete the result?"))
	{
		document.form1.action = "season_view.php?status=delete&season_id="+id;
		document.form1.submit();
	}
}

function deletegallery(id)
{
	if(confirm("Are you sure to delete the image?"))
	{
		document.form1.action = "gallery_view.php?status=delete&gallery_id="+id;
		document.form1.submit();
	}
}
function defaultfocus()
{
	document.frmlogin.txtusername.focus();
}
function validatenews()
{
	if(trim(document.frmnews.news_title.value) == "")
	{
		alert("Please enter the news title");
		document.frmnews.news_title.focus();
		return false;
	}
	if(SpecialValidate(frmnews.news_title,"news title with out special characters")==false){return false;}
	if(trim(document.frmnews.news_date.value) == "")
	{
		alert("Please select a news date");
		document.frmnews.news_date.focus();
		return false;
	}
	if(trim(document.frmnews.news_text.value) == "")
	{
		alert("Please enter the news description");
		document.frmnews.news_title.focus();
		return false;
	}
 	document.frmnews.action = "news.php";
	document.frmnews.submit();
}
function validatecategory()
{
	if(trim(document.frmcategory.category_name.value) == "")
	{
		alert("Please enter the category name");
		document.frmcategory.category_name.focus();
		return false;
	}
	if(SpecialValidate(frmcategory.category_name,"category name with out special characters")==false){return false;}
 	document.frmcategory.action = "category.php";
	document.frmcategory.submit();
}
function validatesubcategory()
{
	if(trim(document.frmsubcategory.subcategory_name.value) == "")
	{
		alert("Please enter the subcategory name");
		document.frmsubcategory.subcategory_name.focus();
		return false;
	}
	if(SpecialValidate(frmsubcategory.subcategory_name,"subcategory name with out special characters")==false){return false;}
	if(trim(document.frmsubcategory.category_id.value) == 0)
	{
		alert("Please select a category name");
		document.frmsubcategory.category_id.focus();
		return false;
	}
	if(trim(document.frmsubcategory.category_id.value) == trim(document.getElementById("hidd_cat_year_id").value)) {
		if(trim(document.frmsubcategory.subcategory_year.value) == "")
		{
			alert("Please select a year");
			document.frmsubcategory.subcategory_year.focus();
			return false;
		}
	}
 	document.frmsubcategory.action = "subcategory.php";
	document.frmsubcategory.submit();
}

function onChangeCat(val) {
	if(trim(val) == trim(document.getElementById("hidd_cat_year_id").value)) {
		document.getElementById("yeartr").style.display = '';		
	}else {
		document.getElementById("subcategory_year").value="";
		document.getElementById("yeartr").style.display = 'none';		
	}
}

function validategallery()
{
	if(document.frmgallery.gallery_category.value == 0)
	{
		alert("Please select a category from the list");
		document.frmgallery.gallery_category.focus();
		return false;
	}	
	
	if(document.getElementById("gallery_subcategory")) {
		if(document.getElementById("gallery_subcategory").value == 0)	{
			alert("Please select a sub category from the list");
			document.getElementById("gallery_subcategory").focus();
			return false;
		}
		
	}
	
	
	if(trim(document.frmgallery.gallery_image.value)=="" && document.frmgallery.old_image.value == "")
	{
		alert("Please browse an image");
		document.frmgallery.gallery_image.focus();
		return false;
	}
	if(trim(document.frmgallery.gallery_image.value)!="")
	{
		var file_type = document.frmgallery.gallery_image.value.split(".");
		var final = file_type.length;
		//alert(file_type[final-1]);
		//return false;
		if(file_type[final-1]!="jpg" && file_type[final-1]!="gif" && file_type[final-1]!="jpeg" && file_type[final-1]!="JPG" && file_type[final-1]!="GIF")
		{
			alert("Please upload JPEG or GIF images");
			return false;
		}
	}
	if(document.getElementById("gallery_comment").value!="")
	{
		if(document.getElementById("gallery_comment").value.length > 100)
		{
			alert("Please enter comment less than 100 characters");
			return false;
		}
	}
	document.frmgallery.action = "gallery.php";
	document.frmgallery.submit();
}
function validateseason()
{
	var urlregex = new RegExp("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
	if(trim(document.frmseason.season_title.value) == "")
	{
		alert("Please enter the title");
		document.frmseason.season_title.focus();
		return false;
	}
	if(SpecialValidate(frmseason.season_title,"title with out special characters")==false){return false;}
	if(trim(document.frmseason.season_date.value) == "")
	{
		alert("Please enter the date");
		document.frmseason.season_date.focus();
		return false;
	}	
	if(trim(document.frmseason.season_placement.value) != "")
	{
		if(SpecialValidate(frmseason.season_placement,"placement with out special characters")==false){return false;}
	}
	if(trim(document.frmseason.season_link.value) != "")
	{
		if(!urlregex.test(document.frmseason.season_link.value))
		{
			alert("Please enter a valid link");	
			document.frmseason.season_link.focus();
			return false;
		}
	}
	document.frmseason.action = "season.php";
	document.frmseason.submit();
} 
function validatearchive()
{
	if(document.frmnews.news_archive.checked == true)
	{
		document.frmnews.news_archive_hidden.value = 1;	
	}else{
		document.frmnews.news_archive_hidden.value = 0;	
	}
}
