/*************************************************************************

		Miscellaneous functions

*************************************************************************/

function openVideoPlayer(url,width,height) {
	w = width  + 60;
	h = height + 60;

	LeftPosition = (screen.width)  ? (screen.width-w)  / 2 : 200;
	TopPosition  = (screen.height) ? (screen.height-h) / 2 : 150;
	settings = 'width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',resizable';

	wName = '/mediaPlayer?url='+url+'&type=a&width='+width+'&height='+height;
	window.open(wName, '_blank', settings);
}

function openAudioPlayer(url,width,height) {
	w = width  + 60;
	h = height + 60;

	LeftPosition = (screen.width)  ? (screen.width-w)  / 2 : 200;
	TopPosition  = (screen.height) ? (screen.height-h) / 2 : 150;
	settings = 'width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',resizable';

	wName = '/mediaPlayer?url='+url+'&type=a&width='+width+'&height='+height;
	window.open(wName, '_blank', settings);
}

function displayBox(elementId) {
	document.getElementById(elementId).style.display = 'block';
}

function hideBox(elementId) {
	document.getElementById(elementId).style.display = 'none';
}

function displayBoxAlt(elementId, size) {
	
	var tmpId;
	var tmpElementId;
	
	for(i = 1; i <= size; i++) {
		tmpId = i;
		tmpElementId = 'trackListing' + tmpId;	
		hideBox(tmpElementId);
		
		tmpElementId = 'buyVendor' + tmpId;	
		hideBox(tmpElementId);
	}
	displayBox(elementId);
}

function expandBiography() {	
	hideBox('element1A');
	displayBox('element1B');
}

function collapseBiography() {	
	displayBox('element1A');
	hideBox('element1B');
}

function expandTours() {
	hideBox('fourTours');
	displayBox('allTours');
}

function expandRingtones(id,begin,end) {
	for(i = (begin+1); i <= end; i++) {
		elementId = id + i;
		document.getElementById(elementId).style.display = '';
	}
	hideBox('ringtonesMore');
	displayBox('ringtonesLess');
}

function collapseRingtones(id,begin,end) {	
	for(i = (begin+1); i <= end; i++) {
		elementId = id + i;
		document.getElementById(elementId).style.display = 'none';
	}
	displayBox('ringtonesMore');
	hideBox('ringtonesLess');
}

function submitBlog() {
	var name     = document.blogCommentForm.author.value;
	var email    = document.blogCommentForm.email.value;
	var comments = document.blogCommentForm.comment.value;
	
	var valid = true;
	
	if(name == '') {
		document.getElementById('nameError').style.display = 'inline';
		valid = false;
	}
	else {
		document.getElementById('nameError').style.display = 'none';
	}
	
	if(!emailValidate(email)) {
		document.getElementById('emailError').style.display = 'inline';
		valid = false;
	}
	else {
		document.getElementById('emailError').style.display = 'none';
	}
	
	if(comments == '') {
		document.getElementById('commentsError').style.display = 'inline';
		valid = false;
	}
	else {
		document.getElementById('commentsError').style.display = 'none';
	}
	
	if(valid)
		document.blogCommentForm.submit();
	else
		return false;
}

function emailValidate(value) {
	var valid = false;
	if(value.length != 0 && value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/))
		valid = true;
	return valid;
}

function modifyNewsletter() {
	var email1 = document.modifyNewsletterSignUp.email.value;
	var email2 = document.modifyNewsletterSignUp.newEmail.value;
	var email3 = document.modifyNewsletterSignUp.emailRetype.value;
	
	
	
	if(email2 != '' && email2 != email3){
		document.getElementById('emailError').innerHTML = 'Email IDs Mismatch.';
		document.getElementById('emailError').style.visibility = 'visible';
		return false;
	}
	else if(email2 != '' && !emailValidate(email2)){
		document.getElementById('emailError').innerHTML = 'Invalid Email ID.';
		document.getElementById('emailError').style.visibility = 'visible';
		return false;
		}
		else{
			if(email2 != ''){
			document.modifyNewsletterSignUp.email.value = email2;
		}
		//alert(document.modifyNewsletterSignUp.email.value);
		document.modifyNewsletterSignUp.submit();
		}
}



