// JavaScript Document
function resizeImage(img, maxWidth){
	var im = img;
	if(maxWidth < im.width)
		im.width = maxWidth;
}

function dateCompare (value1, value2){
	var date1, date2, month1, month2, year1, year2;
	year1 = value1.substring (0, value1.indexOf ("-"));
	month1 = value1.substring(value1.indexOf ("-")+1, value1.lastIndexOf ("-"));
	date1 = value1.substring (value1.lastIndexOf ("-")+1, value1.length);
	year2 = value2.substring (0, value2.indexOf ("-"));
	month2 = value2.substring(value2.indexOf ("-")+1, value2.lastIndexOf ("-"));
	date2 = value2.substring (value2.lastIndexOf ("-")+1, value2.length);
	if (year1 > year2) return 1;
	else if (year1 < year2) return -1;
	else if (month1 > month2) return 1;
	else if (month1 < month2) return -1;
	else if (date1 > date2) return 1;
	else if (date1 < date2) return -1;
	else return 0;
}

function counterText(field, countfield, maxlimit){
	if (field.value.length > maxlimit) // if the current length is more than allowed
		field.value =field.value.substring(0, maxlimit); // don't allow further input
	else
		countfield.value = maxlimit - field.value.length;
}// set the display field to remaining number

/*	validation function */
function trim(b){
	var i=0;
	while(b.charAt(i)==" "){
		i++;
	}
	b=b.substring(i,b.length);
	len=b.length-1;
	while(b.charAt(len)==" "){
		len--;
	}
	b=b.substring(0,len+1);
	return b;
}

function isCharsInBag (s, bag , field){
	var i;
	// Search through string's characters one by one.
	// If character is in bag, append to returnString.
	if(s.length > 40){
		new Effect.Appear('error2');
		document.getElementById('error2').innerHTML = 'Subject can\'t exceed 40 character';
		document.getElementById(field).style.border='2px red solid;';
		return false;
	}
	for (i = 0; i < s.length; i++){
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1){
			new Effect.Appear('error2');
			document.getElementById('error2').innerHTML = 'Must not contain special characters';
			document.getElementById(field).style.border='2px red solid;';
			return false;
		}
	}
	return true;
}

function isAlphaNumeric(s,field){
	if(isEmpty(s)){
		new Effect.Appear('error2');
		document.getElementById('error2').innerHTML = 'can\'t be blank';
		document.getElementById(field).style.border='2px red solid;';
		return false
	}
	return true;
}

function isValidData(s,field){
	if(isEmpty(s)){
		new Effect.Appear('error');
		document.getElementById(field).style.border='2px red solid;';
		return false
	}else{
		return true;
	}
}

function isEmpty(s){
	s=trim(s);
	return ((s == null) || (s.length == 0))
}

//function to check valid email id
function checkmailadd(s){
	var i, found1=false, found2=false, count1=0;
	for (i = 0; i < s.length; i++){
		// Check that current character isn't whitespace.
		if(s.charAt(i)== ' '){found1=true;found2=true;count1=2;}
		if(s.charAt(i)== '@'){found1=true;count1++;}
		if(s.charAt(i)== '.'){found2=true;}
	}
	if(found1==true && found2==true){
		if(count1 > 1)
			return false;
		else
			return true;
	}else
		return false;
	return true;
}

function assign(){
	s = document.getElementById('InviteEmail').value;
	document.getElementById('InviteMailto').value = s;
}

function isDate(strdate){
	var datedelimiter = '/';
	var datesplit = strdate.split(datedelimiter);
	if (datesplit.length > 3){return false;}
	var month = 0;
	month = datesplit[0];
	if (month < 1 || month >12 ){return false;}
	if (isNaN(datesplit[0])){return false;}
	else if (isNaN(datesplit[1])){return false;}
	else if (isNaN(datesplit[2])){return false;}
	else{
		var yearLn = (datesplit[2].length);
		var year= datesplit[2];
		if (yearLn==1){return false;}
		if (yearLn==3){return false;}
		if (year<1){return false;}
		if (yearLn==2){
			year = '20'+ year;
		}
		if(year<2000){return false;}
		if(year>2999){return false;}
		var day = parseInt(datesplit[1],10);
		if(day<0){return false;}
		if (day>31){return false;}
		if ((day > 30) && ((month == 4) || (month == 6) || (month == 9) || (month == 11))){return false;}
		if (month == 2){// This calculates the basic leap year no matter the format, i.e. 2000 or 00. 
			var leap = ((year/4) == parseInt(year/4));
			if (leap){
				if (day > 29){return false;}
			}else{
				if (day > 28){return false;}
			}
		}
	}
	return true;
}

