/* **********************************************************
   Module: EnterMyPC - Validations
   Created By : Nitin Sakhare
   Description: A page containing validation functions to be used.
  *********************************************************** */
// Validates maxlength 


var dhtmlgoodies_slideSpeed = 10;	// Higher value = faster
var dhtmlgoodies_timer = 10;	// Lower value = faster

var objectIdToSlideDown = true;
var dhtmlgoodies_activeId = false; 
var dhtmlgoodies_slideInProgress = false;

function showHideContent(e,inputId)
{
	if(dhtmlgoodies_slideInProgress)return;
	dhtmlgoodies_slideInProgress = true;
	if(!inputId)inputId = this.id;
	inputId = inputId + '';
	var numericId = inputId.replace(/[^0-9]/g,'');
	var answerDiv = document.getElementById('dhtmlgoodies_a' + numericId);

	objectIdToSlideDown = false;
	
	if(!answerDiv.style.display || answerDiv.style.display=='none'){		
		if(dhtmlgoodies_activeId &&  dhtmlgoodies_activeId!=numericId){			
			objectIdToSlideDown = numericId;
			slideContent(dhtmlgoodies_activeId,(dhtmlgoodies_slideSpeed*-1));
		}else{
			
			answerDiv.style.display='block';
			answerDiv.style.visibility = 'visible';
			
			slideContent(numericId,dhtmlgoodies_slideSpeed);
		}
	}else{
		slideContent(numericId,(dhtmlgoodies_slideSpeed*-1));
		dhtmlgoodies_activeId = false;
	}	
}

function slideContent(inputId,direction)
{
	
	var obj =document.getElementById('dhtmlgoodies_a' + inputId);
	var contentObj = document.getElementById('dhtmlgoodies_ac' + inputId);
	height = obj.clientHeight;
	if(height==0)height = obj.offsetHeight;
	height = height + direction;
	rerunFunction = true;
	if(height>contentObj.offsetHeight){
		height = contentObj.offsetHeight;
		rerunFunction = false;
	}
	if(height<=1){
		height = 1;
		rerunFunction = false;
	}

	obj.style.height = height + 'px';
	var topPos = height - contentObj.offsetHeight;
	if(topPos>0)topPos=0;
	contentObj.style.top = topPos + 'px';
	if(rerunFunction){
		setTimeout('slideContent(' + inputId + ',' + direction + ')',dhtmlgoodies_timer);
	}else{
		if(height<=1){
			obj.style.display='none'; 
			if(objectIdToSlideDown && objectIdToSlideDown!=inputId){
				document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.display='block';
				document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.visibility='visible';
				slideContent(objectIdToSlideDown,dhtmlgoodies_slideSpeed);				
			}else{
				dhtmlgoodies_slideInProgress = false;
			}
		}else{
			dhtmlgoodies_activeId = inputId;
			dhtmlgoodies_slideInProgress = false;
		}
	}
}



function initShowHideDivs()
{

	var divs = document.getElementsByTagName('DIV');
	var divCounter = 1;
	for(var no=0;no<divs.length;no++){
		if(divs[no].className=='dhtmlgoodies_question'){
			divs[no].onclick = showHideContent;
			divs[no].id = 'dhtmlgoodies_q'+divCounter;
			var answer = divs[no].nextSibling;
			while(answer && answer.tagName!='DIV'){
				answer = answer.nextSibling;
			}
			answer.id = 'dhtmlgoodies_a'+divCounter;	
			contentDiv = answer.getElementsByTagName('DIV')[0];
			contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px'; 	
			contentDiv.className='dhtmlgoodies_answer_content';
			contentDiv.id = 'dhtmlgoodies_ac' + divCounter;
			answer.style.display='none';
			answer.style.height='1px';
			divCounter++;
		}		
	}	
}


function ValidateLength(poControl, psLength, psName)
{
	lsLength = poControl.value.length
	if (lsLength > psLength)
		{
			alert("Please enter a value less than or equal to " + psLength + " for " + psName + " field.");
			poControl.focus();
			return false;
		}
	return true;
}
// For Validation of blank field and field with only spaces
function ValidateNull(poControl, psName)
{
	var str=" ";
	var count=0;
	var string;
	var maxLength;

	string=poControl.value
	if(string=="")
		{
			alert("Please enter some value. " + psName + " cannot be blank.");
			poControl.focus();
			return false;
		}

	maxLength=string.length;
	while(count < maxLength)
	{
		if(string==str)
		{
			alert("Please enter some value. " + psName + " cannot have all blanks.")
			poControl.value="";
			poControl.focus();
			return false;
		}
		else
		{
			str+=" ";
			count++;
		}
	}
	return true;
}

