//# Generalise function
 var singleDel = "Are You Sure You Want To Delete Record ?";
 var multipleDel = "Are You Sure You Want To Delete These Multiple Record ?";
 var active = "Are you sure you want Active record ?";
 var inactive = "Are you sure you want InActive record ?";
 var default1  = "Are you sure you want set as Default this record ?";
 var selectrecord = "Please Select Records";
 var valid = "Following Field Should Not Blank";
 var searchblank = "Please Enter Search Text.";
 var sel_srcfield = "Please Select Search Field.";
 var validnumber = "Please Enter Numeric Value For ID Search";
 var validPrice = "Please Enter Numeric Value For Price Search";
 var RecordAdded = "Your Record Added Successfully.";
 var RecordUpdated = "Your Record Is Updated Successfully.";
 var RecordDeleted = "Your Record Is Deleted Successfully.";
 var CheckedCustomer = "Are you sure you want to Add Customer?";
// directory of where all the images are
var cmThemeOfficeBase = 'images/';
var cmThemeOffice =
{
  	// main menu display attributes
  	//
  	// Note.  When the menu bar is horizontal,
  	// mainFolderLeft and mainFolderRight are
  	// put in <span></span>.  When the menu
  	// bar is vertical, they would be put in
  	// a separate TD cell.
  	// HTML code to the left of the folder item
  	mainFolderLeft: '&nbsp;',
  	// HTML code to the right of the folder item
  	mainFolderRight: '&nbsp;',
	// HTML code to the left of the regular item
	mainItemLeft: '&nbsp;',
	// HTML code to the right of the regular item
	mainItemRight: '&nbsp;',
	// sub menu display attributes
	// 0, HTML code to the left of the folder item
	folderLeft: '<img alt="" src="' + cmThemeOfficeBase + 'spacer.gif">',
	// 1, HTML code to the right of the folder item
	folderRight: '<img alt="" src="' + cmThemeOfficeBase + 'arrow.gif">',
	// 2, HTML code to the left of the regular item
	itemLeft: '<img alt="" src="' + cmThemeOfficeBase + 'spacer.gif">',
	// 3, HTML code to the right of the regular item
	itemRight: '<img alt="" src="' + cmThemeOfficeBase + 'blank.gif">',
	// 4, cell spacing for main menu
	mainSpacing: 0,
	// 5, cell spacing for sub menus
	subSpacing: 0,
	// 6, auto dispear time for submenus in milli-seconds
	delay: 500
};
// for horizontal menu split
//var cmThemeOfficeHSplit = [_cmNoAction, '<td class="ThemeOfficeMenuItemLeft"></td><td colspan="2"><div class="ThemeOfficeMenuSplit"></div></td>'];
//var cmThemeOfficeMainHSplit = [_cmNoAction, '<td class="ThemeOfficeMainItemLeft"></td><td colspan="2"><div class="ThemeOfficeMenuSplit"></div></td>'];
//var cmThemeOfficeMainVSplit = [_cmNoAction, '&nbsp;']; 
//# This function will checked or unchecked combobox from grid 

function checkall(frm)
		{
			var els = frm.elements; 
			for(i=0; i<els.length; i++)
			{ 
				switch(els[i].type)
			{
				case "checkbox":
					if(document.getElementById("chkAll").checked == true)
						els[i].checked = true;
					else
					els[i].checked = false;
					break;	
			}
		}			
	}		

