// JavaScript Document

function isValidData(val,valid,minl,maxl,fieldname,msg)
{
	if(Trim(val,' ')=="")
	{
		alert(fieldname + " is empty.");
		return false;
	}
	else if(val.length<minl || val.length>maxl)
	{
		alert(fieldname + " must be " + minl + " to " + maxl + " chars long")
		return false;
	}
	else
	{
		for (var i=0, len=val.length; i<len ; i++) 
		{
			if (valid.indexOf(val.substring(i,i+1)) == -1) 
			{
				var wrong = i*1+1;
				alert(msg + ' ' + fieldname + '. Check character no ' + wrong + '.');
				return false;
			}
		}
	}
	return true;
}

function isEmail(val)
{
	if(Trim(val,' ') == "")
	{
		alert("You must type in a mailaddress!");
		return false;
	}
	else if (!(val.indexOf('\@') > -1))
	{
		alert("Mailaddress must contain a \@");
		return false;
	}
	else if (!(val.indexOf('.') > -1))
	{
		alert("Mailaddress must contain a '.' (dot)");
		return false;
	}
	else if (val.indexOf('\@') == (val.indexOf('.')-1))
	{
		alert("Mailaddress can't contain a \@ and '.' side by side");
		return false;
	}
	else if (val.indexOf('.') == (val.length-1))
	{
		alert("Mailaddress can't contain a '.' (dot) at the end");
		return false;
	}
	return true;
}

function Trim(inputString, removeChar) 
{
	var returnString = inputString;
	if (removeChar.length)
	{
	  while(''+returnString.charAt(0)==removeChar)
		{
		  returnString=returnString.substring(1,returnString.length);
		}
		while(''+returnString.charAt(returnString.length-1)==removeChar)
	  {
	    returnString=returnString.substring(0,returnString.length-1);
	  }
	}
	return returnString;
}

function isUpFormOk(frm)
{
	if(!isEmail(frm.emailto.value))
	{
		frm.emailto.focus();
		return false;
	}
	else if(Trim(frm.upfile.value,' ')=="")
	{
		alert("Please select a file to upload.");
		frm.upfile.focus();
		return false;
	}
	else if(Trim(frm.emailfrom.value,' ')!="")
	{
		if(!isEmail(frm.emailfrom.value))
		{
			frm.emailfrom.focus();
			return false;
		}	
	}
	toggleBox('table1');
	toggleBox('table2');
	return true;
}

function isLoginFormOk(frm)
{
	if(Trim(frm.uname.value,' ')=="")
	{
		alert("Please specify username.");
		frm.uname.focus();
		return false;
	}
	else if(Trim(frm.pwd.value,' ')=="")
	{
		alert("Please specify password.");
		frm.pwd.focus();
		return false;	
	}
	return true;
}

function isProfileFormOk(frm)
{
	if(Trim(frm.ppwd.value,' ')=="")
	{
		alert("Please provide your current password.");
		frm.ppwd.focus();
		return false;
	}
	else if(!isValidData(frm.uname.value,"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",3,15,"Username","Only numbers and letters in"))
	{
		frm.uname.focus();
		return false;
	}
	else if(Trim(frm.pwd.value,' ')!="")
	{
		
		if(!isValidData(frm.pwd.value,"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",3,15,"Password","Only numbers and letters in"))
		{
			frm.pwd.focus();
			return false;
		}
		else if(frm.pwd.value!=frm.cpwd.value)
		{
			alert("Password mismatch.");
			frm.cpwd.focus();
			return false;
		}
	}
	
	if(!isEmail(frm.email.value))
	{
		frm.email.focus();
		return false;
	}
	return true;
}

function isSendPwdFormOk(frm)
{
	if(!isEmail(frm.email.value))
	{
		frm.email.focus();
		return false;
	}
	return true;
}


function toggleBox(szDivID) {
  if (document.layers) { // NN4+
    if (document.layers[szDivID].visibility == 'visible') {
      document.layers[szDivID].visibility = "hide";
      document.layers[szDivID].display = "none";
      document.layers[szDivID+"SD"].fontWeight = "normal";
    } else {
      document.layers[szDivID].visibility = "show";
      document.layers[szDivID].display = "inline";
      document.layers[szDivID+"SD"].fontWeight = "bold";
    }
  } else if (document.getElementById) { // gecko(NN6) + IE 5+
    var obj = document.getElementById(szDivID);
    var objSD = document.getElementById(szDivID+"SD");

    if (obj.style.visibility == 'visible') {
      obj.style.visibility = "hidden";
      obj.style.display = "none";
      objSD.style.fontWeight = "normal";
    } else {
      obj.style.visibility = "visible";
      obj.style.display = "inline";
      objSD.style.fontWeight = "bold";
    }
  } else if (document.all) { // IE 4
    if (document.all[szDivID].style.visibility == 'visible') {
      document.all[szDivID].style.visibility = "hidden";
      document.all[szDivID].style.display = "none";
      document.all[szDivID+"SD"].style.fontWeight = "normal";
    } else {
      document.all[szDivID].style.visibility = "visible";
      document.all[szDivID].style.display = "inline";
      document.all[szDivID+"SD"].style.fontWeight = "bold";
    }
  }
}