// For Validation of Number
function IsNumber(poControl, psName)
{
	if (!isFinite(poControl.value))
		{
			window.alert("Please enter valid Number for " + psName + " field.");
			poControl.value = "";
			poControl.focus();
			return false;
		}
	return true;
}


//For Validation of Email Fields
function IsEmail(poControl, psName)
{
	var count;
	var charcount;

	charcount=0;
	email=poControl.value.toLowerCase();

	for(count=0; count<email.length; count++)
	{
		if(email.charAt(count)=='@')
		{
			if(count==0)
			{
				alert("First character cannot be '@' for " + psName);
				poControl.focus();
				return false;
			}
			else
			{
				//Check atleast one character is present after the @
				charcount+=1;
				if(charcount>1)
				{
					//more than one @ present
					alert("There should be only one '@' for " + psName);
					poControl.focus();
					return false;
				}
				else
				{
					if(email.charAt(count+1)=="")
					{
						alert("There should be atleast one character after '@' for " + psName);
						poControl.focus();
						return false;
					}
				}
			}
		}
	}
	if (charcount < 1 )
	{
		alert("There should be atleast one '@' character for " + psName);
		poControl.focus();
		return false;		
	}
	return true;
}

//For Validation of Alphabetic Fields
function IsAlphabet(poControl, psName)
{	
	arg=poControl.value.toLowerCase();
	for(i=0; i<arg.length; i++)
		if(arg.substring(i,i+1)>"z" || arg.substring(i,i+1)<"a")
			if(arg.substring(i,i+1)!=" ")
			{
				alert("Invalid value for field " + psName);
				poControl.focus();
				return false;
			}
	return true;
}

//For validation of webmaster change password
function validatepassword()
{
//	alert ("submit!");
//check if old password is blank
if (frmchangepassword.txtoldpassword.value == "")
{
alert("You must enter your old password!");
frmchangepassword.txtoldpassword.focus();
return (false);
}

// allow ONLY alphanumeric keys, no symbols or punctuation
// this can be altered for any "checkOK" string you desire
var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var checkStr = frmchangepassword.txtoldpassword.value;
var allValid = true;
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}

if (!allValid)
{
alert("Please enter only letter and numeric characters in the password field.");
frmchangepassword.txtoldpassword.focus();
return (false);
}
// check to see if the field is blank
if (frmchangepassword.txtnewpassword.value == "")
{
alert("You must enter a password!");
frmchangepassword.txtnewpassword.focus();
return (false);
}

// require at least 3 characters be entered
if (frmchangepassword.txtnewpassword.value.length < 8)
{
alert("Please enter at least 8 characters in the password field.");
frmchangepassword.txtnewpassword.focus();
return (false);
}

// allow ONLY alphanumeric keys, no symbols or punctuation
// this can be altered for any "checkOK" string you desire
checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
checkStr = frmchangepassword.txtnewpassword.value;
allValid = true;
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert("Please enter only letter and numeric characters in the password field.");
frmchangepassword.txtnewpassword.focus();
return (false);
}

if (frmchangepassword.txtnewpassword.value!=frmchangepassword.txtnewpassword1.value)
{
alert("Please enter identical values when confirming your password!");
frmchangepassword.txtnewpassword1.focus();
return (false);
}

frmchangepassword.pwdchkval.value="valok";
return (true);
}
//For showing disclaimer
function ShowDisclaimer()
{
	window.open("disclaimer.htm");
}


function validalphanumeric(poControl, psName)
{
	// allow ONLY alphanumeric keys, no symbols or punctuation
	// this can be altered for any "checkOK" string you desire
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	var checkStr = poControl.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		{	if (ch == checkOK.charAt(j))
				break;
			if (ch == " ")
				break;
		}
			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
	
	}
	if (!allValid)
	{
		alert("Please enter only letter and numeric characters in the " + psName);
                poControl.value="";
		poControl.focus();
		return (false);
	}
	return true;
}

function IsAlphabetonly(poControl, psName)
{	var i;
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var checkStr = poControl.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
	
	}
	if (!allValid)
	{
		alert("Please enter only letters in field " + psName);
                poControl.value="";
		poControl.focus();
		return (false);
	}
	return true;
}

