function AttachEvent(obj,evt,fnc,useCapture)
{
	if (!useCapture) useCapture=false;
	if (obj.addEventListener)
	{
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} 
	else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else
	{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	}
} 

//The following are for browsers like NS4 or IE5Mac which don't support either
//attachEvent or addEventListener
function MyAttachEvent(obj,evt,fnc)
{
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}

function MyFireEvent(obj,evt)
{
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}

isCtrl=false;

function pasteKillUp(e) 
{
	var e = (e) ? e : window.event; 
	var pressedkey = (e.which) ? e.which : e.keyCode;
	if(pressedkey == 17) isCtrl=false;
}

function pasteKillDown(e) 
{
	var e = (e) ? e : window.event; 
	var pressedkey = (e.which) ? e.which : e.keyCode;
	
	if(pressedkey == 17) 
	isCtrl=true; 
	if(pressedkey == 86 && isCtrl == true) 
	{	
		if (e.stopPropagation) 
		{
   			e.stopPropagation();
    		e.preventDefault();
		}
		else
		{
			e.cancelBubble = true;
			e.returnValue = false;
			
			// return false;
		}
	}
}

function changeFormAction (which,whereToGov)
{
	try
	{
		getO("dateOfBirth").value = lPad(getO("dobDay").value,2) + '/' + lPad(getO("dobMonth").value,2) + '/' + lPad(getO("dobYear").value,4);
	}
	catch(e){};
	
	which.action = whereToGov;
	which.submit();
}

function checkSame(field1val,field2val)
{
	var confImg = getO("tick");
	if (isSameValue(field1val,field2val))
		confImg.style.display='inline';
	else
		confImg.style.display='none';
}


function openClose(which,linkA,closetext,openText)
{
	if (getO(which).style.display == 'none')
	{
		getO(which).style.display = 'block';
		linkA.innerHTML = '[-] ' + closetext;
	}
	else
	{
		getO(which).style.display = 'none';
		linkA.innerHTML = '[+] ' + openText;
	}
}

function produceMessage() 
	{
		var message = '';
		var nl = '<br>';
		
		for (var i =0; i<errorArray.length; i++)
		{
			message = message  + rPad((i+1) + '.',5) + errorArray[i] + nl;
		} 
		return message;
	}
	
	
function windowLicker (whichURL, whichName)
{

	var w = self.screen.width,h = self.screen.height;
	var ww = 1000,wh = 650;
	var leftPos = (w-ww)/2,topPos = (h-wh)/2;
	window.open(whichURL, whichName,'toolbar=no, directories=no, location=no, status=no, menubar=no, resizable=no, scrollbars=yes, width=' + ww + ',height='+wh+',top='+topPos+',left='+leftPos);
	// return false;
}

function passwordStrength(password)
{	
	    var score = 0 ;
	 
	    //password length
	    score += password.length * 4
	    score += ( checkRepetition(1,password).length - password.length ) * 1;
	    score += ( checkRepetition(2,password).length - password.length ) * 1;
	    score += ( checkRepetition(3,password).length - password.length ) * 1;
	    score += ( checkRepetition(4,password).length - password.length ) * 1;
	
	    //password has 3 numbers
	    if (password.match(/(.*[0-9].*[0-9].*[0-9])/))  score += 5 ;
	    
	    //password has 2 symbols
	    if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5 ;
	    
	    //password has Upper and Lower chars
	    if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  score += 10 ;
	    
	    //password has number and chars
	    if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  score += 15 ;
	    //
	    //password has number and symbol
	    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/))  score += 15 ;
	    
	    //password has char and symbol
	    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/))  score += 15 ;
	    
	    //password is just numbers or chars
	    if (password.match(/^\w+$/) || password.match(/^\d+$/) )  score -= 10 ;
	    
	    //alert (score); //verifing 0 < score < 100
	    if ( score < 0 )  score = 0 
	    if ( score > 100 )  score = 100 
	   	return score;
}

function buildIndicator(password)
{
		var greenScore = Math.ceil(passwordStrength(password)*2.55);
		var redScore = 255-greenScore;
		var hexVal = '#' + lPad (redScore.toBase(16),2) + lPad (greenScore.toBase(16),2) + '00'
		getO("indicator").style.backgroundColor = hexVal;
		getO("score").innerHTML = passwordStrength(password) + '/100';
}

function checkPasswords()
{
	if (getO("password").value == getO("renterPassword").value && validateNotEmpty(getO("password").value) && getO("password").value.length >= 6)
	{
		getO("tick").style.display='inline';
		return true;
	}
	else
	{
		getO("tick").style.display='none';
		return false;
	}
}

