//# 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
};

//## 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 IsAlphaNumeric(sText) {
    var ValidChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    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;
}
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 Email Address";
		   return finalStr;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		{
		   finalStr= "Invalid  Email Address"
		   return finalStr;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		{
		    finalStr="Invalid Email Address"
		    return finalStr;
		}

		if (str.indexOf(at,(lat+1))!=-1)
		{
		    finalStr="Invalid Email Address";
		    return finalStr;
		}

		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		{
		    finalStr="Invalid Email Address";
		    return finalStr;
		}

		if (str.indexOf(dot,(lat+2))==-1)
		{
		    finalStr="Invalid Email Address";
		    return finalStr;
		}
		
		if (str.indexOf(" ")!=-1)
		{
		    finalStr="Invalid Email Address";
		    return finalStr;
		}

 		 return finalStr;					
}

function IsValidEmail(str)
{
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str))
        return true;
        
    return  false;
}

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 NumericValidation(e)    //Only Numeric value
{	
	var KeyID = (window.event) ? event.keyCode : e.which;	
   
   	if ((KeyID >= 48 && KeyID <= 57)|| (KeyID == 8)|| (KeyID == 0))
	{
		return true;
	}
	else
	{
		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);
        }
    }
} 

/*The two below given functions are used for validation for image and video file in custom validator*/
function CheckImage(str) 
{
    var filter = /^.+(.jpeg|.JPEG|.jpg|.JPG|.jpe|.JPE|.png|.PNG|.gif|.GIF)$/i;
    
    if (filter.test(str))
        return true;
    else
        return false;
}

function CheckVideo(str) 
{
    var filter = /^.+(.wmv|.WMV|.flv|.FLV|.mpeg|.MPEG|.mpg|.MPG)$/i;
    
    if (filter.test(str))
        return true;
    else
        return false;
}

function CheckCSVFile(str) 
{
    var filter = /^.+(.csv|.CSV)$/i;
    
    if (filter.test(str))
        return true;
    else
        return false;
}


function checkall()
{
    for(intCounter=0; intCounter<(document.getElementsByName('chkBox').length); intCounter++)
    {
	    document.getElementsByName("chkBox")[intCounter].checked = document.getElementById("chkAll").checked;
    }
}

function checkManual()
{
    var intCheckVal;
    intCheckVal = 1;
    for(intCounter=0; intCounter<(document.getElementsByName('chkBox').length); intCounter++)
    {
	    if(document.getElementsByName("chkBox")[intCounter].checked == false)
	        intCheckVal = 0;
    }
    if(intCheckVal == 1)
        document.getElementById("chkAll").checked = true;
    else
        document.getElementById("chkAll").checked = false;
}

function ConfirmForDelete()
{
    return confirm('Are you sure you want to delete record?');
}

function selected(string)
{           
    var intCountControls=0;
    for(intCounter=0; intCounter<(document.getElementsByName('chkBox').length); intCounter++)
    {               
        if(document.getElementsByName("chkBox")[intCounter].checked == true)
        {
           intCountControls++;
        }
    }    
    if(intCountControls == 0)
    {            
        if (string == "delete")
            alert('Please Select a Record(s) to delete');
        else if (string == "active")
            alert('Please Select a Record(s) to change the status');
        else if (string == "inactive")
            alert('Please Select a Record(s) to change the status');
        return false;
    }   
    else if (intCountControls > 1000)
    {
        if (string == "delete")
            alert('You can delete only 1000 Records at a time.');
        else if (string == "active")
            alert('You can active only 1000 Records at a time.');
        else if (string == "inactive")
            alert('You can inactive only 1000 Records at a time.');
        return false;
    } 
    else if(intCountControls > 0)
    {         
         if (string == "delete")
        {            
           return confirm('Are you sure you want to delete record?');
        }
        else if(string=="active")
        {
           return confirm('Are you sure you want to active record?');
        }  
        else if (string=="inactive")        
        {
           return confirm('Are you sure you want to inactive record?');
        }          
    }
}

function SearchValidation(objddlSearchField,objtxtSearchVal)
{
    if (Trim(document.getElementById(objddlSearchField).value)=='0')
    {
        alert('Please select any value for search!');
        document.getElementById(objddlSearchField).focus();
        return false;
    }
    
    if(document.getElementById(objtxtSearchVal).style.display != 'none')
    {
        if (Trim(document.getElementById(objtxtSearchVal).value)=='')
        {
            alert('Please enter any value for search!');
            document.getElementById(objtxtSearchVal).focus();
            return false;
        }
    }
    	        
    return true;
}

function ChangeSearchControl(objddlSearchField,objddlSearchVal,objtxtSearchVal)
{
    if(document.getElementById(objddlSearchField).value == 'IsStatus')
    {
        document.getElementById(objtxtSearchVal).style.display = 'none';
        document.getElementById(objddlSearchVal).style.display = 'block';
    }
    else
    {
        document.getElementById(objtxtSearchVal).style.display = 'block';
        document.getElementById(objddlSearchVal).style.display = 'none';
    }
    
    return false;
}

function IsValidURL(url)
    {
        //var RegExp = /^(ftp|https?):\/\/+(www\.)+[a-z0-9\-\.]{3,}\.[a-z]{2,4}$/;
        var RegExp = /^(ftp|https?):\/\/+[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/
        
        /*
        if(RegExp.test(url))
        {
            return true;
        }
        else
        {
            return false;
        }
        */
        return true;
    }
function IsValidFeedsURL(url) 
      {
        //var RegExp = /^(ftp|https?):\/\/+(www\.)+[a-z0-9\-\.]{3,}\.[a-z]{2,4}$/;
          var RegExp = /^[a-z0-9\-\.\+\:\/\\]*(feeds?)+[a-z0-9\-\.\/]{2,}/;
        
        if (RegExp.test(url))
        {
        return true ;
        }
        else
        {
        return false; 
        } 

        /*
        if(RegExp.test(url))
        {
        return true;
        }
        else
        {
        return false;
        }
        */
        //return true;
    }    

        
function CheckKeySpace(e)
{	
    var KeyID = (window.event) ? event.keyCode : e.which;
    //prevent from (space)        
    if(KeyID == 32)
    {
        return false;
    }
}
function CheckInvalidEmail(objtxt)
{       
    var EmailMsg="";
    var val="";
    var email="";
    var stremail="";    
    val = Trim(document.getElementById(objtxt).value).split(",");
    
    for(i = 0; i < val.length; i++)
    {
        if(val[i].indexOf("<") != -1 && val[i].indexOf(">") != -1)
        {
            email = Trim(val[i].substring(val[i].indexOf("<")+1,val[i].indexOf(">")));
        }   
        else
        {
            email = Trim(val[i]);      
        }
        if(email != "")
        {
            var res = eMailcheck(email);
            if(res != "")
            {
                EmailMsg += " - " + email + "\n";
            }
            else
            {
                stremail += email + ","
            }
        }               
    }
    if(EmailMsg != "")
    {
        return EmailMsg;
    }
    else
    {
        if(stremail != '')
        {
            return  stremail + 'EMAILS' ;
        }
        else
        {
            return stremail;
        }
    }
}    