function isbigdateTime(StDate, StTime, EdDate, EdTime){
	var DateDelimiter='/';
	var TimeDelimiter=':';
	var StDSplit = StDate.split(DateDelimiter);//Splite date into MM/DD/YYYY
	if (StDSplit.length>3){return false;}
	var StMM=parseInt(StDSplit[0]);
	var StDD=parseInt(StDSplit[1]);
	var StYY=parseInt(StDSplit[2]);
	var StTSplit = StTime.split(TimeDelimiter);//Splite time into H:M
	if (StTSplit.length>2){return false;}
	var StH=StTSplit[0];
	var StM=StTSplit[1];
	var EdDSplit = EdDate.split(DateDelimiter);//Splite date into MM/DD/YYYY
	if (EdDSplit.length>3){return false;}
	var EdMM=parseInt(EdDSplit[0]);
	var EdDD=parseInt(EdDSplit[1]);
	var EdYY=parseInt(EdDSplit[2]);
	var EdTSplit = EdTime.split(TimeDelimiter);//Splite time into H:M
	if (EdTSplit.length>2){return false;}
	var EdH=EdTSplit[0];
	var EdM=EdTSplit[1];
	if(StYY>EdYY){
		form1.txtDateEnd.focus();
		return false;
	}
	else if(StYY==EdYY && StMM>EdMM){return false;}	
	else if(StYY==EdYY && StMM==EdMM && StDD>EdDD){return false;}	
	else if(StYY==EdYY && StMM==EdMM && StDD==EdDD && StH>EdH){
		form1.selEndTime.focus(); 
		return false;
	}else if(StYY==EdYY && StMM==EdMM && StDD==EdDD && StH==EdH && StM>=EdM){
		form1.selEndTime.focus(); 
		return false;
	}	return true;	
}

function OpenWin(width,height,URL,title){
	window.open(URL,'Myauditions',"height=" + height + ",width=" + width + ",toolbar=no,location=no,directories=no,status=no,menubar=no,,scrollbars=yes,resizable=yes");
}

function SelectOption(pObj,pSelOption){
	for(var i=0;i<pObj.options.length;i++){
		if(pObj.options[i].value==pSelOption){
			pObj.options[i].selected=true;
		}
	}
}

function trim(b){
	var i=0;
	while(b.charAt(i)==" "){
		i++;
	}
	b=b.substring(i,b.length);
	len=b.length-1;
	while(b.charAt(len)==" "){
		len--;
	}
	b=b.substring(0,len+1);
	return b;
}

//function to open popup window with specified size
function jPopup(sJName, sJURL, sJWidth, sJHeight, sJToolbar){
	if (trim(sJToolbar) == ""){
		sJToolbar = "no";
	}
	sJName = window.open(sJURL, sJName, 'toolbar=' + sJToolbar + ',status=no,width=' + sJWidth +',height=' + sJHeight);
}

//FUNCTION TO CHECK EMPTY SPACES AND RETURN ERROR MESSAGE
function validateSpaces(var1, var2){
	var fld = "";
	fld = trim(var1.value);
	if(fld == ""){
		alert("Please enter a value in \"" +var2 + "\"");
		var1.focus();
		return 0;
	}
	return 1;
}

//Validation for numeric fields
function validateNumeric(var1, var2){
	var fld = "";
	fld = var1.value;
	if ((isCharsInBag (fld, "0123456789") == false || fld < 1) && fld != ""){
		alert("Please enter a numeric value for the \"" + var2 + "\" field.");
		var1.focus();
		return 0;
	}	
	return 1;
}

//Validation for email fields
function validateEmail(var1, var2){
	var fld = "";
	fld = var1.value;
	if (checkmailadd(fld) == false && fld != ""){
		alert("Please enter a valid email address in the \"" + var2 + "\" field.");
		var1.focus();
		return 0;
	}
	return 1;
}

//Validation for maxlength
function validateMaxlength(var1, var2, varMaxlength){
	var fld = "";
	fld = var1.value;
	if (fld.length > varMaxlength && fld != ""){
		alert("You can enter maximum of " + varMaxlength + " characters in the \"" + var2 + "\" field.");
		var1.focus();
		return 0;
	}
	return 1;
}

// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.
// The function returns true if a valid date, false if not.
// ******************************************************************
function isDate(dateStr){
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	months= new Array(12);
	months[0]="Jan";months[1]="Feb";months[2]="Mar";months[3]="Apr";months[4]="May";months[5]="Jun";months[6]="Jul";months[7]="Aug";months[8]="Sep";months[9]="Oct";months[10]="Nov";months[11]="Dec";
	if (matchArray == null){
		alert("Please enter date as either mm/dd/yy or mm-dd-yy.");
		return false;
	}
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[5];
	if (month < 1 || month > 12){// check month range
		alert("Month must be between 1 and 12.");
		return false;
	}
	if (day < 1 || day > 31){
		alert("Day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31){
		alert("Month "+ months[month-1]+" doesn't have 31 days!");
		return false;
	}
	
	if (month == 2){// check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)){
			alert("February " + year + " doesn't have " + day + " days!");
			return false;
		}
	}
	return true; // date is valid
}

function ContinentCountry(){
	selval(document.frmReg.Continent, document.frmReg.hideContinent.value );
	jFilterCTR();
	selval(document.frmReg.Country, document.frmReg.hideCountry.value );
}

function CountryContinent(){
	jFilterCTR();
	selval(document.frmReg.Country, document.frmReg.CountryID.value);
}

//#### Confirmation message on Reset
function jFConfirmReset(theForm){
	if (confirm("All fields including Profile will be Reset to their default values!") == false){
		return false;
	}
	return true;
}

//#### function to submit form 
//Arguments: Action URL, Form name
function jFSubmit(sFURL, theForm){
	theForm.action = sFURL;
	theForm.submit();
}

//#### Confirmation message on Reset
function jFConfirmResetEdit(theForm){
	if (confirm("All fields including Profile will be Reset to their default values!") == true){
		theForm.reset();
		theForm.State.disabled = false;
		theForm.OtherState.disabled = false;
		setfields();
	}
}

//#### Reset with Confirmation message
function jFConfirmResetMsg(theForm, sjFMsg){
	setfields();
	return true;
}

function selval(sellist, selvalue){
	for(iVar=0;iVar<sellist.options.length;iVar++){
		if (selvalue == ""){
			sellist.selectedIndex = 0;
			return;
		}else if (sellist.options[iVar].value == selvalue){
			sellist.selectedIndex = iVar;
			return;
		}
	}
	return;
}

function getObj(name){
	if (document.getElementById){
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}else if (document.all){
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}else if (document.layers){
		this.obj = getObjNN4(document,name);
		this.style = this.obj;
	}
}

function getObjNN4(obj,name){
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++){
		if (x[i].id == name)
			foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

function createDiv(event,id){
	if(!document.getElementById(id)){
		newele = document.createElement('div');
		newele.appendChild(document.createTextNode(' '));
		newele.setAttribute('id',id);
		document.body.appendChild(newele);
		ele = new getObj(id);
		ele.style.position = 'absolute';
		pos_x = event.clientX?(event.clientX):event.pageX;
		pos_y = event.clientY?(event.clientY):event.pageY;
		ele.style.top = pos_y;
		ele.style.left = pos_x;
		ele.style.display = "inline" ;
	}
}

function hideDiv(event,id){
	if(document.getElementById(id)){
		document.getElementById(id).style.visibility="hidden";
	}
}

function showDiv(event,id){
	if(document.getElementById(id)){
		document.getElementById(id).style.visibility="visible";
	}
}

function manage(args){
	var item_id = args.item_id;
	var item_name = args.item_name;
	var item_code = args.item_code;
	var sport_id = args.sport_id;
	var country_id = args.country_id;
	var league_id = args.league_id;
	var image_flag = args.image_flag;
	var parent_id = args.parent_id;
	item_name = item_name.replace('&','_and_');
	item_code = item_code.replace('&','_and_');
	var featureWindow = null;
	wWidth = 500;
	wHeight = 500;
	LeftPosition = (screen.width) ? (screen.width-wWidth)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-wHeight)/2 : 0;
	var mypage= "/categories/manage_club/" + item_id + "/" + item_name + "/" + item_code + "/" + sport_id + "/" + country_id + "/" + league_id + "/" + image_flag + "/" + parent_id;// + "?" + opener_url
	var settings = 'height=' + wHeight + ',width=' + wWidth + ',top=' + TopPosition+',left=' + LeftPosition + ',scrollbars=no,menubar=0,resizable=no,location=no,status=no';
	featureWindow = window.open(mypage, "mynew", settings);
}