function checkRepetition(pLen,str) {
    res = ""
    for ( i=0; i<str.length ; i++ ) {
        repeated=true
        for (j=0;j < pLen && (j+i+pLen) < str.length;j++)
            repeated=repeated && (str.charAt(j+i)==str.charAt(j+i+pLen))
        if (j<pLen) repeated=false
        if (repeated) {
            i+=pLen-1
            repeated=false
        }
        else {
            res+=str.charAt(i)
        }
    }
    return res
}

Number.prototype.toBase = function(b, c){
    var s = "", n = this;
    if(b > (c = (c || "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz").split("")).length || b < 2) return "";
    while(n)
        s = c[n % b] + s, n = Math.floor(n / b);
    return s;
};

String.prototype.parseInt = function(b, c){
    var s = 0, n, l = (n = this.split("")).length, i = 0;
    if(b > (c = c || "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz").length || b < 2) return NaN;
    while(l--)
        s += c.indexOf(n[i++]) * Math.pow(b, l);
    return s;
};



function lPad (str,lenStr)
{
	var padStr = 0;
	var returnStr = '';
	var loops = lenStr-str.length;
	
	for ( var i=0; i < loops; i++)
	{
		returnStr = returnStr + padStr;
	}
	
	return 	returnStr + str;
}

function rPad (str,lenStr)
{
	var padStr = ' ';
	var returnStr = '';
	var loops = lenStr-str.length;
	
	
	for ( var i=0; i < loops; i++)
	{
		returnStr = returnStr + padStr;
	}
	
	return str	+ returnStr;
}


function getO(which)
{
	return document.getElementById(which);
}

function isSameValue(field1value,field2value)
{
	if (field1value == field2value)
		return true
	else
		return false
}

function getNO(which)
{
	return document.getElementsByName(which);
}

function showHide(which)
{
	if (which == 0)
	{
		getO('memberRow').style.display ='';
		getO('addressDetailsRow1').style.display ='none';
		getO('addressDetailsRow2').style.display ='none';
		getO('streetName').value ='';
		getO('postCode').value ='';
	}
	else
	{
		getO('memberRow').style.display ='none';
		getO('addressDetailsRow1').style.display ='';
		getO('addressDetailsRow2').style.display ='';
		getO('memberNumber').value ='';
	}
}

function showHide2(which)
{
	if (which == 0)
	{
		getO('emailRow').style.display ='';
		getO('phoneRow1').style.display ='none';
		getO('phoneRow2').style.display ='none';
		getO('phone').value ='';
		getO('time').value ='';
	}
	else
	{
		getO('emailRow').style.display ='none';
		getO('phoneRow1').style.display ='';
		getO('phoneRow2').style.display ='';
		getO('email').value ='';
	}
}




function toggleContent(which,content)
{
	if (which.value == '')
	{
		which.value = content;
		which.style.color='#cccccc';	
	}
	else if (which.value == content)
	{
		which.value = '';
		which.style.color='#000000';
	}
	else
		which.style.color='#000000';
}

function hasNoSpaces(value)
{
	return (value.indexOf(' ') == -1);
}

function isNumber(value)
{
	var objRegExp  = /(^-?\d\d*$)/;
	return objRegExp.test(value);
}

function checkMemberNumber(value)
{
if(hasNoSpaces(value) && isNumber(value))
	return true;
else 
	return false;
}

function validateNotEmpty(value) 
{
	var strTemp = value;
	strTemp = trimAll(value);
	if(strTemp.length > 0)
	{
   	return true;
	}
   return false;
}

function checkValidMail(str)
{
	//var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var filter  = /^.+\@.+\..+$/;
	if (filter.test(str))
		return true;
	else
		return false;
}



function trimAll( strValue ) 
{
	var objRegExp = /^(\s*)$/;
	
	if(objRegExp.test(strValue)) 
	{
		strValue = strValue.replace(objRegExp, '');
		if( strValue.length == 0)
			return strValue;
	}
	
	objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
	if(objRegExp.test(strValue)) 
	{
		strValue = strValue.replace(objRegExp, '$2');
	}
	return strValue;
}

function checkDate(thisDay, thisMonth, thisYear)
{
	var dayobj = new Date(thisYear, thisMonth-1, thisDay);
		
	if ((dayobj.getMonth()+1!=thisMonth)||(dayobj.getDate()!=thisDay)||(dayobj.getFullYear()!=thisYear))
		return false;
	else
		return true;
}