//#### 
// This function will check wether any records selected or not before getting operation Multiple Delete, Multiple Active in active
 function selectchk(frm,optype)
  {
		var flag;
		flag = "false";
		var els = frm.elements; 
		for(i=0; i<els.length; i++)
			{ 
			switch(els[i].type)
			{
			case "checkbox":
				if(els[i].checked == true)
				{flag = "true";}
				break;	
			}
		}
		if(flag == 'true')
		{
			if (optype == 'delete')
				{
		 			 return delconfirm('multiple')
				}
			else if (optype == 'AddCust')
				{
		 			 return confirm(CheckedCustomer)
				}	
			else if(optype == 'active')
				{
					if(confirm(active)==true)
					{
						return true
					}
					else
					{
					 return false;
					}
				}
				else if(optype == 'inactive')		 
				{
					if(confirm(inactive)==true)
					{
						return true;
					}
					else
					{
						return false;
					}
				}
				
				

		}
		else
		{
		 alert(selectrecord);
		 return false;
		}
   }

 // this function will ask to user for confirmation of deleting records
 function delconfirm(single)
 {

	var msg;
		if(single=='single') 
		{
			msg = singleDel;
		}
		else
		{
			msg = multipleDel;
		}
		
	{
	}
	if(confirm(msg)== true)
		{
			return true;
		}
		else
		{
			return false;
		}
 }
 
 
  // this function will ask to user for confirmation of deleting records
 function closeconfirm()
 {
	var msg;
	msg = "Are you sure you want to Close This Account?";
	if(confirm(msg)== true)
		{
			return true;
		}
		else
		{
			return false;
		}
 }

 //this function check whether control is blank or nor and other validation?
  function validation(frm)
 {
 	var ele = frm.elements;
	var msg = "";
	var finamsg,tempObj;
	var valid = "";
	for(j=0;j<ele.length;j++)
	{		
		if((ele[j].type == "text") || (ele[j].type == "textarea")) // for text
		{
			var obj = ele[j].title;
			tempObj = obj.replace("#","")
			tempObj = tempObj.replace("*","")
			var flag;
			flag = obj.indexOf("*");
			if(flag>0)
			{
				if(vTrim(ele[j].value) == "")
				{
					if (valid == "")
						{
						valid = "Following Should not be blank";
						}
					msg = msg + ele[j].title.replace("*"," ").replace("#","").replace("@","") + "\n";
					ele[j].value = '';
					ele[j].focus();
				}
			}
			flag = obj.indexOf("#");
			if(flag>0)
			{
			if( alphanum(ele[j].value,1,tempObj) == false )
				{
					msg = msg + ele[j].title.replace("#"," ").replace("*","").replace("@","") + " Must be Aplphanumeric " + "\n";
					ele[j].value = '';
				}
			}
			var flgnew = 0;
			flag = obj.indexOf("@");
			if(flag>0)
			{
			 flgnew = ele[j].value.length;
			}
			if(flgnew>0)
			{
			var str=ele[j].value;
			var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
			
			if (filter.test(str))
				testresults=true
			else
			{
				msg = msg + "\n"
				msg = msg + ele[j].title.replace("@"," ").replace("*","").replace("#","") + " Must be valid " + "\n";
				testresults=false;
			}
			}
			
		}
		// For Drop Down control
		// add attribute at run time as
		// e.g. dpMemberType.Attributes.Add("title", "Select Member Type*")
		else if(ele[j].type == "select-one")
		{
			var obj = ele[j].title;
			tempObj = obj.replace("#","")
			tempObj = tempObj.replace("*","")
			var flag;
			flag = obj.indexOf("*");
			if(flag>0)
			{
				if(ele[j].value == -1)
				{
					if (valid == "")
						{
						valid = "Following Should not be blank";
						}
					msg = msg + ele[j].title.replace("*"," ").replace("#","").replace("@","") + "\n";
					ele[j].focus();
				}
			}
		
		}
			
	}
	if(msg != "")
	{
		finalmsg = valid + "\n\n" + msg.replace("#","");
		alert(finalmsg);
		return false;
	}
	else
	{
	 return true;
	}
 }
//# This function gives proper message in searching module
function searchvalid(frm)
{	
    var txt;
    var searchfield;
    if (document.getElementById('txtSearchVal'))	
    {
        document.getElementById('txtSearchVal').value = Trim(document.getElementById('txtSearchVal').value);
        txt=document.getElementById('txtSearchVal').value;
    }
    if (document.getElementById('ddlSearchField'))
    {
        searchfield=document.getElementById('ddlSearchField');
    }
    if(txt=="")
	{
		if(searchfield.options[searchfield.selectedIndex].text == "ALL")
		{
			
			alert(searchblank);
			return true;
		}
		else
		{
				
			alert(searchblank);
			return false;
		}
	}
	else
		{
	
		if(searchfield.selectedIndex == 0 && txt != "")
			{
			 alert(sel_srcfield);
			 return false;
			}
		else
			{
			if(searchfield.options[searchfield.selectedIndex].text == "ID" || searchfield.options[searchfield.selectedIndex].text == "Price")
			{
				
				if(searchfield.options[searchfield.selectedIndex].text == "ID")
				{
				
					if(isNaN(txt) == true)
					{
						alert(validnumber);
						return false;
					}
					else
					{
						return true;
					}
				}
				else if(searchfield.options[searchfield.selectedIndex].text == "Price")
				{
					if(isNaN(txt) == true)
					{
						alert(validPrice);
						return false;
					}
					else
					{
						return true;
					}
				}
				
				
			}
			else
			{
				return true;
			}
			
	
			}
		
		}
		
		
}

//## this function check Numeric value.... 
 function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
	 var Char;
	
	 for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}