function Toggle()
{
	toggleBox('table1');
	toggleBox('table2');
}

function onConfChange(frm)
{
	frm.conf_disptxt.value=frm.conf_val.options[frm.conf_val.selectedIndex].text;
}


/*function PUSH(frm)
{
	var i=frm.types.selectedIndex;
	if(i==-1) 
	{
		alert("Select any type first.");
		frm.types.focus();
		return;
	}	
	else
	{
		
		for(var j=0; j<frm.conf_val.length; j++)
		{
			if(frm.types.options[i].text==frm.conf_val.options[j].text)
			{
				alert("This mime type already exists.");
				frm.types.focus();
				return;
			}
		}
		//var newElem=document.createElement("OPTIONS");		
		//newElem.text=;
		//newElem.value=;
		frm.conf_val.options[j]=new Option(frm.types.options[i].text,frm.types.options[i].text,false,false);
		return;	
	}
}



function POP(frm)
{
	var i=frm.conf_val.selectedIndex;
	if(i==-1) 
	{
		alert("Select any type first.");
		frm.conf_val.focus();
		return;
	}	
	else
	{
		frm.conf_val.options[i]=null;
		return;	
	}
}

function SelectAllList(frm)
{
	for(i=0;i<frm.conf_val.length;i++)
	frm.conf_val.options[i].selected=true;
	return true;
}

function isMimeFormOk(frm)
{
	if(frm.conf_val.length==0)
	{
		alert("Set at least one mime type.");
		frm.conf_val.focus();
		return false;	
	}
	SelectAllList(frm);
	return true;
}*/


function isMimeFormOk(frm)
{
	if(frm.opt[1].checked)
	{
		if(Trim(frm.conf_val.value," ")=="")
		{
			
			alert("Set at least one mime type.");
			frm.conf_val.focus();
			return false;	
		}
	}	
	return true;
}


function isOtherFormOk(frm)
{
	if(Trim(frm.conf_disptxt.value,' ')=="")
	{
		alert("Please specify display text.");
		frm.conf_disptxt.focus();
		return false;
	}
	return true;
}

function isPHGFormOk(frm)
{
	if(!isValidData(frm.uname.value,"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",3,15,"Username","Only numbers and letters in"))
	{
		frm.uname.focus();
		return false;
	}
	else if(!isValidData(frm.pwd.value,"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",3,15,"Password","Only numbers and letters in"))
	{
		frm.pwd.focus();
		return false;
	}
	else if(Trim(frm.cpwd.value," ")=="")
	{
		alert("Confirm Password.");
		frm.cpwd.focus();
		return false;
	}
	else if(frm.pwd.value!=frm.cpwd.value)
	{
		alert("Password mismatch.");
		frm.cpwd.focus();
		return false;
	}
	else if(!isEmail(frm.email.value))
	{
		frm.email.focus();
		return false;
	}
	return true;
}

function isDate(val)
{
	if(Trim(val,' ') == "")
	{
		alert("Please specify Date of publication.");
		return false;
	}
	else if(val.length < 10 || val.length > 10)
	{
		alert("Invalid Date format.");
		return false;
	}
	else if(val.substr(5,2) < 1 || val.substr(5,2) > 12)
	{
		alert("Please specify a month between 01 to 12.");
		return false;
	}
	else if(val.substr(8,2) < 1 || val.substr(8,2) > 31)
	{
		alert("Please specify a date between 01 to 31");
		return false;
	}
	return true;
}