function validparam(poControl,psName)
{	var i;
	var arg=poControl.value.toLowerCase();
	for(i=0; i<arg.length; i++)
		if(arg.charAt(i)=="'" || arg.charAt(i)=="\"")
			{
				alert("Remove single code OR double code from " + psName);
				poControl.focus();
				return false;
			}
			
	return true;
}

function IsValidDate(poDateControl, psName) 
{ 
var monthalphaarray=new Array ("###","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); 
var montharray=new Array ("01","02","03","04","05","06","07","08","09","10","11","12"); 
var dayarray=new Array ("31","28","31","30","31","30","31","31","30","31","30","31"); 
var validationflag=1; 
var leapyrflag=0; 
var DateString=new String(poDateControl.value); 
var len=DateString.length; 

if(len == 0) 
	return true;


if(len > 10 ) 
{ 
alert("Invalid Date value for " + psName); 
poDateControl.focus(); 
return false; 
} 

var char1=DateString.charAt(2); 
var char2=DateString.charAt(5); 
//var dayvalue=DateString.substring(0,2);  for dd-mm-yyyy
//var monthvalue=DateString.substring(3,5); for dd-mm-yyyy
var monthvalue=DateString.substring(0,2); 
var dayvalue=DateString.substring(3,5); 

var yearvalue=DateString.substring(6,10); 

if (((parseInt(monthvalue,10) > 0) && (parseInt(monthvalue,10) < 13))) 
validationflag=1; 
else 
validationflag=0; 

if (((parseInt(dayvalue,10) < 1) || (parseInt(dayvalue,10) > 31))) 
validationflag=0; 

if (((parseInt(yearvalue,10) < 1) || (parseInt(yearvalue,10) >2100))) 
validationflag=0 ; 

if ((monthvalue.length !=2)||(dayvalue.length !=2)||(yearvalue.length !=4)) 
validationflag=0; 

if ( (parseInt(yearvalue,10)%4==0 && parseInt(yearvalue,10)%100 !=0) || (parseInt(yearvalue,10)%400==0) ) 
{ 
leapyrflag=1; 
if(monthvalue=="02") 
if(parseInt(dayvalue,10) > 29) 
validationflag=0; 
} 
else 
{ 
if(monthvalue=="02") 
if(parseInt(dayvalue,10) > 28) 
validationflag=0; 
} 

if (validationflag==0) 
{ 
alert("Invalid Date value for " + psName + "\n\n" + "Valid Date Format: MM/DD/YYYY"); 
poDateControl.focus(); 
return false; 
} 
else 
{ 
for(var j=0;j<12;j=j+1) 
{ 
if(monthvalue==montharray[j]) 
if(dayvalue>dayarray[j]) 
{ 
if(leapyrflag==1) 
{ 
if(monthvalue!="02") 
{ 
alert("Date Entered, " + dayvalue + " Exceeds Maximum No of Days for the Entered Month, " + monthalphaarray[parseInt(monthvalue,10)] + " for " + psName); 
return false; 
} 
} 
else 
{ 
alert("Date Entered, " + dayvalue + " Exceeds Maximum No of Days for the Entered Month, " + monthalphaarray[parseInt(monthvalue,10)] + " for " + psName); 
return false; 
} 
} 
} 
} 
return true; 
} 

function addtext(obj,objtext)
{
if(obj.value=="")
  obj.value=objtext
 document.getElementById('testlable').innerHTML = ""
}


function removetext(obj,objtext)
{
var xy
if(obj.value==objtext)
  obj.value="";

  xy=getAbsolutePosition(obj)
  document.getElementById('testlable').style.top = xy.y
  document.getElementById('testlable').style.left = xy.x + 240
  document.getElementById('testlable').innerHTML = 'Enter ' + objtext
}


function getAbsolutePosition(element){
    var ret = new Point();
    for(; 
        element && element != document.body;
        ret.translate(element.offsetLeft, element.offsetTop), element = element.offsetParent
        );
    
    return ret;
}

function Point(x,y){
        this.x = x || 0;
        this.y = y || 0;
        this.toString = function(){
         return '('+this.x+', '+this.y+')' ;
        };
        this.translate = function(dx, dy){
            this.x += dx || 0;
            this.y += dy || 0;
        };
        this.getX = function(){ return this.x; }
        this.getY = function(){ return this.y; }
      
        this.equals = function(anotherpoint){
           return anotherpoint.x == this.x && anotherpoint.y == this.y;
        };
}

function onmouse(objbut,path)
{
objbut.src=path;
}

function isValidate(obj,objtext)
{
if(obj.value==objtext)
{
alert("Enter "+ objtext);
obj.focus();
return false;
}
return true;
}
