var selectedf = false;
function checkAll(field)
{
	if (!selectedf)
	{
		for (i = 0; i < field.length; i++)
		field[i].checked = true ;
		selectedf = true;
	}
	else
	{
		for (i = 0; i < field.length; i++)
		field[i].checked = false ;
		selectedf = false;
	}
}

function checkAll(field,prefix)
{
	if (!selectedf)
	{
		for (i = 0; i < field.length; i++)
		{
			if ( field[i].name.indexOf(prefix) == 0 )
			{
				field[i].checked = true ;
			}
		}
		selectedf = true;
	}
	else
	{
		for (i = 0; i < field.length; i++)
		{
			if ( field[i].name.indexOf(prefix) == 0 )
			{
				field[i].checked = false ;
			}
		}
		selectedf = false;
	}
}

function isWhitespace (c) 
{
    var whitespace = " \t\r\n\f\'";
    return (whitespace.indexOf (c) != -1);
}

function isDigit (str) 
{
    if (str == null) 
	{
        return (false);
    }
    if (isNaN(str))
    {
	return (false);
    }
	else if(str<=0)
	{
		return (false);
	}
    return (true);
}

function isBlank (str) 
{
    if (str == null) {
        return (true);
        }
    for (var i = 0; i < str.length; i++) {        
        var c = str.charAt (i);
        if (!isWhitespace (c)) {
           return (false);
           }
        }
    return (true);
}