//## Record Added 
function RecordAdded()
{
	alert(RecordAdded);
}
//## Record Added 
function RecordUpdated()
{
	alert(RecordUpdated);
}
function RecordDeleted()
{
	alert(RecordDeleted);
}

function validMessage(ope,frm)
{
     
	if (ope =='Update')
	{
		alert(RecordUpdated);
		document.location.href=frm;
		//window.open(frm,'_self','');
	}	
	else if(ope =='Add')
	{
		alert(RecordAdded);
		//window.open(frm,'_self','');
		document.location.href=frm;
	
	}

	else if(ope =='Delete')
	{
		alert(RecordDeleted);
		
		//window.open(frm,'_self','');
		document.location.href=frm;

	}

}




function Trim(s) 
{
// Remove leading spaces and carriage returns
while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
{
s = s.substring(1,s.length);
}
// Remove trailing spaces and carriage returns
while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
{
s = s.substring(0,s.length-1);
}
return s;
} 
function vTrim(TRIM_VALUE)
{
	if(TRIM_VALUE.length < 1)
	{
		return"";
	}
	TRIM_VALUE = vRTrim(TRIM_VALUE);
	TRIM_VALUE = vLTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){
	return "";
	}
	else
	{
		return TRIM_VALUE;
	}
} //End Function

function vRTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0)
	{
		return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1)
	{
		if(VALUE.charAt(iTemp) == w_space)
		{
		}
		else
		{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;

	} //End While
return strTemp;

} //End Function

function vLTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	if(v_length < 1)
	{
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length)
	{
		if(VALUE.charAt(iTemp) == w_space)
		{
		}
		else
		{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
return strTemp;
} //End Function
function alphanum(value,len,nameId)
{
/*
	var filter = /^[\w]+$/i;
	if (filter.test(value))
	{
		alert('Ok');		
	}else
	{
		alert('wrong');
	}
	
	return false;
*/	

	chk1="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_\- ():;.";
	for(i=0;i<value.length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
		{
			alert(nameId + ' Only Alphanumeric chars are allowed !!');
			return false;
		}	
	}
	return true;
}
function minLen(value,len,nameId)
{
	if(value.length<len)
	{
		alert(nameId + ' - Minumum '+ len +' Chars');
		return false;
	}
}

var curTime = new Date() ;
function SetTimer(ct,id)
{
    try{curTime = new Date(ct) ;}
    catch(err){curTime = new Date() ;}
    window.setInterval("displayTime('" + id + "')", 1000) ;
	//document.getElementById(id).innerHTML = curTime.getMonth() + 1 + " / " + curTime.getDate() + " / " + curTime.getYear() + "&nbsp;&nbsp;&nbsp;<br>" + curTime.getHours() + " : " + curTime.getMinutes() + " : " + curTime.getSeconds();
	var sec, min, hour ;
    sec = curTime.getSeconds() ;
    min = curTime.getMinutes() ;
    hour = curTime.getHours() ;

    if (parseInt(sec) <= 9)
    {
        sec = "0" + sec ;
    }
    if (parseInt(min) <= 9)
        min = "0" + min ;
    if (parseInt(hour) <= 9)
        hour = "0" + hour ;
    
	document.getElementById(id).innerHTML = curTime.getMonth() + 1 + " / " + curTime.getDate() + " / " + curTime.getFullYear() + "&nbsp;&nbsp;&nbsp;<br>" + hour + " : " + min + " : " + sec;

}
function displayTime(id1)
{
	curTime.setSeconds(curTime.getSeconds() + 1) ;
	var sec, min, hour ;
    sec = curTime.getSeconds() ;
    min = curTime.getMinutes() ;
    hour = curTime.getHours() ;
    if (parseInt(sec) <= 9)
        sec = "0" + sec ;
    if (parseInt(min) <= 9)
        min = "0" + min ;
    if (parseInt(hour) <= 9)
        hour = "0" + hour ;
       
	document.getElementById(id1).innerHTML = curTime.getMonth() + 1 + " / " + curTime.getDate() + " / " + curTime.getFullYear() + "&nbsp;&nbsp;&nbsp;<br>" + hour + " : " + min + " : " + sec;
}
 function delconf(frm,optype)
    {
    if (confirm("Are you sure you want to delete this record?") == true)
    {
        return true ;
    }
    else
    {
        return false; 
    }
   }
function moveSelectedOptions(from,to) {
	// Unselect matching options, if required

	if (arguments.length>3) {
		var regex = arguments[3];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
			}
		}


	if (!hasOptions(from)) { return; }
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
			to.options[index] = new Option( o.text, o.value, false, false);
			}
		}

	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
			}
		}
	/*if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(from);
		sortSelect(to);
		}*/
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	}
	
	function moveAllOptions(from,to) {
	selectAllOptions(from);
	if (arguments.length==2) {
		moveSelectedOptions(from,to);
		}
	else if (arguments.length==3) {
		moveSelectedOptions(from,to,arguments[2]);
		}
	else if (arguments.length==4) {
		moveSelectedOptions(from,to,arguments[2],arguments[3]);
		}
	}
	function unSelectMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"unselect",false);
	}
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
	}
	function selectAllOptions(obj) {
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		obj.options[i].selected = true;
		}
	}
	
	function eMailcheckForHarvard(str) 
	{
	    var filter=/^([\w-]+(?:\.[\w-]+)*)@fas.harvard.edu$/i
	    
	    if (filter.test(str))
            return true;
        else
            return false;
	}
	
