// JavaScript Document

/*
Creation Date=18-08-2005
Creation Time=9:30PM
Version 1.0
Since 1.0

Created By :
			Nadeem Shafi
*/
function setFocus(){
	if(this.document.forms.length != 0 ){
		var lenField = this.document.forms[0].elements.length;
		for(i=0;i<lenField;i++)
		{
			if(this.document.forms[0].elements[i].type != "hidden" && this.document.forms[0].elements[i].type != "checkbox" && this.document.forms[0].elements[i].type != "radio" && this.document.forms[0].elements[i].disabled != true)
			{
				this.document.forms[0].elements[i].focus();
				return;
			}
		}
		
	}
}
//===================================================
//		Validate Email
//===================================================
function validEmail(emailfield) {
  var field = document.getElementById(emailfield); // email field
  var str = field.value; // email string
  var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
  if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
    return true;
  }
  alert("\"" + str + "\" is an invalid e-mail!"); // this is also optional
  field.focus();
  field.select();
  return false;
}

//=========================================================
//=========================================================
function modelesswin(url,WinName,mwidth,mheight){

ScreenHeight=(screen.height)/2;
ScreenWidth=(screen.width)/2;
ScreenTop=ScreenHeight-(mheight/2);
ScreenLeft=ScreenWidth-(mwidth/2);
window.open(url,WinName,"width="+mwidth+"px,left="+ScreenLeft+"px,top="+ScreenTop+"px,height="+mheight+"px,help=0,resizable=0;");
}
///////////////////////////////////////////////////////////////////////////////////////////////
function SelectCheckBox(FormName,CheckBoxParent,CheckBoxChild){
	
if(document.getElementById(CheckBoxChild)!=null)
{
		
	var Child=document.getElementById(CheckBoxChild);
	var Parent=document.getElementById(CheckBoxParent);
		//alert(FormName);
	if(Parent.checked)
	{
		
			for (counter = 0; counter < FormName.elements.length; counter++)
			{
				
				var a=FormName.elements[counter];
				//if(a=='[object]' || )
				//{
				//alert(a.id)
				
					if(a.type=='checkbox' && a.id==Child.id && !a.disabled)
					{
					
						a.checked=true;
					}
				//}
			}
	}
	else
		{
			for (counter = 0; counter < FormName.elements.length; counter++)
			{
				var a=FormName.elements[counter];
				//if(a=='[object]')
				//{
					if(a.type=='checkbox' && a.id==Child.id && !a.disabled)
						a.checked=false;
				//}
			}
	
	
	
	}
}
}
//=========================================================================
//=========================================================================
function SelectChildCheckBox(FormName,CheckBoxParent,CheckBoxChild)
{

	//alert(FormName.elements[1].type)
	var Child=document.getElementById(CheckBoxChild);
	var Parent=document.getElementById(CheckBoxParent);
	//alert(FormName.all.length);
	var checkbox_choices = 0;
	var toltalbox=0;
	for (counter = 0; counter < FormName.elements.length; counter++)
	{
	var a=FormName.elements[counter];
	//if(a=='[object]')
	//{
		if(a.type=='checkbox' && a.id==Child.id){
		toltalbox=toltalbox+1;
		}
	//}
}

for (counter = 0; counter < FormName.elements.length; counter++)
	{
	var a=FormName.elements[counter];
	//if(a=='[object]')
	//{
		if(a.type=='checkbox' && a.id==Child.id && a.checked){
			checkbox_choices=checkbox_choices+1;
		}
	//}
}

	//alert(checkbox_choices);
	//alert(toltalbox);
	if(checkbox_choices==toltalbox) Parent.checked=true; else  Parent.checked=false;

}
//
/*
	This is A Generic Function Which Set The Focus on The First Element on the Calling Form.
	Created By 
	Muhammad Shamshad Habib
*/
function setFocus()
{
	if(this.document.forms.length != 0 )
	{
		var lenField = this.document.forms[0].elements.length;
		for(i=0;i<lenField;i++)
		{
			if(this.document.forms[0].elements[i].type != "hidden" && this.document.forms[0].elements[i].type != "checkbox" && this.document.forms[0].elements[i].type != "radio" && this.document.forms[0].elements[i].disabled != true)
			{
				this.document.forms[0].elements[i].focus();
				return;
			}
		}
		
	}
}
//
/////////////////////////////////////
String.prototype.trim = function() {
  var x=this;
  x=x.replace(/^\s*(.*)/, "$1");
  x=x.replace(/(.*?)\s*$/, "$1");
  return x;
  }
/////////////////this variable use for cheking the max value of check box
var countChildCheckBox=1;
///////////////////////////////////////////////////////////////////////////////////
//=========================================================
function redir(frmaction,module,queryString)
{
	document.frmred.fname.value=frmaction;
	document.frmred.module.value=module;
	document.frmred.qString.value=queryString;	
	document.frmred.submit();	
}

//=========================================================
function comboSelect(val,comboName)
{
	document.getElementById(comboName).value=val;
}
//==============================================
//==================Check Field Length===================
function checkLength(frm,field,maxLength){
	var val=document.getElementById(field).value.length;
	if (val >= maxLength){
		   return false;
		}
		return true;
	
}
//=======================================================================
function getkey(e)
{
if (window.event)
   return window.event.keyCode;
else if (e)
   return e.which;
else
   return null;
}