function submitNewsletter() {
	var name = document.newsletterSignUp.firstname.value;
	var country = document.newsletterSignUp.country.value;
	var province = document.newsletterSignUp.state.value;
	var email1 = document.newsletterSignUp.email.value;
	var email2 = document.newsletterSignUp.emailRetype.value;
	var cell1 = document.getElementById('cell1').value;
	var cell2 = document.getElementById('cell2').value;
	var cell3 = document.getElementById('cell3').value;
	var network = document.newsletterSignUp.networkId.value;

	var prefs  = false;	
	var valid = true;
	var mobile = cell1 + cell2 + cell3;
	
	document.newsletterSignUp.mobile.value = mobile;

	if(name == '') {
		document.getElementById('nameError').style.display = 'inline';
		document.getElementById('nameError').innerHTML='Please enter your name.'
		document.getElementById('nameError').style.lineHeight="1.6em";
		valid = false;
	}
	else {
		document.getElementById('nameError').style.display = 'none';
	}
	if( (name != '') && (name.match(/[0-9!@#$%^&*()_=+\\|.\{\}\[\];:",<>/?]+/g))  ) {
		nameobj=document.getElementById('nameError');
		if((nameobj!="")||(nameobj!=null)){
			document.getElementById('nameError').style.display = 'inline';
		}
		document.getElementById('nameError').style.lineHeight="13px";
		document.getElementById('nameError').innerHTML = 'The characters !@#$%^&*()_=+\|.{}[];:",<>/? and digits are not allowed in first name';
		valid = false;
	}	
	if(country == 'Canada' && province == '') {
		document.getElementById('provinceError').style.display = 'inline';
		valid = false;
	}
	else {
		document.getElementById('provinceError').style.display = 'none';
	}
	
	for(i = 0;i < 7; i++) {
		if(document.newsletterSignUp.newsletterId[i].checked) {
			prefs = true;
			break;
		}			
	}
	
	if(!prefs && email1 != '') {
		document.getElementById('prefsError').style.display = 'inline';
		valid = false;
	}
	else {
		document.getElementById('prefsError').style.display = 'none';
	}
	
	if((email1 == '' && email2 == '' && mobile == '')
	   || (email1 != email2)
	   || (email1 != '' && !emailValidate(email1)) ) {	
	
		if(email1 == '' && email2 == '' && mobile == '')
			document.getElementById('emailError').innerHTML = 'Please enter your email ID.';
		else if(email1 != email2)
			document.getElementById('emailError').innerHTML = 'Email IDs Mismatch.';
		else if(email1 != '' && !emailValidate(email1))
			document.getElementById('emailError').innerHTML = 'Invalid Email ID.';
			
		document.getElementById('emailError').style.visibility = 'visible';
		valid = false;
	}
	else {
		document.getElementById('emailError').style.visibility = 'hidden';
	}
	
	if(mobile != '' && network == '') {
		
		document.getElementById('networkError').style.visibility = 'visible';
		document.getElementById('networkError').innerHTML = 'Please select your carrier.';
		valid = false;
	}
	else {
		document.getElementById('networkError').style.visibility = 'hidden';
	}
	
	if(mobile != '' &&  !(mobile.match(/^[0-9]+$/)) ) {		
		nameobj=document.getElementById('networkError');
		if((nameobj!="")||(nameobj!=null)){
			document.getElementById('networkError').style.visibility = 'visible';
		}
		document.getElementById('networkError').innerHTML = 'Only digits allowed in cell#';
		valid = false;
	}	
	
	if(valid)
		document.newsletterSignUp.submit();
	else
		return false;
}

function submitContestForm() {

	var name = document.contestForm.firstname.value;
	var country = document.contestForm.country.value;
	var province = document.contestForm.state.value;
	var email1 = document.contestForm.email.value;
	var email2 = document.contestForm.emailRetype.value;
	var cell1 = document.getElementById('cell1').value;
	var cell2 = document.getElementById('cell2').value;
	var cell3 = document.getElementById('cell3').value;
	var network = document.contestForm.networkId.value;

	var prefs  = false;	
	var valid = true;
	var mobile = cell1 + cell2 + cell3;
	
	document.contestForm.mobile.value = mobile;
	
	var valid = true;
	
	if(name == '') {
		document.getElementById('nameError').style.display = 'block';
		valid = false;
	}
	else {
		document.getElementById('nameError').style.display = 'none';
	}
	
	if(email1 != email2 || !emailValidate(email1)) {
		document.getElementById('emailError').style.display = 'block';
		valid = false;
	}
	else {
		document.getElementById('emailError').style.display = 'none';
	}

	if(mobile != '' && network == '') {
		document.getElementById('networkError').style.visibility = 'visible';
		valid = false;
	}
	else {
		document.getElementById('networkError').style.visibility = 'hidden';
	}	
	if(valid)
		document.contestForm.submit();
	else
		return false;
}

function submitMobilePinForm() {
	document.mobilePinForm.submit();
}

function submitForm(formName) {
	if(formName == 'blogCommentForm') {
		submitBlog();
	}	
	
	else if(formName == 'newsletterSignUp')	{ 
		omnitureSignUp();
		submitNewsletter();
	}	
	else if(formName == 'modifyNewsletterSignUp') 
		modifyNewsletter();
	else if(formName == 'contestForm')
		submitContestForm();
		
	else if(formName == 'mobilePinForm')
		submitMobilePinForm();
	return;
}

function omnitureBlog(p25,p27) {
	var s = s_gi('wmg00,wmgcan,wmgwarnermusic.ca');
	s.linkTrackVars = 'events,prop25,prop27,eVar25,eVar27';
	s.linkTrackEvents = 'event19';
	s.prop25 = p25;
	s.prop27 = p27;
	s.eVar25 = '';
	s.eVar27 = '';
	s.events = 'event19';
	s.tl(this,'o','Blog Post Reply');
}

function omnitureSignUp() {
	var s = s_gi('wmg00,wmgcan,wmgwarnermusic.ca');
	s.linkTrackVars = 'events,prop1,eVar4';
	s.linkTrackEvents = 'event3';
	s.prop1 = 'WMC';
	s.eVar4 = 'WMC';
	s.events = 'event3';
	s.tl(this,'o','Mailing List Signup');
}

function clearText(searchInput, defaultText) {
	if(searchInput.value == defaultText)
		searchInput.value = '';
}

function reappearText(obj,value) {
	if (obj.value=='') { 
      		obj.value=value; 
	}
}

function manageProvince(country,page) {
	if(country == 'Canada') {
		document.getElementById('provinceLabel').style.color = '#fff';
		document.getElementById('provinceSelect').disabled = false;
	}
	else {
		document.getElementById('provinceLabel').style.color = '#808080';
		document.getElementById('provinceSelect').disabled = true;
		if(page == 'signup') {
			document.getElementById('provinceError').style.display = 'none';
		}
	}
}

function unsubscribe(email,successurl,failurl){
	var chkArray = document.getElementsByName("newsletterId");
	/*var successurl = document.getElementById("unsubsuccessurl");
	var failurl = document.getElementById("unsubfailurl");*/
	var total=0;
	settings = 'width='+screen.width+', height='+screen.height+',top=0, left=0, fullscreen=yes';
	url='http://cdc.svc.wmg.com/mailinglist.dyn?email='+email;
	
	for(var i=0; i < chkArray.length; i++){
		if(chkArray[i].checked)	{	
			total +=1;
			id = (chkArray[i].value).substring(0,7);			   
			url += '&newsletterId='+id;			
		}
	}
	if(total == 0){
		alert("Please select the the musical preferences you want to unsubscribe to.");  
	}else{
	url += '&unsubscribe=1&successurl='+successurl+'&failurl='+failurl;
	window.location.href = url;
	}
}