function isValidEmail (str) 
{
    if (str == null) {
        return (false);
        }
    str = trim (str);                        // Start by trimming off whitespace at both ends
    for (var i = 0; i < str.length; i++) {   // Check that the address does not contain whitespace
        var c = str.charAt (i);
        if (isWhitespace (c)) {
           return (false);
           }
        }
    if (window.RegExp) {
        var tempStr = "a";  // First check that regular expression support is present
        var tempReg = new RegExp (tempStr);
        if (tempReg.test (tempStr)) {
            var r1 = new RegExp ("(@.*@)|(@\\.)|(^\\.)");
            var r2 = new RegExp ("^[a-zA-Z0-9\\!\\#\\$\\%\\&\\'\\*\\+\\-\\.\\/\\=\\?\\^\\_\\`\\{\\|\\}\\~]+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); // Filter according to RFC822 rules
            return (!r1.test(str) && r2.test(str));
            }
        }
    return (str.indexOf (".") > 2) && (str.indexOf ("@") > 0);
}

function isValidZIP (str) 
{
    if (str == null) {
        return (false);
        }
    str = trim (str);                        // Start by trimming off whitespace at both ends
    if ((str.length != 5) && (str.length != 10)) {
       return (false);
       }
    for (var i = 0; i < str.length; i++) {   // Check that the address does not contain whitespace
        var c = str.charAt (i);
        if (i == 5) {
           if (c != '-') {
              return (false);
              }
           }
        else {
           if (!isDigit (c)) {
              return (false);
              }
           }
        }
    return (true);
}

function checkemail(str) 
{

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}


/* ============================================================================
 * Validate User Login
 * ============================================================================
 */
function validateUserLogin()
{
	var userName		=	 document.getElementById("txUserName");
	var password		=	 document.getElementById("txPassword");

	if (isBlank(userName.value))
	{
		alert("Please enter user name");
		userName.focus();
		return false;
	}
	if (isBlank(password.value))
	{
		alert("Please enter password");
		password.focus()
		return false;
	}
	return true;
}


/* ============================================================================
 * Validate Add Project Group
 * ============================================================================
 */
function validateAddProjectGroup()
{
	var projGroup		=	 document.getElementById("tbNewProjGrp");
	if (isBlank(projGroup.value))
	{
		alert("Please enter Project Group Name");
		projGroup.focus();
		return false;
	}
	return true;
}


/* ============================================================================
 * Validate Add Project
 * ============================================================================
 */
function validateAddProject()
{
	var projGroup		=	 document.getElementById("SelPrjGrp");
	var projName		=	 document.getElementById("tbNewProj");
	var viewLimit		=	 document.getElementById("tbMaxView");

	if (projGroup.selectedIndex=="")
	{
		alert("Please select a Project Group");
		projGroup.focus();
		return false;
	}
	if (isBlank(projName.value))
	{
		alert("Please enter Project Name");
		projName.focus();
		return false;
	}
	if (!isBlank(viewLimit.value))
	{
		if (!isDigit(viewLimit.value))
		{
			alert("Please enter valid number for View Limit");
			viewLimit.focus();
			return false;
		}
	}
	return true;
}


/* ============================================================================
 * Validate Admin Login
 * ============================================================================
 */
function validateAdminLogin()
{
	var userName		=	 document.getElementById("txUserName");
	var password		=	 document.getElementById("txPassword");

	if (isBlank(userName.value))
	{
		alert("Please enter User name");
		userName.focus();
		return false;
	}
	if (isBlank(password.value))
	{
		alert("Please enter Password");
		password.focus()
		return false;
	}
	return true;
}


/* ============================================================================
 * Validate Admin Save Account Type
 * ============================================================================
 */
function validateSaveAccType()
{

	var accDesc			=	 document.getElementById("txtAccTypeName");
	var playBackCode	=	 document.getElementById("TxtPlaybackCode");

	if (isBlank(accDesc.value))
	{
		alert("Please enter Account Type Desciption/Name");
		accDesc.focus();
		return false;
	}
	if (isBlank(playBackCode.value))
	{
		alert("Please enter Playback Code");
		playBackCode.focus()
		return false;
	}

	return true;
}

/* ============================================================================
 * Validate Admin Add/Edit Companies
 * ============================================================================
 */
function validateSaveCompanies()
{
	var compName		=	 document.getElementById("n_name");
	var accType			=	 document.getElementById("SelAccType");

	if (isBlank(compName.value))
	{
		alert("Please enter Company Name");
		compName.focus();
		return false;
	}
	if (accType.selectedIndex=="0")
	{
		alert("Please select Account Type");
		accType.focus()
		return false;
	}

	return true;
}


/* ============================================================================
 * Validate Admin Add/Edit Users
 * ============================================================================
 */
function validateSaveCompanies()
{
	var logName			=	 document.getElementById("n_logName");
	var logPwd			=	 document.getElementById("n_logPwd");
	var firstname		=	 document.getElementById("n_firstname");
	var lastname		=	 document.getElementById("n_lastname");
	var contact			=	 document.getElementById("n_contact");
	var email			=	 document.getElementById("n_email");
	var company			=	 document.getElementById("ComNameList");

	if (isBlank(logName.value))
	{
		alert("Please enter Login Name");
		logName.focus();
		return false;
	}
	if (isBlank(logPwd.value))
	{
		alert("Please enter Password");
		logPwd.focus()
		return false;
	}
	if (isBlank(firstname.value))
	{
		alert("Please enter First Name");
		firstname.focus();
		return false;
	}
	if (isBlank(lastname.value))
	{
		alert("Please enter Last Name");
		lastname.focus();
		return false;
	}
	if (isBlank(contact.value))
	{
		alert("Please enter Contact");
		contact.focus();
		return false;
	}
	if (isBlank(email.value))
	{
		alert("Please enter Email");
		email.focus();
		return false;
	}
	else if (!isValidEmail(email.value))
	{
		email.focus();
		return false;
	}
	if (company.selectedIndex=="0")
	{
		alert("Please select Company");
		company.focus()
		return false;
	}
	return true;
}

function extractVideoName(videoFile)
{
	var varr=videoFile.split("\\");	
	if ( varr.length > 0 )	{
		return varr[varr.length-1];
	}
	return videoFile;
}

function videoExists(videoFile)
{		
	if ( fileList != "" )	{	
		var arr = fileList.split(';');
		for(i=0; i < arr.length; i++)
		{		
			if ( arr[i] != "" && videoFile != "" && arr[i] == videoFile ) return true;
		}
	}
	return false;
}

function validateVideos()
{
	if ( videoExists(extractVideoName(document.Form1.FileUpload1.value)) )	{
		alert("Video "+extractVideoName(document.Form1.FileUpload1.value)+" already exists.");
		return false;
	}	
		
	if ( videoExists(extractVideoName(document.Form1.FileUpload2.value)) )	{
		alert("Video "+extractVideoName(document.Form1.FileUpload2.value)+" already exists.");
		return false;
	}	
		
	if ( videoExists(extractVideoName(document.Form1.FileUpload3.value)) )	{
		alert("Video "+extractVideoName(document.Form1.FileUpload3.value)+" already exists.");
		return false;
	}
	
	// none of the above filenames should be same.
	return true;
}

function validateVideo1()
{
	if ( videoExists(extractVideoName(document.Form1.FileUpload1.value)) )	{
		alert("Video "+extractVideoName(document.Form1.FileUpload1.value)+" already exists.");
		return false;
	}	
	return true;
}

function validateVideo2()
{
	if ( videoExists(extractVideoName(document.Form1.FileUpload2.value)) )	{
		alert("Video "+extractVideoName(document.Form1.FileUpload2.value)+" already exists.");
		return false;
	}	
	return true;
}

function validateVideo3()
{
	if ( videoExists(extractVideoName(document.Form1.FileUpload3.value)) )	{
		alert("Video "+extractVideoName(document.Form1.FileUpload3.value)+" already exists.");
		return false;
	}
	return true;
}