//=========================================================================================<!--
function charsCheck(e, goods)
{
	if(goods=='a...z' || goods=='A...Z')
		goods='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	if(goods=='a...z,0...9')
		goods='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
	
	if(goods=='0...9')
		goods='0123456789';
	if(goods=='0...9.')
		goods='0123456789.';	
	
	var key, keychar;
		key = getkey(e);
	if (key == null) return true;
	
	// get character
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	goods = goods.toLowerCase();
	
	// check goodkeys
	if (goods.indexOf(keychar) != -1)
		return true;
	
	// control keys
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
	   return true;
	
	// else return false
	return false;
}
//-->
//======================================================================================

function delRecord(frm)
{
	delConfirm=confirm("Are you sure, you want to delete the current record.");
	if(delConfirm)
	{
		document.getElementById(frm).action.value='delete';
		document.getElementById(frm).submit();
	}
}
function submitDelForm(frm)
{
	var valString='';
	//alert(checkChkBox(frm))
	if(checkChkBox(frm))
	{
		delConfirm=confirm("Are you sure, you want to delete the selected records.");
		if(delConfirm)
		{
			var el = frm.elements;
			for(var i = 0 ; i < el.length ; ++i) 
			{
				 if(el[i].type == "checkbox") 
				 {
					if(el[i].name!='CheckBoxParent')
					{
						var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
						
						for(var j = 0 ; j < radiogroup.length ; ++j) 
						{
							if(radiogroup[j].checked)
							{

								valString=valString+radiogroup[j].value;
								valString=valString+',';
								
							}
							
						}
						break;
					}
				}
			}
			//alert(frm.CheckBoxChild.checked)
			//======================if not a array======================
			if(frm.CheckBoxChild.checked)
			valString=frm.CheckBoxChild.value+',';
			
			frm.chkValues.value=valString.substring(0,valString.length-1);
			
			frm.submit();
		}
	}
	
}

//=================================================================
//			Credit card validation
//=================================================================
function checkCC(ccNumb) 
{  // v2.0
	var valid = "0123456789"  // Valid digits in a credit card number
	var len = ccNumb.length;  // The length of the submitted cc number
	var iCCN = parseInt(ccNumb);  // integer of ccNumb
	var sCCN = ccNumb.toString();  // string of ccNumb
	sCCN = sCCN.replace (/^\s+|\s+$/g,'');  // strip spaces
	var iTotal = 0;  // integer total set at zero
	var bNum = true;  // by default assume it is a number
	var bResult = false;  // by default assume it is NOT a valid cc
	var temp;  // temp variable for parsing string
	var calc;  // used for calculation of each digit
	
	// Determine if the ccNumb is in fact all numbers
	for (var j=0; j<len; j++) 
	{
		temp = "" + sCCN.substring(j, j+1);
		if (valid.indexOf(temp) == "-1"){bNum = false;}
	}

	// if it is NOT a number, you can either alert to the fact, or just pass a failure
	if(!bNum)
	{
		/*alert("Not a Number");*/bResult = false;
	}
	
	// Determine if it is the proper length 
	if((len == 0)&&(bResult))
	{  // nothing, field is blank AND passed above # check
		bResult = false;
	} 
	else
	{  // ccNumb is a number and the proper length - let's see if it is a valid card number
		if(len >= 15)
		{  // 15 or 16 for Amex or V/MC
			for(var i=len;i>0;i--)
			{  // LOOP throught the digits of the card
				calc = parseInt(iCCN) % 10;  // right most digit
				calc = parseInt(calc);  // assure it is an integer
				iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
				i--;  // decrement the count - move to the next digit in the card
				iCCN = iCCN / 10;                               // subtracts right most digit from ccNumb
				calc = parseInt(iCCN) % 10 ;    // NEXT right most digit
				calc = calc *2;                                 // multiply the digit by two
				// Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
				// I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
				switch(calc)
				{
					case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
					case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
					case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
					case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
					case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
					default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
				}                                               
				iCCN = iCCN / 10;  // subtracts right most digit from ccNum
				iTotal += calc;  // running total of the card number as we loop
			}  // END OF LOOP
			if ((iTotal%10)==0)
			{  // check to see if the sum Mod 10 is zero
				bResult = true;  // This IS (or could be) a valid credit card number.
			} 
			else 
			{
				bResult = false;  // This could NOT be a valid credit card number
			}
		}
	}
	// change alert to on-page display or other indication as needed.
	/*if(bResult) 
	{
		alert("This IS a valid Credit Card Number!");
	}*/
	if(!bResult){
		alert("Please enter a valid Credit Card Number!");
	}
	return bResult; // Return the results
}
/*
Creation Date=14-08-2005
Purpose of this function:
			This function is use to get all requered filed in form
Parameter:
			This function get form Name.
Return:
			This function return error if its is accour

Version 1.0
Since 1.0

Created By :
			Nadeem Shafi

example
checkrequired("form name")

*/

function checkrequired(which) {
	var msg="";
	var pass=true;
	//alert(document.images.length);
	if (document.images) 
	{
		
		for (i=0;i<which.length;i++)
		 {
			var tempobj=which.elements[i];
			if (tempobj.getAttribute('required')=="yes" && tempobj.value=='')
			 {
				msg = msg + tempobj.getAttribute('field') + ' field can not be left blank.\n';
				tempobj.style.backgroundColor = "#FFB7B7";
		  	 }
			 else
			 	tempobj.style.backgroundColor = "";
			  if(tempobj.disabled)
				tempobj.disabled=false;
	   		}
		}
	if(msg!=''){
		alert(msg);
		msg = '';
		return false;
	}
	return true;
}
/////////////////////////////////////