function isBFromOk(frm)
{
	if(Trim(frm.title.value," ")=="")
	{
		alert("Please specify book name.");
		frm.title.focus();
		return false;
	}
	else if(Trim(frm.author.value," ")=="")
	{
		alert("Please specify author.");
		frm.author.focus();
		return false;
	}
	else if(Trim(frm.publisher.value," ")=="")
	{
		alert("Please specify publisher.");
		frm.publisher.focus();
		return false;
	}
	else if(Trim(frm.isbn.value," ")=="")
	{
		alert("Please specify ISBN no.");
		frm.isbn.focus();
		return false;
	}
	else if(!isDate(frm.dt_of_pub.value))
	{
		frm.dt_of_pub.focus();
		return false;
	}
	else if(Trim(frm.price.value," ")=="")
	{
		alert("Please specify retail price.");
		frm.price.focus();		
		return false;
	}
	else if(Trim(frm.s_qty.value," ")=="")
	{
		alert("Please specify stock quantity.");
		frm.s_qty.focus();		
		return false;
	}
	/*else if(Trim(frm.imgfile.value," ")=="")
	{
		alert("Please select an image file.");
		frm.imgfile.focus();
		return false;
	}
	else if(Trim(frm.cpage.value," ")=="")
	{
		alert("Please specify content page.");
		frm.cpage.focus();
		return false;
	}
	else if(Trim(frm.description.value," ")=="")
	{
		alert("Please specify description.");
		frm.description.focus();
		return false;
	}*/
	return true;
}

//-------------Function to show gridview---------------//
var smooth_timer;
function smoothHeight(id, curH, targetH, stepH, mode) {
  diff = targetH - curH;
  if (diff != 0) {
    newH = (diff > 0) ? curH + stepH : curH - stepH;
    ((document.getElementById) ? document.getElementById(id) : eval("document.all['" + id + "']")).style.height = newH + "px";
    if (smooth_timer) window.clearTimeout(smooth_timer);
    smooth_timer = window.setTimeout( "smoothHeight('" + id + "'," + newH + "," + targetH + "," + stepH + ",'" + mode + "')", 20 );
  }
  else if (mode != "o") ((document.getElementById) ? document.getElementById(mode) : eval("document.all['" + mode + "']")).style.display="none";
}
function rowOver(i, nColor) {
  if (!nColor) nColor = "#ECECD9";
  var nameObj = (document.getElementById) ? document.getElementById('name' + i) : eval("document.all['name" + i + "']");
  if (nameObj != null) nameObj.style.background=nColor;
}
function rowOut(i, nColor) {
  var trObj = (document.getElementById) ? document.getElementById('ihtr' + i) : eval("document.all['ihtr" + i + "']");
  var nameObj = (document.getElementById) ? document.getElementById('name' + i) : eval("document.all['name" + i + "']");
  if (trObj == null || trObj.style.display=="none") nameObj.style.background=nColor;
}
function servOC(i, href, nColor,m) {
  var trObj = (document.getElementById) ? document.getElementById('ihtr' + i) : eval("document.all['ihtr" + i + "']");
  var nameObj = (document.getElementById) ? document.getElementById('name' + i) : eval("document.all['name" + i + "']");
  var ifObj = (document.getElementById) ? document.getElementById('ihif' + i) : eval("document.all['ihif" + i + "']");
  if (trObj != null) {
    if (trObj.style.display=="none") {
      trObj.style.display="";
      nameObj.style.background="#ECECD9";
      if (!ifObj.src) ifObj.src = href;
      smoothHeight('ihif' + i, 0, 42*m, 42, 'o');
    }
    else {
      nameObj.style.background=nColor;
      smoothHeight('ihif' + i, 42*m, 0, 42, 'ihtr' + i);
    }
  }
}
function trOC(idHL, idOC, idArrow) {
  var trObj = (document.getElementById) ? document.getElementById(idOC) : eval("document.all['" + idOC + "']");
  var hlObj = (document.getElementById) ? document.getElementById(idHL) : eval("document.all['" + idHL + "']");
  var arrowObj = (document.getElementById) ? document.getElementById(idArrow) : eval("document.all['" + idArrow + "']");
  if (trObj != null && hlObj != null) {
    if (trObj.style.display=="none") {
      trObj.style.display="";
      hlObj.style.background="#003366";
      hlObj.style.color="#EDF6F9";
      arrowObj.innerHTML="v";
    }
    else {
      trObj.style.display="none";
      hlObj.style.background="#ECECD9";
      hlObj.style.color="#000000";
      arrowObj.innerHTML="^";
    }
  }
}
function firstFocus()
{
   if (document.forms.length > 0)
   {
      var TForm = document.forms[0];
      for (i=0;i<TForm.length;i++)
      {
         if ((TForm.elements[i].type=="text")||
           (TForm.elements[i].type=="textarea")||
           (TForm.elements[i].type.toString().charAt(0)=="s"))
         {
            document.forms[0].elements[i].focus();
            break;
         }
      }
   }
}
function HighlightAll(theField) {
  var tempval=eval("document."+theField);
  tempval.focus();
  tempval.select();
  if (document.all) {
    therange=tempval.createTextRange();
    therange.execCommand("Copy");
    window.status="Contents highlighted and copied to clipboard!";
    setTimeout("window.status=''",5000);
  }
}
function NewWindow(mypage, myname, w, h, scroll) {
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
  win = window.open(mypage, myname, winprops)
  if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}