function featureit(){
	var type = arguments[0];
	var item_id = arguments[1];
	var sport_id = arguments[2];
	var featureWindow = null;
	wWidth = 350;
	wHeight = 470;
	LeftPosition = (screen.width) ? (screen.width-wWidth)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-wHeight)/2 : 0;
	var url = "/categories/manageFeatured/"+ type + "/" + item_id + "/" + sport_id;
	var settings = 'height='+wHeight+',width='+wWidth+',top='+TopPosition+',left='+LeftPosition+',scrollbars=no,menubar=0,resizable=no,location=no,status=no';
	featureWindow = window.open(url, "mynew", settings);
}

function featurearena(){
	var matchid = arguments[0];
	wWidth = 350;
	wHeight = 470;
	LeftPosition = (screen.width) ? (screen.width-wWidth)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-wHeight)/2 : 0;
	var url = "http://sports.solodi.com/categories/featureArena/"+ matchid;
	var settings = 'height='+wHeight+',width='+wWidth+',top='+TopPosition+',left='+LeftPosition+',scrollbars=no,menubar=0,resizable=no,location=no,status=no';
	featureWindow = window.open(url, "mynew", settings);
}

function admin_manage(){
	var id = arguments[0];
	var type = arguments[1];
	var action = ' ';
	var extra = ' ';	
	var add_one='';	
	if(arguments.length>2){
		var ctid = arguments[2];
		add_one = "/" + ctid;
	}
	if(arguments.length>3){
		action = arguments[3];
	}
	wWidth = 500;
	wHeight = 520;
	LeftPosition = (screen.width) ? (screen.width-wWidth)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-wHeight)/2 : 0;
	var url = "/admin/categories/manage_it/"+ id + "/" + type + "/"+ action + "/" + extra + add_one;
	var settings = 'height='+wHeight+',width='+wWidth+',top='+TopPosition+',left='+LeftPosition+',scrollbars=no,menubar=0,resizable=no,location=no,status=no';
	featureWindow = window.open(url, "mynew", settings);
	if (window.focus){featureWindow.focus()}
}

function admin_manage_club(){
	wWidth = 620;
	wHeight = 620;
	LeftPosition = (screen.width) ? (screen.width-wWidth)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-wHeight)/2 : 0;
	var settings = 'height='+wHeight+',width='+wWidth+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,menubar=0,resizable=no,location=no,status=no';
	if(arguments.length<3){
		var club_name	= arguments[0];
		var category_id	= arguments[1];
		var url = "http://sports.solodi.com/admin/categories/manage_edit_club/"+ club_name+"/"+category_id;
	}else{
		var sports_name	= arguments[0];
		var club_name	= arguments[1];
		var league_name	= arguments[2];
		var country_name= arguments[3];
		var url = "http://sports.solodi.com/admin/categories/manage_club/"+ sports_name +"/"+ club_name + "/" + league_name + "/"+ country_name;
	}
	featureWindow = window.open(url, "mynew", settings);
	if (window.focus){featureWindow.focus()}
}

function admin_manage_league(){	
	var sports_name	= arguments[0];
	var league_name	= arguments[1];
	var country_name= arguments[2];
	wWidth = 620;
	wHeight = 620;
	LeftPosition = (screen.width) ? (screen.width-wWidth)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-wHeight)/2 : 0;
	var url = "/admin/categories/manage_league/"+ sports_name +"/"+ league_name + "/"+ country_name;
	var settings = 'height='+wHeight+',width='+wWidth+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,menubar=0,resizable=no,location=no,status=no';
	featureWindow = window.open(url, "mynew", settings);
	if (window.focus){featureWindow.focus()}
}

function admin_manage_country(){
	var country_name= arguments[0];
	wWidth = 620;
	wHeight = 620;
	LeftPosition = (screen.width) ? (screen.width-wWidth)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-wHeight)/2 : 0;
	var url = "/admin/categories/manage_country/"+ country_name;
	var settings = 'height='+wHeight+',width='+wWidth+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,menubar=0,resizable=no,location=no,status=no';
	featureWindow = window.open(url, "mynew", settings);
	if (window.focus){featureWindow.focus()}
}

