/**
 * File:          $Workfile: se_general.js $"
 * Changed:       $Date: 03-05-27 14:03 $"
 * Modified:      $Modtime: 03-05-27 13:55 $"
 * Changed by:    $Author: Mats $"
 * 
 *
 *				
 * @author		Thomas Malm (TM)
 * @version		$Revision: 3 $
 *  
 * Date			Author			Changes
 * 2003-03-27	TM				Created
 */
 
/**
 * Validering av svenska personnummer med kontroll mot kontrollsiffra
 * IN: ååååmmdd-nnnn
 */
function validateSSN(sSSN) {
	var iTmp = 0;
	var iSum = 0;
	var sRegExp = new RegExp("[0-9]{6}-[0-9]{4}");
	
	if (sRegExp.test(sSSN)) {
		var iMonth = parseInt(sSSN.substr(2, 2));
		var iDay = parseInt(sSSN.substr(4, 2));
		
		if (iMonth < 0 || iMonth > 12 || iDay < 0 || iDay > 91) //91 - på tillfälliga personnummer läggs 60 på dagen 
			return false;
		
		sSSN = sSSN.substr(0, 6) + sSSN.substr(7, 4); //ta bort minustecknet		
		
		for (var i = 8; i >= 0; i--) {
			iTmp = parseInt(sSSN.charAt(i));
						
			iTmp = (((9 - i) % 2) + 1) * iTmp; //ojämna positioner multipliceras med 2
			
			iSum += Math.floor((iTmp / 10)) + (iTmp % 10); //ökar summan med tiotals- + entalssiffra
		}
		
		iSum = (10 - (iSum % 10)) % 10; //subtraherar summan från närmaste tiotal
		
		if (iSum == sSSN.charAt(9))
			return true;				
	}
	return false;
}
/**********************************************************************/ 
/*Function name :isDigit(theDigit) */ 
/*Usage of this function :test for an digit */ 
/*Input parameter required:thedata=string for test whether is digit */ 
/*Return value :if is digit,return true */ 
/* else return false */ 
/**********************************************************************/ 
function isDigit(theDigit) 
{ 
var digitArray = new Array('0','1','2','3','4','5','6','7','8','9'),j; 

for (j = 0; j < digitArray.length; j++) 
{if (theDigit == digitArray[j]) 
return true 
} 
return false 

} 
/*************************************************************************/ 
/*Function name :isPositiveInteger(theString) */ 
/*Usage of this function :test for an +ve integer */ 
/*Input parameter required:thedata=string for test whether is +ve integer*/ 
/*Return value :if is +ve integer,return true */ 
/* else return false */ 
/*function require :isDigit */ 
/*************************************************************************/ 
function isPositiveInteger(theString) 
{ 
var theData = new String(theString) 

if (!isDigit(theData.charAt(0))) 
if (!(theData.charAt(0)== '+')) 
return false 

for (var i = 1; i < theData.length; i++) 
if (!isDigit(theData.charAt(i))) 
return false 
return true 
} 
/**
* This method checks if a valid date is entered. A valid format is '2000-01-01'
*/
function checkDateFormat(sDate)
{
	return isDate(sDate,0);
}
/**
* Check if a date is before or after today's date 
* If the comparetype parameter="" then true is returned if today's date is earlier then
* the compared date. If comparetype is !="" then true is returned if today's date is later
* than the compared date.
*/ 
function compareThisDateWithToday(datestring, comparetype)
{
	var today = new Date();
	var comparedate = new Date();
	
	var arrDate = datestring.split("-");
	comparedate.setYear(arrDate[0]);
	month = arrDate[1] - 1;
	comparedate.setMonth(month);
	comparedate.setDate(arrDate[2]);
	
	if (comparetype == "")
	{
		if (today.getTime() <= comparedate.getTime())
		{
			return true;
		} 
		return false;
	}
	
	if (!(comparetype == ""))
	{
		if (today.getTime() >= comparedate.getTime())
		{
			return true;
		} 
		return false;
	}
}

/**********************************************************************/ 
/*Function name :isDate(s,f) */ 
/*Usage of this function :To check s is a valid format */ 
/*Input parameter required:s=input string */ 
/* f=input string format */ 
/* =1,in mm/dd/yyyy format */ 
/* else in dd/mm/yyyy */ 
/*Return value :if is a valid date return 1 */ 
/* else return 0 */ 
/*Function required :isPositiveInteger() */ 
/**********************************************************************/ 
function isDate(s,f) 
{var a1=s.split("/"); 
var a2=s.split("-"); 
var e=true; 
if ((a1.length!=3) && (a2.length!=3)) 
{ 
e=false; 
} 
else 
{if (a1.length==3) 
var na=a1; 
if (a2.length==3) 
var na=a2; 
if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2])) 
{ if (f==1) 
{var d=na[1],m=na[0]; 
} 
else 
{var d=na[2],m=na[1]; 
} 
var y=na[0]; 
if (((e) && (y<1000)||y.length>4)) 
e=false 
if (e) 
{ 
v=new Date(m+"/"+d+"/"+y); 
if (v.getMonth()!=m-1) 
e=false; 
} 
} 
else 
{ 
e=false; 
} 
} 
return e 
}
function CheckApplicationYear(RequestedDate) 
{
	var sCurrentDate = new Date();
	
	if (RequestedDate.substr(0, 4) > sCurrentDate.getFullYear() + 1) {
		return true;
	}
}
