//get cookie value
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = document.cookie.indexOf (arg);
  while (i < clen) {
    var offset = i + alen;
    if (document.cookie.substring(i, offset) == arg)
		  var endstr = document.cookie.indexOf ("&", offset);
  		if (endstr == -1)
			  var endstr = document.cookie.indexOf (";", offset);
  			if (endstr == -1)
   				endstr = document.cookie.length;
  		return unescape(document.cookie.substring(offset, endstr));
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return  "";
}
//check for leap year
function checkLeapYear(year){
	if((year % 4 == 0 && year % 100!=0) || year % 400==0) return true;
	return false;
}
//get total no of days of a month dependending upon the leap year
function getDays(month,year){
	theMonths= new Array(31,(checkLeapYear(year)) ? 29 : 28,31,30,31,30,31,31,30,31,30,31);
	return theMonths[--month];
}
//check the date - whether correct or not
function isTrueDate(year,day,month){
        var dayL = day * 1;
        var yearL = year * 1;
	var actualdays = getDays(parseInt(month),parseInt(yearL));
	if(parseInt(dayL) > actualdays || parseInt(dayL) <= 0) return false;
	return true;
}
//trimming a string
String.prototype.trim = function(){
	return this.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
//email validation
String.prototype.emailChk = function(){
	return /^.+@.+\..{2,}$/.test(this);
}
//radio button value
function rv(r){
	var rvalue='';
	for(var i=0;i<r.length;i++){
		if(r[i].checked){
			rvalue=r[i].value;
			break;
		}
	}
	return rvalue;
}
//selected value
function sv(s){
	var prt=(s.options[s.selectedIndex].value) ? s.options[s.selectedIndex].value : s.options[s.selectedIndex].text;
	return prt;
}
//selected text
function st(s){
	return s.options[s.selectedIndex].text;
}
//selected index
function si(s){
	return s.selectedIndex;
}
//set index of dropdown
function setSI(s,n){
	if(!s.options[n]) alert('Not getting element no. '+n+' in '+s.name+' field');
	else s.selectedIndex=n;
}
//new window
function newWin(sHref) {
	var win = window.open("","remotewin","width=300,height=300,scrollbars=auto");
	win.location.href = sHref;
	if (win.opener == null) win.opener = window;
	win.opener.name = "opener";
}
function largeImage(sHref) {
	var win = window.open("","largeimage",'toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,height=300,width=300,right=100,top=100');
	if (win.opener == null) win.opener = window;
	win.opener.name = "opener";
	win.document.open();
	win.document.write('<html><head><title><!--DomainWord--></title></head><center><br><table><tr><td align=center valign=middle><img src="'+sHref+'"></td></tr></table><br><a href="javascript:self.close();">Close Window</a></center></html>');
	win.document.close();
}
function viewmovie(sHref) {
	var win = window.open("","largeimage",'toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no,height=400,width=400');
	win.location.href = sHref;
	if (win.opener == null) win.opener = window;
	win.opener.name = "opener";
}
function isBannedExists(val,bnval){
	var allBanned = bnval.split(' ');
	for(var i=0;i<allBanned.length;i++){
		if(val.indexOf(allBanned[i]) != -1) return true;
	}
	return false;
}
function isLetter(c){
	return (((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")));
}
function isSpaceLetter(val){
	for(var i=0;i<val.length;i++){
		if(!(isLetter(val.charAt(i)) || isSpace(val.charAt(i)))) return false;
	}
	return true;
}
function isSpace(c){
	return (c == " ");
}
function isDigit(c){
	return ((c >= "0") && (c <= "9"));
}
function isNumber(val){
	for(var i=0;i<val.length;i++){
		if(!isDigit(val.charAt(i))) return false;
	}
	return true;
}
function isNonNumber(val){
	for(var i=0;i<val.length;i++){
		if(isDigit(val.charAt(i))) return false;
	}
	return true;
}
function isAlphaNumericExactSymbol(val,c){
	for(var i=0;i<val.length;i++){
		if(!(isLetter(val.charAt(i)) || isDigit(val.charAt(i)) || val.charAt(i)==c)) return false;
	}
	return true;
}
function isAlphaNumericExceptSymbol(val,c){
	var filter = new RegExp(c);
	for(var i=0;i<val.length;i++){
		if(filter.test(val.charAt(i))) return false;
	}
	return true;
}
function makeStandard(val){
	val = val.toLowerCase();
	var strArray=val.trim().split(/ +/);
	for(var i=0;i<strArray.length;i++){
		strArray[i]=strArray[i].replace(/^.?/,strArray[i].charAt(0).toUpperCase());
	}
	return strArray.join(" ");
}
/////////////////////////////////////////////////
function checkFieldChkBox(fld,fldtxt,l,n,s,mnl,mxl,mnv,mxv,sc,vm,bn,cat){
	if(fld.checked){
		checkField(fld,fldtxt,l,n,s,mnl,mxl,mnv,mxv,sc,vm,bn,cat);
	}else{
		var indx=isInArray(fld);
		errorArray[indx][2]='';
	}
}
function checkField(fld,fldtxt,l,n,s,mnl,mxl,mnv,mxv,sc,vm,bn,cat){
	if(cat == 'n' || (cat == 'c' && fld.value != '')){
		var indx=isInArray(fld);
		if(indx==-1){
			indx=errorArray.length;
			errorArray[indx]=new Array(fld,fldtxt,'');
		}else{
			errorArray[indx][2]='';
		}
		fld.value=fld.value.trim();

		if(l=='no' && n=='yes' && s=='no' && !isNumber(fld.value)){
			if(errorArray[indx][2] == '') errorArray[indx][2]='"'+fldtxt+'" field can have only numbers';
		}else if(l=='yes' && n=='no' && s=='no' && !isSpaceLetter(fld.value)){
			if(errorArray[indx][2] == '') errorArray[indx][2]='"'+fldtxt+'" field can have only alphabetics and spaces';
		}else if(l=='yes' && n=='no' && s=='yes' && !isNonNumber(fld.value)){
			if(errorArray[indx][2] == '') errorArray[indx][2]='"'+fldtxt+'" field can not have numbers';
		}else if(l=='yes' && n=='yes' && !/^$|^yes$|^no$|^all except/.test(s) && !isAlphaNumericExactSymbol(fld.value,s)){
			if(errorArray[indx][2] == '') errorArray[indx][2]='"'+fldtxt+'" field can have only alphabetics, numbers and "'+s+'" symbols';
		}else if(l=='yes' && n=='yes' && /^all except\s*(.*)$/.test(s)){
			s=RegExp.$1;
			if(!isAlphaNumericExceptSymbol(fld.value,s)){
				if(errorArray[indx][2] == '') errorArray[indx][2]='"'+fldtxt+'" field can not have "'+s+'" symbols';
			}
		}
		if(mnl != '' && fld.value.length < mnl){
			if(errorArray[indx][2] == '') errorArray[indx][2]='"'+fldtxt+'" field can not be less than '+mnl+' characters';
		}
		if(mxl != '' && fld.value.length > mxl){
			if(errorArray[indx][2] == '') errorArray[indx][2]='"'+fldtxt+'" field can not be more than '+mxl+' characters';
		}
		if(mnv != '' && parseInt(fld.value) < parseInt(mnv)){
			if(errorArray[indx][2] == '') errorArray[indx][2]='"'+fldtxt+'" field can not be less than '+mnv;
		}
		if(mxv != '' && parseInt(fld.value) > parseInt(mxv)){
			if(errorArray[indx][2] == '') errorArray[indx][2]='"'+fldtxt+'" field can not be more than '+mxv;
		}
		if(vm=='icq' && !isNumber(fld.value)){
			if(errorArray[indx][2] == '') errorArray[indx][2]='"'+fldtxt+'" field can have only numbers';
		}else if(vm=='email' && !fld.value.emailChk()){
			if(errorArray[indx][2] == '') errorArray[indx][2]='"'+fldtxt+'" field only allows the format "a@b.cc"';
		}else if(vm=='date'){
			if(previousField(fld).value.length < 1){
				if(errorArray[indx][2] == '') errorArray[indx][2]='Date of "'+fldtxt+'" field can not have value of less than 1 digit';
			}else if(fld.value.length < 2){
				if(errorArray[indx][2] == '') errorArray[indx][2]='Year of "'+fldtxt+'" field can not have value of less than 2 digit';
			}else if(!isNumber(previousField(fld).value)){
				if(errorArray[indx][2] == '') errorArray[indx][2]='Date of "'+fldtxt+'" field can have only numbers';
			}else if(!isNumber(fld.value)){
				if(errorArray[indx][2] == '') errorArray[indx][2]='Year of "'+fldtxt+'" field can have only numbers';
			}
			var fldval=(fldtxt == "Birthday") ? '19'+fld.value:'20'+fld.value;
			if(!isTrueDate(fldval,previousField(fld).value,sv(previousField(fld,2)))){
				if(errorArray[indx][2] == '') errorArray[indx][2]='Month, Date and Year of "'+fldtxt+'" field doesn\'t match';
			}
		}
		if(sc=='yes') fld.value=makeStandard(fld.value);
		if(bn != '' && isBannedExists(fld.value,bn)){
			var bn1= bn.split(' ').join(', ');
			if(errorArray[indx][2] == '') errorArray[indx][2]='"'+fldtxt+'" field can not have words like "'+bn1+'" etc...';
		}
	}else{
		var indx=isInArray(fld);
		if(indx != -1){
			errorArray[indx][2]='';
		}
	}
}
function totalCheckField(){
	for(var i=0;i<errorArray.length;i++){
		if(errorArray[i][2] != ''){
			alert(errorArray[i][2]);
			errorArray[i][0].focus();
			errorArray[i][0].select();
			return false;
		}
	}
	return true;
}
function isInArray(e){
	for(var j=0;j<errorArray.length;j++){
		if(errorArray[j][0] == e) return j;
	}
	return -1;
}
function fieldNo(fld){
	for(var k=0;k<fld.form.elements.length;k++){
		if(fld==fld.form.elements[k]){
			break;
		}
	}
	return k;
}
function nextField(fld,n){
	var k=fieldNo(fld);
	k=(n) ? k+n:++k;
	return fld.form.elements[k];
}
function previousField(fld,n){
	var k=fieldNo(fld);
	k=(n) ? k-n:--k;
	return fld.form.elements[k];
}
function setLimit(form,curpage){
	form.cpage.value=curpage;
	form.submit();
}
function checkBlankState(fldst,fldcnt){
	var fldsttxt = st(fldst);
	if(fldsttxt == 'Unspecified'){
		alert('Please select the city where you live.');
		fldst.focus();
		return false;
	}
	if(fldcnt){
		var fldcnttxt = st(fldcnt);
		if(fldcnttxt == 'Unspecified'){
			alert('Please select the region where you live.');
			fldcnt.focus();
			return false;
		}
	}
	return true;
}
function checkRadioButtons(){
        for (count=0; count<form.elements.length; count++)
	{
		if(form.elements[count].checked)
		{	
                }
		else
		{	
			alert("Please select an option!")
			return false;
		}
	}
	return true;
}
function checkRadioButtons2(form){
                        question = 0;
			count = 0;
                        sum = 0;
                        total = form.elements.length - 2;
			for(i=0;i<total;i++){
				count+=1;

				if(count < 4){
					if (form.elements[i].checked){
						sum=1;
					}
				}
						
				if (count == 3){
                                        question+=1;
					if (sum==0){
						message='Question '+question+' needs to be answered';
						alert(message);
						return false;
					}
					count = 0;
                                        sum = 0;
				}
			}
			form.submit();
}
function openpopup(name){
var popurl=name;
winpops=window.open(popurl,"","width=400,height=338,resizable");
}
