jQuery(document).ready(function(){

		var url = window.location.href;
		if(url.indexOf('registercustomer')!=-1){
			makeDefaultChecked();
		} else if(url.indexOf('registermysony')!=-1){
			makeDefaultChecked();
		}
		else if(url.indexOf('mysonynewsletter')!=-1){
			setFocusOnError();
		}
}
);

function setFocusOnError(){
	
	var errorTop ="";
	if(document.getElementById("subscription.errors") != null){
		
		errorTop = document.getElementById("subscription.errors").innerHTML;
	}	
	// Fix for S&M tracker id 64597 
	if(document.getElementById("prodalert_errormsg.errors") != null && 
			document.getElementById("prodalert_errormsg.errors")!=undefined){
		
		errorTop = document.getElementById("prodalert_errormsg.errors").innerHTML;
	}
	if(errorTop == null || errorTop == ""){
		if(document.getElementById("termsAndConditions.errors") != null){			
			
			var errorBottom =document.getElementById("termsAndConditions.errors").innerHTML;
			if(errorBottom != null || errorBottom != ""){			
				document.getElementById("submitButton").focus();
			}
		}
		
	}else{
		//Fix done for Radar Defect Id : AA00001RQ4 :JS error on the Newsletter Subscription page
		// Fix for S&M tracker id 64597 
		if(document.getElementById("subscription.errors") != null &&
				document.getElementById("subscription.errors") != undefined){
			 location.href="#a_subscription";
		}else if(document.getElementById("prodalert_errormsg.errors") != null && 
			document.getElementById("prodalert_errormsg.errors")!=undefined){
			 location.href="#a_prodalert_errormsg";
		}			
	}

}
		
function makeDefaultChecked(){
	var locale = jQuery('#locale').val();
	var countryCode = locale.split("_");
	var upperCaseCountryCode = countryCode[1].toUpperCase();
	if(upperCaseCountryCode == 'SG'){
		var subscription = document.getElementsByName("subscription");
		var newsLetterWithSubCategory = document.getElementsByName("newsLetterWithSubCategory");
		for (var i=0 ; i<subscription.length ; i++){
			subscription[i].checked=true;
		}
		for (var i=0 ; i<newsLetterWithSubCategory.length ; i++){
			newsLetterWithSubCategory[i].checked=true;
		}	
	}
}	

function openArchiveWindow(index){
	if(index.value != ""){
		window.open(index.value);
	}
	
}

function submitMysonyPostLoginNewsletter(){
	
	if(document.getElementById("submitNewsletter") != null){	
		document.newsletterForm.action=document.getElementById("submitNewsletter").value;
				
		document.newsletterForm.submit();
	}
}

function uncheckSubCategory(element){
	if(element.checked==false){
		var categoryValue = element.value;
		var subscription = document.getElementsByName("subscription");
		for (var i=0 ; i<subscription.length ; i++){
			if(subscription[i].checked==true){
				var subscriptionValue = subscription[i].value;
				if(subscriptionValue.match(categoryValue)){
					subscription[i].checked=false;
				}
			}
		}
	}
}

function submitnonMySonyNewsletterForm(){
	
	if(document.getElementById("submitNonMySonyNewsletter") != null){	
		document.nonMysonyNewsletterForm.action=document.getElementById("submitNonMySonyNewsletter").value;
		document.nonMysonyNewsletterForm.action = document.nonMysonyNewsletterForm.action;		
		document.nonMysonyNewsletterForm.submit();
	}
}

function submitNonMysonyNewsletter(){

if(document.getElementById("submitNewsletter") != null){		
		document.nonMysonyNewsletterForm.action=document.getElementById("submitNewsletter").value;
		document.nonMysonyNewsletterForm.action = document.nonMysonyNewsletterForm.action+"&action=subscribenonmysonynewsletter";		
		document.nonMysonyNewsletterForm.submit();
	}
}

function submitOnEnterNewsLetterSignUp(event) {
	if (event.keyCode == 13) { // No need to do browser specific checks. It is always 13.  
          document.sonyStylePromotionForm.action = document.getElementById('signUpSubmitURL').value;
          document.sonyStylePromotionForm.submit();  
    	return false;
    }  
    else {
    	return true;
    }
}

function submitNewsLetterSignUp() {
	document.sonyStylePromotionForm.action = document.getElementById('signUpSubmitURL').value;
	document.sonyStylePromotionForm.submit();
}

function submitSubscriptionNonMysonyNewsletter() {
	document.subscribeNonMysonyNewsletterForm.action = document.getElementById('submitSubscriptionNonMySonyNewsletter').value;
	document.subscribeNonMysonyNewsletterForm.action = document.subscribeNonMysonyNewsletterForm.action+"&action=subscribenonmysonynewsletter";
	document.subscribeNonMysonyNewsletterForm.submit();
}

function submitNonMySonyPersonalDetailsForm(){
	document.nonMySonyPersonalDetailsForm.action = document.getElementById('submitNonMySonyPersonalDetailsForm').value;
	document.nonMySonyPersonalDetailsForm.action = document.nonMySonyPersonalDetailsForm.action+"&action=submitNonMySonyPersonalDetails";	
	document.nonMySonyPersonalDetailsForm.submit();
}
function submitSubscribeNLPersonalDetails() {
	document.subscribeNonMysonyNewsletterForm.action = document.getElementById('submitUrl').value;	
	document.subscribeNonMysonyNewsletterForm.submit();
}

function unSubscribeAll(){
	
	// for parent checkbox
	var parentCheckBox = document.getElementsByName("newsLetterWithSubCategory");
	for(var i=0 ; i<parentCheckBox.length ; i++){		
		parentCheckBox[i].checked=false;
	}	

	var allCheckBox = document.getElementsByName("subscription");
	for(var j=0 ; j<allCheckBox.length ; j++){		
		allCheckBox[j].checked=false;
	}
}

function checkMainCategory(element){
	if(element.checked==true){
		var subValue = element.value;
		var splitSubValue = subValue.split("_");
		var checkBoxNewsLetterWithSubCategoryTypeId = document.getElementsByName("newsLetterWithSubCategory");
		for (var i=0 ; i<checkBoxNewsLetterWithSubCategoryTypeId.length ; i++){
			if(checkBoxNewsLetterWithSubCategoryTypeId[i].value==splitSubValue[0]+"_"){
				checkBoxNewsLetterWithSubCategoryTypeId[i].checked=true;
			}
		}
	}
}

function enableDisableSubCategory(element){
	var categoryValue = element.value;
	var subscription = document.getElementsByName("subscription");
	if(element.checked==false){
		for (var i=0 ; i<subscription.length ; i++){
			if(subscription[i].checked==true){
				var subscriptionValue = subscription[i].value;
				if(subscriptionValue.match(categoryValue)){
					subscription[i].checked=false;
				}
			}
		}
	}else{
		for (var i=0 ; i<subscription.length ; i++){
			if(subscription[i].checked==false){
				var subscriptionValue = subscription[i].value;
				if(subscriptionValue.match(categoryValue)){
					subscription[i].checked=true;
				}
			}
		}
	}
}