function eMailcheck(str) 
{

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		var finalStr="";
		if (str.indexOf(at)==-1)
		{
		   finalStr="Invalid E-mail ID";
		   return finalStr;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		{
		   finalStr= "Invalid E-mail ID"
		   return finalStr;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		{
		    finalStr="Invalid E-mail ID"
		    return finalStr;
		}

		if (str.indexOf(at,(lat+1))!=-1)
		{
		    finalStr="Invalid E-mail ID";
		    return finalStr;
		}

		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		{
		    finalStr="Invalid E-mail ID";
		    return finalStr;
		}

		if (str.indexOf(dot,(lat+2))==-1)
		{
		    finalStr="Invalid E-mail ID";
		    return finalStr;
		}
		
		if (str.indexOf(" ")!=-1)
		{
		    finalStr="Invalid E-mail ID";
		    return finalStr;
		}

 		 return finalStr;					
}

function KeyCheck(e)
{
    var KeyID = (window.event) ? event.keyCode : e.which;    	    
   	if((KeyID >= 65 && KeyID <= 90) || (KeyID >= 97 && KeyID <= 122) || (KeyID >= 33 && KeyID <= 47 && KeyID != 46) ||
	    (KeyID >= 58 && KeyID <= 64) || (KeyID >= 91 && KeyID <= 96) || (KeyID >= 123 && KeyID <= 126) ) 
	    {
			return false;
		}
}


function KeyPhone(e)    //This is for Phone No. textbox. Numeric with ( ) and - 
{	
	var KeyID = (window.event) ? event.keyCode : e.which;	
	if((KeyID >= 65 && KeyID <= 90) || (KeyID >= 97 && KeyID <= 122) || (KeyID >= 33 && KeyID <=39)|| KeyID==42 ||KeyID == 44 || KeyID==46 || KeyID==47 ||
	   (KeyID >= 58 && KeyID <= 64) || (KeyID >= 91 && KeyID <= 96) || (KeyID >= 123 && KeyID <= 126))
	{	
		return false;
	}
	if(KeyID == 32)
	{
	    return false;
	}	
}

function KeyNumeric(e)    //This is for PostCode textbox. Only Numeric value
{	
	var KeyID = (window.event) ? event.keyCode : e.which;	

	if((KeyID >= 65 && KeyID <= 90) || (KeyID >= 97 && KeyID <= 122) || (KeyID >= 33 && KeyID <= 47 && KeyID != 46) ||
	    (KeyID >= 58 && KeyID <= 64) || (KeyID >= 91 && KeyID <= 96) || (KeyID >= 123 && KeyID <= 126) ) 
	    {
			return false;
		}
	if(KeyID == 32)
	{
	    return false;
	}	
}

function KeyDate(e)    //This is Date field i.e. user can't type date but select from calender button
{	
	var KeyID = (window.event) ? event.keyCode : e.which;
	if(KeyID != 9)
	{
	    alert('Please select date form calender button.!');
	    return false;
	}	
}

// This Function reset the FCK Editor text
function resetEditors()
{
    // If the editor API does not exist, there are no editors
    if (typeof FCKeditorAPI == "undefined") return;

    // Loop through all FCK instances, in case there are several editors
    for (var sEditorName in FCKeditorAPI.__Instances)
    {
        // The initial value that was set when the form was created
        // is stored in a hidden <INPUT> with the same name as the
        // editor (the editor itself is in an <IFRAME> with ___Frame
        // appended to the name.  Check whether that INPUT exists
        if (document.getElementById(sEditorName))
        {
            // Get the initial value
            var sInitialValue = document.getElementById(sEditorName).value;

            // Overwrite the editor's current value
            FCKeditorAPI.__Instances[sEditorName].SetHTML(sInitialValue);
        }
    }
} 