function admin_manage_category(){	
	var category_id = arguments[0];
	wWidth = 620;
	wHeight = 620;
	LeftPosition = (screen.width) ? (screen.width-wWidth)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-wHeight)/2 : 0;
	var url = "/admin/categories/manage_category/" + category_id;
	var settings = 'height='+wHeight+',width='+wWidth+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,menubar=0,resizable=no,location=no,status=no';
	featureWindow = window.open(url, "mynew", settings);
	if (window.focus){featureWindow.focus()}
}

function openEditer(filename,folderName,action){
	wWidth = 600;
	wHeight = 600;
	LeftPosition = (screen.width) ? (screen.width-wWidth)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-wHeight)/2 : 0;
	var settings = 'height='+wHeight+',width='+wWidth+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,menubar=0,resizable=yes,location=no,status=no';
	var temp = window.open('/users/editLanguagaFile/'+ filename + "/" + folderName + "/" + action,"newwindow",settings);
}

var resetRolls = new Object();

function resetimage(src){
	this.src=src;
	this.confirm=true;
	this.alt="Reset";
	this.write=resetimage_write;
}

function resetimage_write(){
	document.write('<A ');
	if (this.rollover){
		if (! this.name){
			alert('to create a rollover you must give the image a name');
			return;
		}
		resetRolls[this.name] = new Object();
		resetRolls[this.name].over = new Image();
		resetRolls[this.name].over.src=this.rollover;
		resetRolls[this.name].out = new Image();
		resetRolls[this.name].out.src=this.src;
		document.write(' onMouseOver="if (document.images)document.images[\'' + this.name + '\'].src=resetRolls[\'' + this.name + '\'].over.src"' + ' onMouseOut="if (document.images)document.images[\'' + this.name + '\'].src=resetRolls[\'' + this.name + '\'].out.src"');
	}
	document.write(' HREF="javascript:');
	if (this.confirm)
		document.write('if(confirm(\'Are you sure you want to reset?\'))');
	document.write('document.forms[' + (document.forms.length - 1) + '].reset();void(0);">');
	document.write('<IMG SRC="' + this.src + '" ALT="' + this.alt + '"');
	document.write(' BORDER=0');
	if (this.name)document.write(' NAME="' + this.name + '"');
	if (this.height)document.write(' HEIGHT=' + this.height);
	if (this.width)document.write(' WIDTH=' + this.width);
	if (this.otheratts)document.write(' '+ this.otheratts);
	document.write('></A>');
}

/* SUGGEST CLUB IMAGE */
function suggestImage(){
	var item_name= arguments[0];
	var item_type= arguments[1];
	var item_parent='';
	if(arguments.length>2){
		var item_parent = '/' + arguments[2];
	}
	wWidth = 500;
	wHeight = 500;
	LeftPosition = (screen.width) ? (screen.width-wWidth)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-wHeight)/2 : 0;
	var url = "/users/suggest_image/"+ item_name+"/"+item_type+item_parent;
	var settings = 'height='+wHeight+',width='+wWidth+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,menubar=0,resizable=no,location=no,status=no';
	featureWindow = window.open(url, "mynew", settings);
	if (window.focus){featureWindow.focus()}
}

function suggestLeagueImage(){
	var league_name = arguments[0];
	var country_name = arguments[1];
	var cat_id = arguments[2];		
	wWidth = 500;
	wHeight = 500;
	LeftPosition = (screen.width) ? (screen.width-wWidth)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-wHeight)/2 : 0;
	var url = "/users/suggest_league_image/"+ league_name+"/"+country_name+"/"+cat_id;
	var settings = 'height='+wHeight+',width='+wWidth+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,menubar=0,resizable=no,location=no,status=no';
	featureWindow = window.open(url, "mynew", settings);
	if (window.focus){featureWindow.focus()}
}

function suggestCountryImage(){
	var country_name = arguments[0];
	wWidth = 500;
	wHeight = 500;
	LeftPosition = (screen.width) ? (screen.width-wWidth)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-wHeight)/2 : 0;
	var url = "/users/suggest_country_image/"+country_name;
	var settings = 'height='+wHeight+',width='+wWidth+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,menubar=0,resizable=no,location=no,status=no';
	featureWindow = window.open(url, "mynew", settings);
	if (window.focus){featureWindow.focus()}
}

function hideOpened(id){	
	var openedId = document.getElementById('openedId').value;
	if(id != '' && openedId != ''){
		if(document.getElementById('t_block_' + openedId).style.display != 'none')
			document.getElementById('t_block_' + openedId).style.display='none';
	}
	document.getElementById('openedId').value = id;
}